@leofcoin/chain 1.7.156 → 1.8.0
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 +5 -54
- package/exports/chain.js +5 -54
- 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
|
}
|
|
@@ -4576,7 +4576,7 @@ class Machine {
|
|
|
4576
4576
|
}
|
|
4577
4577
|
async addLoadedBlock(block) {
|
|
4578
4578
|
debug$2(`adding loaded block: ${block.index}@${block.hash}`);
|
|
4579
|
-
|
|
4579
|
+
debug$2(JSON.stringify(block, jsonStringifyBigInt));
|
|
4580
4580
|
if (block.decoded)
|
|
4581
4581
|
block = { ...block.decoded, hash: await block.hash() };
|
|
4582
4582
|
return this.#askWorker('addLoadedBlock', JSON.stringify(block, jsonStringifyBigInt));
|
|
@@ -4640,7 +4640,6 @@ class State extends Contract {
|
|
|
4640
4640
|
// Block resolution state
|
|
4641
4641
|
#resolvingBlocks;
|
|
4642
4642
|
#maxConcurrentResolves;
|
|
4643
|
-
#txConcurrency;
|
|
4644
4643
|
#totalSize;
|
|
4645
4644
|
#lastResolved;
|
|
4646
4645
|
#lastResolvedTime;
|
|
@@ -4727,7 +4726,6 @@ class State extends Contract {
|
|
|
4727
4726
|
// Block resolution state
|
|
4728
4727
|
this.#resolvingBlocks = new Set();
|
|
4729
4728
|
this.#maxConcurrentResolves = 10;
|
|
4730
|
-
this.#txConcurrency = 25;
|
|
4731
4729
|
this.knownBlocks = [];
|
|
4732
4730
|
this.#totalSize = 0;
|
|
4733
4731
|
this._wantList = [];
|
|
@@ -4896,52 +4894,6 @@ class State extends Contract {
|
|
|
4896
4894
|
this.#resolvingBlocks.delete(hash);
|
|
4897
4895
|
}
|
|
4898
4896
|
}
|
|
4899
|
-
// Small utility to keep event loop responsive
|
|
4900
|
-
async #sleep(ms) {
|
|
4901
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
4902
|
-
}
|
|
4903
|
-
// Run a list of tasks with bounded concurrency
|
|
4904
|
-
async #runWithConcurrency(items, concurrency, handler) {
|
|
4905
|
-
let index = 0;
|
|
4906
|
-
const total = items.length;
|
|
4907
|
-
const worker = async () => {
|
|
4908
|
-
while (true) {
|
|
4909
|
-
const current = index++;
|
|
4910
|
-
if (current >= total)
|
|
4911
|
-
break;
|
|
4912
|
-
try {
|
|
4913
|
-
await handler(items[current], current);
|
|
4914
|
-
if (current % 50 === 0)
|
|
4915
|
-
debug$1(`executed ${current}/${total}`);
|
|
4916
|
-
}
|
|
4917
|
-
catch (e) {
|
|
4918
|
-
debug$1(`transaction handler error at ${current}: ${e}`);
|
|
4919
|
-
}
|
|
4920
|
-
// yield a tick every few ops to avoid blocking
|
|
4921
|
-
if (current % 25 === 0)
|
|
4922
|
-
await this.#sleep(1);
|
|
4923
|
-
}
|
|
4924
|
-
};
|
|
4925
|
-
const workers = Array.from({ length: Math.min(concurrency, Math.max(1, total)) }, () => worker());
|
|
4926
|
-
await Promise.all(workers);
|
|
4927
|
-
}
|
|
4928
|
-
// Execute a single transaction with a timeout to avoid indefinite hangs
|
|
4929
|
-
async #executeWithTimeout(transaction, timeoutMs) {
|
|
4930
|
-
const timeout = timeoutMs ?? this.resolveTimeout ?? 5000;
|
|
4931
|
-
let timer;
|
|
4932
|
-
try {
|
|
4933
|
-
return await Promise.race([
|
|
4934
|
-
this.#machine.execute(transaction.decoded.to, transaction.decoded.method, transaction.decoded.params),
|
|
4935
|
-
new Promise((_, reject) => {
|
|
4936
|
-
timer = setTimeout(() => reject(new Error('transaction execution timeout')), timeout);
|
|
4937
|
-
})
|
|
4938
|
-
]);
|
|
4939
|
-
}
|
|
4940
|
-
finally {
|
|
4941
|
-
if (timer)
|
|
4942
|
-
clearTimeout(timer);
|
|
4943
|
-
}
|
|
4944
|
-
}
|
|
4945
4897
|
async #buildBlockChain(latestHash, maxBlocks = 1000) {
|
|
4946
4898
|
const chain = [];
|
|
4947
4899
|
let currentHash = latestHash;
|
|
@@ -5137,7 +5089,7 @@ class State extends Contract {
|
|
|
5137
5089
|
// todo throw error
|
|
5138
5090
|
async #_executeTransaction(transaction) {
|
|
5139
5091
|
try {
|
|
5140
|
-
await this.#
|
|
5092
|
+
await this.#machine.execute(transaction.decoded.to, transaction.decoded.method, transaction.decoded.params);
|
|
5141
5093
|
// await globalThis.accountsStore.put(transaction.decoded.from, String(transaction.decoded.nonce))
|
|
5142
5094
|
// if (transaction.decoded.to === nativeToken) {
|
|
5143
5095
|
// this.#nativeCalls += 1
|
|
@@ -5203,8 +5155,7 @@ class State extends Contract {
|
|
|
5203
5155
|
}
|
|
5204
5156
|
transactions = transactions.filter((transaction) => !transaction.decoded.priority);
|
|
5205
5157
|
debug$1(`executing ${transactions.length} transactions for block ${block.index}`);
|
|
5206
|
-
|
|
5207
|
-
await this.#runWithConcurrency(transactions, this.#txConcurrency, async (tx) => this.#_executeTransaction(tx));
|
|
5158
|
+
await Promise.all(transactions.map((transaction) => this.#_executeTransaction(transaction)));
|
|
5208
5159
|
this.#blocks[block.index].loaded = true;
|
|
5209
5160
|
debug$1(`executed transactions for block ${block.index}`);
|
|
5210
5161
|
if (Number(block.index) === 0)
|
|
@@ -6192,7 +6143,7 @@ class Chain extends VersionControl {
|
|
|
6192
6143
|
const transactionMessage = await new TransactionMessage({ ...transaction });
|
|
6193
6144
|
const event = await super.sendTransaction(transactionMessage);
|
|
6194
6145
|
this.#sendTransaction(transactionMessage.encoded);
|
|
6195
|
-
globalThis.peernet.publish('send-transaction', transactionMessage.
|
|
6146
|
+
globalThis.peernet.publish('send-transaction', transactionMessage.decoded);
|
|
6196
6147
|
return event;
|
|
6197
6148
|
}
|
|
6198
6149
|
async addContract(transaction, contractMessage) {
|
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
|
}
|
|
@@ -717,7 +717,7 @@ class Machine {
|
|
|
717
717
|
}
|
|
718
718
|
async addLoadedBlock(block) {
|
|
719
719
|
debug$2(`adding loaded block: ${block.index}@${block.hash}`);
|
|
720
|
-
|
|
720
|
+
debug$2(JSON.stringify(block, jsonStringifyBigInt));
|
|
721
721
|
if (block.decoded)
|
|
722
722
|
block = { ...block.decoded, hash: await block.hash() };
|
|
723
723
|
return this.#askWorker('addLoadedBlock', JSON.stringify(block, jsonStringifyBigInt));
|
|
@@ -781,7 +781,6 @@ class State extends Contract {
|
|
|
781
781
|
// Block resolution state
|
|
782
782
|
#resolvingBlocks;
|
|
783
783
|
#maxConcurrentResolves;
|
|
784
|
-
#txConcurrency;
|
|
785
784
|
#totalSize;
|
|
786
785
|
#lastResolved;
|
|
787
786
|
#lastResolvedTime;
|
|
@@ -868,7 +867,6 @@ class State extends Contract {
|
|
|
868
867
|
// Block resolution state
|
|
869
868
|
this.#resolvingBlocks = new Set();
|
|
870
869
|
this.#maxConcurrentResolves = 10;
|
|
871
|
-
this.#txConcurrency = 25;
|
|
872
870
|
this.knownBlocks = [];
|
|
873
871
|
this.#totalSize = 0;
|
|
874
872
|
this._wantList = [];
|
|
@@ -1037,52 +1035,6 @@ class State extends Contract {
|
|
|
1037
1035
|
this.#resolvingBlocks.delete(hash);
|
|
1038
1036
|
}
|
|
1039
1037
|
}
|
|
1040
|
-
// Small utility to keep event loop responsive
|
|
1041
|
-
async #sleep(ms) {
|
|
1042
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1043
|
-
}
|
|
1044
|
-
// Run a list of tasks with bounded concurrency
|
|
1045
|
-
async #runWithConcurrency(items, concurrency, handler) {
|
|
1046
|
-
let index = 0;
|
|
1047
|
-
const total = items.length;
|
|
1048
|
-
const worker = async () => {
|
|
1049
|
-
while (true) {
|
|
1050
|
-
const current = index++;
|
|
1051
|
-
if (current >= total)
|
|
1052
|
-
break;
|
|
1053
|
-
try {
|
|
1054
|
-
await handler(items[current], current);
|
|
1055
|
-
if (current % 50 === 0)
|
|
1056
|
-
debug$1(`executed ${current}/${total}`);
|
|
1057
|
-
}
|
|
1058
|
-
catch (e) {
|
|
1059
|
-
debug$1(`transaction handler error at ${current}: ${e}`);
|
|
1060
|
-
}
|
|
1061
|
-
// yield a tick every few ops to avoid blocking
|
|
1062
|
-
if (current % 25 === 0)
|
|
1063
|
-
await this.#sleep(1);
|
|
1064
|
-
}
|
|
1065
|
-
};
|
|
1066
|
-
const workers = Array.from({ length: Math.min(concurrency, Math.max(1, total)) }, () => worker());
|
|
1067
|
-
await Promise.all(workers);
|
|
1068
|
-
}
|
|
1069
|
-
// Execute a single transaction with a timeout to avoid indefinite hangs
|
|
1070
|
-
async #executeWithTimeout(transaction, timeoutMs) {
|
|
1071
|
-
const timeout = timeoutMs ?? this.resolveTimeout ?? 5000;
|
|
1072
|
-
let timer;
|
|
1073
|
-
try {
|
|
1074
|
-
return await Promise.race([
|
|
1075
|
-
this.#machine.execute(transaction.decoded.to, transaction.decoded.method, transaction.decoded.params),
|
|
1076
|
-
new Promise((_, reject) => {
|
|
1077
|
-
timer = setTimeout(() => reject(new Error('transaction execution timeout')), timeout);
|
|
1078
|
-
})
|
|
1079
|
-
]);
|
|
1080
|
-
}
|
|
1081
|
-
finally {
|
|
1082
|
-
if (timer)
|
|
1083
|
-
clearTimeout(timer);
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
1038
|
async #buildBlockChain(latestHash, maxBlocks = 1000) {
|
|
1087
1039
|
const chain = [];
|
|
1088
1040
|
let currentHash = latestHash;
|
|
@@ -1278,7 +1230,7 @@ class State extends Contract {
|
|
|
1278
1230
|
// todo throw error
|
|
1279
1231
|
async #_executeTransaction(transaction) {
|
|
1280
1232
|
try {
|
|
1281
|
-
await this.#
|
|
1233
|
+
await this.#machine.execute(transaction.decoded.to, transaction.decoded.method, transaction.decoded.params);
|
|
1282
1234
|
// await globalThis.accountsStore.put(transaction.decoded.from, String(transaction.decoded.nonce))
|
|
1283
1235
|
// if (transaction.decoded.to === nativeToken) {
|
|
1284
1236
|
// this.#nativeCalls += 1
|
|
@@ -1344,8 +1296,7 @@ class State extends Contract {
|
|
|
1344
1296
|
}
|
|
1345
1297
|
transactions = transactions.filter((transaction) => !transaction.decoded.priority);
|
|
1346
1298
|
debug$1(`executing ${transactions.length} transactions for block ${block.index}`);
|
|
1347
|
-
|
|
1348
|
-
await this.#runWithConcurrency(transactions, this.#txConcurrency, async (tx) => this.#_executeTransaction(tx));
|
|
1299
|
+
await Promise.all(transactions.map((transaction) => this.#_executeTransaction(transaction)));
|
|
1349
1300
|
this.#blocks[block.index].loaded = true;
|
|
1350
1301
|
debug$1(`executed transactions for block ${block.index}`);
|
|
1351
1302
|
if (Number(block.index) === 0)
|
|
@@ -2333,7 +2284,7 @@ class Chain extends VersionControl {
|
|
|
2333
2284
|
const transactionMessage = await new TransactionMessage({ ...transaction });
|
|
2334
2285
|
const event = await super.sendTransaction(transactionMessage);
|
|
2335
2286
|
this.#sendTransaction(transactionMessage.encoded);
|
|
2336
|
-
globalThis.peernet.publish('send-transaction', transactionMessage.
|
|
2287
|
+
globalThis.peernet.publish('send-transaction', transactionMessage.decoded);
|
|
2337
2288
|
return event;
|
|
2338
2289
|
}
|
|
2339
2290
|
async addContract(transaction, contractMessage) {
|