@leofcoin/chain 1.7.152 → 1.7.153
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 +49 -3
- package/exports/chain.js +49 -3
- package/exports/state.d.ts +2 -0
- package/package.json +1 -1
package/exports/browser/chain.js
CHANGED
|
@@ -4189,7 +4189,7 @@ class Machine {
|
|
|
4189
4189
|
break;
|
|
4190
4190
|
}
|
|
4191
4191
|
case 'debug': {
|
|
4192
|
-
debug
|
|
4192
|
+
// debug(data.message)
|
|
4193
4193
|
if (data.message.includes('loaded transactions for block:')) {
|
|
4194
4194
|
pubsub.publish('block-loaded', data.message.replace('loaded transactions for block: ', '').split(' @')[0]);
|
|
4195
4195
|
}
|
|
@@ -4598,6 +4598,36 @@ class Machine {
|
|
|
4598
4598
|
}
|
|
4599
4599
|
}
|
|
4600
4600
|
|
|
4601
|
+
class Jobber {
|
|
4602
|
+
constructor(timeout) {
|
|
4603
|
+
this.busy = false;
|
|
4604
|
+
this.timeout = timeout;
|
|
4605
|
+
}
|
|
4606
|
+
add(fn) {
|
|
4607
|
+
this.busy = true;
|
|
4608
|
+
return new Promise(async (resolve, reject) => {
|
|
4609
|
+
const timeout = setTimeout(() => {
|
|
4610
|
+
reject('timeout');
|
|
4611
|
+
}, this.timeout);
|
|
4612
|
+
this.destroy = () => {
|
|
4613
|
+
clearTimeout(timeout);
|
|
4614
|
+
this.busy = false;
|
|
4615
|
+
resolve('stopped');
|
|
4616
|
+
};
|
|
4617
|
+
try {
|
|
4618
|
+
const result = await fn();
|
|
4619
|
+
clearTimeout(timeout);
|
|
4620
|
+
this.busy = false;
|
|
4621
|
+
resolve(result);
|
|
4622
|
+
}
|
|
4623
|
+
catch (error) {
|
|
4624
|
+
clearTimeout(timeout);
|
|
4625
|
+
reject(error);
|
|
4626
|
+
}
|
|
4627
|
+
});
|
|
4628
|
+
}
|
|
4629
|
+
}
|
|
4630
|
+
|
|
4601
4631
|
const debug$1 = createDebugger('leofcoin/state');
|
|
4602
4632
|
class State extends Contract {
|
|
4603
4633
|
#blockHashMap;
|
|
@@ -4743,6 +4773,8 @@ class State extends Contract {
|
|
|
4743
4773
|
#lastBlockHandler;
|
|
4744
4774
|
#knownBlocksHandler;
|
|
4745
4775
|
async init() {
|
|
4776
|
+
// Initialize jobber for timed, cancelable tasks
|
|
4777
|
+
this.jobber = new Jobber(this.resolveTimeout);
|
|
4746
4778
|
// Register request handlers
|
|
4747
4779
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
4748
4780
|
await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
@@ -4924,7 +4956,11 @@ class State extends Contract {
|
|
|
4924
4956
|
const blockChain = await this.#buildBlockChain(hash);
|
|
4925
4957
|
debug$1(`Built chain of ${blockChain.length} blocks`);
|
|
4926
4958
|
if (blockChain.length > 0) {
|
|
4927
|
-
|
|
4959
|
+
// If a previous resolve job is still running, cancel it
|
|
4960
|
+
if (this.jobber?.busy && this.jobber.destroy)
|
|
4961
|
+
await this.jobber.destroy();
|
|
4962
|
+
// Run the parallel resolution inside a timed jobber task
|
|
4963
|
+
await this.jobber.add(() => this.#resolveBlocksInParallel(blockChain));
|
|
4928
4964
|
}
|
|
4929
4965
|
}
|
|
4930
4966
|
catch (error) {
|
|
@@ -4942,7 +4978,17 @@ class State extends Contract {
|
|
|
4942
4978
|
const localBlock = await globalThis.chainStore.get('lastBlock');
|
|
4943
4979
|
const hash = new TextDecoder().decode(localBlock);
|
|
4944
4980
|
if (hash && hash !== '0x0') {
|
|
4945
|
-
|
|
4981
|
+
// Cancel any in-flight job before starting a new one
|
|
4982
|
+
if (this.jobber?.busy && this.jobber.destroy)
|
|
4983
|
+
await this.jobber.destroy();
|
|
4984
|
+
// Build chain and resolve in parallel under jobber control
|
|
4985
|
+
const run = async () => {
|
|
4986
|
+
const chain = await this.#buildBlockChain(hash);
|
|
4987
|
+
if (chain.length > 0) {
|
|
4988
|
+
await this.#resolveBlocksInParallel(chain);
|
|
4989
|
+
}
|
|
4990
|
+
};
|
|
4991
|
+
await this.jobber.add(run);
|
|
4946
4992
|
}
|
|
4947
4993
|
}
|
|
4948
4994
|
catch (error) {
|
package/exports/chain.js
CHANGED
|
@@ -330,7 +330,7 @@ class Machine {
|
|
|
330
330
|
break;
|
|
331
331
|
}
|
|
332
332
|
case 'debug': {
|
|
333
|
-
debug
|
|
333
|
+
// debug(data.message)
|
|
334
334
|
if (data.message.includes('loaded transactions for block:')) {
|
|
335
335
|
pubsub.publish('block-loaded', data.message.replace('loaded transactions for block: ', '').split(' @')[0]);
|
|
336
336
|
}
|
|
@@ -739,6 +739,36 @@ class Machine {
|
|
|
739
739
|
}
|
|
740
740
|
}
|
|
741
741
|
|
|
742
|
+
class Jobber {
|
|
743
|
+
constructor(timeout) {
|
|
744
|
+
this.busy = false;
|
|
745
|
+
this.timeout = timeout;
|
|
746
|
+
}
|
|
747
|
+
add(fn) {
|
|
748
|
+
this.busy = true;
|
|
749
|
+
return new Promise(async (resolve, reject) => {
|
|
750
|
+
const timeout = setTimeout(() => {
|
|
751
|
+
reject('timeout');
|
|
752
|
+
}, this.timeout);
|
|
753
|
+
this.destroy = () => {
|
|
754
|
+
clearTimeout(timeout);
|
|
755
|
+
this.busy = false;
|
|
756
|
+
resolve('stopped');
|
|
757
|
+
};
|
|
758
|
+
try {
|
|
759
|
+
const result = await fn();
|
|
760
|
+
clearTimeout(timeout);
|
|
761
|
+
this.busy = false;
|
|
762
|
+
resolve(result);
|
|
763
|
+
}
|
|
764
|
+
catch (error) {
|
|
765
|
+
clearTimeout(timeout);
|
|
766
|
+
reject(error);
|
|
767
|
+
}
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
742
772
|
const debug$1 = createDebugger('leofcoin/state');
|
|
743
773
|
class State extends Contract {
|
|
744
774
|
#blockHashMap;
|
|
@@ -884,6 +914,8 @@ class State extends Contract {
|
|
|
884
914
|
#lastBlockHandler;
|
|
885
915
|
#knownBlocksHandler;
|
|
886
916
|
async init() {
|
|
917
|
+
// Initialize jobber for timed, cancelable tasks
|
|
918
|
+
this.jobber = new Jobber(this.resolveTimeout);
|
|
887
919
|
// Register request handlers
|
|
888
920
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
889
921
|
await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
@@ -1065,7 +1097,11 @@ class State extends Contract {
|
|
|
1065
1097
|
const blockChain = await this.#buildBlockChain(hash);
|
|
1066
1098
|
debug$1(`Built chain of ${blockChain.length} blocks`);
|
|
1067
1099
|
if (blockChain.length > 0) {
|
|
1068
|
-
|
|
1100
|
+
// If a previous resolve job is still running, cancel it
|
|
1101
|
+
if (this.jobber?.busy && this.jobber.destroy)
|
|
1102
|
+
await this.jobber.destroy();
|
|
1103
|
+
// Run the parallel resolution inside a timed jobber task
|
|
1104
|
+
await this.jobber.add(() => this.#resolveBlocksInParallel(blockChain));
|
|
1069
1105
|
}
|
|
1070
1106
|
}
|
|
1071
1107
|
catch (error) {
|
|
@@ -1083,7 +1119,17 @@ class State extends Contract {
|
|
|
1083
1119
|
const localBlock = await globalThis.chainStore.get('lastBlock');
|
|
1084
1120
|
const hash = new TextDecoder().decode(localBlock);
|
|
1085
1121
|
if (hash && hash !== '0x0') {
|
|
1086
|
-
|
|
1122
|
+
// Cancel any in-flight job before starting a new one
|
|
1123
|
+
if (this.jobber?.busy && this.jobber.destroy)
|
|
1124
|
+
await this.jobber.destroy();
|
|
1125
|
+
// Build chain and resolve in parallel under jobber control
|
|
1126
|
+
const run = async () => {
|
|
1127
|
+
const chain = await this.#buildBlockChain(hash);
|
|
1128
|
+
if (chain.length > 0) {
|
|
1129
|
+
await this.#resolveBlocksInParallel(chain);
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
await this.jobber.add(run);
|
|
1087
1133
|
}
|
|
1088
1134
|
}
|
|
1089
1135
|
catch (error) {
|
package/exports/state.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { BlockMessage } from '@leofcoin/messages';
|
|
2
2
|
import Contract from './contract.js';
|
|
3
3
|
import Machine from './machine.js';
|
|
4
|
+
import Jobber from './jobs/jobber.js';
|
|
4
5
|
import { BlockHash } from './types.js';
|
|
5
6
|
export default class State extends Contract {
|
|
6
7
|
#private;
|
|
8
|
+
jobber: Jobber;
|
|
7
9
|
knownBlocks: BlockHash[];
|
|
8
10
|
_wantList: any[];
|
|
9
11
|
/**
|