@leofcoin/chain 1.5.66 → 1.5.68

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 (24) hide show
  1. package/exports/browser/{_polyfill-node.child_process-rc1HO9Xs.js → _polyfill-node.child_process-CblghArn.js} +48 -47
  2. package/exports/browser/{_polyfill-node.url-1q06MepH.js → _polyfill-node.url-BLK_MhDf.js} +1 -1
  3. package/exports/browser/{browser-Ei0BXMlu-irDJ6Blw.js → browser-Ei0BXMlu-C0woTcHa.js} +2 -2
  4. package/exports/browser/browser-store.js +96 -15
  5. package/exports/browser/chain.js +118 -32
  6. package/exports/browser/{client-A009z8av-uBa1w1TW.js → client-A009z8av-Ssch8Yea.js} +5 -5
  7. package/exports/browser/{index-G74WLzL7-tmiSHjUc.js → index-G74WLzL7-BnLRqHoy.js} +2 -2
  8. package/exports/browser/{index-YQrIDBUQ-OXiMI4eT.js → index-YQrIDBUQ-APwDgUUG.js} +2 -2
  9. package/exports/browser/{messages-lWRTai7t-Bc6ghkkt.js → messages-lWRTai7t-DJnHZSdM.js} +2 -2
  10. package/exports/browser/{node-browser-TcXF8FDB.js → node-browser-CeM_F-HL.js} +25 -6
  11. package/exports/browser/node-browser.js +2 -2
  12. package/exports/browser/workers/block-worker.js +1 -1
  13. package/exports/browser/workers/machine-worker.js +218 -91
  14. package/exports/browser/workers/{worker-hbo9s9AT.js → worker-CbAak_hM.js} +47 -46
  15. package/exports/chain.js +83 -14
  16. package/exports/machine.d.ts +8 -0
  17. package/exports/simplifiers/state.d.ts +3 -0
  18. package/exports/workers/block-worker.js +1 -1
  19. package/exports/workers/machine-worker.js +218 -91
  20. package/exports/workers/{worker-hbo9s9AT.js → worker-CbAak_hM.js} +47 -46
  21. package/package.json +110 -109
  22. /package/exports/browser/{browser-AyxSBUXj-AyxSBUXj.js → browser-AyxSBUXj-pguCHlVu.js} +0 -0
  23. /package/exports/browser/{password-JCRBtU5A.js → password-oDixGC8h.js} +0 -0
  24. /package/exports/browser/{qr-scanner-worker.min-RaSiJc_R-RaSiJc_R.js → qr-scanner-worker.min-RaSiJc_R-Dy0qkKA4.js} +0 -0
@@ -14499,55 +14499,56 @@ class ValidatorMessage extends FormatInterface {
14499
14499
  }
14500
14500
 
14501
14501
  var proto$4 = {
14502
- index: Number(),
14503
- previousHash: String(),
14504
- timestamp: Number(),
14505
- reward: BigNumber.from(0),
14506
- fees: BigNumber.from(0),
14507
- transactions: new Uint8Array(),
14508
- validators: new Uint8Array()
14502
+ index: Number(),
14503
+ previousHash: String(),
14504
+ timestamp: Number(),
14505
+ reward: BigNumber.from(0),
14506
+ fees: BigNumber.from(0),
14507
+ transactions: new Uint8Array(),
14508
+ validators: new Uint8Array()
14509
14509
  };
14510
14510
 
14511
14511
  class BlockMessage extends FormatInterface {
14512
- get messageName() {
14513
- return 'BlockMessage'
14514
- }
14515
- constructor(buffer) {
14516
- if (buffer instanceof BlockMessage) return buffer
14517
- const name = 'block-message';
14518
- super(buffer, proto$4, { name });
14519
- }
14520
- encode(decoded) {
14521
- decoded = decoded || this.decoded;
14522
- const validators = [];
14523
- const transactions = [];
14524
- for (const validator of decoded.validators) {
14525
- if (validator instanceof ValidatorMessage) validators.push(validator.encode());
14526
- else validators.push(new ValidatorMessage(validator).encode());
14527
- }
14528
- for (const transaction of decoded.transactions) {
14529
- if (transaction instanceof TransactionMessage) transactions.push(transaction.encode());
14530
- else transactions.push(new TransactionMessage(transaction).encode());
14531
- }
14532
- return super.encode({
14533
- ...decoded,
14534
- validators: index$5(validators),
14535
- transactions: index$5(transactions)
14536
- })
14537
- }
14538
- decode(encoded) {
14539
- encoded = encoded || this.encoded;
14540
- super.decode(encoded);
14541
- // @ts-ignore
14542
- this.decoded.transactions = index$4(this.decoded.transactions).map(
14543
- (transaction) => new TransactionMessage(transaction).decoded
14544
- );
14545
- // @ts-ignore
14546
- this.decoded.validators = index$4(this.decoded.validators).map(
14547
- (validator) => new ValidatorMessage(validator).decoded
14548
- );
14549
- return this.decoded
14550
- }
14512
+ get messageName() {
14513
+ return 'BlockMessage';
14514
+ }
14515
+ constructor(buffer) {
14516
+ if (buffer instanceof BlockMessage)
14517
+ return buffer;
14518
+ const name = 'block-message';
14519
+ super(buffer, proto$4, { name });
14520
+ }
14521
+ encode(decoded) {
14522
+ decoded = decoded || this.decoded;
14523
+ const validators = [];
14524
+ const transactions = [];
14525
+ for (const validator of decoded.validators) {
14526
+ if (validator instanceof ValidatorMessage)
14527
+ validators.push(validator.encode());
14528
+ else
14529
+ validators.push(new ValidatorMessage(validator).encode());
14530
+ }
14531
+ for (const transaction of decoded.transactions) {
14532
+ if (transaction instanceof TransactionMessage)
14533
+ transactions.push(transaction.encode());
14534
+ else
14535
+ transactions.push(new TransactionMessage(transaction).encode());
14536
+ }
14537
+ return super.encode({
14538
+ ...decoded,
14539
+ validators: index$5(validators),
14540
+ transactions: index$5(transactions)
14541
+ });
14542
+ }
14543
+ decode(encoded) {
14544
+ encoded = encoded || this.encoded;
14545
+ super.decode(encoded);
14546
+ // @ts-ignore
14547
+ this.decoded.transactions = index$4(this.decoded.transactions).map((transaction) => new TransactionMessage(transaction).decoded);
14548
+ // @ts-ignore
14549
+ this.decoded.validators = index$4(this.decoded.validators).map((validator) => new ValidatorMessage(validator).decoded);
14550
+ return this.decoded;
14551
+ }
14551
14552
  }
14552
14553
 
14553
14554
  var proto$3 = {
@@ -14623,4 +14624,4 @@ var _polyfillNode_child_process = /*#__PURE__*/Object.freeze({
14623
14624
  __proto__: null
14624
14625
  });
14625
14626
 
14626
- export { BlockMessage as B, ContractMessage as C, Logger as L, RawTransactionMessage as R, TransactionMessage as T, ValidatorMessage as V, _polyfillNode_child_process as _, BWMessage as a, BWRequestMessage as b, BigNumber as c, arrayify as d, isBytes as e, getDefaultExportFromCjs as f, global$1 as g, hexZeroPad as h, isBigNumberish as i, toBase58 as t, version as v };
14627
+ export { BlockMessage as B, ContractMessage as C, FormatInterface as F, Logger as L, RawTransactionMessage as R, TransactionMessage as T, ValidatorMessage as V, _polyfillNode_child_process as _, BWMessage as a, BWRequestMessage as b, BigNumber as c, arrayify as d, isBytes as e, getDefaultExportFromCjs as f, global$1 as g, hexZeroPad as h, isBigNumberish as i, toBase58 as t, version as v };
@@ -1,4 +1,4 @@
1
- import { g as global } from './_polyfill-node.child_process-rc1HO9Xs.js';
1
+ import { g as global } from './_polyfill-node.child_process-CblghArn.js';
2
2
 
3
3
  /*! https://mths.be/punycode v1.4.1 by @mathias */
4
4
 
@@ -1,5 +1,5 @@
1
- import { g as getDefaultExportFromCjs } from './node-browser-TcXF8FDB.js';
2
- import './_polyfill-node.child_process-rc1HO9Xs.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-CeM_F-HL.js';
2
+ import './_polyfill-node.child_process-CblghArn.js';
3
3
 
4
4
  var global;
5
5
  var hasRequiredGlobal;
@@ -70,23 +70,56 @@ class KeyValue {
70
70
  }
71
71
  }
72
72
 
73
+ if (!globalThis.DEBUG) {
74
+ let DEBUG = [];
75
+ if (globalThis.localStorage) {
76
+ DEBUG = globalThis.localStorage.getItem('DEBUG');
77
+ globalThis.DEBUG = DEBUG ? DEBUG.split(',') : [DEBUG];
78
+ }
79
+ }
80
+
81
+ const debug$1 = (target, text) => {
82
+ if (!globalThis.DEBUG && globalThis.DEBUG.length === 0) return;
83
+ if (
84
+ globalThis.DEBUG === 'true' ||
85
+ globalThis.DEBUG === true ||
86
+ globalThis.DEBUG?.indexOf(target) !== -1 ||
87
+ globalThis.DEBUG?.indexOf('*') !== -1 ||
88
+ globalThis.DEBUG?.indexOf(target.split('/')[0]) !== -1
89
+ )
90
+ if (text) console.log('\x1b[34m\x1b[1m%s', `${target}: ${text}`, '\x1b[0m');
91
+ else console.log('\x1b[34m\x1b[1m%s', `${target}`, '\x1b[0m');
92
+ };
93
+
94
+ if (!globalThis.debug) {
95
+ globalThis.debug = debug$1;
96
+
97
+ globalThis.createDebugger = (target) => (text) => debug$1(target, text);
98
+ }
99
+
100
+ const debug = globalThis.createDebugger('leofcoin/storage');
73
101
  const opfsRoot = await navigator.storage.getDirectory();
74
102
  class BrowerStore {
75
103
  db;
76
104
  name;
77
105
  root;
106
+ inWorker;
78
107
  version;
79
- async init(name = 'storage', root = '.leofcoin', version = '1') {
80
- console.log('init');
108
+ busy;
109
+ queue = [];
110
+ async init(name = 'storage', root = '.leofcoin', version = '1', inWorker = false) {
81
111
  this.version = version;
82
112
  this.name = name;
83
113
  this.root = opfsRoot;
84
- console.log(`${this.root}/${this.name}`);
114
+ this.inWorker = inWorker;
85
115
  let directoryHandle;
86
116
  try {
87
117
  directoryHandle = await opfsRoot.getDirectoryHandle(this.name, {
88
118
  create: true
89
119
  });
120
+ if (inWorker) {
121
+ directoryHandle = await directoryHandle.createSyncAccessHandle();
122
+ }
90
123
  }
91
124
  catch (error) {
92
125
  console.error(error);
@@ -105,6 +138,7 @@ class BrowerStore {
105
138
  return value.uint8Array;
106
139
  }
107
140
  async has(key) {
141
+ debug(`has ${this.toKeyPath(key)}`);
108
142
  try {
109
143
  await this.db.getFileHandle(this.toKeyPath(key));
110
144
  return true;
@@ -114,34 +148,81 @@ class BrowerStore {
114
148
  }
115
149
  }
116
150
  async get(key) {
117
- const handle = await this.db.getFileHandle(this.toKeyPath(key));
118
- const file = await handle.getFile();
119
- return new Uint8Array(await file.arrayBuffer());
151
+ debug(`get ${this.toKeyPath(key)}`);
152
+ let handle = await this.db.getFileHandle(this.toKeyPath(key));
153
+ let readBuffer;
154
+ if (this.inWorker) {
155
+ handle = await handle.createSyncAccessHandle();
156
+ const fileSize = handle.getSize();
157
+ // Read file content to a buffer.
158
+ const buffer = new DataView(new ArrayBuffer(fileSize));
159
+ readBuffer = handle.read(buffer, { at: 0 });
160
+ handle.close();
161
+ }
162
+ else {
163
+ const file = await handle.getFile();
164
+ readBuffer = await file.arrayBuffer();
165
+ }
166
+ return new Uint8Array(readBuffer);
120
167
  }
121
168
  async put(key, value) {
122
- const handle = await this.db.getFileHandle(this.toKeyPath(key), { create: true });
123
- const writeable = handle.createWritable();
124
- (await writeable).write(this.toKeyValue(value));
125
- (await writeable).close();
169
+ const promise = () => new Promise(async (resolve) => {
170
+ debug(`put ${this.toKeyPath(key)}`);
171
+ let handle = await this.db.getFileHandle(this.toKeyPath(key), { create: true });
172
+ let writeable;
173
+ if (this.inWorker) {
174
+ writeable = await handle.createSyncAccessHandle();
175
+ }
176
+ else {
177
+ writeable = await handle.createWritable();
178
+ }
179
+ (await writeable).write(this.toKeyValue(value));
180
+ (await writeable).close();
181
+ resolve(true);
182
+ if (this.queue.length > 0 && !this.busy) {
183
+ this.runQueue();
184
+ }
185
+ });
186
+ this.queue.push(promise);
187
+ this.runQueue();
188
+ return promise;
189
+ }
190
+ async runQueue() {
191
+ if (this.queue.length > 0) {
192
+ this.busy = true;
193
+ const next = this.queue.shift();
194
+ await next();
195
+ return this.runQueue();
196
+ }
197
+ else {
198
+ this.busy = false;
199
+ }
126
200
  }
127
201
  async delete(key) {
202
+ debug(`delete ${this.toKeyPath(key)}`);
128
203
  return this.db.removeEntry(this.toKeyPath(key));
129
204
  }
130
205
  async clear() {
206
+ debug(`clear ${this.toKeyPath(key)}`);
131
207
  for await (const key of this.db.keys()) {
132
208
  await this.db.removeEntry(key);
133
209
  }
134
210
  }
135
211
  async values(limit = -1) {
136
- const values = [];
212
+ debug(`values ${limit}`);
213
+ let values = [];
137
214
  for await (const cursor of this.db.values()) {
138
- values.push(cursor);
139
- if (limit && values.length === limit)
140
- return values;
215
+ values.push(cursor.getFile());
216
+ if (limit && values.length === limit) {
217
+ values = await Promise.all(values);
218
+ return Promise.all(values.map(async (file) => new Uint8Array(await file.arrayBuffer())));
219
+ }
141
220
  }
142
- return values;
221
+ values = await Promise.all(values);
222
+ return Promise.all(values.map(async (file) => new Uint8Array(await file.arrayBuffer())));
143
223
  }
144
224
  async keys(limit = -1) {
225
+ debug(`keys ${limit}`);
145
226
  const keys = [];
146
227
  for await (const cursor of this.db.keys()) {
147
228
  keys.push(cursor);