@leofcoin/chain 1.4.78 → 1.4.80
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/browser-store.js +122 -196
- package/exports/browser/chain.js +282 -246
- package/exports/browser/{index-502d190c-60cfcc02.js → index-5f6ba5a4-78b5c5ea.js} +2 -2
- package/exports/browser/index-6311966e-b5f647d8.js +37 -0
- package/exports/browser/{index-fa2c6beb.js → index-c3f4012c.js} +2 -0
- package/exports/browser/messages-54aa3cad-5309745b.js +225 -0
- package/exports/browser/messages-5569e6ce-833af778.js +225 -0
- package/exports/browser/{node-browser-b35d8f46.js → node-browser-3cbee72b.js} +200 -195
- package/exports/browser/node-browser-5fe4a30f.js +21003 -0
- package/exports/browser/node-browser.js +2 -2
- package/exports/browser/workers/block-worker.js +2 -0
- package/exports/browser/workers/machine-worker.js +2 -0
- package/exports/chain.js +278 -242
- package/exports/typings/sync-controller.d.ts +13 -0
- package/exports/typings/types.d.ts +1 -0
- package/package.json +2 -1
- package/exports/browser/messages-cd01524c-8d88d84b.js +0 -205
- package/exports/typings/chain.d.ts +0 -71
- package/exports/typings/config/config.d.ts +0 -1
- package/exports/typings/config/main.d.ts +0 -5
- package/exports/typings/config/protocol.d.ts +0 -6
- package/exports/typings/contract.d.ts +0 -31
- package/exports/typings/fee/config.d.ts +0 -4
- package/exports/typings/machine.d.ts +0 -26
- package/exports/typings/typer.d.ts +0 -6
package/exports/browser/chain.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, C as ContractMessage, T as TransactionMessage, R as RawTransactionMessage, g as getDefaultExportFromCjs, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index-
|
|
1
|
+
import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, C as ContractMessage, T as TransactionMessage, R as RawTransactionMessage, g as getDefaultExportFromCjs, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index-c3f4012c.js';
|
|
2
2
|
|
|
3
3
|
const logger$1 = new Logger(version$1);
|
|
4
4
|
const _constructorGuard = {};
|
|
@@ -1302,11 +1302,11 @@ _PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = n
|
|
|
1302
1302
|
|
|
1303
1303
|
const limit = 1800;
|
|
1304
1304
|
const transactionLimit = 1800;
|
|
1305
|
-
const requestTimeout =
|
|
1306
|
-
const syncTimeout =
|
|
1305
|
+
const requestTimeout = 30000;
|
|
1306
|
+
const syncTimeout = 30000;
|
|
1307
1307
|
class Protocol {
|
|
1308
1308
|
version;
|
|
1309
|
-
resolveTimeout =
|
|
1309
|
+
resolveTimeout = 30000;
|
|
1310
1310
|
get limit() {
|
|
1311
1311
|
return limit;
|
|
1312
1312
|
}
|
|
@@ -1472,7 +1472,7 @@ class Transaction extends Protocol {
|
|
|
1472
1472
|
result.status === 'fulfilled' ? resolve(result.hash) : reject({ hash: result.hash, error: result.error });
|
|
1473
1473
|
setTimeout(async () => {
|
|
1474
1474
|
pubsub.unsubscribe(`transaction.completed.${hash}`, completed);
|
|
1475
|
-
},
|
|
1475
|
+
}, 10000);
|
|
1476
1476
|
};
|
|
1477
1477
|
pubsub.subscribe(`transaction.completed.${hash}`, completed);
|
|
1478
1478
|
}
|
|
@@ -1492,50 +1492,58 @@ class Transaction extends Protocol {
|
|
|
1492
1492
|
* @extends {Transaction}
|
|
1493
1493
|
*/
|
|
1494
1494
|
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
|
-
}
|
|
1495
|
+
constructor() {
|
|
1496
|
+
super();
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
async init() {
|
|
1500
|
+
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
/**
|
|
1504
|
+
*
|
|
1505
|
+
* @param {Address} creator
|
|
1506
|
+
* @param {String} contract
|
|
1507
|
+
* @param {Array} constructorParameters
|
|
1508
|
+
* @returns lib.createContractMessage
|
|
1509
|
+
*/
|
|
1510
|
+
async createContractMessage(creator, contract, constructorParameters = []) {
|
|
1511
|
+
return createContractMessage(creator, contract, constructorParameters)
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
/**
|
|
1515
|
+
*
|
|
1516
|
+
* @param {Address} creator
|
|
1517
|
+
* @param {String} contract
|
|
1518
|
+
* @param {Array} constructorParameters
|
|
1519
|
+
* @returns {Address}
|
|
1520
|
+
*/
|
|
1521
|
+
async createContractAddress(creator, contract, constructorParameters = []) {
|
|
1522
|
+
contract = await this.createContractMessage(creator, contract, constructorParameters);
|
|
1523
|
+
return contract.hash()
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
/**
|
|
1527
|
+
*
|
|
1528
|
+
* @param {String} contract
|
|
1529
|
+
* @param {Array} parameters
|
|
1530
|
+
* @returns
|
|
1531
|
+
*/
|
|
1532
|
+
async deployContract(contract, constructorParameters = []) {
|
|
1533
|
+
const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters);
|
|
1534
|
+
try {
|
|
1535
|
+
await contractStore.put(await message.hash(), message.encoded);
|
|
1536
|
+
} catch (error) {
|
|
1537
|
+
throw error
|
|
1538
|
+
}
|
|
1539
|
+
return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()])
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
async deployContractMessage(message) {
|
|
1543
|
+
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
|
|
1539
1547
|
}
|
|
1540
1548
|
|
|
1541
1549
|
const randombytes = strength => crypto.getRandomValues(new Uint8Array(strength));
|
|
@@ -1619,203 +1627,214 @@ class EasyWorker {
|
|
|
1619
1627
|
}
|
|
1620
1628
|
|
|
1621
1629
|
// import State from './state'
|
|
1630
|
+
|
|
1622
1631
|
class Machine {
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
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
|
-
pubsub.unsubscribe('machine.ready', machineReady);
|
|
1672
|
-
resolve(this);
|
|
1673
|
-
};
|
|
1674
|
-
pubsub.subscribe('machine.ready', machineReady);
|
|
1675
|
-
this.worker = await new EasyWorker('./exports/browser/workers/machine-worker.js', { serialization: 'advanced', type: 'module' });
|
|
1676
|
-
this.worker.onmessage(this.#onmessage.bind(this));
|
|
1677
|
-
// const blocks = await blockStore.values()
|
|
1678
|
-
const contracts = await Promise.all([
|
|
1679
|
-
contractStore.get(contractFactory$1),
|
|
1680
|
-
contractStore.get(nativeToken$1),
|
|
1681
|
-
contractStore.get(validators$1),
|
|
1682
|
-
contractStore.get(nameService$1)
|
|
1683
|
-
]);
|
|
1684
|
-
const message = {
|
|
1685
|
-
type: 'init',
|
|
1686
|
-
input: {
|
|
1687
|
-
contracts,
|
|
1688
|
-
blocks,
|
|
1689
|
-
peerid: peernet.peerId
|
|
1690
|
-
}
|
|
1691
|
-
};
|
|
1692
|
-
this.worker.postMessage(message);
|
|
1693
|
-
});
|
|
1694
|
-
}
|
|
1695
|
-
async #runContract(contractMessage) {
|
|
1696
|
-
const hash = await contractMessage.hash();
|
|
1697
|
-
return new Promise((resolve, reject) => {
|
|
1698
|
-
const id = randombytes(20).toString('hex');
|
|
1699
|
-
const onmessage = message => {
|
|
1700
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1701
|
-
if (message?.error)
|
|
1702
|
-
reject(message.error);
|
|
1703
|
-
else
|
|
1704
|
-
resolve(message);
|
|
1705
|
-
};
|
|
1706
|
-
pubsub.subscribe(id, onmessage);
|
|
1707
|
-
this.worker.postMessage({
|
|
1708
|
-
type: 'run',
|
|
1709
|
-
id,
|
|
1710
|
-
input: {
|
|
1711
|
-
decoded: contractMessage.decoded,
|
|
1712
|
-
encoded: contractMessage.encoded,
|
|
1713
|
-
hash
|
|
1714
|
-
}
|
|
1715
|
-
});
|
|
1716
|
-
});
|
|
1717
|
-
}
|
|
1718
|
-
/**
|
|
1719
|
-
*
|
|
1720
|
-
* @param {Address} contract
|
|
1721
|
-
* @param {String} method
|
|
1722
|
-
* @param {Array} parameters
|
|
1723
|
-
* @returns Promise<message>
|
|
1724
|
-
*/
|
|
1725
|
-
async execute(contract, method, parameters) {
|
|
1726
|
-
try {
|
|
1727
|
-
if (contract === contractFactory$1 && method === 'registerContract') {
|
|
1728
|
-
if (await this.has(parameters[0]))
|
|
1729
|
-
throw new Error(`duplicate contract @${parameters[0]}`);
|
|
1730
|
-
let message;
|
|
1731
|
-
if (!await contractStore.has(parameters[0])) {
|
|
1732
|
-
message = await peernet.get(parameters[0], 'contract');
|
|
1733
|
-
message = await new ContractMessage(message);
|
|
1734
|
-
await contractStore.put(await message.hash(), message.encoded);
|
|
1735
|
-
}
|
|
1736
|
-
if (!message) {
|
|
1737
|
-
message = await contractStore.get(parameters[0]);
|
|
1738
|
-
message = await new ContractMessage(message);
|
|
1739
|
-
}
|
|
1740
|
-
if (!await this.has(await message.hash()))
|
|
1741
|
-
await this.#runContract(message);
|
|
1742
|
-
}
|
|
1743
|
-
}
|
|
1744
|
-
catch (error) {
|
|
1745
|
-
throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`);
|
|
1746
|
-
}
|
|
1747
|
-
return new Promise((resolve, reject) => {
|
|
1748
|
-
const id = randombytes(20).toString('hex');
|
|
1749
|
-
const onmessage = message => {
|
|
1750
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1751
|
-
if (message?.error)
|
|
1752
|
-
reject(message.error);
|
|
1753
|
-
else
|
|
1754
|
-
resolve(message);
|
|
1755
|
-
};
|
|
1756
|
-
pubsub.subscribe(id, onmessage);
|
|
1757
|
-
this.worker.postMessage({
|
|
1758
|
-
type: 'execute',
|
|
1759
|
-
id,
|
|
1760
|
-
input: {
|
|
1761
|
-
contract,
|
|
1762
|
-
method,
|
|
1763
|
-
params: parameters
|
|
1764
|
-
}
|
|
1765
|
-
});
|
|
1766
|
-
});
|
|
1767
|
-
}
|
|
1768
|
-
get(contract, method, parameters) {
|
|
1769
|
-
return new Promise((resolve, reject) => {
|
|
1770
|
-
const id = randombytes(20).toString();
|
|
1771
|
-
const onmessage = message => {
|
|
1772
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1773
|
-
resolve(message);
|
|
1774
|
-
};
|
|
1775
|
-
pubsub.subscribe(id, onmessage);
|
|
1776
|
-
this.worker.postMessage({
|
|
1777
|
-
type: 'get',
|
|
1778
|
-
id,
|
|
1779
|
-
input: {
|
|
1780
|
-
contract,
|
|
1781
|
-
method,
|
|
1782
|
-
params: parameters
|
|
1783
|
-
}
|
|
1784
|
-
});
|
|
1785
|
-
});
|
|
1786
|
-
}
|
|
1787
|
-
async has(address) {
|
|
1788
|
-
return new Promise((resolve, reject) => {
|
|
1789
|
-
const id = randombytes(20).toString('hex');
|
|
1790
|
-
const onmessage = message => {
|
|
1791
|
-
pubsub.unsubscribe(id, onmessage);
|
|
1792
|
-
if (message?.error)
|
|
1793
|
-
reject(message.error);
|
|
1794
|
-
else
|
|
1795
|
-
resolve(message);
|
|
1796
|
-
};
|
|
1797
|
-
pubsub.subscribe(id, onmessage);
|
|
1798
|
-
this.worker.postMessage({
|
|
1799
|
-
type: 'has',
|
|
1800
|
-
id,
|
|
1801
|
-
input: {
|
|
1802
|
-
address
|
|
1803
|
-
}
|
|
1804
|
-
});
|
|
1805
|
-
});
|
|
1806
|
-
}
|
|
1807
|
-
async delete(hash) {
|
|
1808
|
-
return contractStore.delete(hash);
|
|
1809
|
-
}
|
|
1810
|
-
/**
|
|
1811
|
-
*
|
|
1812
|
-
* @returns Promise
|
|
1813
|
-
*/
|
|
1814
|
-
async deleteAll() {
|
|
1815
|
-
let hashes = await contractStore.get();
|
|
1816
|
-
hashes = Object.keys(hashes).map(hash => this.delete(hash));
|
|
1817
|
-
return Promise.all(hashes);
|
|
1632
|
+
#contracts = {}
|
|
1633
|
+
#nonces = {}
|
|
1634
|
+
lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
|
|
1635
|
+
|
|
1636
|
+
constructor(blocks) {
|
|
1637
|
+
return this.#init(blocks)
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
#createMessage(sender = peernet.selectedAccount) {
|
|
1641
|
+
return {
|
|
1642
|
+
sender,
|
|
1643
|
+
call: this.execute,
|
|
1644
|
+
staticCall: this.get.bind(this)
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
async #onmessage(data) {
|
|
1649
|
+
switch (data.type) {
|
|
1650
|
+
case 'contractError': {
|
|
1651
|
+
console.warn(`removing contract ${await data.hash()}`);
|
|
1652
|
+
await contractStore.delete(await data.hash());
|
|
1653
|
+
break
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
case 'initError': {
|
|
1657
|
+
console.error(`init error: ${data.message}`);
|
|
1658
|
+
break
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
case 'executionError': {
|
|
1662
|
+
// console.warn(`error executing transaction ${data.message}`);
|
|
1663
|
+
pubsub.publish(data.id, {error: data.message});
|
|
1664
|
+
break
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
case 'debug': {
|
|
1668
|
+
for (const message of data.messages) debug(message);
|
|
1669
|
+
break
|
|
1670
|
+
}
|
|
1671
|
+
case 'machine-ready': {
|
|
1672
|
+
this.lastBlock = data.lastBlock;
|
|
1673
|
+
pubsub.publish('machine.ready', true);
|
|
1674
|
+
break
|
|
1675
|
+
}
|
|
1676
|
+
case 'response': {
|
|
1677
|
+
pubsub.publish(data.id, data.value || false);
|
|
1678
|
+
break
|
|
1679
|
+
}
|
|
1818
1680
|
}
|
|
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
|
+
}
|
|
1819
1838
|
}
|
|
1820
1839
|
|
|
1821
1840
|
class Jobber {
|
|
@@ -1922,7 +1941,7 @@ class State extends Contract {
|
|
|
1922
1941
|
super();
|
|
1923
1942
|
}
|
|
1924
1943
|
async init() {
|
|
1925
|
-
this.jobber = new Jobber(
|
|
1944
|
+
this.jobber = new Jobber(30000);
|
|
1926
1945
|
if (super.init)
|
|
1927
1946
|
await super.init();
|
|
1928
1947
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
@@ -2286,7 +2305,7 @@ class Chain extends State {
|
|
|
2286
2305
|
console.log('epoch');
|
|
2287
2306
|
const validators = await this.staticCall(addresses.validators, 'validators');
|
|
2288
2307
|
console.log({ validators });
|
|
2289
|
-
if (!validators[
|
|
2308
|
+
if (!validators[peernet.selectedAccount]?.active)
|
|
2290
2309
|
return;
|
|
2291
2310
|
const start = Date.now();
|
|
2292
2311
|
try {
|
|
@@ -2368,6 +2387,7 @@ class Chain extends State {
|
|
|
2368
2387
|
globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
2369
2388
|
globalThis.peernet.subscribe('invalid-transaction', this.#invalidTransaction.bind(this));
|
|
2370
2389
|
globalThis.peernet.subscribe('send-transaction', this.#sendTransaction.bind(this));
|
|
2390
|
+
globalThis.peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
|
|
2371
2391
|
globalThis.peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
|
|
2372
2392
|
globalThis.pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
|
|
2373
2393
|
// todo some functions rely on state
|
|
@@ -2385,6 +2405,9 @@ class Chain extends State {
|
|
|
2385
2405
|
}, validatorInfo.timeout);
|
|
2386
2406
|
this.#jail.push(validatorInfo.address);
|
|
2387
2407
|
}
|
|
2408
|
+
#addTransaction(message) {
|
|
2409
|
+
console.log(message);
|
|
2410
|
+
}
|
|
2388
2411
|
async #prepareRequest(request) {
|
|
2389
2412
|
let node = await new globalThis.peernet.protos['peernet-request']({ request });
|
|
2390
2413
|
return globalThis.peernet.prepareMessage(node);
|
|
@@ -2399,13 +2422,26 @@ class Chain extends State {
|
|
|
2399
2422
|
if (!peer.version || peer.version !== this.version)
|
|
2400
2423
|
return;
|
|
2401
2424
|
const lastBlock = await this.#makeRequest(peer, 'lastBlock');
|
|
2425
|
+
let transactionsInPool = await this.#makeRequest(peer, 'transactionPool');
|
|
2426
|
+
const transactions = await globalThis.transactionPoolStore.keys();
|
|
2427
|
+
console.log({ transactionsInPool });
|
|
2428
|
+
const transactionsToGet = [];
|
|
2429
|
+
for (const key of transactionsInPool) {
|
|
2430
|
+
if (!transactions.includes(key))
|
|
2431
|
+
transactionsToGet.push(transactionPoolStore.put(key, (await peernet.get(key))));
|
|
2432
|
+
}
|
|
2433
|
+
await Promise.all(transactionsToGet);
|
|
2402
2434
|
if (Object.keys(lastBlock).length > 0) {
|
|
2403
2435
|
if (!this.lastBlock || !this.blocks[this.blocks.length - 1]?.loaded || lastBlock && lastBlock.index > this.lastBlock?.index || !this.loaded && !this.resolving) {
|
|
2404
2436
|
this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
2405
2437
|
await this.syncChain(lastBlock);
|
|
2406
2438
|
// if (await this.hasTransactionToHandle() && this.#participating) this.#runEpoch()
|
|
2407
2439
|
}
|
|
2440
|
+
else if (!this.knownBlocks)
|
|
2441
|
+
this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
2408
2442
|
}
|
|
2443
|
+
if (this.#participating)
|
|
2444
|
+
this.#runEpoch();
|
|
2409
2445
|
}
|
|
2410
2446
|
#epochTimeout;
|
|
2411
2447
|
async #transactionPoolHandler() {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-
|
|
2
|
-
import './index-
|
|
1
|
+
import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-5fe4a30f.js';
|
|
2
|
+
import './index-c3f4012c.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @params {String} network
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-3cbee72b.js';
|
|
2
|
+
import './index-c3f4012c.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @params {String} network
|
|
6
|
+
* @return {object} { identity, accounts, config }
|
|
7
|
+
*/
|
|
8
|
+
var index = async (password, network) => {
|
|
9
|
+
if (!password)
|
|
10
|
+
throw new Error('wallets need to be password protected.');
|
|
11
|
+
let wallet = new MultiWallet(network);
|
|
12
|
+
/**
|
|
13
|
+
* @type {string}
|
|
14
|
+
*/
|
|
15
|
+
let mnemonic = await wallet.generate(password);
|
|
16
|
+
wallet = new MultiWallet(network);
|
|
17
|
+
await wallet.recover(mnemonic, password, network);
|
|
18
|
+
mnemonic = new Uint8Array(await encrypt(password, mnemonic));
|
|
19
|
+
const multiWIF = new Uint8Array(await encrypt(password, await wallet.multiWIF));
|
|
20
|
+
/**
|
|
21
|
+
* @type {object}
|
|
22
|
+
*/
|
|
23
|
+
const external = await wallet.account(1).external(1);
|
|
24
|
+
const externalAddress = await external.address;
|
|
25
|
+
const internal = await wallet.account(1).internal(1);
|
|
26
|
+
const internalAddress = await internal.address;
|
|
27
|
+
return {
|
|
28
|
+
identity: {
|
|
29
|
+
mnemonic: base58$1.encode(mnemonic),
|
|
30
|
+
multiWIF: base58$1.encode(multiWIF),
|
|
31
|
+
walletId: await external.id
|
|
32
|
+
},
|
|
33
|
+
accounts: [['main account', externalAddress, internalAddress]]
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { index as default };
|