@leofcoin/chain 1.4.37 → 1.4.39
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 +43 -14
- package/exports/browser/{client-033977a1-60f68d3b.js → client-de444350-a666f9b6.js} +25 -16
- package/exports/browser/{index-123daa04-05013cc5.js → index-712688fc-ad40bdec.js} +1 -1
- package/exports/browser/{messages-5ddc1083-1bd0ab64.js → messages-3f370680-fb7e5b5b.js} +1 -1
- package/exports/browser/{node-browser-c95add6b.js → node-browser-bd6a5de3.js} +4248 -145
- package/exports/browser/node-browser.js +1 -1
- package/exports/chain.js +43 -14
- package/exports/typings/chain.d.ts +1 -0
- package/package.json +1 -1
package/exports/browser/chain.js
CHANGED
|
@@ -8022,7 +8022,7 @@ class Chain extends Contract {
|
|
|
8022
8022
|
let data = await new globalThis.peernet.protos['peernet-request']({ request: 'lastBlock' });
|
|
8023
8023
|
let node = await globalThis.peernet.prepareMessage(data);
|
|
8024
8024
|
for (const peer of globalThis.peernet?.connections) {
|
|
8025
|
-
if (peer.connected
|
|
8025
|
+
if (peer.connected) {
|
|
8026
8026
|
promises.push(async () => {
|
|
8027
8027
|
try {
|
|
8028
8028
|
const result = await peer.request(node.encoded);
|
|
@@ -8033,7 +8033,11 @@ class Chain extends Contract {
|
|
|
8033
8033
|
}
|
|
8034
8034
|
});
|
|
8035
8035
|
}
|
|
8036
|
-
else if (!peer.connected
|
|
8036
|
+
else if (!peer.connected) {
|
|
8037
|
+
globalThis.peernet.removePeer(peer);
|
|
8038
|
+
// todo: remove peer
|
|
8039
|
+
// reinitiate channel?
|
|
8040
|
+
}
|
|
8037
8041
|
}
|
|
8038
8042
|
promises = await this.promiseRequests(promises);
|
|
8039
8043
|
let latest = { index: 0, hash: '0x0', previousHash: '0x0' };
|
|
@@ -8074,6 +8078,7 @@ class Chain extends Contract {
|
|
|
8074
8078
|
// let node =
|
|
8075
8079
|
// globalThis.peernet.protos['peernet-response']({response: node.encoded})
|
|
8076
8080
|
// })
|
|
8081
|
+
await globalThis.peernet.addRequestHandler('transactionPool', this.#transactionPoolHandler.bind(this));
|
|
8077
8082
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
8078
8083
|
await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
8079
8084
|
globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
@@ -8119,6 +8124,13 @@ class Chain extends Contract {
|
|
|
8119
8124
|
}, validatorInfo.timeout);
|
|
8120
8125
|
this.#jail.push(validatorInfo.address);
|
|
8121
8126
|
}
|
|
8127
|
+
async triggerSync() {
|
|
8128
|
+
if (this.#chainSyncing)
|
|
8129
|
+
return 'already syncing';
|
|
8130
|
+
const latest = await this.#getLatestBlock();
|
|
8131
|
+
await this.#syncChain(latest);
|
|
8132
|
+
return 'synced';
|
|
8133
|
+
}
|
|
8122
8134
|
async #syncChain(lastBlock) {
|
|
8123
8135
|
if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.hash)
|
|
8124
8136
|
return;
|
|
@@ -8146,21 +8158,38 @@ class Chain extends Contract {
|
|
|
8146
8158
|
}
|
|
8147
8159
|
this.#chainSyncing = false;
|
|
8148
8160
|
}
|
|
8149
|
-
async #
|
|
8150
|
-
let node = await new globalThis.peernet.protos['peernet-request']({ request
|
|
8151
|
-
|
|
8161
|
+
async #prepareRequest(request) {
|
|
8162
|
+
let node = await new globalThis.peernet.protos['peernet-request']({ request });
|
|
8163
|
+
return globalThis.peernet.prepareMessage(node);
|
|
8164
|
+
}
|
|
8165
|
+
async #makeRequest(peer, request) {
|
|
8166
|
+
const node = await this.#prepareRequest(request);
|
|
8152
8167
|
let response = await peer.request(node.encoded);
|
|
8153
|
-
response = await new globalThis.
|
|
8154
|
-
|
|
8155
|
-
|
|
8156
|
-
|
|
8157
|
-
|
|
8158
|
-
|
|
8159
|
-
|
|
8160
|
-
|
|
8161
|
-
|
|
8168
|
+
response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
|
|
8169
|
+
return response.decoded.response;
|
|
8170
|
+
}
|
|
8171
|
+
async #peerConnected(peer) {
|
|
8172
|
+
const lastBlock = await this.#makeRequest(peer, 'lastBlock');
|
|
8173
|
+
this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
8174
|
+
let pool = await this.#makeRequest(peer, 'transactionPool');
|
|
8175
|
+
pool = await Promise.all(pool.map(async (hash) => {
|
|
8176
|
+
const has = await globalThis.peernet.has(hash);
|
|
8177
|
+
return { has, hash };
|
|
8178
|
+
}));
|
|
8179
|
+
pool = pool.filter(item => !item.has);
|
|
8180
|
+
await Promise.all(pool.map(async ({ hash }) => {
|
|
8181
|
+
const result = await globalThis.peernet.get(hash);
|
|
8182
|
+
await globalThis.peernet.put(hash, result);
|
|
8183
|
+
}));
|
|
8184
|
+
console.log(pool);
|
|
8185
|
+
if (lastBlock)
|
|
8186
|
+
this.#syncChain(lastBlock);
|
|
8162
8187
|
}
|
|
8163
8188
|
#epochTimeout;
|
|
8189
|
+
async #transactionPoolHandler() {
|
|
8190
|
+
const pool = await globalThis.transactionPoolStore.keys();
|
|
8191
|
+
return new globalThis.peernet.protos['peernet-response']({ response: pool });
|
|
8192
|
+
}
|
|
8164
8193
|
async #lastBlockHandler() {
|
|
8165
8194
|
return new globalThis.peernet.protos['peernet-response']({ response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index } });
|
|
8166
8195
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-
|
|
1
|
+
import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './node-browser-bd6a5de3.js';
|
|
2
2
|
import './contract-32687834.js';
|
|
3
3
|
|
|
4
4
|
function commonjsRequire(path) {
|
|
@@ -10624,6 +10624,29 @@ var p2pt_umd = {
|
|
|
10624
10624
|
}
|
|
10625
10625
|
}
|
|
10626
10626
|
|
|
10627
|
+
_trySend(peer, data) {
|
|
10628
|
+
let shouldTry = data.msg.length > 0;
|
|
10629
|
+
let chunks = 0;
|
|
10630
|
+
let remaining = '';
|
|
10631
|
+
|
|
10632
|
+
while (shouldTry) {
|
|
10633
|
+
data.c = chunks;
|
|
10634
|
+
|
|
10635
|
+
remaining = data.msg.slice(MAX_MESSAGE_LENGTH);
|
|
10636
|
+
data.msg = data.msg.slice(0, MAX_MESSAGE_LENGTH);
|
|
10637
|
+
|
|
10638
|
+
if (!remaining) { data.last = true; }
|
|
10639
|
+
if (peer.connected) {
|
|
10640
|
+
peer.send(JSON_MESSAGE_IDENTIFIER + JSON.stringify(data));
|
|
10641
|
+
data.msg = remaining;
|
|
10642
|
+
chunks++;
|
|
10643
|
+
shouldTry = data.msg.length > 0;
|
|
10644
|
+
} else {
|
|
10645
|
+
shouldTry = false;
|
|
10646
|
+
this._removePeer(peer);
|
|
10647
|
+
}
|
|
10648
|
+
}
|
|
10649
|
+
}
|
|
10627
10650
|
/**
|
|
10628
10651
|
* Send a msg and get response for it
|
|
10629
10652
|
* @param Peer peer simple-peer object to send msg to
|
|
@@ -10664,21 +10687,7 @@ var p2pt_umd = {
|
|
|
10664
10687
|
return reject(Error('Connection to peer closed' + e))
|
|
10665
10688
|
}
|
|
10666
10689
|
|
|
10667
|
-
|
|
10668
|
-
let remaining = '';
|
|
10669
|
-
while (data.msg.length > 0) {
|
|
10670
|
-
data.c = chunks;
|
|
10671
|
-
|
|
10672
|
-
remaining = data.msg.slice(MAX_MESSAGE_LENGTH);
|
|
10673
|
-
data.msg = data.msg.slice(0, MAX_MESSAGE_LENGTH);
|
|
10674
|
-
|
|
10675
|
-
if (!remaining) { data.last = true; }
|
|
10676
|
-
|
|
10677
|
-
peer.send(JSON_MESSAGE_IDENTIFIER + JSON.stringify(data));
|
|
10678
|
-
|
|
10679
|
-
data.msg = remaining;
|
|
10680
|
-
chunks++;
|
|
10681
|
-
}
|
|
10690
|
+
this._trySend(peer, data);
|
|
10682
10691
|
|
|
10683
10692
|
debug$2('sent a message to ' + peer.id);
|
|
10684
10693
|
})
|