@leofcoin/chain 1.9.19 → 1.9.21
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 +63 -23
- package/exports/chain.js +63 -23
- package/package.json +1 -1
package/exports/browser/chain.js
CHANGED
|
@@ -5137,7 +5137,11 @@ class State extends Contract {
|
|
|
5137
5137
|
let index = this.#blockHashMap.get(hash);
|
|
5138
5138
|
let localHash = "0x0";
|
|
5139
5139
|
try {
|
|
5140
|
-
|
|
5140
|
+
const rawLocalHash = await globalThis.stateStore.get("lastBlock");
|
|
5141
|
+
if (rawLocalHash) {
|
|
5142
|
+
const decoded = JSON.parse(new TextDecoder().decode(rawLocalHash));
|
|
5143
|
+
localHash = typeof decoded === "string" ? decoded : decoded?.hash ?? "0x0";
|
|
5144
|
+
}
|
|
5141
5145
|
} catch (error) {
|
|
5142
5146
|
debug$1("no local state found");
|
|
5143
5147
|
}
|
|
@@ -5367,10 +5371,21 @@ class State extends Contract {
|
|
|
5367
5371
|
compatiblePeerCount += 1;
|
|
5368
5372
|
const task = async () => {
|
|
5369
5373
|
try {
|
|
5370
|
-
|
|
5374
|
+
let result = await peer.request(node.encoded);
|
|
5371
5375
|
const resultType = result instanceof Uint8Array ? `bytes:${result.length}` : typeof result;
|
|
5372
5376
|
debug$1(`lastBlock result type: ${resultType}`);
|
|
5373
5377
|
console.log({ result });
|
|
5378
|
+
if (result instanceof Uint8Array) {
|
|
5379
|
+
for (let i = 0; i < 3; i += 1) {
|
|
5380
|
+
try {
|
|
5381
|
+
const wrapped = await new globalThis.peernet.protos["peernet-response"](result);
|
|
5382
|
+
if (wrapped?.decoded?.response === void 0) break;
|
|
5383
|
+
result = wrapped.decoded.response;
|
|
5384
|
+
} catch {
|
|
5385
|
+
break;
|
|
5386
|
+
}
|
|
5387
|
+
}
|
|
5388
|
+
}
|
|
5374
5389
|
return { result: new LastBlockMessage(result), peer };
|
|
5375
5390
|
} catch (error) {
|
|
5376
5391
|
const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
|
|
@@ -5447,6 +5462,9 @@ class State extends Contract {
|
|
|
5447
5462
|
*/
|
|
5448
5463
|
async #loadBlocks(blocks) {
|
|
5449
5464
|
this.#chainState = "loading";
|
|
5465
|
+
if (blocks.some((block) => !block)) {
|
|
5466
|
+
throw new Error("missing block data during load; chain resolution incomplete");
|
|
5467
|
+
}
|
|
5450
5468
|
const poolTransactionKeys = new Set(await globalThis.transactionPoolStore.keys());
|
|
5451
5469
|
debug$1(`pool transactions: ${poolTransactionKeys.size}`);
|
|
5452
5470
|
debug$1(`loading ${blocks.length} blocks`);
|
|
@@ -6381,25 +6399,25 @@ class Chain extends VersionControl {
|
|
|
6381
6399
|
const node = await this.#prepareRequest(request);
|
|
6382
6400
|
try {
|
|
6383
6401
|
let response = await peer.request(node.encoded);
|
|
6402
|
+
console.log(`raw response for request ${request}:`, response);
|
|
6384
6403
|
if (response === void 0 || response === null) return response;
|
|
6385
6404
|
const normalizeResponse = async (payload) => {
|
|
6386
6405
|
if (payload === void 0 || payload === null) return payload;
|
|
6387
|
-
if (payload && typeof payload === "object") {
|
|
6388
|
-
if ("decoded" in payload && payload?.decoded && "response" in payload.decoded) {
|
|
6389
|
-
return normalizeResponse(payload.decoded.response);
|
|
6390
|
-
}
|
|
6391
|
-
if ("response" in payload) {
|
|
6392
|
-
return normalizeResponse(payload.response);
|
|
6393
|
-
}
|
|
6394
|
-
}
|
|
6395
6406
|
if (payload instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(payload)) {
|
|
6396
|
-
|
|
6397
|
-
|
|
6398
|
-
|
|
6399
|
-
|
|
6400
|
-
|
|
6407
|
+
let bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
|
|
6408
|
+
for (let i = 0; i < 3; i += 1) {
|
|
6409
|
+
try {
|
|
6410
|
+
const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
|
|
6411
|
+
if (wrapped?.decoded?.response === void 0) break;
|
|
6412
|
+
const next = wrapped.decoded.response;
|
|
6413
|
+
if (next instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(next)) {
|
|
6414
|
+
bytes = next instanceof Uint8Array ? next : new Uint8Array(next);
|
|
6415
|
+
continue;
|
|
6416
|
+
}
|
|
6417
|
+
return normalizeResponse(next);
|
|
6418
|
+
} catch {
|
|
6419
|
+
break;
|
|
6401
6420
|
}
|
|
6402
|
-
} catch {
|
|
6403
6421
|
}
|
|
6404
6422
|
try {
|
|
6405
6423
|
return JSON.parse(new TextDecoder().decode(bytes));
|
|
@@ -6407,6 +6425,16 @@ class Chain extends VersionControl {
|
|
|
6407
6425
|
return bytes;
|
|
6408
6426
|
}
|
|
6409
6427
|
}
|
|
6428
|
+
if (payload && typeof payload === "object") {
|
|
6429
|
+
const objectPayload = payload;
|
|
6430
|
+
const decoded = objectPayload.decoded;
|
|
6431
|
+
if ("decoded" in objectPayload && decoded && "response" in decoded) {
|
|
6432
|
+
return normalizeResponse(decoded.response);
|
|
6433
|
+
}
|
|
6434
|
+
if ("response" in objectPayload) {
|
|
6435
|
+
return normalizeResponse(objectPayload.response);
|
|
6436
|
+
}
|
|
6437
|
+
}
|
|
6410
6438
|
if (typeof payload === "string") {
|
|
6411
6439
|
try {
|
|
6412
6440
|
return JSON.parse(payload);
|
|
@@ -6472,9 +6500,21 @@ class Chain extends VersionControl {
|
|
|
6472
6500
|
}
|
|
6473
6501
|
async getPeerTransactionPool(peer) {
|
|
6474
6502
|
let transactionsInPool = await this.#makeRequest(peer, "transactionPool");
|
|
6503
|
+
console.log("raw response for request transactionPool:", transactionsInPool);
|
|
6475
6504
|
if (transactionsInPool instanceof Uint8Array) {
|
|
6476
|
-
|
|
6477
|
-
|
|
6505
|
+
try {
|
|
6506
|
+
transactionsInPool = JSON.parse(new TextDecoder().decode(transactionsInPool));
|
|
6507
|
+
} catch {
|
|
6508
|
+
debug("transactionPool response must be decoded array payload");
|
|
6509
|
+
return [];
|
|
6510
|
+
}
|
|
6511
|
+
}
|
|
6512
|
+
if (typeof transactionsInPool === "string") {
|
|
6513
|
+
try {
|
|
6514
|
+
transactionsInPool = JSON.parse(transactionsInPool);
|
|
6515
|
+
} catch {
|
|
6516
|
+
return [];
|
|
6517
|
+
}
|
|
6478
6518
|
}
|
|
6479
6519
|
if (!Array.isArray(transactionsInPool)) return [];
|
|
6480
6520
|
const localTransactions = new Set(await globalThis.transactionPoolStore.keys());
|
|
@@ -6535,6 +6575,7 @@ class Chain extends VersionControl {
|
|
|
6535
6575
|
console.log("requesting last block from peer...");
|
|
6536
6576
|
console.log(await this.lastBlock);
|
|
6537
6577
|
const lastBlockRaw = await this.#makeRequest(peer, "lastBlock");
|
|
6578
|
+
console.log("raw last block response:", lastBlockRaw);
|
|
6538
6579
|
if (lastBlockRaw === void 0 || lastBlockRaw === null || typeof lastBlockRaw !== "object" && !(lastBlockRaw instanceof Uint8Array)) {
|
|
6539
6580
|
throw new Error(`invalid lastBlock payload: ${typeof lastBlockRaw}`);
|
|
6540
6581
|
}
|
|
@@ -6558,9 +6599,9 @@ class Chain extends VersionControl {
|
|
|
6558
6599
|
debug(`peer has no lastBlock: ${peerId}`);
|
|
6559
6600
|
return;
|
|
6560
6601
|
}
|
|
6561
|
-
const higherThenCurrentLocal =
|
|
6602
|
+
const higherThenCurrentLocal = localBlock?.index == null ? true : lastBlock.index > localBlock.index;
|
|
6562
6603
|
if (lastBlock) {
|
|
6563
|
-
if (!
|
|
6604
|
+
if (!localBlock || higherThenCurrentLocal) {
|
|
6564
6605
|
try {
|
|
6565
6606
|
const knownBlocksRaw = await this.#makeRequest(peer, "knownBlocks");
|
|
6566
6607
|
const knownBlocksResponse = await this.#decodeKnownBlocksResponse(knownBlocksRaw);
|
|
@@ -6588,9 +6629,8 @@ class Chain extends VersionControl {
|
|
|
6588
6629
|
}
|
|
6589
6630
|
if (this.wantList.length > 0) {
|
|
6590
6631
|
const promises = await Promise.allSettled(this.wantList.map((hash) => peernet.get(hash, "block")));
|
|
6591
|
-
for (let i =
|
|
6592
|
-
|
|
6593
|
-
if (result.status === "fulfilled") this.wantList.splice(i, 1);
|
|
6632
|
+
for (let i = promises.length - 1; i >= 0; i -= 1) {
|
|
6633
|
+
if (promises[i].status === "fulfilled") this.wantList.splice(i, 1);
|
|
6594
6634
|
}
|
|
6595
6635
|
if (this.wantList.length === 0) await this.triggerSync();
|
|
6596
6636
|
}
|
package/exports/chain.js
CHANGED
|
@@ -1343,7 +1343,11 @@ class State extends Contract {
|
|
|
1343
1343
|
let index = this.#blockHashMap.get(hash);
|
|
1344
1344
|
let localHash = "0x0";
|
|
1345
1345
|
try {
|
|
1346
|
-
|
|
1346
|
+
const rawLocalHash = await globalThis.stateStore.get("lastBlock");
|
|
1347
|
+
if (rawLocalHash) {
|
|
1348
|
+
const decoded = JSON.parse(new TextDecoder().decode(rawLocalHash));
|
|
1349
|
+
localHash = typeof decoded === "string" ? decoded : decoded?.hash ?? "0x0";
|
|
1350
|
+
}
|
|
1347
1351
|
} catch (error) {
|
|
1348
1352
|
debug$1("no local state found");
|
|
1349
1353
|
}
|
|
@@ -1573,10 +1577,21 @@ class State extends Contract {
|
|
|
1573
1577
|
compatiblePeerCount += 1;
|
|
1574
1578
|
const task = async () => {
|
|
1575
1579
|
try {
|
|
1576
|
-
|
|
1580
|
+
let result = await peer.request(node.encoded);
|
|
1577
1581
|
const resultType = result instanceof Uint8Array ? `bytes:${result.length}` : typeof result;
|
|
1578
1582
|
debug$1(`lastBlock result type: ${resultType}`);
|
|
1579
1583
|
console.log({ result });
|
|
1584
|
+
if (result instanceof Uint8Array) {
|
|
1585
|
+
for (let i = 0; i < 3; i += 1) {
|
|
1586
|
+
try {
|
|
1587
|
+
const wrapped = await new globalThis.peernet.protos["peernet-response"](result);
|
|
1588
|
+
if (wrapped?.decoded?.response === void 0) break;
|
|
1589
|
+
result = wrapped.decoded.response;
|
|
1590
|
+
} catch {
|
|
1591
|
+
break;
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1580
1595
|
return { result: new LastBlockMessage(result), peer };
|
|
1581
1596
|
} catch (error) {
|
|
1582
1597
|
const peerId = peer?.peerId || peer?.id || peer?.address || "unknown";
|
|
@@ -1653,6 +1668,9 @@ class State extends Contract {
|
|
|
1653
1668
|
*/
|
|
1654
1669
|
async #loadBlocks(blocks) {
|
|
1655
1670
|
this.#chainState = "loading";
|
|
1671
|
+
if (blocks.some((block) => !block)) {
|
|
1672
|
+
throw new Error("missing block data during load; chain resolution incomplete");
|
|
1673
|
+
}
|
|
1656
1674
|
const poolTransactionKeys = new Set(await globalThis.transactionPoolStore.keys());
|
|
1657
1675
|
debug$1(`pool transactions: ${poolTransactionKeys.size}`);
|
|
1658
1676
|
debug$1(`loading ${blocks.length} blocks`);
|
|
@@ -2587,25 +2605,25 @@ class Chain extends VersionControl {
|
|
|
2587
2605
|
const node = await this.#prepareRequest(request);
|
|
2588
2606
|
try {
|
|
2589
2607
|
let response = await peer.request(node.encoded);
|
|
2608
|
+
console.log(`raw response for request ${request}:`, response);
|
|
2590
2609
|
if (response === void 0 || response === null) return response;
|
|
2591
2610
|
const normalizeResponse = async (payload) => {
|
|
2592
2611
|
if (payload === void 0 || payload === null) return payload;
|
|
2593
|
-
if (payload && typeof payload === "object") {
|
|
2594
|
-
if ("decoded" in payload && payload?.decoded && "response" in payload.decoded) {
|
|
2595
|
-
return normalizeResponse(payload.decoded.response);
|
|
2596
|
-
}
|
|
2597
|
-
if ("response" in payload) {
|
|
2598
|
-
return normalizeResponse(payload.response);
|
|
2599
|
-
}
|
|
2600
|
-
}
|
|
2601
2612
|
if (payload instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(payload)) {
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2613
|
+
let bytes = payload instanceof Uint8Array ? payload : new Uint8Array(payload);
|
|
2614
|
+
for (let i = 0; i < 3; i += 1) {
|
|
2615
|
+
try {
|
|
2616
|
+
const wrapped = await new globalThis.peernet.protos["peernet-response"](bytes);
|
|
2617
|
+
if (wrapped?.decoded?.response === void 0) break;
|
|
2618
|
+
const next = wrapped.decoded.response;
|
|
2619
|
+
if (next instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(next)) {
|
|
2620
|
+
bytes = next instanceof Uint8Array ? next : new Uint8Array(next);
|
|
2621
|
+
continue;
|
|
2622
|
+
}
|
|
2623
|
+
return normalizeResponse(next);
|
|
2624
|
+
} catch {
|
|
2625
|
+
break;
|
|
2607
2626
|
}
|
|
2608
|
-
} catch {
|
|
2609
2627
|
}
|
|
2610
2628
|
try {
|
|
2611
2629
|
return JSON.parse(new TextDecoder().decode(bytes));
|
|
@@ -2613,6 +2631,16 @@ class Chain extends VersionControl {
|
|
|
2613
2631
|
return bytes;
|
|
2614
2632
|
}
|
|
2615
2633
|
}
|
|
2634
|
+
if (payload && typeof payload === "object") {
|
|
2635
|
+
const objectPayload = payload;
|
|
2636
|
+
const decoded = objectPayload.decoded;
|
|
2637
|
+
if ("decoded" in objectPayload && decoded && "response" in decoded) {
|
|
2638
|
+
return normalizeResponse(decoded.response);
|
|
2639
|
+
}
|
|
2640
|
+
if ("response" in objectPayload) {
|
|
2641
|
+
return normalizeResponse(objectPayload.response);
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2616
2644
|
if (typeof payload === "string") {
|
|
2617
2645
|
try {
|
|
2618
2646
|
return JSON.parse(payload);
|
|
@@ -2678,9 +2706,21 @@ class Chain extends VersionControl {
|
|
|
2678
2706
|
}
|
|
2679
2707
|
async getPeerTransactionPool(peer) {
|
|
2680
2708
|
let transactionsInPool = await this.#makeRequest(peer, "transactionPool");
|
|
2709
|
+
console.log("raw response for request transactionPool:", transactionsInPool);
|
|
2681
2710
|
if (transactionsInPool instanceof Uint8Array) {
|
|
2682
|
-
|
|
2683
|
-
|
|
2711
|
+
try {
|
|
2712
|
+
transactionsInPool = JSON.parse(new TextDecoder().decode(transactionsInPool));
|
|
2713
|
+
} catch {
|
|
2714
|
+
debug("transactionPool response must be decoded array payload");
|
|
2715
|
+
return [];
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
if (typeof transactionsInPool === "string") {
|
|
2719
|
+
try {
|
|
2720
|
+
transactionsInPool = JSON.parse(transactionsInPool);
|
|
2721
|
+
} catch {
|
|
2722
|
+
return [];
|
|
2723
|
+
}
|
|
2684
2724
|
}
|
|
2685
2725
|
if (!Array.isArray(transactionsInPool)) return [];
|
|
2686
2726
|
const localTransactions = new Set(await globalThis.transactionPoolStore.keys());
|
|
@@ -2741,6 +2781,7 @@ class Chain extends VersionControl {
|
|
|
2741
2781
|
console.log("requesting last block from peer...");
|
|
2742
2782
|
console.log(await this.lastBlock);
|
|
2743
2783
|
const lastBlockRaw = await this.#makeRequest(peer, "lastBlock");
|
|
2784
|
+
console.log("raw last block response:", lastBlockRaw);
|
|
2744
2785
|
if (lastBlockRaw === void 0 || lastBlockRaw === null || typeof lastBlockRaw !== "object" && !(lastBlockRaw instanceof Uint8Array)) {
|
|
2745
2786
|
throw new Error(`invalid lastBlock payload: ${typeof lastBlockRaw}`);
|
|
2746
2787
|
}
|
|
@@ -2764,9 +2805,9 @@ class Chain extends VersionControl {
|
|
|
2764
2805
|
debug(`peer has no lastBlock: ${peerId}`);
|
|
2765
2806
|
return;
|
|
2766
2807
|
}
|
|
2767
|
-
const higherThenCurrentLocal =
|
|
2808
|
+
const higherThenCurrentLocal = localBlock?.index == null ? true : lastBlock.index > localBlock.index;
|
|
2768
2809
|
if (lastBlock) {
|
|
2769
|
-
if (!
|
|
2810
|
+
if (!localBlock || higherThenCurrentLocal) {
|
|
2770
2811
|
try {
|
|
2771
2812
|
const knownBlocksRaw = await this.#makeRequest(peer, "knownBlocks");
|
|
2772
2813
|
const knownBlocksResponse = await this.#decodeKnownBlocksResponse(knownBlocksRaw);
|
|
@@ -2794,9 +2835,8 @@ class Chain extends VersionControl {
|
|
|
2794
2835
|
}
|
|
2795
2836
|
if (this.wantList.length > 0) {
|
|
2796
2837
|
const promises = await Promise.allSettled(this.wantList.map((hash) => peernet.get(hash, "block")));
|
|
2797
|
-
for (let i =
|
|
2798
|
-
|
|
2799
|
-
if (result.status === "fulfilled") this.wantList.splice(i, 1);
|
|
2838
|
+
for (let i = promises.length - 1; i >= 0; i -= 1) {
|
|
2839
|
+
if (promises[i].status === "fulfilled") this.wantList.splice(i, 1);
|
|
2800
2840
|
}
|
|
2801
2841
|
if (this.wantList.length === 0) await this.triggerSync();
|
|
2802
2842
|
}
|