@leofcoin/chain 1.10.22 → 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 (28) hide show
  1. package/README.md +74 -0
  2. package/exports/browser/beacon-wire.js +2 -2
  3. package/exports/browser/{browser-CCfR9KOM-UO9mzy-j.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 +1234 -885
  6. package/exports/browser/{client-DbZBfpG6-CYDyUGUC.js → client-DbZBfpG6-C1cXgvfr.js} +4 -4
  7. package/exports/browser/{connection-monitor-DMGAhcC0.js → connection-monitor-BzbeYGEt.js} +46 -26
  8. package/exports/browser/{index-dTQVgkpf-C6SJ0ZYW.js → index-dTQVgkpf-CkoAiRNX.js} +3 -3
  9. package/exports/browser/{index-dxD17Fom.js → index-phv5KvaE.js} +7 -7
  10. package/exports/browser/{messages-DZYCxoZI-D6kIiLKF.js → messages-DZYCxoZI-B472zldo.js} +3 -3
  11. package/exports/browser/{node-browser-BMfelxXV.js → node-browser-BmyPBkL5.js} +36 -8
  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 +248 -960
  17. package/exports/{connection-monitor-h2L8vsS7.js → connection-monitor-BdFLRh-J.js} +33 -3
  18. package/exports/connection-monitor.js +1 -1
  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 +33 -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 +7 -5
@@ -1,6 +1,6 @@
1
- import { c as createDebugger, L as LittlePubSub } from './node-browser-BMfelxXV.js';
2
- import './browser-crypto-CH0ua22L.js';
3
- import './connection-monitor-DMGAhcC0.js';
1
+ import { c as createDebugger, L as LittlePubSub } 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
 
@@ -233,7 +233,7 @@ class SocketRequestClient {
233
233
  const init = async () => {
234
234
  // @ts-ignore
235
235
  if (!globalThis.WebSocket && !this.#experimentalWebsocket)
236
- globalThis.WebSocket = (await import('./browser-CCfR9KOM-UO9mzy-j.js').then(function (n) { return n.b; })).default.w3cwebsocket;
236
+ globalThis.WebSocket = (await import('./browser-CCfR9KOM-CNWKLusu.js').then(function (n) { return n.b; })).default.w3cwebsocket;
237
237
  const client = new WebSocket(this.#url, this.#protocol);
238
238
  if (this.#experimentalWebsocket) {
239
239
  client.addEventListener('error', this.onerror);
@@ -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, d as Codec } 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,7 @@ 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 });
90
80
  }
91
81
  }
92
82
 
@@ -141,7 +131,7 @@ var networks$1 = /*#__PURE__*/Object.freeze({
141
131
  default: networks
142
132
  });
143
133
 
144
- const PROTOCOL_VERSION = "0.4.0";
134
+ const PROTOCOL_VERSION = "0.5.0";
145
135
  const REACHED_ONE_ZERO_ZERO = false;
146
136
  const DEFAULT_NODE_OPTIONS = {
147
137
  autoStart: false,
@@ -153,7 +143,35 @@ const DEFAULT_NODE_OPTIONS = {
153
143
 
154
144
  const INITIAL_RECONNECT_DELAY = 5e3;
155
145
  const MAX_RECONNECT_DELAY = 3e4;
146
+ const isChainStore = (store) => store === "block" || store === "transaction" || store === "transactionPool";
156
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
+ };
157
175
  peernet.walk = async (hash) => {
158
176
  if (!hash) throw new Error("hash expected for provider discovery");
159
177
  const request = new peernet.protos["peernet-dht"]({ hash });
@@ -170,7 +188,8 @@ const guardProviderDiscovery = (peernet) => {
170
188
  timeout = setTimeout(() => reject(new Error("provider discovery timed out")), requestTimeout);
171
189
  })
172
190
  ]);
173
- const bytes = result instanceof Uint8Array ? result : new Uint8Array(Object.values(result));
191
+ if (!(result instanceof Uint8Array)) throw new TypeError("Peer response must be Uint8Array");
192
+ const bytes = result;
174
193
  if (new Codec(bytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
175
194
  const response = new peernet.protos["peernet-dht-response"](bytes).decoded;
176
195
  if (response.hash !== hash) throw new Error("provider response hash mismatch");
@@ -196,7 +215,7 @@ const guardProviderDiscovery = (peernet) => {
196
215
  const requestData = peernet.requestData.bind(peernet);
197
216
  peernet.requestData = async (hash, store, options) => {
198
217
  const storeName = typeof store === "string" ? store : store?.name;
199
- if (!["block", "transaction", "transactionPool"].includes(storeName)) {
218
+ if (!isChainStore(storeName)) {
200
219
  return requestData(hash, store, options);
201
220
  }
202
221
  const probe = await peernet.prepareMessage(new peernet.protos["peernet-dht"]({ hash, store: storeName }));
@@ -209,7 +228,8 @@ const guardProviderDiscovery = (peernet) => {
209
228
  try {
210
229
  const fetch = async () => {
211
230
  const availability = await peer.request(probe.encoded);
212
- const probeBytes = availability instanceof Uint8Array ? availability : new Uint8Array(Object.values(availability));
231
+ if (!(availability instanceof Uint8Array)) throw new TypeError("Peer response must be Uint8Array");
232
+ const probeBytes = availability;
213
233
  if (new Codec(probeBytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
214
234
  const provider = new peernet.protos["peernet-dht-response"](probeBytes).decoded;
215
235
  if (provider.hash !== hash || !provider.has) throw new Error("peer does not provide content in requested store");
@@ -1,6 +1,6 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-BMfelxXV.js';
2
- import './browser-crypto-CH0ua22L.js';
3
- import './connection-monitor-DMGAhcC0.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