@leofcoin/chain 1.5.67 → 1.5.68
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/{_polyfill-node.child_process-BsjYmFff.js → _polyfill-node.child_process-CblghArn.js} +48 -47
- package/exports/browser/{_polyfill-node.url-Bp9XkJZf.js → _polyfill-node.url-BLK_MhDf.js} +1 -1
- package/exports/browser/{browser-Ei0BXMlu-C1_XUL9Y.js → browser-Ei0BXMlu-C0woTcHa.js} +2 -2
- package/exports/browser/chain.js +83 -21
- package/exports/browser/{client-A009z8av-DQfd3vOY.js → client-A009z8av-Ssch8Yea.js} +4 -4
- package/exports/browser/{index-G74WLzL7-Dt2dwaXq.js → index-G74WLzL7-BnLRqHoy.js} +2 -2
- package/exports/browser/{index-YQrIDBUQ-c7011Ab4.js → index-YQrIDBUQ-APwDgUUG.js} +2 -2
- package/exports/browser/{messages-lWRTai7t-ClhClfC1.js → messages-lWRTai7t-DJnHZSdM.js} +2 -2
- package/exports/browser/{node-browser-DqIklEKv.js → node-browser-CeM_F-HL.js} +23 -4
- package/exports/browser/node-browser.js +2 -2
- package/exports/browser/workers/block-worker.js +1 -1
- package/exports/browser/workers/machine-worker.js +218 -91
- package/exports/browser/workers/{worker-CltAyHf1.js → worker-CbAak_hM.js} +47 -46
- package/exports/chain.js +75 -13
- package/exports/machine.d.ts +8 -0
- package/exports/simplifiers/state.d.ts +3 -0
- package/exports/workers/block-worker.js +1 -1
- package/exports/workers/machine-worker.js +218 -91
- package/exports/workers/{worker-CltAyHf1.js → worker-CbAak_hM.js} +47 -46
- package/package.json +110 -109
|
@@ -12430,55 +12430,56 @@ class ValidatorMessage extends FormatInterface {
|
|
|
12430
12430
|
}
|
|
12431
12431
|
|
|
12432
12432
|
var proto$1 = {
|
|
12433
|
-
|
|
12434
|
-
|
|
12435
|
-
|
|
12436
|
-
|
|
12437
|
-
|
|
12438
|
-
|
|
12439
|
-
|
|
12433
|
+
index: Number(),
|
|
12434
|
+
previousHash: String(),
|
|
12435
|
+
timestamp: Number(),
|
|
12436
|
+
reward: BigNumber.from(0),
|
|
12437
|
+
fees: BigNumber.from(0),
|
|
12438
|
+
transactions: new Uint8Array(),
|
|
12439
|
+
validators: new Uint8Array()
|
|
12440
12440
|
};
|
|
12441
12441
|
|
|
12442
12442
|
class BlockMessage extends FormatInterface {
|
|
12443
|
-
|
|
12444
|
-
|
|
12445
|
-
|
|
12446
|
-
|
|
12447
|
-
|
|
12448
|
-
|
|
12449
|
-
|
|
12450
|
-
|
|
12451
|
-
|
|
12452
|
-
decoded
|
|
12453
|
-
|
|
12454
|
-
|
|
12455
|
-
|
|
12456
|
-
|
|
12457
|
-
|
|
12458
|
-
|
|
12459
|
-
|
|
12460
|
-
|
|
12461
|
-
|
|
12462
|
-
|
|
12463
|
-
|
|
12464
|
-
|
|
12465
|
-
|
|
12466
|
-
|
|
12467
|
-
|
|
12468
|
-
|
|
12469
|
-
|
|
12470
|
-
|
|
12471
|
-
|
|
12472
|
-
|
|
12473
|
-
|
|
12474
|
-
|
|
12475
|
-
|
|
12476
|
-
|
|
12477
|
-
|
|
12478
|
-
|
|
12479
|
-
|
|
12480
|
-
|
|
12481
|
-
|
|
12443
|
+
get messageName() {
|
|
12444
|
+
return 'BlockMessage';
|
|
12445
|
+
}
|
|
12446
|
+
constructor(buffer) {
|
|
12447
|
+
if (buffer instanceof BlockMessage)
|
|
12448
|
+
return buffer;
|
|
12449
|
+
const name = 'block-message';
|
|
12450
|
+
super(buffer, proto$1, { name });
|
|
12451
|
+
}
|
|
12452
|
+
encode(decoded) {
|
|
12453
|
+
decoded = decoded || this.decoded;
|
|
12454
|
+
const validators = [];
|
|
12455
|
+
const transactions = [];
|
|
12456
|
+
for (const validator of decoded.validators) {
|
|
12457
|
+
if (validator instanceof ValidatorMessage)
|
|
12458
|
+
validators.push(validator.encode());
|
|
12459
|
+
else
|
|
12460
|
+
validators.push(new ValidatorMessage(validator).encode());
|
|
12461
|
+
}
|
|
12462
|
+
for (const transaction of decoded.transactions) {
|
|
12463
|
+
if (transaction instanceof TransactionMessage)
|
|
12464
|
+
transactions.push(transaction.encode());
|
|
12465
|
+
else
|
|
12466
|
+
transactions.push(new TransactionMessage(transaction).encode());
|
|
12467
|
+
}
|
|
12468
|
+
return super.encode({
|
|
12469
|
+
...decoded,
|
|
12470
|
+
validators: index$5(validators),
|
|
12471
|
+
transactions: index$5(transactions)
|
|
12472
|
+
});
|
|
12473
|
+
}
|
|
12474
|
+
decode(encoded) {
|
|
12475
|
+
encoded = encoded || this.encoded;
|
|
12476
|
+
super.decode(encoded);
|
|
12477
|
+
// @ts-ignore
|
|
12478
|
+
this.decoded.transactions = index$4(this.decoded.transactions).map((transaction) => new TransactionMessage(transaction).decoded);
|
|
12479
|
+
// @ts-ignore
|
|
12480
|
+
this.decoded.validators = index$4(this.decoded.validators).map((validator) => new ValidatorMessage(validator).decoded);
|
|
12481
|
+
return this.decoded;
|
|
12482
|
+
}
|
|
12482
12483
|
}
|
|
12483
12484
|
|
|
12484
12485
|
var proto = {
|
package/exports/chain.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug';
|
|
2
2
|
import { formatBytes, BigNumber, formatUnits, parseUnits } from '@leofcoin/utils';
|
|
3
3
|
import { TransactionMessage, BlockMessage, ContractMessage, BWMessage, BWRequestMessage } from '@leofcoin/messages';
|
|
4
|
-
import addresses, { contractFactory
|
|
4
|
+
import addresses, { contractFactory } from '@leofcoin/addresses';
|
|
5
5
|
import { calculateFee, createContractMessage, signTransaction, contractFactoryMessage, nativeTokenMessage, validatorsMessage, nameServiceMessage } from '@leofcoin/lib';
|
|
6
6
|
import semver from 'semver';
|
|
7
7
|
import { randombytes } from '@leofcoin/crypto';
|
|
@@ -273,9 +273,14 @@ class Contract extends Transaction {
|
|
|
273
273
|
// import State from './state'
|
|
274
274
|
const debug$2 = globalThis.createDebugger('leofcoin/machine');
|
|
275
275
|
class Machine {
|
|
276
|
-
#contracts = {};
|
|
277
|
-
#nonces = {};
|
|
278
276
|
constructor(blocks) {
|
|
277
|
+
this.states = {
|
|
278
|
+
states: {},
|
|
279
|
+
lastBlock: {
|
|
280
|
+
index: 0,
|
|
281
|
+
hash: ''
|
|
282
|
+
}
|
|
283
|
+
};
|
|
279
284
|
// @ts-ignore
|
|
280
285
|
return this.#init(blocks);
|
|
281
286
|
}
|
|
@@ -336,12 +341,54 @@ class Machine {
|
|
|
336
341
|
pubsub.publish(data.id, data.value || false);
|
|
337
342
|
break;
|
|
338
343
|
}
|
|
344
|
+
case 'ask': {
|
|
345
|
+
if (data.question === 'contract') {
|
|
346
|
+
const input = await peernet.get(data.input, 'contract');
|
|
347
|
+
this.worker.postMessage({ id: data.id, input });
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async updateState() {
|
|
353
|
+
try {
|
|
354
|
+
if ((await this.lastBlock).index > this.states.lastBlock.index) {
|
|
355
|
+
// todo only get state for changed contracts
|
|
356
|
+
const blocks = (await this.blocks).slice(this.states.lastBlock.index);
|
|
357
|
+
const contractsToGet = blocks.reduce((set, current) => {
|
|
358
|
+
for (const transaction of current.transactions) {
|
|
359
|
+
const contract = transaction.to;
|
|
360
|
+
if (!set.includes(contract))
|
|
361
|
+
set.push(contract);
|
|
362
|
+
}
|
|
363
|
+
return set;
|
|
364
|
+
}, []);
|
|
365
|
+
const state = {};
|
|
366
|
+
console.log({ contractsToGet });
|
|
367
|
+
if (!contractsToGet.includes(addresses.contractFactory))
|
|
368
|
+
contractsToGet.push(addresses.contractFactory);
|
|
369
|
+
if (!contractsToGet.includes(addresses.nativeToken))
|
|
370
|
+
contractsToGet.push(addresses.nativeToken);
|
|
371
|
+
if (!contractsToGet.includes(addresses.nameService))
|
|
372
|
+
contractsToGet.push(addresses.nameService);
|
|
373
|
+
if (!contractsToGet.includes(addresses.validators))
|
|
374
|
+
contractsToGet.push(addresses.validators);
|
|
375
|
+
await Promise.all(contractsToGet.map(async (contract) => {
|
|
376
|
+
const value = await this.#askWorker('get', { contract, method: 'state', params: [] });
|
|
377
|
+
state[contract] = value;
|
|
378
|
+
}));
|
|
379
|
+
await stateStore.put('lastBlock', JSON.stringify(await this.lastBlock));
|
|
380
|
+
await stateStore.put('states', JSON.stringify(state));
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
console.error(error);
|
|
339
385
|
}
|
|
340
386
|
}
|
|
341
387
|
async #init(blocks) {
|
|
342
388
|
return new Promise(async (resolve) => {
|
|
343
|
-
const machineReady = () => {
|
|
389
|
+
const machineReady = async () => {
|
|
344
390
|
pubsub.unsubscribe('machine.ready', machineReady);
|
|
391
|
+
await this.updateState();
|
|
345
392
|
resolve(this);
|
|
346
393
|
};
|
|
347
394
|
pubsub.subscribe('machine.ready', machineReady);
|
|
@@ -361,18 +408,19 @@ class Machine {
|
|
|
361
408
|
type: 'module'
|
|
362
409
|
});
|
|
363
410
|
this.worker.onmessage(this.#onmessage.bind(this));
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
]);
|
|
411
|
+
if (await stateStore.has('lastBlock')) {
|
|
412
|
+
this.states.lastBlock = JSON.parse(new TextDecoder().decode(await stateStore.get('lastBlock')));
|
|
413
|
+
this.states.states = JSON.parse(new TextDecoder().decode(await stateStore.get('states')));
|
|
414
|
+
console.log(await stateStore.get('states'));
|
|
415
|
+
console.log({ states: this.states });
|
|
416
|
+
}
|
|
371
417
|
const message = {
|
|
372
418
|
type: 'init',
|
|
373
419
|
input: {
|
|
374
|
-
contracts,
|
|
375
420
|
blocks,
|
|
421
|
+
fromState: this.states.lastBlock.index > 0,
|
|
422
|
+
lastBlock: this.states.lastBlock,
|
|
423
|
+
state: this.states.states,
|
|
376
424
|
// @ts-ignore
|
|
377
425
|
peerid: peernet.peerId
|
|
378
426
|
}
|
|
@@ -692,8 +740,10 @@ class State extends Contract {
|
|
|
692
740
|
this.#loadBlockTransactions = (transactions) => Promise.all(transactions.map((transaction) => new TransactionMessage(transaction)));
|
|
693
741
|
this.#getLastTransactions = async () => {
|
|
694
742
|
let lastTransactions = (await Promise.all((await this.blocks)
|
|
743
|
+
// @ts-ignore
|
|
695
744
|
.filter((block) => block.loaded)
|
|
696
745
|
.slice(-24)
|
|
746
|
+
// @ts-ignore
|
|
697
747
|
.map((block) => this.#loadBlockTransactions(block.transactions)))).reduce((all, transactions) => [...all, ...transactions], []);
|
|
698
748
|
return Promise.all(lastTransactions.map((transaction) => transaction.hash()));
|
|
699
749
|
};
|
|
@@ -736,7 +786,18 @@ class State extends Contract {
|
|
|
736
786
|
throw error;
|
|
737
787
|
}
|
|
738
788
|
try {
|
|
739
|
-
await
|
|
789
|
+
const localBlock = await globalThis.chainStore.get('lastBlock');
|
|
790
|
+
console.log({ localBlock });
|
|
791
|
+
const localBlockHash = new TextDecoder().decode(localBlock);
|
|
792
|
+
if (localBlockHash !== '0x0') {
|
|
793
|
+
const blockMessage = new BlockMessage(await peernet.get(localBlockHash, 'block'));
|
|
794
|
+
const states = { lastBlock: JSON.parse(new TextDecoder().decode(await globalThis.stateStore.get('lastBlock'))) };
|
|
795
|
+
if (blockMessage.decoded.index > states.lastBlock.index)
|
|
796
|
+
await this.resolveBlocks();
|
|
797
|
+
}
|
|
798
|
+
else {
|
|
799
|
+
await this.resolveBlocks();
|
|
800
|
+
}
|
|
740
801
|
this.#machine = await new Machine(this.#blocks);
|
|
741
802
|
const lastBlock = await this.#machine.lastBlock;
|
|
742
803
|
this.updateState(new BlockMessage(lastBlock));
|
|
@@ -760,6 +821,7 @@ class State extends Contract {
|
|
|
760
821
|
catch (error) {
|
|
761
822
|
console.error(error);
|
|
762
823
|
}
|
|
824
|
+
await this.#machine.updateState();
|
|
763
825
|
}
|
|
764
826
|
getLatestBlock() {
|
|
765
827
|
// @ts-ignore
|
package/exports/machine.d.ts
CHANGED
|
@@ -2,7 +2,15 @@ import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
|
2
2
|
export default class Machine {
|
|
3
3
|
#private;
|
|
4
4
|
worker: EasyWorker;
|
|
5
|
+
states: {
|
|
6
|
+
states: {};
|
|
7
|
+
lastBlock: {
|
|
8
|
+
index: number;
|
|
9
|
+
hash: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
5
12
|
constructor(blocks: any);
|
|
13
|
+
updateState(): Promise<void>;
|
|
6
14
|
/**
|
|
7
15
|
*
|
|
8
16
|
* @param {Address} contract
|