@leofcoin/chain 1.7.7 → 1.7.9

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/chain.js CHANGED
@@ -292,6 +292,7 @@ class Machine {
292
292
  totalBlocks: 0
293
293
  }
294
294
  };
295
+ this.wantList = [];
295
296
  // @ts-ignore
296
297
  return this.#init(blocks);
297
298
  }
@@ -347,8 +348,14 @@ class Machine {
347
348
  }
348
349
  case 'ask': {
349
350
  if (data.question === 'contract' || data.question === 'transaction') {
350
- const input = await peernet.get(data.input);
351
- this.worker.postMessage({ id: data.id, input });
351
+ try {
352
+ const input = await peernet.get(data.input);
353
+ this.worker.postMessage({ id: data.id, input });
354
+ }
355
+ catch (error) {
356
+ console.error(error);
357
+ this.wantList.push(data.input);
358
+ }
352
359
  }
353
360
  }
354
361
  }
@@ -709,6 +716,12 @@ class State extends Contract {
709
716
  #totalSize;
710
717
  #machine;
711
718
  #loaded;
719
+ /**
720
+ * contains transactions we need before we can successfully load
721
+ */
722
+ get wantList() {
723
+ return this.#machine.wantList;
724
+ }
712
725
  get state() {
713
726
  return {
714
727
  sync: this.#syncState,
@@ -1160,38 +1173,46 @@ class State extends Contract {
1160
1173
  let poolTransactionKeys = await globalThis.transactionPoolStore.keys();
1161
1174
  for (const block of blocks) {
1162
1175
  if (block && !block.loaded) {
1163
- if (block.index === 0)
1164
- this.#loaded = true;
1165
- let transactions = await this.#loadBlockTransactions([...block.transactions] || []);
1166
- const lastTransactions = await this.#getLastTransactions();
1167
- let priority = [];
1168
- for (const transaction of transactions) {
1169
- const hash = await transaction.hash();
1170
- if (lastTransactions.includes(hash)) {
1171
- console.log('removing invalid block');
1172
- await globalThis.blockStore.delete(await (await new BlockMessage(block)).hash());
1173
- blocks.splice(block.index - 1, 1);
1174
- return this.#loadBlocks(blocks);
1176
+ try {
1177
+ let transactions = await this.#loadBlockTransactions([...block.transactions] || []);
1178
+ const lastTransactions = await this.#getLastTransactions();
1179
+ let priority = [];
1180
+ for (const transaction of transactions) {
1181
+ const hash = await transaction.hash();
1182
+ if (lastTransactions.includes(hash)) {
1183
+ console.log('removing invalid block');
1184
+ await globalThis.blockStore.delete(await (await new BlockMessage(block)).hash());
1185
+ blocks.splice(block.index - 1, 1);
1186
+ return this.#loadBlocks(blocks);
1187
+ }
1188
+ if (transaction.decoded.priority)
1189
+ priority.push(transaction);
1190
+ if (poolTransactionKeys.includes(hash))
1191
+ await globalThis.transactionPoolStore.delete(hash);
1192
+ }
1193
+ // prority blocks execution from the rest so result in higher fees.
1194
+ if (priority.length > 0) {
1195
+ priority = priority.sort((a, b) => a.nonce - b.nonce);
1196
+ for (const transaction of priority) {
1197
+ await this.#_executeTransaction(transaction);
1198
+ }
1175
1199
  }
1176
- if (transaction.decoded.priority)
1177
- priority.push(transaction);
1178
- if (poolTransactionKeys.includes(hash))
1179
- await globalThis.transactionPoolStore.delete(hash);
1200
+ transactions = transactions.filter((transaction) => !transaction.decoded.priority);
1201
+ await Promise.all(transactions.map((transaction) => this.#_executeTransaction(transaction)));
1202
+ this.#blocks[block.index].loaded = true;
1203
+ if (block.index === 0)
1204
+ this.#loaded = true;
1205
+ await this.#machine.addLoadedBlock(block);
1206
+ // @ts-ignore
1207
+ debug$1(`loaded block: ${block.hash} @${block.index}`);
1208
+ globalThis.pubsub.publish('block-loaded', { ...block });
1180
1209
  }
1181
- // prority blocks execution from the rest so result in higher fees.
1182
- if (priority.length > 0) {
1183
- priority = priority.sort((a, b) => a.nonce - b.nonce);
1184
- for (const transaction of priority) {
1185
- await this.#_executeTransaction(transaction);
1210
+ catch (error) {
1211
+ console.error(error);
1212
+ for (const transaction of block.transactions) {
1213
+ this.wantList.push(transaction);
1186
1214
  }
1187
1215
  }
1188
- transactions = transactions.filter((transaction) => !transaction.decoded.priority);
1189
- await Promise.all(transactions.map((transaction) => this.#_executeTransaction(transaction)));
1190
- this.#blocks[block.index].loaded = true;
1191
- await this.#machine.addLoadedBlock(block);
1192
- // @ts-ignore
1193
- debug$1(`loaded block: ${block.hash} @${block.index}`);
1194
- globalThis.pubsub.publish('block-loaded', { ...block });
1195
1216
  }
1196
1217
  }
1197
1218
  this.#chainState = 'loaded';
@@ -1464,6 +1485,17 @@ class Chain extends VersionControl {
1464
1485
  else if (!this.knownBlocks)
1465
1486
  this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
1466
1487
  }
1488
+ if (this.wantList.length > 0) {
1489
+ const promises = await Promise.allSettled(this.wantList.map((hash) => peernet.get(hash)));
1490
+ for (let i = 0; i < promises.length; i++) {
1491
+ const result = promises[i];
1492
+ if (result.status === 'fulfilled')
1493
+ this.wantList.splice(i, 1);
1494
+ }
1495
+ // todo trigger load instead?
1496
+ if (this.wantList.length === 0)
1497
+ await this.triggerSync();
1498
+ }
1467
1499
  setTimeout(async () => {
1468
1500
  const peerTransactionPool = (higherThenCurrentLocal && (await this.getPeerTransactionPool(peer))) || [];
1469
1501
  if (this.#participating && peerTransactionPool.length > 0)
@@ -18,6 +18,7 @@ export default class Machine {
18
18
  totalBlocks: number;
19
19
  };
20
20
  };
21
+ wantList: string[];
21
22
  constructor(blocks: any);
22
23
  updateState(): Promise<void>;
23
24
  /**
@@ -9,6 +9,10 @@ export default class State extends Contract {
9
9
  #private;
10
10
  knownBlocks: BlockHash[];
11
11
  jobber: Jobber;
12
+ /**
13
+ * contains transactions we need before we can successfully load
14
+ */
15
+ get wantList(): string[];
12
16
  get state(): {
13
17
  sync: SyncState;
14
18
  chain: ChainState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.7.7",
3
+ "version": "1.7.9",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {