@leofcoin/chain 1.7.151 → 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 +53 -3
- package/exports/chain.js +53 -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;
|
|
@@ -4611,6 +4641,10 @@ class State extends Contract {
|
|
|
4611
4641
|
#resolvingBlocks;
|
|
4612
4642
|
#maxConcurrentResolves;
|
|
4613
4643
|
#totalSize;
|
|
4644
|
+
#lastResolved;
|
|
4645
|
+
#lastResolvedTime;
|
|
4646
|
+
#blockResolveQueue;
|
|
4647
|
+
#chainState;
|
|
4614
4648
|
/**
|
|
4615
4649
|
* contains transactions we need before we can successfully load
|
|
4616
4650
|
*/
|
|
@@ -4739,6 +4773,8 @@ class State extends Contract {
|
|
|
4739
4773
|
#lastBlockHandler;
|
|
4740
4774
|
#knownBlocksHandler;
|
|
4741
4775
|
async init() {
|
|
4776
|
+
// Initialize jobber for timed, cancelable tasks
|
|
4777
|
+
this.jobber = new Jobber(this.resolveTimeout);
|
|
4742
4778
|
// Register request handlers
|
|
4743
4779
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
4744
4780
|
await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
@@ -4920,7 +4956,11 @@ class State extends Contract {
|
|
|
4920
4956
|
const blockChain = await this.#buildBlockChain(hash);
|
|
4921
4957
|
debug$1(`Built chain of ${blockChain.length} blocks`);
|
|
4922
4958
|
if (blockChain.length > 0) {
|
|
4923
|
-
|
|
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));
|
|
4924
4964
|
}
|
|
4925
4965
|
}
|
|
4926
4966
|
catch (error) {
|
|
@@ -4938,7 +4978,17 @@ class State extends Contract {
|
|
|
4938
4978
|
const localBlock = await globalThis.chainStore.get('lastBlock');
|
|
4939
4979
|
const hash = new TextDecoder().decode(localBlock);
|
|
4940
4980
|
if (hash && hash !== '0x0') {
|
|
4941
|
-
|
|
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);
|
|
4942
4992
|
}
|
|
4943
4993
|
}
|
|
4944
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;
|
|
@@ -752,6 +782,10 @@ class State extends Contract {
|
|
|
752
782
|
#resolvingBlocks;
|
|
753
783
|
#maxConcurrentResolves;
|
|
754
784
|
#totalSize;
|
|
785
|
+
#lastResolved;
|
|
786
|
+
#lastResolvedTime;
|
|
787
|
+
#blockResolveQueue;
|
|
788
|
+
#chainState;
|
|
755
789
|
/**
|
|
756
790
|
* contains transactions we need before we can successfully load
|
|
757
791
|
*/
|
|
@@ -880,6 +914,8 @@ class State extends Contract {
|
|
|
880
914
|
#lastBlockHandler;
|
|
881
915
|
#knownBlocksHandler;
|
|
882
916
|
async init() {
|
|
917
|
+
// Initialize jobber for timed, cancelable tasks
|
|
918
|
+
this.jobber = new Jobber(this.resolveTimeout);
|
|
883
919
|
// Register request handlers
|
|
884
920
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
885
921
|
await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
@@ -1061,7 +1097,11 @@ class State extends Contract {
|
|
|
1061
1097
|
const blockChain = await this.#buildBlockChain(hash);
|
|
1062
1098
|
debug$1(`Built chain of ${blockChain.length} blocks`);
|
|
1063
1099
|
if (blockChain.length > 0) {
|
|
1064
|
-
|
|
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));
|
|
1065
1105
|
}
|
|
1066
1106
|
}
|
|
1067
1107
|
catch (error) {
|
|
@@ -1079,7 +1119,17 @@ class State extends Contract {
|
|
|
1079
1119
|
const localBlock = await globalThis.chainStore.get('lastBlock');
|
|
1080
1120
|
const hash = new TextDecoder().decode(localBlock);
|
|
1081
1121
|
if (hash && hash !== '0x0') {
|
|
1082
|
-
|
|
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);
|
|
1083
1133
|
}
|
|
1084
1134
|
}
|
|
1085
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
|
/**
|