@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.
@@ -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
  }
@@ -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
- // const blocks = await blockStore.values()
365
- const contracts = await Promise.all([
366
- globalThis.contractStore.get(contractFactory),
367
- globalThis.contractStore.get(nativeToken),
368
- globalThis.contractStore.get(validators),
369
- globalThis.contractStore.get(nameService)
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 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
+ }
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
@@ -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-CltAyHf1.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