@leofcoin/chain 1.5.68 → 1.6.0
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 +77 -79
- package/exports/browser/workers/machine-worker.js +18 -16
- package/exports/chain.js +69 -71
- package/exports/protocol.d.ts +1 -1
- package/exports/state.d.ts +1 -1
- package/exports/transaction.d.ts +1 -8
- package/exports/workers/machine-worker.js +18 -16
- package/package.json +8 -8
package/exports/chain.js
CHANGED
|
@@ -9,7 +9,7 @@ import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
|
9
9
|
import { ContractDeploymentError, ExecutionError, isResolveError, ResolveError, isExecutionError } from '@leofcoin/errors';
|
|
10
10
|
|
|
11
11
|
const limit = 1800;
|
|
12
|
-
const transactionLimit =
|
|
12
|
+
const transactionLimit = 1000;
|
|
13
13
|
const requestTimeout = 30000;
|
|
14
14
|
const syncTimeout = 30000;
|
|
15
15
|
class Protocol {
|
|
@@ -158,10 +158,7 @@ class Transaction extends Protocol {
|
|
|
158
158
|
}
|
|
159
159
|
async createTransaction(transaction) {
|
|
160
160
|
return {
|
|
161
|
-
|
|
162
|
-
to: transaction.to,
|
|
163
|
-
method: transaction.method,
|
|
164
|
-
params: transaction.params,
|
|
161
|
+
...transaction,
|
|
165
162
|
timestamp: transaction.timestamp || Date.now(),
|
|
166
163
|
nonce: transaction.nonce || (await this.getNonce(transaction.from)) + 1
|
|
167
164
|
};
|
|
@@ -284,13 +281,6 @@ class Machine {
|
|
|
284
281
|
// @ts-ignore
|
|
285
282
|
return this.#init(blocks);
|
|
286
283
|
}
|
|
287
|
-
#createMessage(sender = peernet.selectedAccount) {
|
|
288
|
-
return {
|
|
289
|
-
sender,
|
|
290
|
-
call: this.execute,
|
|
291
|
-
staticCall: this.get.bind(this)
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
284
|
async #onmessage(data) {
|
|
295
285
|
switch (data.type) {
|
|
296
286
|
case 'transactionLoaded': {
|
|
@@ -411,8 +401,7 @@ class Machine {
|
|
|
411
401
|
if (await stateStore.has('lastBlock')) {
|
|
412
402
|
this.states.lastBlock = JSON.parse(new TextDecoder().decode(await stateStore.get('lastBlock')));
|
|
413
403
|
this.states.states = JSON.parse(new TextDecoder().decode(await stateStore.get('states')));
|
|
414
|
-
console.log(
|
|
415
|
-
console.log({ states: this.states });
|
|
404
|
+
console.log({ balances: this.states.states[addresses.nativeToken].balances });
|
|
416
405
|
}
|
|
417
406
|
const message = {
|
|
418
407
|
type: 'init',
|
|
@@ -786,25 +775,39 @@ class State extends Contract {
|
|
|
786
775
|
throw error;
|
|
787
776
|
}
|
|
788
777
|
try {
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
778
|
+
let localBlockHash;
|
|
779
|
+
try {
|
|
780
|
+
const localBlock = await globalThis.chainStore.get('lastBlock');
|
|
781
|
+
localBlockHash = new TextDecoder().decode(localBlock);
|
|
782
|
+
}
|
|
783
|
+
catch (error) { }
|
|
784
|
+
if (localBlockHash && localBlockHash !== '0x0') {
|
|
793
785
|
const blockMessage = new BlockMessage(await peernet.get(localBlockHash, 'block'));
|
|
794
|
-
|
|
795
|
-
|
|
786
|
+
try {
|
|
787
|
+
const states = {
|
|
788
|
+
lastBlock: JSON.parse(new TextDecoder().decode(await globalThis.stateStore.get('lastBlock')))
|
|
789
|
+
};
|
|
790
|
+
if (blockMessage.decoded.index > states.lastBlock.index)
|
|
791
|
+
await this.resolveBlocks();
|
|
792
|
+
}
|
|
793
|
+
catch (error) {
|
|
794
|
+
// no states found, try resolving blocks
|
|
796
795
|
await this.resolveBlocks();
|
|
796
|
+
}
|
|
797
797
|
}
|
|
798
798
|
else {
|
|
799
799
|
await this.resolveBlocks();
|
|
800
800
|
}
|
|
801
801
|
this.#machine = await new Machine(this.#blocks);
|
|
802
802
|
const lastBlock = await this.#machine.lastBlock;
|
|
803
|
-
|
|
803
|
+
if (lastBlock.hash !== '0x0') {
|
|
804
|
+
this.updateState(new BlockMessage(lastBlock));
|
|
805
|
+
}
|
|
804
806
|
this.#loaded = true;
|
|
805
807
|
// await this.#loadBlocks(this.#blocks)
|
|
806
808
|
}
|
|
807
809
|
catch (error) {
|
|
810
|
+
console.log('e');
|
|
808
811
|
if (isResolveError(error)) {
|
|
809
812
|
console.error(error);
|
|
810
813
|
}
|
|
@@ -814,14 +817,13 @@ class State extends Contract {
|
|
|
814
817
|
async updateState(message) {
|
|
815
818
|
try {
|
|
816
819
|
const hash = await message.hash();
|
|
817
|
-
// await this.state.updateState(message)
|
|
818
820
|
await globalThis.chainStore.put('lastBlock', hash);
|
|
819
821
|
globalThis.pubsub.publish('lastBlock', message.encoded);
|
|
822
|
+
await this.#machine.updateState();
|
|
820
823
|
}
|
|
821
824
|
catch (error) {
|
|
822
825
|
console.error(error);
|
|
823
826
|
}
|
|
824
|
-
await this.#machine.updateState();
|
|
825
827
|
}
|
|
826
828
|
getLatestBlock() {
|
|
827
829
|
// @ts-ignore
|
|
@@ -842,9 +844,9 @@ class State extends Contract {
|
|
|
842
844
|
}
|
|
843
845
|
async #resolveBlock(hash) {
|
|
844
846
|
let index = this.#blockHashMap.get(hash);
|
|
845
|
-
if (this.#blocks[index
|
|
846
|
-
if (this.#blocks[index
|
|
847
|
-
return this.resolveBlock(this.#blocks[index
|
|
847
|
+
if (this.#blocks[index]) {
|
|
848
|
+
if (this.#blocks[index].previousHash !== '0x0') {
|
|
849
|
+
return this.resolveBlock(this.#blocks[index].previousHash);
|
|
848
850
|
}
|
|
849
851
|
else {
|
|
850
852
|
return;
|
|
@@ -855,11 +857,11 @@ class State extends Contract {
|
|
|
855
857
|
index = block.decoded.index;
|
|
856
858
|
const size = block.encoded.length > 0 ? block.encoded.length : block.encoded.byteLength;
|
|
857
859
|
this.#totalSize += size;
|
|
858
|
-
this.#blocks[index
|
|
860
|
+
this.#blocks[index] = { hash, ...block.decoded };
|
|
859
861
|
this.#blockHashMap.set(hash, index);
|
|
860
862
|
debug$1(`resolved block: ${hash} @${index} ${formatBytes(size)}`);
|
|
861
863
|
globalThis.pubsub.publish('block-resolved', { hash, index });
|
|
862
|
-
this.#lastResolved = this.#blocks[index
|
|
864
|
+
this.#lastResolved = this.#blocks[index];
|
|
863
865
|
this.#lastResolvedTime = Date.now();
|
|
864
866
|
}
|
|
865
867
|
catch (error) {
|
|
@@ -1271,7 +1273,7 @@ class Chain extends VersionControl {
|
|
|
1271
1273
|
console.log('epoch');
|
|
1272
1274
|
const validators = await this.staticCall(addresses.validators, 'validators');
|
|
1273
1275
|
console.log({ validators });
|
|
1274
|
-
if (!validators
|
|
1276
|
+
if (!validators.includes(peernet.selectedAccount))
|
|
1275
1277
|
return;
|
|
1276
1278
|
const start = Date.now();
|
|
1277
1279
|
try {
|
|
@@ -1431,17 +1433,17 @@ class Chain extends VersionControl {
|
|
|
1431
1433
|
await Promise.all(blockMessage.decoded.transactions
|
|
1432
1434
|
// @ts-ignore
|
|
1433
1435
|
.map(async (transaction) => {
|
|
1436
|
+
// @ts-ignore
|
|
1434
1437
|
let hash = transaction.hash;
|
|
1435
1438
|
if (!hash) {
|
|
1436
1439
|
hash = await new TransactionMessage(transaction).hash();
|
|
1440
|
+
// @ts-ignore
|
|
1437
1441
|
transaction.hash = hash;
|
|
1438
1442
|
}
|
|
1439
1443
|
(await transactionPoolStore.has(hash)) && (await transactionPoolStore.delete(hash));
|
|
1440
1444
|
return transaction;
|
|
1441
1445
|
}));
|
|
1442
1446
|
await globalThis.blockStore.put(hash, blockMessage.encoded);
|
|
1443
|
-
if ((await this.lastBlock).index < Number(blockMessage.decoded.index))
|
|
1444
|
-
await this.updateState(blockMessage);
|
|
1445
1447
|
debug(`added block: ${hash}`);
|
|
1446
1448
|
let promises = [];
|
|
1447
1449
|
let contracts = [];
|
|
@@ -1462,13 +1464,10 @@ class Chain extends VersionControl {
|
|
|
1462
1464
|
globalThis.pubsub.publish('account-transaction-processed', transaction);
|
|
1463
1465
|
await globalThis.accountsStore.put(transaction.from, String(transaction.nonce));
|
|
1464
1466
|
}
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
// console.log(state);
|
|
1470
|
-
// }
|
|
1471
|
-
// await this.machine.addLoadedBlock({ ...blockMessage.decoded, loaded: true, hash: await blockMessage.hash() })
|
|
1467
|
+
if ((await this.lastBlock).index < Number(blockMessage.decoded.index)) {
|
|
1468
|
+
await this.machine.addLoadedBlock({ ...blockMessage.decoded, loaded: true, hash: await blockMessage.hash() });
|
|
1469
|
+
await this.updateState(blockMessage);
|
|
1470
|
+
}
|
|
1472
1471
|
globalThis.pubsub.publish('block-processed', blockMessage.decoded);
|
|
1473
1472
|
}
|
|
1474
1473
|
catch (error) {
|
|
@@ -1531,6 +1530,7 @@ class Chain extends VersionControl {
|
|
|
1531
1530
|
}
|
|
1532
1531
|
// todo filter tx that need to wait on prev nonce
|
|
1533
1532
|
async #createBlock(limit = this.transactionLimit) {
|
|
1533
|
+
console.log(await globalThis.transactionPoolStore.size());
|
|
1534
1534
|
// vote for transactions
|
|
1535
1535
|
if ((await globalThis.transactionPoolStore.size()) === 0)
|
|
1536
1536
|
return;
|
|
@@ -1548,19 +1548,22 @@ class Chain extends VersionControl {
|
|
|
1548
1548
|
index: 0
|
|
1549
1549
|
};
|
|
1550
1550
|
const latestTransactions = await this.machine.latestTransactions();
|
|
1551
|
+
console.log({ latestTransactions });
|
|
1551
1552
|
// exclude failing tx
|
|
1552
1553
|
transactions = await this.promiseTransactions(transactions);
|
|
1553
|
-
const priority = transactions
|
|
1554
|
-
|
|
1555
|
-
.sort((a, b) => a.nonce - b.nonce)
|
|
1556
|
-
|
|
1554
|
+
const priority = transactions
|
|
1555
|
+
.filter((transaction) => transaction.decoded.priority)
|
|
1556
|
+
.sort((a, b) => a.decoded.nonce - b.decoded.nonce);
|
|
1557
|
+
for (const transaction of priority) {
|
|
1558
|
+
await this.#handleTransaction(transaction, latestTransactions, block);
|
|
1559
|
+
}
|
|
1557
1560
|
await Promise.all(transactions
|
|
1558
|
-
.filter((transaction) => !transaction.priority)
|
|
1561
|
+
.filter((transaction) => !transaction.decoded.priority)
|
|
1559
1562
|
.map((transaction) => this.#handleTransaction(transaction, latestTransactions, block)));
|
|
1560
1563
|
// don't add empty block
|
|
1561
1564
|
if (block.transactions.length === 0)
|
|
1562
1565
|
return;
|
|
1563
|
-
const validators = await this.staticCall(addresses.validators, 'validators');
|
|
1566
|
+
const validators = (await this.staticCall(addresses.validators, 'validators'));
|
|
1564
1567
|
// block.validators = Object.keys(block.validators).reduce((set, key) => {
|
|
1565
1568
|
// if (block.validators[key].active) {
|
|
1566
1569
|
// push({
|
|
@@ -1572,27 +1575,25 @@ class Chain extends VersionControl {
|
|
|
1572
1575
|
for (const entry of globalThis.peernet.peers) {
|
|
1573
1576
|
peers[entry[0]] = entry[1];
|
|
1574
1577
|
}
|
|
1575
|
-
for (const validator of
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
const bw = await peer.request(node.encoded);
|
|
1583
|
-
block.validators.push({
|
|
1584
|
-
address: validator,
|
|
1585
|
-
bw: bw.up + bw.down
|
|
1586
|
-
});
|
|
1587
|
-
}
|
|
1588
|
-
catch { }
|
|
1589
|
-
}
|
|
1590
|
-
else if (globalThis.peernet.selectedAccount === validator) {
|
|
1578
|
+
for (const validator of validators) {
|
|
1579
|
+
const peer = peers[validator];
|
|
1580
|
+
if (peer && peer.connected && peer.version === this.version) {
|
|
1581
|
+
let data = await new BWRequestMessage();
|
|
1582
|
+
const node = await globalThis.peernet.prepareMessage(data.encoded);
|
|
1583
|
+
try {
|
|
1584
|
+
const bw = await peer.request(node.encoded);
|
|
1591
1585
|
block.validators.push({
|
|
1592
|
-
address:
|
|
1593
|
-
bw:
|
|
1586
|
+
address: validator,
|
|
1587
|
+
bw: bw.up + bw.down
|
|
1594
1588
|
});
|
|
1595
1589
|
}
|
|
1590
|
+
catch { }
|
|
1591
|
+
}
|
|
1592
|
+
else if (globalThis.peernet.selectedAccount === validator) {
|
|
1593
|
+
block.validators.push({
|
|
1594
|
+
address: globalThis.peernet.selectedAccount,
|
|
1595
|
+
bw: globalThis.peernet.bw.up + globalThis.peernet.bw.down
|
|
1596
|
+
});
|
|
1596
1597
|
}
|
|
1597
1598
|
}
|
|
1598
1599
|
block.validators = block.validators.map((validator) => {
|
|
@@ -1621,15 +1622,13 @@ class Chain extends VersionControl {
|
|
|
1621
1622
|
let blockMessage = await new BlockMessage(block);
|
|
1622
1623
|
const hash = await blockMessage.hash();
|
|
1623
1624
|
await globalThis.peernet.put(hash, blockMessage.encoded, 'block');
|
|
1624
|
-
await this.updateState(blockMessage);
|
|
1625
1625
|
await this.machine.addLoadedBlock({ ...blockMessage.decoded, loaded: true, hash: await blockMessage.hash() });
|
|
1626
|
+
await this.updateState(blockMessage);
|
|
1626
1627
|
debug(`created block: ${hash} @${block.index}`);
|
|
1627
1628
|
globalThis.peernet.publish('add-block', blockMessage.encoded);
|
|
1628
1629
|
globalThis.pubsub.publish('add-block', blockMessage.decoded);
|
|
1629
1630
|
}
|
|
1630
1631
|
catch (error) {
|
|
1631
|
-
console.log(error);
|
|
1632
|
-
console.log('eeeee');
|
|
1633
1632
|
throw new Error(`invalid block ${block}`);
|
|
1634
1633
|
}
|
|
1635
1634
|
// data = await this.machine.execute(to, method, params)
|
|
@@ -1647,8 +1646,6 @@ class Chain extends VersionControl {
|
|
|
1647
1646
|
this.#runEpoch();
|
|
1648
1647
|
}
|
|
1649
1648
|
catch (e) {
|
|
1650
|
-
console.log(e);
|
|
1651
|
-
console.log('rrrrr');
|
|
1652
1649
|
globalThis.peernet.publish('invalid-transaction', hash);
|
|
1653
1650
|
throw new Error('invalid transaction');
|
|
1654
1651
|
}
|
|
@@ -1679,8 +1676,9 @@ class Chain extends VersionControl {
|
|
|
1679
1676
|
* @param {Address} sender
|
|
1680
1677
|
* @returns {globalMessage}
|
|
1681
1678
|
*/
|
|
1682
|
-
#createMessage(sender = globalThis.peernet.selectedAccount) {
|
|
1679
|
+
#createMessage(sender = globalThis.peernet.selectedAccount, contract) {
|
|
1683
1680
|
return {
|
|
1681
|
+
contract,
|
|
1684
1682
|
sender,
|
|
1685
1683
|
call: this.call,
|
|
1686
1684
|
staticCall: this.staticCall
|
|
@@ -1695,7 +1693,7 @@ class Chain extends VersionControl {
|
|
|
1695
1693
|
* @returns
|
|
1696
1694
|
*/
|
|
1697
1695
|
internalCall(sender, contract, method, parameters) {
|
|
1698
|
-
globalThis.msg = this.#createMessage(sender);
|
|
1696
|
+
globalThis.msg = this.#createMessage(sender, contract);
|
|
1699
1697
|
return this.machine.execute(contract, method, parameters);
|
|
1700
1698
|
}
|
|
1701
1699
|
/**
|
|
@@ -1706,11 +1704,11 @@ class Chain extends VersionControl {
|
|
|
1706
1704
|
* @returns
|
|
1707
1705
|
*/
|
|
1708
1706
|
call(contract, method, parameters) {
|
|
1709
|
-
globalThis.msg = this.#createMessage();
|
|
1707
|
+
globalThis.msg = this.#createMessage(peernet.selectedAccount, contract);
|
|
1710
1708
|
return this.machine.execute(contract, method, parameters);
|
|
1711
1709
|
}
|
|
1712
1710
|
staticCall(contract, method, parameters) {
|
|
1713
|
-
globalThis.msg = this.#createMessage();
|
|
1711
|
+
globalThis.msg = this.#createMessage(peernet.selectedAccount, contract);
|
|
1714
1712
|
return this.machine.get(contract, method, parameters);
|
|
1715
1713
|
}
|
|
1716
1714
|
mint(to, amount) {
|
package/exports/protocol.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ChainConfig } from './types.js';
|
|
2
2
|
export declare const limit = 1800;
|
|
3
|
-
export declare const transactionLimit =
|
|
3
|
+
export declare const transactionLimit = 1000;
|
|
4
4
|
export declare const requestTimeout = 30000;
|
|
5
5
|
export declare const syncTimeout = 30000;
|
|
6
6
|
export declare class Protocol {
|
package/exports/state.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export default class State extends Contract {
|
|
|
38
38
|
*/
|
|
39
39
|
clearAll(): Promise<void>;
|
|
40
40
|
init(): Promise<void>;
|
|
41
|
-
updateState(message:
|
|
41
|
+
updateState(message: BlockMessage): Promise<void>;
|
|
42
42
|
getLatestBlock(): Promise<BlockMessage['decoded']>;
|
|
43
43
|
getAndPutBlock(hash: string): Promise<BlockMessage>;
|
|
44
44
|
resolveBlock(hash: any): any;
|
package/exports/transaction.d.ts
CHANGED
|
@@ -30,14 +30,7 @@ export default class Transaction extends Protocol {
|
|
|
30
30
|
validateNonce(address: any, nonce: any): Promise<void>;
|
|
31
31
|
isTransactionMessage(message: any): boolean;
|
|
32
32
|
createTransactionMessage(transaction: any, signature: any): Promise<TransactionMessage>;
|
|
33
|
-
createTransaction(transaction: any): Promise<
|
|
34
|
-
from: any;
|
|
35
|
-
to: any;
|
|
36
|
-
method: any;
|
|
37
|
-
params: any;
|
|
38
|
-
timestamp: any;
|
|
39
|
-
nonce: any;
|
|
40
|
-
}>;
|
|
33
|
+
createTransaction(transaction: any): Promise<any>;
|
|
41
34
|
sendTransaction(message: any): Promise<{
|
|
42
35
|
hash: any;
|
|
43
36
|
data: any;
|