@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.
@@ -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 && peer.readyState === 'open' && peer.peerId !== this.id) {
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 || peer.readyState !== 'open') ;
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 #peerConnected(peer) {
8150
- let node = await new globalThis.peernet.protos['peernet-request']({ request: 'lastBlock' });
8151
- node = await globalThis.peernet.prepareMessage(node);
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.globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
8154
- let lastBlock = response.decoded.response;
8155
- // try catch known blocks
8156
- node = await new globalThis.peernet.protos['peernet-request']({ request: 'knownBlocks' });
8157
- node = await globalThis.peernet.prepareMessage(node);
8158
- response = await peer.request(node.encoded);
8159
- response = await new globalThis.globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
8160
- this.#knownBlocks = response.decoded.response;
8161
- this.#syncChain(lastBlock);
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-c95add6b.js';
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
- let chunks = 0;
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
  })
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-c95add6b.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-bd6a5de3.js';
2
2
  import './contract-32687834.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './node-browser-c95add6b.js';
1
+ import { F as FormatInterface } from './node-browser-bd6a5de3.js';
2
2
  import './contract-32687834.js';
3
3
 
4
4
  var proto$b = {