@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/browser/chain.js +735 -686
- package/exports/chain.js +62 -30
- package/exports/machine.d.ts +1 -0
- package/exports/state.d.ts +4 -0
- package/package.json +1 -1
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
|
-
|
|
351
|
-
|
|
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
|
-
|
|
1164
|
-
this.#
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
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
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
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
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
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)
|
package/exports/machine.d.ts
CHANGED
package/exports/state.d.ts
CHANGED
|
@@ -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;
|