@leofcoin/chain 1.5.66 → 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.
Files changed (24) hide show
  1. package/exports/browser/{_polyfill-node.child_process-rc1HO9Xs.js → _polyfill-node.child_process-CblghArn.js} +48 -47
  2. package/exports/browser/{_polyfill-node.url-1q06MepH.js → _polyfill-node.url-BLK_MhDf.js} +1 -1
  3. package/exports/browser/{browser-Ei0BXMlu-irDJ6Blw.js → browser-Ei0BXMlu-C0woTcHa.js} +2 -2
  4. package/exports/browser/browser-store.js +96 -15
  5. package/exports/browser/chain.js +118 -32
  6. package/exports/browser/{client-A009z8av-uBa1w1TW.js → client-A009z8av-Ssch8Yea.js} +5 -5
  7. package/exports/browser/{index-G74WLzL7-tmiSHjUc.js → index-G74WLzL7-BnLRqHoy.js} +2 -2
  8. package/exports/browser/{index-YQrIDBUQ-OXiMI4eT.js → index-YQrIDBUQ-APwDgUUG.js} +2 -2
  9. package/exports/browser/{messages-lWRTai7t-Bc6ghkkt.js → messages-lWRTai7t-DJnHZSdM.js} +2 -2
  10. package/exports/browser/{node-browser-TcXF8FDB.js → node-browser-CeM_F-HL.js} +25 -6
  11. package/exports/browser/node-browser.js +2 -2
  12. package/exports/browser/workers/block-worker.js +1 -1
  13. package/exports/browser/workers/machine-worker.js +218 -91
  14. package/exports/browser/workers/{worker-hbo9s9AT.js → worker-CbAak_hM.js} +47 -46
  15. package/exports/chain.js +83 -14
  16. package/exports/machine.d.ts +8 -0
  17. package/exports/simplifiers/state.d.ts +3 -0
  18. package/exports/workers/block-worker.js +1 -1
  19. package/exports/workers/machine-worker.js +218 -91
  20. package/exports/workers/{worker-hbo9s9AT.js → worker-CbAak_hM.js} +47 -46
  21. package/package.json +110 -109
  22. /package/exports/browser/{browser-AyxSBUXj-AyxSBUXj.js → browser-AyxSBUXj-pguCHlVu.js} +0 -0
  23. /package/exports/browser/{password-JCRBtU5A.js → password-oDixGC8h.js} +0 -0
  24. /package/exports/browser/{qr-scanner-worker.min-RaSiJc_R-RaSiJc_R.js → qr-scanner-worker.min-RaSiJc_R-Dy0qkKA4.js} +0 -0
@@ -12430,55 +12430,56 @@ class ValidatorMessage extends FormatInterface {
12430
12430
  }
12431
12431
 
12432
12432
  var proto$1 = {
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()
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
- get messageName() {
12444
- return 'BlockMessage'
12445
- }
12446
- constructor(buffer) {
12447
- if (buffer instanceof BlockMessage) return buffer
12448
- const name = 'block-message';
12449
- super(buffer, proto$1, { name });
12450
- }
12451
- encode(decoded) {
12452
- decoded = decoded || this.decoded;
12453
- const validators = [];
12454
- const transactions = [];
12455
- for (const validator of decoded.validators) {
12456
- if (validator instanceof ValidatorMessage) validators.push(validator.encode());
12457
- else validators.push(new ValidatorMessage(validator).encode());
12458
- }
12459
- for (const transaction of decoded.transactions) {
12460
- if (transaction instanceof TransactionMessage) transactions.push(transaction.encode());
12461
- else transactions.push(new TransactionMessage(transaction).encode());
12462
- }
12463
- return super.encode({
12464
- ...decoded,
12465
- validators: index$5(validators),
12466
- transactions: index$5(transactions)
12467
- })
12468
- }
12469
- decode(encoded) {
12470
- encoded = encoded || this.encoded;
12471
- super.decode(encoded);
12472
- // @ts-ignore
12473
- this.decoded.transactions = index$4(this.decoded.transactions).map(
12474
- (transaction) => new TransactionMessage(transaction).decoded
12475
- );
12476
- // @ts-ignore
12477
- this.decoded.validators = index$4(this.decoded.validators).map(
12478
- (validator) => new ValidatorMessage(validator).decoded
12479
- );
12480
- return this.decoded
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, nativeToken, validators, nameService } from '@leofcoin/addresses';
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
  }
@@ -291,7 +296,14 @@ class Machine {
291
296
  case 'transactionLoaded': {
292
297
  const { from, nonce, hash } = data.result;
293
298
  (await transactionPoolStore.has(hash)) && (await transactionPoolStore.delete(hash));
294
- await accountsStore.put(from, nonce);
299
+ try {
300
+ const _nonce = await accountsStore.get(from);
301
+ if (nonce > _nonce)
302
+ await accountsStore.put(from, nonce);
303
+ }
304
+ catch (error) {
305
+ await accountsStore.put(from, nonce);
306
+ }
295
307
  break;
296
308
  }
297
309
  case 'contractError': {
@@ -329,12 +341,54 @@ class Machine {
329
341
  pubsub.publish(data.id, data.value || false);
330
342
  break;
331
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);
332
385
  }
333
386
  }
334
387
  async #init(blocks) {
335
388
  return new Promise(async (resolve) => {
336
- const machineReady = () => {
389
+ const machineReady = async () => {
337
390
  pubsub.unsubscribe('machine.ready', machineReady);
391
+ await this.updateState();
338
392
  resolve(this);
339
393
  };
340
394
  pubsub.subscribe('machine.ready', machineReady);
@@ -354,18 +408,19 @@ class Machine {
354
408
  type: 'module'
355
409
  });
356
410
  this.worker.onmessage(this.#onmessage.bind(this));
357
- // const blocks = await blockStore.values()
358
- const contracts = await Promise.all([
359
- globalThis.contractStore.get(contractFactory),
360
- globalThis.contractStore.get(nativeToken),
361
- globalThis.contractStore.get(validators),
362
- globalThis.contractStore.get(nameService)
363
- ]);
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
+ }
364
417
  const message = {
365
418
  type: 'init',
366
419
  input: {
367
- contracts,
368
420
  blocks,
421
+ fromState: this.states.lastBlock.index > 0,
422
+ lastBlock: this.states.lastBlock,
423
+ state: this.states.states,
369
424
  // @ts-ignore
370
425
  peerid: peernet.peerId
371
426
  }
@@ -685,8 +740,10 @@ class State extends Contract {
685
740
  this.#loadBlockTransactions = (transactions) => Promise.all(transactions.map((transaction) => new TransactionMessage(transaction)));
686
741
  this.#getLastTransactions = async () => {
687
742
  let lastTransactions = (await Promise.all((await this.blocks)
743
+ // @ts-ignore
688
744
  .filter((block) => block.loaded)
689
745
  .slice(-24)
746
+ // @ts-ignore
690
747
  .map((block) => this.#loadBlockTransactions(block.transactions)))).reduce((all, transactions) => [...all, ...transactions], []);
691
748
  return Promise.all(lastTransactions.map((transaction) => transaction.hash()));
692
749
  };
@@ -729,7 +786,18 @@ class State extends Contract {
729
786
  throw error;
730
787
  }
731
788
  try {
732
- await this.resolveBlocks();
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
+ }
733
801
  this.#machine = await new Machine(this.#blocks);
734
802
  const lastBlock = await this.#machine.lastBlock;
735
803
  this.updateState(new BlockMessage(lastBlock));
@@ -753,6 +821,7 @@ class State extends Contract {
753
821
  catch (error) {
754
822
  console.error(error);
755
823
  }
824
+ await this.#machine.updateState();
756
825
  }
757
826
  getLatestBlock() {
758
827
  // @ts-ignore
@@ -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
@@ -0,0 +1,3 @@
1
+ export declare const shouldResolveBlocks: () => void;
2
+ export declare const shouldLoadBlocks: () => void;
3
+ export declare const shouldLoadFromState: () => void;
@@ -1,4 +1,4 @@
1
- import { E as EasyWorker, B as BigNumber, a as BlockMessage } from './worker-hbo9s9AT.js';
1
+ import { E as EasyWorker, B as BigNumber, a as BlockMessage } from './worker-CbAak_hM.js';
2
2
 
3
3
  const worker = new EasyWorker();
4
4