@leofcoin/chain 1.10.21 → 1.10.22

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.
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface, o as proto$2, q as proto$3, r as encodeFrames, s as decodeFrames, t as proto$4 } from './browser-crypto-CH0ua22L.js';
1
+ import { F as FormatInterface, o as proto$2, q as proto$3, r as encodeFrames, s as decodeFrames, t as proto$4, d as Codec } from './browser-crypto-CH0ua22L.js';
2
2
 
3
3
  class ValidatorMessage extends FormatInterface {
4
4
  get messageName() {
@@ -90,6 +90,29 @@ class StateMessage extends FormatInterface {
90
90
  }
91
91
  }
92
92
 
93
+ const limit = 1800;
94
+ const transactionLimit = 2500;
95
+ const requestTimeout = 3e4;
96
+ const syncTimeout = 3e4;
97
+ class Protocol {
98
+ constructor(config) {
99
+ this.resolveTimeout = 1e4;
100
+ if (config?.resolveTimeout) this.resolveTimeout = config.resolveTimeout;
101
+ }
102
+ get limit() {
103
+ return limit;
104
+ }
105
+ get transactionLimit() {
106
+ return transactionLimit;
107
+ }
108
+ get requestTimeout() {
109
+ return requestTimeout;
110
+ }
111
+ get syncTimeout() {
112
+ return syncTimeout;
113
+ }
114
+ }
115
+
93
116
  var networks = {
94
117
  leofcoin: {
95
118
  mainnet: {
@@ -130,6 +153,92 @@ const DEFAULT_NODE_OPTIONS = {
130
153
 
131
154
  const INITIAL_RECONNECT_DELAY = 5e3;
132
155
  const MAX_RECONNECT_DELAY = 3e4;
156
+ const guardProviderDiscovery = (peernet) => {
157
+ peernet.walk = async (hash) => {
158
+ if (!hash) throw new Error("hash expected for provider discovery");
159
+ const request = new peernet.protos["peernet-dht"]({ hash });
160
+ const message = await peernet.prepareMessage(request);
161
+ const connections = peernet.connections;
162
+ const requests = Object.entries(connections).filter(
163
+ ([id, peer]) => id !== peernet.id && peer.connected
164
+ ).map(async ([id, peer]) => {
165
+ let timeout;
166
+ try {
167
+ const result = await Promise.race([
168
+ peer.request(message.encoded),
169
+ new Promise((_, reject) => {
170
+ timeout = setTimeout(() => reject(new Error("provider discovery timed out")), requestTimeout);
171
+ })
172
+ ]);
173
+ const bytes = result instanceof Uint8Array ? result : new Uint8Array(Object.values(result));
174
+ if (new Codec(bytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
175
+ const response = new peernet.protos["peernet-dht-response"](bytes).decoded;
176
+ if (response.hash !== hash) throw new Error("provider response hash mismatch");
177
+ if (!response.has) throw new Error("peer does not provide content");
178
+ peernet.dht.addProvider({ id, address: peer.remoteAddress }, hash);
179
+ } catch (error) {
180
+ if (error.message !== "peer does not provide content") {
181
+ console.warn(`Provider discovery failed for ${id}:`, error?.message ?? error);
182
+ }
183
+ throw error;
184
+ } finally {
185
+ clearTimeout(timeout);
186
+ }
187
+ });
188
+ if (requests.length === 0) return;
189
+ try {
190
+ await Promise.any(requests);
191
+ } catch (error) {
192
+ if (!(error instanceof AggregateError)) throw error;
193
+ console.warn(`No connected provider found for ${hash}`);
194
+ }
195
+ };
196
+ const requestData = peernet.requestData.bind(peernet);
197
+ peernet.requestData = async (hash, store, options) => {
198
+ const storeName = typeof store === "string" ? store : store?.name;
199
+ if (!["block", "transaction", "transactionPool"].includes(storeName)) {
200
+ return requestData(hash, store, options);
201
+ }
202
+ const probe = await peernet.prepareMessage(new peernet.protos["peernet-dht"]({ hash, store: storeName }));
203
+ const request = await peernet.prepareMessage(new peernet.protos["peernet-data"]({ hash, store: storeName }));
204
+ const connections = peernet.connections;
205
+ const requests = Object.entries(connections).filter(
206
+ ([id, peer]) => id !== peernet.id && peer.connected
207
+ ).map(async ([id, peer]) => {
208
+ let timeout;
209
+ try {
210
+ const fetch = async () => {
211
+ const availability = await peer.request(probe.encoded);
212
+ const probeBytes = availability instanceof Uint8Array ? availability : new Uint8Array(Object.values(availability));
213
+ if (new Codec(probeBytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
214
+ const provider = new peernet.protos["peernet-dht-response"](probeBytes).decoded;
215
+ if (provider.hash !== hash || !provider.has) throw new Error("peer does not provide content in requested store");
216
+ const result = await peer.request(request.encoded);
217
+ const bytes = result instanceof Uint8Array ? result : new Uint8Array(Object.values(result));
218
+ if (new Codec(bytes).name !== "peernet-data-response") throw new Error("invalid data response codec");
219
+ const response = new peernet.protos["peernet-data-response"](bytes).decoded;
220
+ if (response.hash !== hash || !(response.data instanceof Uint8Array) || response.data.length === 0) {
221
+ throw new Error("invalid content response");
222
+ }
223
+ return response.data;
224
+ };
225
+ return await Promise.race([
226
+ fetch(),
227
+ new Promise((_, reject) => {
228
+ timeout = setTimeout(() => reject(new Error(`content request timed out for ${id}`)), requestTimeout);
229
+ })
230
+ ]);
231
+ } finally {
232
+ clearTimeout(timeout);
233
+ }
234
+ });
235
+ try {
236
+ return await Promise.any(requests);
237
+ } catch (error) {
238
+ throw new Error(`Content unavailable in ${storeName}: ${hash}`, { cause: error });
239
+ }
240
+ };
241
+ };
133
242
  const isSendablePeer = (peer) => {
134
243
  if (!peer || typeof peer !== "object") return false;
135
244
  const candidate = peer;
@@ -451,4 +560,4 @@ class ConnectionMonitor {
451
560
  }
452
561
  }
453
562
 
454
- export { BlockMessage as B, ConnectionMonitor as C, DEFAULT_NODE_OPTIONS as D, LastBlockMessage as L, PROTOCOL_VERSION as P, REACHED_ONE_ZERO_ZERO as R, StateMessage as S, ValidatorMessage as V, BWMessage as a, guardPeerMessages as g };
563
+ export { BlockMessage as B, ConnectionMonitor as C, DEFAULT_NODE_OPTIONS as D, LastBlockMessage as L, Protocol as P, REACHED_ONE_ZERO_ZERO as R, StateMessage as S, ValidatorMessage as V, PROTOCOL_VERSION as a, BWMessage as b, guardProviderDiscovery as c, guardPeerMessages as g };
@@ -1,6 +1,6 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-Ha4Lvntj.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-BMfelxXV.js';
2
2
  import './browser-crypto-CH0ua22L.js';
3
- import './connection-monitor-D-krNiRH.js';
3
+ import './connection-monitor-DMGAhcC0.js';
4
4
  import './index-CvKt4UDE.js';
5
5
  import './sha2-BKcEJzgd.js';
6
6