@leofcoin/chain 1.7.9 → 1.7.11

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.
@@ -150,10 +150,6 @@ const get = ({ contract, method, params }) => {
150
150
  return result
151
151
  };
152
152
 
153
- const resolveContract = (hash) => askFor('contract', hash);
154
-
155
- const resolveTransaction = (hash) => askFor('transaction', hash);
156
-
157
153
  const respond = (id, value) => {
158
154
  worker.postMessage({
159
155
  type: 'response',
@@ -255,6 +251,13 @@ const _executeTransaction = async (transaction) => {
255
251
  }
256
252
  };
257
253
 
254
+ const addToWantList = (hash) => {
255
+ worker.postMessage({
256
+ type: 'addToWantList',
257
+ hash
258
+ });
259
+ };
260
+
258
261
  _.init = async (message) => {
259
262
  let { peerid, fromState, state, info } = message;
260
263
  globalThis.peerid = peerid;
@@ -272,6 +275,10 @@ _.init = async (message) => {
272
275
  lastBlock = message.lastBlock;
273
276
  const setState = async (address, state) => {
274
277
  const contractBytes = await resolveContract(address);
278
+ if (contractBytes === address) {
279
+ addToWantList(address);
280
+ return
281
+ }
275
282
  const contract = await new ContractMessage(contractBytes);
276
283
 
277
284
  await _.runContract({ hash: address, decoded: contract.decoded, encoded: contract.encoded }, state);
@@ -341,30 +348,42 @@ _.init = async (message) => {
341
348
  latestTransactions.splice(-transactionCount, latestTransactions.length);
342
349
  }
343
350
  if (!block.loaded && !fromState) {
344
- const transactions = await Promise.all(
345
- block.transactions.map(async (transaction) =>
346
- new TransactionMessage(await resolveTransaction(transaction)).decode()
347
- )
348
- );
349
- const priority = transactions.filter((transaction) => transaction.priority)?.sort((a, b) => a.nonce - b.nonce);
350
- if (priority.length > 0)
351
- for (const transaction of priority) {
352
- await _executeTransaction(transaction);
353
- }
354
-
355
- await Promise.all(
356
- transactions
357
- .filter((transaction) => !transaction.priority)
358
- .map(async (transaction) => _executeTransaction(transaction))
359
- );
351
+ try {
352
+ const transactions = await Promise.all(
353
+ block.transactions.map(async (transaction) => {
354
+ const message = new TransactionMessage(await resolveTransaction(transaction)).decode();
355
+ if (message === transaction) {
356
+ throw new Error(`nothing found for ${transaction}`)
357
+ }
358
+ })
359
+ );
360
+ const priority = transactions
361
+ .filter((transaction) => transaction.priority)
362
+ ?.sort((a, b) => a.nonce - b.nonce);
363
+ if (priority.length > 0)
364
+ for (const transaction of priority) {
365
+ await _executeTransaction(transaction);
366
+ }
367
+
368
+ await Promise.all(
369
+ transactions
370
+ .filter((transaction) => !transaction.priority)
371
+ .map(async (transaction) => _executeTransaction(transaction))
372
+ );
373
+ block.loaded = true;
374
+ worker.postMessage({
375
+ type: 'debug',
376
+ message: `loaded transactions for block: ${block.hash} @${block.index}`
377
+ });
378
+ } catch (error) {
379
+ // just tell chain it's ready so we can get this node sync
380
+ // when a node connects this node will try to resolve the wantList
381
+ // this should result in the node beeing sync
382
+ if (error.message.includes('nothing found for')) worker.postMessage({ type: 'machine-ready', lastBlock });
383
+ else console.error(error);
384
+ }
360
385
  }
361
- block.loaded = true;
362
- worker.postMessage({
363
- type: 'debug',
364
- message: `loaded transactions for block: ${block.hash} @${block.index}`
365
- });
366
386
  }
367
-
368
387
  if (blocks.length > 0) {
369
388
  lastBlock = blocks[blocks.length - 1];
370
389
  }
@@ -373,8 +392,6 @@ _.init = async (message) => {
373
392
  }
374
393
 
375
394
  worker.postMessage({ type: 'machine-ready', lastBlock });
376
-
377
- // worker.postMessage({blocks});
378
395
  };
379
396
 
380
397
  _.addLoadedBlock = (block) => {
@@ -399,6 +416,10 @@ const askFor = (question, input) =>
399
416
  });
400
417
  });
401
418
 
419
+ const resolveContract = (hash) => askFor('contract', hash);
420
+
421
+ const resolveTransaction = (hash) => askFor('transaction', hash);
422
+
402
423
  const runTask = async (id, taskName, input) => {
403
424
  try {
404
425
  const result = await _[taskName](input);
package/exports/chain.js CHANGED
@@ -346,6 +346,9 @@ class Machine {
346
346
  pubsub.publish(data.id, data.value || false);
347
347
  break;
348
348
  }
349
+ case 'addToWantList': {
350
+ this.wantList.push(data.hash);
351
+ }
349
352
  case 'ask': {
350
353
  if (data.question === 'contract' || data.question === 'transaction') {
351
354
  try {
@@ -354,6 +357,7 @@ class Machine {
354
357
  }
355
358
  catch (error) {
356
359
  console.error(error);
360
+ this.worker.postMessage({ id: data.id, input: data.input });
357
361
  this.wantList.push(data.input);
358
362
  }
359
363
  }
@@ -720,7 +724,7 @@ class State extends Contract {
720
724
  * contains transactions we need before we can successfully load
721
725
  */
722
726
  get wantList() {
723
- return this.#machine.wantList;
727
+ return this.#machine.wantList ?? this._wantList;
724
728
  }
725
729
  get state() {
726
730
  return {
@@ -783,6 +787,7 @@ class State extends Contract {
783
787
  this.knownBlocks = [];
784
788
  this.#totalSize = 0;
785
789
  this.#loaded = false;
790
+ this._wantList = [];
786
791
  this.#chainStateHandler = () => {
787
792
  return new globalThis.peernet.protos['peernet-response']({
788
793
  response: this.#chainState
@@ -966,6 +971,7 @@ class State extends Contract {
966
971
  if (this.#resolveErrorCount < 3)
967
972
  return this.resolveBlock(hash);
968
973
  this.#resolveErrorCount = 0;
974
+ this.wantList.push(hash);
969
975
  throw new ResolveError(`block: ${hash}`, { cause: error });
970
976
  }
971
977
  }
@@ -9,6 +9,7 @@ export default class State extends Contract {
9
9
  #private;
10
10
  knownBlocks: BlockHash[];
11
11
  jobber: Jobber;
12
+ _wantList: any[];
12
13
  /**
13
14
  * contains transactions we need before we can successfully load
14
15
  */
@@ -150,10 +150,6 @@ const get = ({ contract, method, params }) => {
150
150
  return result
151
151
  };
152
152
 
153
- const resolveContract = (hash) => askFor('contract', hash);
154
-
155
- const resolveTransaction = (hash) => askFor('transaction', hash);
156
-
157
153
  const respond = (id, value) => {
158
154
  worker.postMessage({
159
155
  type: 'response',
@@ -255,6 +251,13 @@ const _executeTransaction = async (transaction) => {
255
251
  }
256
252
  };
257
253
 
254
+ const addToWantList = (hash) => {
255
+ worker.postMessage({
256
+ type: 'addToWantList',
257
+ hash
258
+ });
259
+ };
260
+
258
261
  _.init = async (message) => {
259
262
  let { peerid, fromState, state, info } = message;
260
263
  globalThis.peerid = peerid;
@@ -272,6 +275,10 @@ _.init = async (message) => {
272
275
  lastBlock = message.lastBlock;
273
276
  const setState = async (address, state) => {
274
277
  const contractBytes = await resolveContract(address);
278
+ if (contractBytes === address) {
279
+ addToWantList(address);
280
+ return
281
+ }
275
282
  const contract = await new ContractMessage(contractBytes);
276
283
 
277
284
  await _.runContract({ hash: address, decoded: contract.decoded, encoded: contract.encoded }, state);
@@ -341,30 +348,42 @@ _.init = async (message) => {
341
348
  latestTransactions.splice(-transactionCount, latestTransactions.length);
342
349
  }
343
350
  if (!block.loaded && !fromState) {
344
- const transactions = await Promise.all(
345
- block.transactions.map(async (transaction) =>
346
- new TransactionMessage(await resolveTransaction(transaction)).decode()
347
- )
348
- );
349
- const priority = transactions.filter((transaction) => transaction.priority)?.sort((a, b) => a.nonce - b.nonce);
350
- if (priority.length > 0)
351
- for (const transaction of priority) {
352
- await _executeTransaction(transaction);
353
- }
354
-
355
- await Promise.all(
356
- transactions
357
- .filter((transaction) => !transaction.priority)
358
- .map(async (transaction) => _executeTransaction(transaction))
359
- );
351
+ try {
352
+ const transactions = await Promise.all(
353
+ block.transactions.map(async (transaction) => {
354
+ const message = new TransactionMessage(await resolveTransaction(transaction)).decode();
355
+ if (message === transaction) {
356
+ throw new Error(`nothing found for ${transaction}`)
357
+ }
358
+ })
359
+ );
360
+ const priority = transactions
361
+ .filter((transaction) => transaction.priority)
362
+ ?.sort((a, b) => a.nonce - b.nonce);
363
+ if (priority.length > 0)
364
+ for (const transaction of priority) {
365
+ await _executeTransaction(transaction);
366
+ }
367
+
368
+ await Promise.all(
369
+ transactions
370
+ .filter((transaction) => !transaction.priority)
371
+ .map(async (transaction) => _executeTransaction(transaction))
372
+ );
373
+ block.loaded = true;
374
+ worker.postMessage({
375
+ type: 'debug',
376
+ message: `loaded transactions for block: ${block.hash} @${block.index}`
377
+ });
378
+ } catch (error) {
379
+ // just tell chain it's ready so we can get this node sync
380
+ // when a node connects this node will try to resolve the wantList
381
+ // this should result in the node beeing sync
382
+ if (error.message.includes('nothing found for')) worker.postMessage({ type: 'machine-ready', lastBlock });
383
+ else console.error(error);
384
+ }
360
385
  }
361
- block.loaded = true;
362
- worker.postMessage({
363
- type: 'debug',
364
- message: `loaded transactions for block: ${block.hash} @${block.index}`
365
- });
366
386
  }
367
-
368
387
  if (blocks.length > 0) {
369
388
  lastBlock = blocks[blocks.length - 1];
370
389
  }
@@ -373,8 +392,6 @@ _.init = async (message) => {
373
392
  }
374
393
 
375
394
  worker.postMessage({ type: 'machine-ready', lastBlock });
376
-
377
- // worker.postMessage({blocks});
378
395
  };
379
396
 
380
397
  _.addLoadedBlock = (block) => {
@@ -399,6 +416,10 @@ const askFor = (question, input) =>
399
416
  });
400
417
  });
401
418
 
419
+ const resolveContract = (hash) => askFor('contract', hash);
420
+
421
+ const resolveTransaction = (hash) => askFor('transaction', hash);
422
+
402
423
  const runTask = async (id, taskName, input) => {
403
424
  try {
404
425
  const result = await _[taskName](input);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.9",
3
+ "version": "1.7.11",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {