@leofcoin/chain 1.4.80 → 1.4.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/exports/browser/chain.js +298 -282
- package/exports/chain.js +295 -279
- package/exports/typings/config/config.d.ts +1 -0
- package/exports/typings/config/main.d.ts +5 -0
- package/exports/typings/config/protocol.d.ts +6 -0
- package/exports/typings/contract.d.ts +31 -0
- package/exports/typings/fee/config.d.ts +4 -0
- package/exports/typings/machine.d.ts +26 -0
- package/exports/typings/typer.d.ts +6 -0
- package/package.json +1 -1
- package/exports/browser/index-5f6ba5a4-78b5c5ea.js +0 -37
- package/exports/browser/messages-5569e6ce-833af778.js +0 -225
- package/exports/browser/node-browser-5fe4a30f.js +0 -21003
package/exports/browser/chain.js
CHANGED
|
@@ -1377,7 +1377,7 @@ class Transaction extends Protocol {
|
|
|
1377
1377
|
transactions = transactions.filter(tx => tx.decoded.from === address);
|
|
1378
1378
|
transactions = await this.promiseTransactionsContent(transactions);
|
|
1379
1379
|
if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
|
|
1380
|
-
let block = await peernet.get(this.lastBlock.hash);
|
|
1380
|
+
let block = await peernet.get(this.lastBlock.hash, 'block');
|
|
1381
1381
|
block = await new BlockMessage(block);
|
|
1382
1382
|
// for (let tx of block.decoded?.transactions) {
|
|
1383
1383
|
// tx = await peernet.get(tx, 'transaction')
|
|
@@ -1457,18 +1457,24 @@ class Transaction extends Protocol {
|
|
|
1457
1457
|
throw new Error(`transaction not signed`);
|
|
1458
1458
|
if (message.decoded.nonce === undefined)
|
|
1459
1459
|
throw new Error(`nonce required`);
|
|
1460
|
+
await this.validateNonce(message.decoded.from, message.decoded.nonce);
|
|
1461
|
+
// todo check if signature is valid
|
|
1462
|
+
const hash = await message.hash();
|
|
1460
1463
|
try {
|
|
1461
|
-
await this.validateNonce(message.decoded.from, message.decoded.nonce);
|
|
1462
|
-
// todo check if signature is valid
|
|
1463
|
-
const hash = await message.hash();
|
|
1464
1464
|
let data;
|
|
1465
1465
|
const wait = new Promise(async (resolve, reject) => {
|
|
1466
1466
|
if (pubsub.subscribers[`transaction.completed.${hash}`]) {
|
|
1467
1467
|
const result = pubsub.subscribers[`transaction.completed.${hash}`].value;
|
|
1468
|
+
if (result.status !== 'fulfilled') {
|
|
1469
|
+
await transactionPoolStore.delete(hash);
|
|
1470
|
+
}
|
|
1468
1471
|
result.status === 'fulfilled' ? resolve(result.hash) : reject({ hash: result.hash, error: result.error });
|
|
1469
1472
|
}
|
|
1470
1473
|
else {
|
|
1471
1474
|
const completed = async (result) => {
|
|
1475
|
+
if (result.status !== 'fulfilled') {
|
|
1476
|
+
await transactionPoolStore.delete(hash);
|
|
1477
|
+
}
|
|
1472
1478
|
result.status === 'fulfilled' ? resolve(result.hash) : reject({ hash: result.hash, error: result.error });
|
|
1473
1479
|
setTimeout(async () => {
|
|
1474
1480
|
pubsub.unsubscribe(`transaction.completed.${hash}`, completed);
|
|
@@ -1483,6 +1489,8 @@ class Transaction extends Protocol {
|
|
|
1483
1489
|
return { hash, data, fee: await calculateFee(message.decoded), wait, message };
|
|
1484
1490
|
}
|
|
1485
1491
|
catch (error) {
|
|
1492
|
+
console.log('remo');
|
|
1493
|
+
await transactionPoolStore.delete(hash);
|
|
1486
1494
|
throw error;
|
|
1487
1495
|
}
|
|
1488
1496
|
}
|
|
@@ -1492,58 +1500,50 @@ class Transaction extends Protocol {
|
|
|
1492
1500
|
* @extends {Transaction}
|
|
1493
1501
|
*/
|
|
1494
1502
|
class Contract extends Transaction {
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
}
|
|
1537
|
-
|
|
1538
|
-
}
|
|
1539
|
-
return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()])
|
|
1540
|
-
}
|
|
1541
|
-
|
|
1542
|
-
async deployContractMessage(message) {
|
|
1543
|
-
|
|
1544
|
-
}
|
|
1545
|
-
|
|
1546
|
-
|
|
1503
|
+
constructor() {
|
|
1504
|
+
super();
|
|
1505
|
+
}
|
|
1506
|
+
async init() {
|
|
1507
|
+
}
|
|
1508
|
+
/**
|
|
1509
|
+
*
|
|
1510
|
+
* @param {Address} creator
|
|
1511
|
+
* @param {String} contract
|
|
1512
|
+
* @param {Array} constructorParameters
|
|
1513
|
+
* @returns lib.createContractMessage
|
|
1514
|
+
*/
|
|
1515
|
+
async createContractMessage(creator, contract, constructorParameters = []) {
|
|
1516
|
+
return createContractMessage(creator, contract, constructorParameters);
|
|
1517
|
+
}
|
|
1518
|
+
/**
|
|
1519
|
+
*
|
|
1520
|
+
* @param {Address} creator
|
|
1521
|
+
* @param {String} contract
|
|
1522
|
+
* @param {Array} constructorParameters
|
|
1523
|
+
* @returns {Address}
|
|
1524
|
+
*/
|
|
1525
|
+
async createContractAddress(creator, contract, constructorParameters = []) {
|
|
1526
|
+
contract = await this.createContractMessage(creator, contract, constructorParameters);
|
|
1527
|
+
return contract.hash();
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
*
|
|
1531
|
+
* @param {String} contract
|
|
1532
|
+
* @param {Array} parameters
|
|
1533
|
+
* @returns
|
|
1534
|
+
*/
|
|
1535
|
+
async deployContract(contract, constructorParameters = []) {
|
|
1536
|
+
const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters);
|
|
1537
|
+
try {
|
|
1538
|
+
await contractStore.put(await message.hash(), message.encoded);
|
|
1539
|
+
}
|
|
1540
|
+
catch (error) {
|
|
1541
|
+
throw error;
|
|
1542
|
+
}
|
|
1543
|
+
return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()]);
|
|
1544
|
+
}
|
|
1545
|
+
async deployContractMessage(message) {
|
|
1546
|
+
}
|
|
1547
1547
|
}
|
|
1548
1548
|
|
|
1549
1549
|
const randombytes = strength => crypto.getRandomValues(new Uint8Array(strength));
|
|
@@ -1627,214 +1627,203 @@ class EasyWorker {
|
|
|
1627
1627
|
}
|
|
1628
1628
|
|
|
1629
1629
|
// import State from './state'
|
|
1630
|
-
|
|
1631
1630
|
class Machine {
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1631
|
+
#contracts = {};
|
|
1632
|
+
#nonces = {};
|
|
1633
|
+
lastBlock = { index: 0, hash: '0x0', previousHash: '0x0' };
|
|
1634
|
+
constructor(blocks) {
|
|
1635
|
+
return this.#init(blocks);
|
|
1636
|
+
}
|
|
1637
|
+
#createMessage(sender = peernet.selectedAccount) {
|
|
1638
|
+
return {
|
|
1639
|
+
sender,
|
|
1640
|
+
call: this.execute,
|
|
1641
|
+
staticCall: this.get.bind(this)
|
|
1642
|
+
};
|
|
1643
|
+
}
|
|
1644
|
+
async #onmessage(data) {
|
|
1645
|
+
switch (data.type) {
|
|
1646
|
+
case 'contractError': {
|
|
1647
|
+
console.warn(`removing contract ${await data.hash()}`);
|
|
1648
|
+
await contractStore.delete(await data.hash());
|
|
1649
|
+
break;
|
|
1650
|
+
}
|
|
1651
|
+
case 'initError': {
|
|
1652
|
+
console.error(`init error: ${data.message}`);
|
|
1653
|
+
break;
|
|
1654
|
+
}
|
|
1655
|
+
case 'executionError': {
|
|
1656
|
+
// console.warn(`error executing transaction ${data.message}`);
|
|
1657
|
+
pubsub.publish(data.id, { error: data.message });
|
|
1658
|
+
break;
|
|
1659
|
+
}
|
|
1660
|
+
case 'debug': {
|
|
1661
|
+
for (const message of data.messages)
|
|
1662
|
+
debug(message);
|
|
1663
|
+
break;
|
|
1664
|
+
}
|
|
1665
|
+
case 'machine-ready': {
|
|
1666
|
+
this.lastBlock = data.lastBlock;
|
|
1667
|
+
pubsub.publish('machine.ready', true);
|
|
1668
|
+
break;
|
|
1669
|
+
}
|
|
1670
|
+
case 'response': {
|
|
1671
|
+
pubsub.publish(data.id, data.value || false);
|
|
1672
|
+
break;
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
async #init(blocks) {
|
|
1677
|
+
return new Promise(async (resolve) => {
|
|
1678
|
+
const machineReady = () => {
|
|
1679
|
+
pubsub.unsubscribe('machine.ready', machineReady);
|
|
1680
|
+
resolve(this);
|
|
1681
|
+
};
|
|
1682
|
+
pubsub.subscribe('machine.ready', machineReady);
|
|
1683
|
+
this.worker = await new EasyWorker('./exports/browser/workers/machine-worker.js', { serialization: 'advanced', type: 'module' });
|
|
1684
|
+
this.worker.onmessage(this.#onmessage.bind(this));
|
|
1685
|
+
// const blocks = await blockStore.values()
|
|
1686
|
+
const contracts = await Promise.all([
|
|
1687
|
+
contractStore.get(contractFactory$1),
|
|
1688
|
+
contractStore.get(nativeToken$1),
|
|
1689
|
+
contractStore.get(validators$1),
|
|
1690
|
+
contractStore.get(nameService$1)
|
|
1691
|
+
]);
|
|
1692
|
+
const message = {
|
|
1693
|
+
type: 'init',
|
|
1694
|
+
input: {
|
|
1695
|
+
contracts,
|
|
1696
|
+
blocks,
|
|
1697
|
+
peerid: peernet.peerId
|
|
1698
|
+
}
|
|
1699
|
+
};
|
|
1700
|
+
this.worker.postMessage(message);
|
|
1701
|
+
});
|
|
1702
|
+
}
|
|
1703
|
+
async #runContract(contractMessage) {
|
|
1704
|
+
const hash = await contractMessage.hash();
|
|
1705
|
+
return new Promise((resolve, reject) => {
|
|
1706
|
+
const id = randombytes(20).toString('hex');
|
|
1707
|
+
const onmessage = message => {
|
|
1708
|
+
pubsub.unsubscribe(id, onmessage);
|
|
1709
|
+
if (message?.error)
|
|
1710
|
+
reject(message.error);
|
|
1711
|
+
else
|
|
1712
|
+
resolve(message);
|
|
1713
|
+
};
|
|
1714
|
+
pubsub.subscribe(id, onmessage);
|
|
1715
|
+
this.worker.postMessage({
|
|
1716
|
+
type: 'run',
|
|
1717
|
+
id,
|
|
1718
|
+
input: {
|
|
1719
|
+
decoded: contractMessage.decoded,
|
|
1720
|
+
encoded: contractMessage.encoded,
|
|
1721
|
+
hash
|
|
1722
|
+
}
|
|
1723
|
+
});
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
/**
|
|
1727
|
+
*
|
|
1728
|
+
* @param {Address} contract
|
|
1729
|
+
* @param {String} method
|
|
1730
|
+
* @param {Array} parameters
|
|
1731
|
+
* @returns Promise<message>
|
|
1732
|
+
*/
|
|
1733
|
+
async execute(contract, method, parameters) {
|
|
1734
|
+
try {
|
|
1735
|
+
if (contract === contractFactory$1 && method === 'registerContract') {
|
|
1736
|
+
if (await this.has(parameters[0]))
|
|
1737
|
+
throw new Error(`duplicate contract @${parameters[0]}`);
|
|
1738
|
+
let message;
|
|
1739
|
+
if (!await contractStore.has(parameters[0])) {
|
|
1740
|
+
message = await peernet.get(parameters[0], 'contract');
|
|
1741
|
+
message = await new ContractMessage(message);
|
|
1742
|
+
await contractStore.put(await message.hash(), message.encoded);
|
|
1743
|
+
}
|
|
1744
|
+
if (!message) {
|
|
1745
|
+
message = await contractStore.get(parameters[0]);
|
|
1746
|
+
message = await new ContractMessage(message);
|
|
1747
|
+
}
|
|
1748
|
+
if (!await this.has(await message.hash()))
|
|
1749
|
+
await this.#runContract(message);
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
catch (error) {
|
|
1753
|
+
throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`);
|
|
1754
|
+
}
|
|
1755
|
+
return new Promise((resolve, reject) => {
|
|
1756
|
+
const id = randombytes(20).toString('hex');
|
|
1757
|
+
const onmessage = message => {
|
|
1758
|
+
pubsub.unsubscribe(id, onmessage);
|
|
1759
|
+
if (message?.error)
|
|
1760
|
+
reject(message.error);
|
|
1761
|
+
else
|
|
1762
|
+
resolve(message);
|
|
1763
|
+
};
|
|
1764
|
+
pubsub.subscribe(id, onmessage);
|
|
1765
|
+
this.worker.postMessage({
|
|
1766
|
+
type: 'execute',
|
|
1767
|
+
id,
|
|
1768
|
+
input: {
|
|
1769
|
+
contract,
|
|
1770
|
+
method,
|
|
1771
|
+
params: parameters
|
|
1772
|
+
}
|
|
1773
|
+
});
|
|
1774
|
+
});
|
|
1775
|
+
}
|
|
1776
|
+
get(contract, method, parameters) {
|
|
1777
|
+
return new Promise((resolve, reject) => {
|
|
1778
|
+
const id = randombytes(20).toString();
|
|
1779
|
+
const onmessage = message => {
|
|
1780
|
+
pubsub.unsubscribe(id, onmessage);
|
|
1781
|
+
resolve(message);
|
|
1782
|
+
};
|
|
1783
|
+
pubsub.subscribe(id, onmessage);
|
|
1784
|
+
this.worker.postMessage({
|
|
1785
|
+
type: 'get',
|
|
1786
|
+
id,
|
|
1787
|
+
input: {
|
|
1788
|
+
contract,
|
|
1789
|
+
method,
|
|
1790
|
+
params: parameters
|
|
1791
|
+
}
|
|
1792
|
+
});
|
|
1793
|
+
});
|
|
1794
|
+
}
|
|
1795
|
+
async has(address) {
|
|
1796
|
+
return new Promise((resolve, reject) => {
|
|
1797
|
+
const id = randombytes(20).toString('hex');
|
|
1798
|
+
const onmessage = message => {
|
|
1799
|
+
pubsub.unsubscribe(id, onmessage);
|
|
1800
|
+
if (message?.error)
|
|
1801
|
+
reject(message.error);
|
|
1802
|
+
else
|
|
1803
|
+
resolve(message);
|
|
1804
|
+
};
|
|
1805
|
+
pubsub.subscribe(id, onmessage);
|
|
1806
|
+
this.worker.postMessage({
|
|
1807
|
+
type: 'has',
|
|
1808
|
+
id,
|
|
1809
|
+
input: {
|
|
1810
|
+
address
|
|
1811
|
+
}
|
|
1812
|
+
});
|
|
1813
|
+
});
|
|
1814
|
+
}
|
|
1815
|
+
async delete(hash) {
|
|
1816
|
+
return contractStore.delete(hash);
|
|
1817
|
+
}
|
|
1818
|
+
/**
|
|
1819
|
+
*
|
|
1820
|
+
* @returns Promise
|
|
1821
|
+
*/
|
|
1822
|
+
async deleteAll() {
|
|
1823
|
+
let hashes = await contractStore.get();
|
|
1824
|
+
hashes = Object.keys(hashes).map(hash => this.delete(hash));
|
|
1825
|
+
return Promise.all(hashes);
|
|
1680
1826
|
}
|
|
1681
|
-
|
|
1682
|
-
}
|
|
1683
|
-
|
|
1684
|
-
async #init(blocks) {
|
|
1685
|
-
return new Promise(async (resolve) => {
|
|
1686
|
-
const machineReady = () => {
|
|
1687
|
-
pubsub.unsubscribe('machine.ready', machineReady);
|
|
1688
|
-
resolve(this);
|
|
1689
|
-
};
|
|
1690
|
-
pubsub.subscribe('machine.ready', machineReady);
|
|
1691
|
-
|
|
1692
|
-
this.worker = await new EasyWorker('./exports/browser/workers/machine-worker.js', {serialization: 'advanced', type:'module'});
|
|
1693
|
-
this.worker.onmessage(this.#onmessage.bind(this));
|
|
1694
|
-
// const blocks = await blockStore.values()
|
|
1695
|
-
const contracts = await Promise.all([
|
|
1696
|
-
contractStore.get(contractFactory$1),
|
|
1697
|
-
contractStore.get(nativeToken$1),
|
|
1698
|
-
contractStore.get(validators$1),
|
|
1699
|
-
contractStore.get(nameService$1)
|
|
1700
|
-
]);
|
|
1701
|
-
|
|
1702
|
-
const message = {
|
|
1703
|
-
type: 'init',
|
|
1704
|
-
input: {
|
|
1705
|
-
contracts,
|
|
1706
|
-
blocks,
|
|
1707
|
-
peerid: peernet.peerId
|
|
1708
|
-
}
|
|
1709
|
-
};
|
|
1710
|
-
this.worker.postMessage(message);
|
|
1711
|
-
})
|
|
1712
|
-
|
|
1713
|
-
}
|
|
1714
|
-
|
|
1715
|
-
async #runContract(contractMessage) {
|
|
1716
|
-
const hash = await contractMessage.hash();
|
|
1717
|
-
return new Promise((resolve, reject) => {
|
|
1718
|
-
const id = randombytes(20).toString('hex');
|
|
1719
|
-
const onmessage = message => {
|
|
1720
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1721
|
-
if (message?.error) reject(message.error);
|
|
1722
|
-
else resolve(message);
|
|
1723
|
-
};
|
|
1724
|
-
pubsub.subscribe(id, onmessage);
|
|
1725
|
-
this.worker.postMessage({
|
|
1726
|
-
type: 'run',
|
|
1727
|
-
id,
|
|
1728
|
-
input: {
|
|
1729
|
-
decoded: contractMessage.decoded,
|
|
1730
|
-
encoded: contractMessage.encoded,
|
|
1731
|
-
hash
|
|
1732
|
-
}
|
|
1733
|
-
});
|
|
1734
|
-
})
|
|
1735
|
-
|
|
1736
|
-
}
|
|
1737
|
-
|
|
1738
|
-
/**
|
|
1739
|
-
*
|
|
1740
|
-
* @param {Address} contract
|
|
1741
|
-
* @param {String} method
|
|
1742
|
-
* @param {Array} parameters
|
|
1743
|
-
* @returns Promise<message>
|
|
1744
|
-
*/
|
|
1745
|
-
async execute(contract, method, parameters) {
|
|
1746
|
-
try {
|
|
1747
|
-
if (contract === contractFactory$1 && method === 'registerContract') {
|
|
1748
|
-
if (await this.has(parameters[0])) throw new Error(`duplicate contract @${parameters[0]}`)
|
|
1749
|
-
let message;
|
|
1750
|
-
if (!await contractStore.has(parameters[0])) {
|
|
1751
|
-
message = await peernet.get(parameters[0], 'contract');
|
|
1752
|
-
message = await new ContractMessage(message);
|
|
1753
|
-
await contractStore.put(await message.hash(), message.encoded);
|
|
1754
|
-
}
|
|
1755
|
-
if (!message) {
|
|
1756
|
-
message = await contractStore.get(parameters[0]);
|
|
1757
|
-
message = await new ContractMessage(message);
|
|
1758
|
-
}
|
|
1759
|
-
if (!await this.has(await message.hash())) await this.#runContract(message);
|
|
1760
|
-
}
|
|
1761
|
-
} catch (error) {
|
|
1762
|
-
throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`)
|
|
1763
|
-
}
|
|
1764
|
-
return new Promise((resolve, reject) => {
|
|
1765
|
-
const id = randombytes(20).toString('hex');
|
|
1766
|
-
const onmessage = message => {
|
|
1767
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1768
|
-
if (message?.error) reject(message.error);
|
|
1769
|
-
else resolve(message);
|
|
1770
|
-
};
|
|
1771
|
-
pubsub.subscribe(id, onmessage);
|
|
1772
|
-
this.worker.postMessage({
|
|
1773
|
-
type: 'execute',
|
|
1774
|
-
id,
|
|
1775
|
-
input: {
|
|
1776
|
-
contract,
|
|
1777
|
-
method,
|
|
1778
|
-
params: parameters
|
|
1779
|
-
}
|
|
1780
|
-
});
|
|
1781
|
-
})
|
|
1782
|
-
|
|
1783
|
-
}
|
|
1784
|
-
|
|
1785
|
-
get(contract, method, parameters) {
|
|
1786
|
-
return new Promise((resolve, reject) => {
|
|
1787
|
-
const id = randombytes(20).toString();
|
|
1788
|
-
const onmessage = message => {
|
|
1789
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1790
|
-
resolve(message);
|
|
1791
|
-
};
|
|
1792
|
-
pubsub.subscribe(id, onmessage);
|
|
1793
|
-
this.worker.postMessage({
|
|
1794
|
-
type: 'get',
|
|
1795
|
-
id,
|
|
1796
|
-
input: {
|
|
1797
|
-
contract,
|
|
1798
|
-
method,
|
|
1799
|
-
params: parameters
|
|
1800
|
-
}
|
|
1801
|
-
});
|
|
1802
|
-
})
|
|
1803
|
-
}
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
async has(address) {
|
|
1807
|
-
return new Promise((resolve, reject) => {
|
|
1808
|
-
const id = randombytes(20).toString('hex');
|
|
1809
|
-
const onmessage = message => {
|
|
1810
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1811
|
-
if (message?.error) reject(message.error);
|
|
1812
|
-
else resolve(message);
|
|
1813
|
-
};
|
|
1814
|
-
pubsub.subscribe(id, onmessage);
|
|
1815
|
-
this.worker.postMessage({
|
|
1816
|
-
type: 'has',
|
|
1817
|
-
id,
|
|
1818
|
-
input: {
|
|
1819
|
-
address
|
|
1820
|
-
}
|
|
1821
|
-
});
|
|
1822
|
-
})
|
|
1823
|
-
}
|
|
1824
|
-
|
|
1825
|
-
async delete(hash) {
|
|
1826
|
-
return contractStore.delete(hash)
|
|
1827
|
-
}
|
|
1828
|
-
|
|
1829
|
-
/**
|
|
1830
|
-
*
|
|
1831
|
-
* @returns Promise
|
|
1832
|
-
*/
|
|
1833
|
-
async deleteAll() {
|
|
1834
|
-
let hashes = await contractStore.get();
|
|
1835
|
-
hashes = Object.keys(hashes).map(hash => this.delete(hash));
|
|
1836
|
-
return Promise.all(hashes)
|
|
1837
|
-
}
|
|
1838
1827
|
}
|
|
1839
1828
|
|
|
1840
1829
|
class Jobber {
|
|
@@ -1970,9 +1959,17 @@ class State extends Contract {
|
|
|
1970
1959
|
}
|
|
1971
1960
|
catch (error) {
|
|
1972
1961
|
console.log({ e: error });
|
|
1962
|
+
console.log({ e: error });
|
|
1973
1963
|
}
|
|
1974
1964
|
globalThis.pubsub.publish('lastBlock', this.lastBlock);
|
|
1975
1965
|
// load local blocks
|
|
1966
|
+
try {
|
|
1967
|
+
this.knownBlocks = await blockStore.keys();
|
|
1968
|
+
}
|
|
1969
|
+
catch (error) {
|
|
1970
|
+
console.error(error);
|
|
1971
|
+
throw error;
|
|
1972
|
+
}
|
|
1976
1973
|
await this.resolveBlocks();
|
|
1977
1974
|
this.#machine = await new Machine(this.#blocks);
|
|
1978
1975
|
await this.#loadBlocks(this.#blocks);
|
|
@@ -2218,6 +2215,8 @@ class State extends Contract {
|
|
|
2218
2215
|
this.#totalTransactions += 1;
|
|
2219
2216
|
}
|
|
2220
2217
|
catch (error) {
|
|
2218
|
+
await globalThis.transactionPoolStore.delete(transaction.hash ? transaction.hash : await (new TransactionMessage(transaction)).hash());
|
|
2219
|
+
console.log('removing invalid transaction');
|
|
2221
2220
|
console.log(error);
|
|
2222
2221
|
return false;
|
|
2223
2222
|
}
|
|
@@ -2271,6 +2270,7 @@ class State extends Contract {
|
|
|
2271
2270
|
}
|
|
2272
2271
|
|
|
2273
2272
|
globalThis.BigNumber = BigNumber;
|
|
2273
|
+
const ignorelist = ['BA5XUACBBBAT653LT3GHP2Z5SUHVCA42BP6IBFBJACHOZIHHR4DUPG2XMB', 'BA5XUACK6K5XA5P4BHRZ4SZT6FCLO6GLGCLUAD62WBPVLFK73RHZZUFLEG'];
|
|
2274
2274
|
// check if browser or local
|
|
2275
2275
|
class Chain extends State {
|
|
2276
2276
|
#state;
|
|
@@ -2313,6 +2313,7 @@ class Chain extends State {
|
|
|
2313
2313
|
}
|
|
2314
2314
|
catch (error) {
|
|
2315
2315
|
console.error(error);
|
|
2316
|
+
console.log('ttttt');
|
|
2316
2317
|
}
|
|
2317
2318
|
const end = Date.now();
|
|
2318
2319
|
console.log(((end - start) / 1000) + ' s');
|
|
@@ -2343,7 +2344,7 @@ class Chain extends State {
|
|
|
2343
2344
|
console.log('handle native contracts');
|
|
2344
2345
|
// handle native contracts
|
|
2345
2346
|
}
|
|
2346
|
-
async
|
|
2347
|
+
async clearPool() {
|
|
2347
2348
|
await globalThis.transactionPoolStore.clear();
|
|
2348
2349
|
}
|
|
2349
2350
|
async #init() {
|
|
@@ -2392,7 +2393,7 @@ class Chain extends State {
|
|
|
2392
2393
|
globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
2393
2394
|
// todo some functions rely on state
|
|
2394
2395
|
await super.init();
|
|
2395
|
-
globalThis.
|
|
2396
|
+
globalThis.pubsub.publish('chain:ready', true);
|
|
2396
2397
|
return this;
|
|
2397
2398
|
}
|
|
2398
2399
|
async #invalidTransaction(hash) {
|
|
@@ -2427,9 +2428,10 @@ class Chain extends State {
|
|
|
2427
2428
|
console.log({ transactionsInPool });
|
|
2428
2429
|
const transactionsToGet = [];
|
|
2429
2430
|
for (const key of transactionsInPool) {
|
|
2430
|
-
if (!transactions.includes(key))
|
|
2431
|
-
transactionsToGet.push(transactionPoolStore.put(key, (await peernet.get(key))));
|
|
2431
|
+
if (!transactions.includes(key) && !ignorelist.includes(key))
|
|
2432
|
+
transactionsToGet.push(transactionPoolStore.put(key, (await peernet.get(key, 'transaction'))));
|
|
2432
2433
|
}
|
|
2434
|
+
console.log(await transactionPoolStore.keys());
|
|
2433
2435
|
await Promise.all(transactionsToGet);
|
|
2434
2436
|
if (Object.keys(lastBlock).length > 0) {
|
|
2435
2437
|
if (!this.lastBlock || !this.blocks[this.blocks.length - 1]?.loaded || lastBlock && lastBlock.index > this.lastBlock?.index || !this.loaded && !this.resolving) {
|
|
@@ -2459,7 +2461,7 @@ class Chain extends State {
|
|
|
2459
2461
|
return result || 'no state change';
|
|
2460
2462
|
}
|
|
2461
2463
|
catch (error) {
|
|
2462
|
-
|
|
2464
|
+
await transactionPoolStore.delete(hash);
|
|
2463
2465
|
globalThis.peernet.publish('invalid-transaction', hash);
|
|
2464
2466
|
globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fail', hash, error: error });
|
|
2465
2467
|
throw { error, hash, from, to, params, nonce };
|
|
@@ -2501,6 +2503,9 @@ class Chain extends State {
|
|
|
2501
2503
|
globalThis.pubsub.publish('block-processed', blockMessage.decoded);
|
|
2502
2504
|
}
|
|
2503
2505
|
catch (error) {
|
|
2506
|
+
console.log(error.hash);
|
|
2507
|
+
console.log('errrrr');
|
|
2508
|
+
await transactionPoolStore.delete(error.hash);
|
|
2504
2509
|
console.log({ e: error });
|
|
2505
2510
|
}
|
|
2506
2511
|
}
|
|
@@ -2533,6 +2538,10 @@ class Chain extends State {
|
|
|
2533
2538
|
if (await globalThis.transactionPoolStore.size() === 0)
|
|
2534
2539
|
return;
|
|
2535
2540
|
let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
|
|
2541
|
+
for (const hash of await globalThis.transactionPoolStore.keys()) {
|
|
2542
|
+
if (ignorelist.includes(hash))
|
|
2543
|
+
await globalThis.transactionPoolStore.delete(hash);
|
|
2544
|
+
}
|
|
2536
2545
|
if (Object.keys(transactions)?.length === 0)
|
|
2537
2546
|
return;
|
|
2538
2547
|
const timestamp = Date.now();
|
|
@@ -2548,6 +2557,7 @@ class Chain extends State {
|
|
|
2548
2557
|
// exclude failing tx
|
|
2549
2558
|
transactions = await this.promiseTransactions(transactions);
|
|
2550
2559
|
transactions = transactions.sort((a, b) => a.nonce - b.nonce);
|
|
2560
|
+
console.log({ transactions });
|
|
2551
2561
|
for (let transaction of transactions) {
|
|
2552
2562
|
const hash = await transaction.hash();
|
|
2553
2563
|
const doubleTransactions = [];
|
|
@@ -2561,21 +2571,25 @@ class Chain extends State {
|
|
|
2561
2571
|
if (doubleTransactions.length > 0) {
|
|
2562
2572
|
await globalThis.transactionPoolStore.delete(hash);
|
|
2563
2573
|
await globalThis.peernet.publish('invalid-transaction', hash);
|
|
2574
|
+
return;
|
|
2564
2575
|
}
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2576
|
+
// if (timestamp + this.#slotTime > Date.now()) {
|
|
2577
|
+
try {
|
|
2578
|
+
const result = await this.#executeTransaction({ ...transaction.decoded, hash });
|
|
2579
|
+
console.log({ result });
|
|
2580
|
+
block.transactions.push(transaction);
|
|
2581
|
+
block.fees = block.fees.add(await calculateFee(transaction.decoded));
|
|
2582
|
+
await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
|
|
2583
|
+
}
|
|
2584
|
+
catch (e) {
|
|
2585
|
+
console.log('vvvvvv');
|
|
2586
|
+
console.log({ e });
|
|
2587
|
+
console.log(hash);
|
|
2588
|
+
peernet.publish('invalid-transaction', hash);
|
|
2589
|
+
console.log(await globalThis.transactionPoolStore.keys());
|
|
2590
|
+
console.log(await globalThis.transactionPoolStore.has(e.hash));
|
|
2591
|
+
await globalThis.transactionPoolStore.delete(e.hash);
|
|
2592
|
+
console.log(await globalThis.transactionPoolStore.has(e.hash));
|
|
2579
2593
|
}
|
|
2580
2594
|
}
|
|
2581
2595
|
// don't add empty block
|
|
@@ -2649,6 +2663,7 @@ class Chain extends State {
|
|
|
2649
2663
|
}
|
|
2650
2664
|
catch (error) {
|
|
2651
2665
|
console.log(error);
|
|
2666
|
+
console.log('eeeee');
|
|
2652
2667
|
throw new Error(`invalid block ${block}`);
|
|
2653
2668
|
}
|
|
2654
2669
|
// data = await this.machine.execute(to, method, params)
|
|
@@ -2667,6 +2682,7 @@ class Chain extends State {
|
|
|
2667
2682
|
}
|
|
2668
2683
|
catch (e) {
|
|
2669
2684
|
console.log(e);
|
|
2685
|
+
console.log('rrrrr');
|
|
2670
2686
|
globalThis.peernet.publish('invalid-transaction', hash);
|
|
2671
2687
|
throw new Error('invalid transaction');
|
|
2672
2688
|
}
|
|
@@ -2696,7 +2712,7 @@ class Chain extends State {
|
|
|
2696
2712
|
* @param {Address} sender
|
|
2697
2713
|
* @returns {globalMessage}
|
|
2698
2714
|
*/
|
|
2699
|
-
#createMessage(sender = globalThis.
|
|
2715
|
+
#createMessage(sender = globalThis.peernet.selectedAccount) {
|
|
2700
2716
|
return {
|
|
2701
2717
|
sender,
|
|
2702
2718
|
call: this.call,
|