@leofcoin/chain 1.7.7 → 1.7.8
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 +720 -684
- package/exports/chain.js +47 -28
- package/exports/state.d.ts +4 -0
- package/package.json +1 -1
package/exports/chain.js
CHANGED
|
@@ -1160,38 +1160,46 @@ class State extends Contract {
|
|
|
1160
1160
|
let poolTransactionKeys = await globalThis.transactionPoolStore.keys();
|
|
1161
1161
|
for (const block of blocks) {
|
|
1162
1162
|
if (block && !block.loaded) {
|
|
1163
|
-
|
|
1164
|
-
this.#
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1163
|
+
try {
|
|
1164
|
+
let transactions = await this.#loadBlockTransactions([...block.transactions] || []);
|
|
1165
|
+
const lastTransactions = await this.#getLastTransactions();
|
|
1166
|
+
let priority = [];
|
|
1167
|
+
for (const transaction of transactions) {
|
|
1168
|
+
const hash = await transaction.hash();
|
|
1169
|
+
if (lastTransactions.includes(hash)) {
|
|
1170
|
+
console.log('removing invalid block');
|
|
1171
|
+
await globalThis.blockStore.delete(await (await new BlockMessage(block)).hash());
|
|
1172
|
+
blocks.splice(block.index - 1, 1);
|
|
1173
|
+
return this.#loadBlocks(blocks);
|
|
1174
|
+
}
|
|
1175
|
+
if (transaction.decoded.priority)
|
|
1176
|
+
priority.push(transaction);
|
|
1177
|
+
if (poolTransactionKeys.includes(hash))
|
|
1178
|
+
await globalThis.transactionPoolStore.delete(hash);
|
|
1175
1179
|
}
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
+
// prority blocks execution from the rest so result in higher fees.
|
|
1181
|
+
if (priority.length > 0) {
|
|
1182
|
+
priority = priority.sort((a, b) => a.nonce - b.nonce);
|
|
1183
|
+
for (const transaction of priority) {
|
|
1184
|
+
await this.#_executeTransaction(transaction);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
transactions = transactions.filter((transaction) => !transaction.decoded.priority);
|
|
1188
|
+
await Promise.all(transactions.map((transaction) => this.#_executeTransaction(transaction)));
|
|
1189
|
+
this.#blocks[block.index].loaded = true;
|
|
1190
|
+
if (block.index === 0)
|
|
1191
|
+
this.#loaded = true;
|
|
1192
|
+
await this.#machine.addLoadedBlock(block);
|
|
1193
|
+
// @ts-ignore
|
|
1194
|
+
debug$1(`loaded block: ${block.hash} @${block.index}`);
|
|
1195
|
+
globalThis.pubsub.publish('block-loaded', { ...block });
|
|
1180
1196
|
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
await this.#_executeTransaction(transaction);
|
|
1197
|
+
catch (error) {
|
|
1198
|
+
console.error(error);
|
|
1199
|
+
for (const transaction of block.transactions) {
|
|
1200
|
+
this.wantList.push(transaction);
|
|
1186
1201
|
}
|
|
1187
1202
|
}
|
|
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
1203
|
}
|
|
1196
1204
|
}
|
|
1197
1205
|
this.#chainState = 'loaded';
|
|
@@ -1464,6 +1472,17 @@ class Chain extends VersionControl {
|
|
|
1464
1472
|
else if (!this.knownBlocks)
|
|
1465
1473
|
this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
1466
1474
|
}
|
|
1475
|
+
if (this.wantList.length > 0) {
|
|
1476
|
+
const promises = await Promise.allSettled(this.wantList.map((hash) => peernet.get(hash)));
|
|
1477
|
+
for (let i = 0; i < promises.length; i++) {
|
|
1478
|
+
const result = promises[i];
|
|
1479
|
+
if (result.status === 'fulfilled')
|
|
1480
|
+
this.wantList.splice(i, 1);
|
|
1481
|
+
}
|
|
1482
|
+
// todo trigger load instead?
|
|
1483
|
+
if (this.wantList.length === 0)
|
|
1484
|
+
await this.triggerSync();
|
|
1485
|
+
}
|
|
1467
1486
|
setTimeout(async () => {
|
|
1468
1487
|
const peerTransactionPool = (higherThenCurrentLocal && (await this.getPeerTransactionPool(peer))) || [];
|
|
1469
1488
|
if (this.#participating && peerTransactionPool.length > 0)
|
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
|
+
wantList: string[];
|
|
12
16
|
get state(): {
|
|
13
17
|
sync: SyncState;
|
|
14
18
|
chain: ChainState;
|