@leofcoin/chain 1.10.21 → 1.11.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.
Files changed (29) hide show
  1. package/README.md +95 -0
  2. package/exports/browser/beacon-wire.js +2 -2
  3. package/exports/browser/{browser-CCfR9KOM-BGMMnVsR.js → browser-CCfR9KOM-CNWKLusu.js} +3 -3
  4. package/exports/browser/{browser-crypto-CH0ua22L.js → browser-crypto-B_UJguhk.js} +453 -35
  5. package/exports/browser/chain.js +1275 -996
  6. package/exports/browser/client-DbZBfpG6-C1cXgvfr.js +5943 -0
  7. package/exports/browser/{connection-monitor-D-krNiRH.js → connection-monitor-BzbeYGEt.js} +153 -24
  8. package/exports/browser/{index-dTQVgkpf-DOL2rIjp.js → index-dTQVgkpf-CkoAiRNX.js} +3 -3
  9. package/exports/browser/{index-DCbLvMKL.js → index-phv5KvaE.js} +238 -234
  10. package/exports/browser/{messages-DCePyqKC-BSvJxDWZ.js → messages-DZYCxoZI-B472zldo.js} +3 -3
  11. package/exports/browser/{node-browser-Ha4Lvntj.js → node-browser-BmyPBkL5.js} +55 -11
  12. package/exports/browser/node-browser.js +3 -3
  13. package/exports/browser/workers/block-worker.js +8 -9
  14. package/exports/browser/workers/machine-worker.js +141 -1195
  15. package/exports/{workers/worker-BDTWOCzS-COrAszCt.js → browser/workers/proposal.proto-8zgMfutf-B3So_tnS.js} +910 -398
  16. package/exports/chain.js +296 -1078
  17. package/exports/connection-monitor-BdFLRh-J.js +465 -0
  18. package/exports/connection-monitor.js +2 -324
  19. package/exports/{constants-Dy9i9S_o.js → constants-BQ6f-_OT.js} +1 -1
  20. package/exports/hash-format-state.js +3 -3
  21. package/exports/machine.js +550 -0
  22. package/exports/node.js +35 -2
  23. package/exports/state-checkpoint-BiQ1EXzo.js +110 -0
  24. package/exports/state-checkpoint.js +3 -0
  25. package/exports/workers/block-worker.js +8 -9
  26. package/exports/workers/machine-worker.js +141 -1195
  27. package/exports/{browser/workers/worker-BDTWOCzS-COrAszCt.js → workers/proposal.proto-8zgMfutf-B3So_tnS.js} +910 -398
  28. package/package.json +9 -7
  29. package/exports/browser/client-DKKzPSlb-C_1ug6ux.js +0 -2401
@@ -1,6 +1,6 @@
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 { B as BinaryFormat, t as proto, u as proto$1, w as encodeFrames, x as decodeFrames, y as proto$2, z as proto$3, A as proto$4, C as Codec } from './browser-crypto-B_UJguhk.js';
2
2
 
3
- class ValidatorMessage extends FormatInterface {
3
+ class ValidatorMessage extends BinaryFormat {
4
4
  get messageName() {
5
5
  return "ValidatorMessage";
6
6
  }
@@ -8,11 +8,11 @@ class ValidatorMessage extends FormatInterface {
8
8
  if (buffer instanceof ValidatorMessage)
9
9
  return buffer;
10
10
  const name = "validator-message";
11
- super(buffer, proto$2, { name });
11
+ super(buffer, proto, { name });
12
12
  }
13
13
  }
14
14
 
15
- class BlockMessage extends FormatInterface {
15
+ class BlockMessage extends BinaryFormat {
16
16
  get messageName() {
17
17
  return "BlockMessage";
18
18
  }
@@ -20,7 +20,7 @@ class BlockMessage extends FormatInterface {
20
20
  if (buffer instanceof BlockMessage)
21
21
  return buffer;
22
22
  const name = "block-message";
23
- super(buffer, proto$3, { name });
23
+ super(buffer, proto$1, { name });
24
24
  }
25
25
  encode(decoded) {
26
26
  decoded = decoded || this.decoded;
@@ -39,17 +39,12 @@ class BlockMessage extends FormatInterface {
39
39
  decode(encoded) {
40
40
  encoded = encoded || this.encoded;
41
41
  super.decode(encoded);
42
- this.decoded.validators = decodeFrames(this.decoded.validators, 0, { canonical: false }).map((validator) => new ValidatorMessage(validator).decoded);
42
+ this.decoded.validators = decodeFrames(this.decoded.validators).map((validator) => new ValidatorMessage(validator).decoded);
43
43
  return this.decoded;
44
44
  }
45
45
  }
46
46
 
47
- var proto$1 = {
48
- up: Number(),
49
- down: Number()
50
- };
51
-
52
- class BWMessage extends FormatInterface {
47
+ class BWMessage extends BinaryFormat {
53
48
  get messageName() {
54
49
  return "BWMessage";
55
50
  }
@@ -57,11 +52,11 @@ class BWMessage extends FormatInterface {
57
52
  if (buffer instanceof BWMessage)
58
53
  return buffer;
59
54
  const name = "bw-message";
60
- super(buffer, proto$1, { name });
55
+ super(buffer, proto$2, { name });
61
56
  }
62
57
  }
63
58
 
64
- class LastBlockMessage extends FormatInterface {
59
+ class LastBlockMessage extends BinaryFormat {
65
60
  get messageName() {
66
61
  return "LastBlockMessage";
67
62
  }
@@ -69,16 +64,11 @@ class LastBlockMessage extends FormatInterface {
69
64
  if (buffer instanceof LastBlockMessage)
70
65
  return buffer;
71
66
  const name = "last-block-message";
72
- super(buffer, proto$4, { name });
67
+ super(buffer, proto$3, { name });
73
68
  }
74
69
  }
75
70
 
76
- var proto = {
77
- lastblock: Object(),
78
- values: Object()
79
- };
80
-
81
- class StateMessage extends FormatInterface {
71
+ class StateMessage extends BinaryFormat {
82
72
  get messageName() {
83
73
  return "StateMessage";
84
74
  }
@@ -86,7 +76,30 @@ class StateMessage extends FormatInterface {
86
76
  if (buffer instanceof StateMessage)
87
77
  return buffer;
88
78
  const name = "state-message";
89
- super(buffer, proto, { name });
79
+ super(buffer, proto$4, { name });
80
+ }
81
+ }
82
+
83
+ const limit = 1800;
84
+ const transactionLimit = 2500;
85
+ const requestTimeout = 3e4;
86
+ const syncTimeout = 3e4;
87
+ class Protocol {
88
+ constructor(config) {
89
+ this.resolveTimeout = 1e4;
90
+ if (config?.resolveTimeout) this.resolveTimeout = config.resolveTimeout;
91
+ }
92
+ get limit() {
93
+ return limit;
94
+ }
95
+ get transactionLimit() {
96
+ return transactionLimit;
97
+ }
98
+ get requestTimeout() {
99
+ return requestTimeout;
100
+ }
101
+ get syncTimeout() {
102
+ return syncTimeout;
90
103
  }
91
104
  }
92
105
 
@@ -118,7 +131,7 @@ var networks$1 = /*#__PURE__*/Object.freeze({
118
131
  default: networks
119
132
  });
120
133
 
121
- const PROTOCOL_VERSION = "0.4.0";
134
+ const PROTOCOL_VERSION = "0.5.0";
122
135
  const REACHED_ONE_ZERO_ZERO = false;
123
136
  const DEFAULT_NODE_OPTIONS = {
124
137
  autoStart: false,
@@ -130,6 +143,122 @@ const DEFAULT_NODE_OPTIONS = {
130
143
 
131
144
  const INITIAL_RECONNECT_DELAY = 5e3;
132
145
  const MAX_RECONNECT_DELAY = 3e4;
146
+ const isChainStore = (store) => store === "block" || store === "transaction" || store === "transactionPool";
147
+ const guardProviderDiscovery = (peernet) => {
148
+ const handleData = peernet.handleData.bind(peernet);
149
+ peernet.handleData = async (peer, id, proto) => {
150
+ const { hash, store: storeName } = proto.decoded;
151
+ if (!isChainStore(storeName)) return handleData(peer, id, proto);
152
+ let data = new Uint8Array();
153
+ try {
154
+ const store = globalThis[`${storeName}Store`];
155
+ if (store && !("private" in store && store.private) && await store.has(hash)) {
156
+ const bytes = await store.get(hash);
157
+ if (!(bytes instanceof Uint8Array) || bytes.length === 0) {
158
+ throw new Error("local content is unavailable");
159
+ }
160
+ data = bytes;
161
+ } else {
162
+ console.warn(`Requested content unavailable in ${storeName}: ${hash}`);
163
+ }
164
+ } catch (error) {
165
+ console.error(`Local chain data read failed in ${storeName}: ${hash}`, error);
166
+ }
167
+ try {
168
+ const response = new peernet.protos["peernet-data-response"]({ hash, data });
169
+ const message = await peernet.prepareMessage(response);
170
+ await peernet.sendMessage(peer, id, message.encoded);
171
+ } catch (error) {
172
+ console.error(`Incoming chain data response failed in ${storeName}: ${hash}`, error);
173
+ }
174
+ };
175
+ peernet.walk = async (hash) => {
176
+ if (!hash) throw new Error("hash expected for provider discovery");
177
+ const request = new peernet.protos["peernet-dht"]({ hash });
178
+ const message = await peernet.prepareMessage(request);
179
+ const connections = peernet.connections;
180
+ const requests = Object.entries(connections).filter(
181
+ ([id, peer]) => id !== peernet.id && peer.connected
182
+ ).map(async ([id, peer]) => {
183
+ let timeout;
184
+ try {
185
+ const result = await Promise.race([
186
+ peer.request(message.encoded),
187
+ new Promise((_, reject) => {
188
+ timeout = setTimeout(() => reject(new Error("provider discovery timed out")), requestTimeout);
189
+ })
190
+ ]);
191
+ if (!(result instanceof Uint8Array)) throw new TypeError("Peer response must be Uint8Array");
192
+ const bytes = result;
193
+ if (new Codec(bytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
194
+ const response = new peernet.protos["peernet-dht-response"](bytes).decoded;
195
+ if (response.hash !== hash) throw new Error("provider response hash mismatch");
196
+ if (!response.has) throw new Error("peer does not provide content");
197
+ peernet.dht.addProvider({ id, address: peer.remoteAddress }, hash);
198
+ } catch (error) {
199
+ if (error.message !== "peer does not provide content") {
200
+ console.warn(`Provider discovery failed for ${id}:`, error?.message ?? error);
201
+ }
202
+ throw error;
203
+ } finally {
204
+ clearTimeout(timeout);
205
+ }
206
+ });
207
+ if (requests.length === 0) return;
208
+ try {
209
+ await Promise.any(requests);
210
+ } catch (error) {
211
+ if (!(error instanceof AggregateError)) throw error;
212
+ console.warn(`No connected provider found for ${hash}`);
213
+ }
214
+ };
215
+ const requestData = peernet.requestData.bind(peernet);
216
+ peernet.requestData = async (hash, store, options) => {
217
+ const storeName = typeof store === "string" ? store : store?.name;
218
+ if (!isChainStore(storeName)) {
219
+ return requestData(hash, store, options);
220
+ }
221
+ const probe = await peernet.prepareMessage(new peernet.protos["peernet-dht"]({ hash, store: storeName }));
222
+ const request = await peernet.prepareMessage(new peernet.protos["peernet-data"]({ hash, store: storeName }));
223
+ const connections = peernet.connections;
224
+ const requests = Object.entries(connections).filter(
225
+ ([id, peer]) => id !== peernet.id && peer.connected
226
+ ).map(async ([id, peer]) => {
227
+ let timeout;
228
+ try {
229
+ const fetch = async () => {
230
+ const availability = await peer.request(probe.encoded);
231
+ if (!(availability instanceof Uint8Array)) throw new TypeError("Peer response must be Uint8Array");
232
+ const probeBytes = availability;
233
+ if (new Codec(probeBytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
234
+ const provider = new peernet.protos["peernet-dht-response"](probeBytes).decoded;
235
+ if (provider.hash !== hash || !provider.has) throw new Error("peer does not provide content in requested store");
236
+ const result = await peer.request(request.encoded);
237
+ const bytes = result instanceof Uint8Array ? result : new Uint8Array(Object.values(result));
238
+ if (new Codec(bytes).name !== "peernet-data-response") throw new Error("invalid data response codec");
239
+ const response = new peernet.protos["peernet-data-response"](bytes).decoded;
240
+ if (response.hash !== hash || !(response.data instanceof Uint8Array) || response.data.length === 0) {
241
+ throw new Error("invalid content response");
242
+ }
243
+ return response.data;
244
+ };
245
+ return await Promise.race([
246
+ fetch(),
247
+ new Promise((_, reject) => {
248
+ timeout = setTimeout(() => reject(new Error(`content request timed out for ${id}`)), requestTimeout);
249
+ })
250
+ ]);
251
+ } finally {
252
+ clearTimeout(timeout);
253
+ }
254
+ });
255
+ try {
256
+ return await Promise.any(requests);
257
+ } catch (error) {
258
+ throw new Error(`Content unavailable in ${storeName}: ${hash}`, { cause: error });
259
+ }
260
+ };
261
+ };
133
262
  const isSendablePeer = (peer) => {
134
263
  if (!peer || typeof peer !== "object") return false;
135
264
  const candidate = peer;
@@ -451,4 +580,4 @@ class ConnectionMonitor {
451
580
  }
452
581
  }
453
582
 
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 };
583
+ 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';
2
- import './browser-crypto-CH0ua22L.js';
3
- import './connection-monitor-D-krNiRH.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-BmyPBkL5.js';
2
+ import './browser-crypto-B_UJguhk.js';
3
+ import './connection-monitor-BzbeYGEt.js';
4
4
  import './index-CvKt4UDE.js';
5
5
  import './sha2-BKcEJzgd.js';
6
6