@leofcoin/chain 1.6.12 → 1.6.13

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,5 +1,5 @@
1
- import { g as getDefaultExportFromCjs } from './node-browser-CC3grAN3.js';
2
- import './index-Bz6K16zr.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-DRoVQRWp.js';
2
+ import './index--C9N9ZXK.js';
3
3
 
4
4
  var global;
5
5
  var hasRequiredGlobal;
@@ -1,4 +1,4 @@
1
- import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, T as TransactionMessage, t as toBase58, C as ContractMessage, R as RawTransactionMessage, g as getDefaultExportFromCjs, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index-Bz6K16zr.js';
1
+ import { B as BigNumber, L as Logger, v as version$1, h as hexZeroPad, i as isBigNumberish, a as arrayify, b as isBytes, T as TransactionMessage, t as toBase58, C as ContractMessage, R as RawTransactionMessage, g as getDefaultExportFromCjs, c as BlockMessage, d as BWMessage, e as BWRequestMessage } from './index--C9N9ZXK.js';
2
2
 
3
3
  if (!globalThis.DEBUG) {
4
4
  let DEBUG = [];
@@ -3979,8 +3979,8 @@ class Machine {
3979
3979
  break;
3980
3980
  }
3981
3981
  case 'ask': {
3982
- if (data.question === 'contract') {
3983
- const input = await peernet.get(data.input, 'contract');
3982
+ if (data.question === 'contract' || data.question === 'transaction') {
3983
+ const input = await peernet.get(data.input, data.question);
3984
3984
  this.worker.postMessage({ id: data.id, input });
3985
3985
  }
3986
3986
  }
@@ -5123,27 +5123,25 @@ class Chain extends VersionControl {
5123
5123
  async #addBlock(block) {
5124
5124
  const blockMessage = await new BlockMessage(block);
5125
5125
  const hash = await blockMessage.hash();
5126
- await Promise.all(blockMessage.decoded.transactions
5126
+ const transactionsMessages = await Promise.all(blockMessage.decoded.transactions
5127
5127
  // @ts-ignore
5128
- .map(async (transaction) => {
5129
- // @ts-ignore
5130
- let hash = transaction.hash;
5131
- if (!hash) {
5132
- hash = await new TransactionMessage(transaction).hash();
5133
- // @ts-ignore
5134
- transaction.hash = hash;
5135
- }
5128
+ .map(async (hash) => {
5129
+ let data;
5136
5130
  if (!(await transactionStore.has(hash))) {
5137
- transactionStore.put(hash, await blockMessage.hash());
5131
+ data = await peernet.get(hash, 'transaction');
5132
+ transactionStore.put(hash, data);
5133
+ }
5134
+ else {
5135
+ data = transactionStore.get(hash);
5138
5136
  }
5139
5137
  (await transactionPoolStore.has(hash)) && (await transactionPoolStore.delete(hash));
5140
- return transaction;
5138
+ return new TransactionMessage(data).decode();
5141
5139
  }));
5142
5140
  await globalThis.blockStore.put(hash, blockMessage.encoded);
5143
5141
  debug(`added block: ${hash}`);
5144
5142
  let promises = [];
5145
5143
  let contracts = [];
5146
- for (let transaction of blockMessage.decoded.transactions) {
5144
+ for (let transaction of transactionsMessages) {
5147
5145
  // await transactionStore.put(transaction.hash, transaction.encoded)
5148
5146
  if (!contracts.includes(transaction.to)) {
5149
5147
  contracts.push(transaction.to);
@@ -5155,7 +5153,7 @@ class Chain extends VersionControl {
5155
5153
  try {
5156
5154
  promises = await Promise.allSettled(promises);
5157
5155
  const noncesByAddress = {};
5158
- for (let transaction of blockMessage.decoded.transactions) {
5156
+ for (let transaction of transactionsMessages) {
5159
5157
  globalThis.pubsub.publish('transaction-processed', transaction);
5160
5158
  if (transaction.to === globalThis.peernet.selectedAccount)
5161
5159
  globalThis.pubsub.publish('account-transaction-processed', transaction);
@@ -5202,7 +5200,7 @@ class Chain extends VersionControl {
5202
5200
  async #handleTransaction(transaction, latestTransactions, block) {
5203
5201
  const hash = await transaction.hash();
5204
5202
  const doubleTransactions = [];
5205
- if (latestTransactions.includes(hash)) {
5203
+ if (latestTransactions.includes(hash) || transactionStore.has(hash)) {
5206
5204
  doubleTransactions.push(hash);
5207
5205
  }
5208
5206
  if (doubleTransactions.length > 0) {
@@ -5213,7 +5211,7 @@ class Chain extends VersionControl {
5213
5211
  // if (timestamp + this.#slotTime > Date.now()) {
5214
5212
  try {
5215
5213
  const result = await this.#executeTransaction({ ...transaction.decoded, hash });
5216
- block.transactions.push(transaction);
5214
+ block.transactions.push(hash);
5217
5215
  block.fees = block.fees.add(await calculateFee(transaction.decoded));
5218
5216
  await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
5219
5217
  }
@@ -5318,10 +5316,7 @@ class Chain extends VersionControl {
5318
5316
  // block.reward = block.reward.toString()
5319
5317
  // block.fees = block.fees.toString()
5320
5318
  try {
5321
- block.transactions = await Promise.all(block.transactions.map(async (transaction) => {
5322
- await globalThis.transactionPoolStore.delete(await transaction.hash());
5323
- return transaction.decoded;
5324
- }));
5319
+ await Promise.all(block.transactions.map(async (transaction) => await globalThis.transactionPoolStore.delete(transaction)));
5325
5320
  let blockMessage = await new BlockMessage(block);
5326
5321
  const hash = await blockMessage.hash();
5327
5322
  await globalThis.peernet.put(hash, blockMessage.encoded, 'block');
@@ -1,5 +1,5 @@
1
- import { L as LittlePubSub } from './node-browser-CC3grAN3.js';
2
- import './index-Bz6K16zr.js';
1
+ import { L as LittlePubSub } from './node-browser-DRoVQRWp.js';
2
+ import './index--C9N9ZXK.js';
3
3
 
4
4
  class Api {
5
5
  _pubsub;
@@ -206,7 +206,7 @@ class SocketRequestClient {
206
206
  const init = async () => {
207
207
  // @ts-ignore
208
208
  if (!globalThis.WebSocket)
209
- globalThis.WebSocket = (await import('./browser-Ei0BXMlu-9eQR3AGp.js').then(function (n) { return n.b; })).default.w3cwebsocket;
209
+ globalThis.WebSocket = (await import('./browser-Ei0BXMlu-_DPyExz0.js').then(function (n) { return n.b; })).default.w3cwebsocket;
210
210
  const client = new WebSocket(this.#url, this.#protocol);
211
211
  client.onmessage = this.onmessage;
212
212
  client.onerror = this.onerror;
@@ -280,7 +280,7 @@ const iceServers = [
280
280
  credential: 'openrelayproject'
281
281
  }
282
282
  ];
283
- const SimplePeer = (await import('./index-YQrIDBUQ-CbL47K_x.js').then(function (n) { return n.i; })).default;
283
+ const SimplePeer = (await import('./index-YQrIDBUQ-CqYDmtzG.js').then(function (n) { return n.i; })).default;
284
284
  class Peer extends SimplePeer {
285
285
  peerId;
286
286
  channelName;
@@ -12458,37 +12458,6 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
12458
12458
  const FormatInterface = FormatInterface$1;
12459
12459
 
12460
12460
  var proto$6 = {
12461
- timestamp: Number(),
12462
- from: String(),
12463
- to: String(),
12464
- method: String(),
12465
- params: Array(),
12466
- signature: String(),
12467
- 'nonce?': Number(),
12468
- 'dependsOn?': Array(),
12469
- 'priority?': Boolean()
12470
- };
12471
-
12472
- class TransactionMessage extends FormatInterface {
12473
- get messageName() {
12474
- return 'TransactionMessage';
12475
- }
12476
- constructor(buffer) {
12477
- if (buffer instanceof TransactionMessage)
12478
- return buffer;
12479
- const name = 'transaction-message';
12480
- super(buffer, proto$6, { name });
12481
- }
12482
- beforeHashing(decoded) {
12483
- decoded = super.beforeHashing(decoded);
12484
- delete decoded.signature;
12485
- delete decoded.priority;
12486
- delete decoded.dependsOn;
12487
- return decoded;
12488
- }
12489
- }
12490
-
12491
- var proto$5 = {
12492
12461
  address: String(),
12493
12462
  reward: BigNumber.from(0)
12494
12463
  };
@@ -12501,17 +12470,17 @@ class ValidatorMessage extends FormatInterface {
12501
12470
  if (buffer instanceof ValidatorMessage)
12502
12471
  return buffer;
12503
12472
  const name = 'validator-message';
12504
- super(buffer, proto$5, { name });
12473
+ super(buffer, proto$6, { name });
12505
12474
  }
12506
12475
  }
12507
12476
 
12508
- var proto$4 = {
12477
+ var proto$5 = {
12509
12478
  index: Number(),
12510
12479
  previousHash: String(),
12511
12480
  timestamp: Number(),
12512
12481
  reward: BigNumber.from(0),
12513
12482
  fees: BigNumber.from(0),
12514
- transactions: new Uint8Array(),
12483
+ transactions: Array(),
12515
12484
  validators: new Uint8Array()
12516
12485
  };
12517
12486
 
@@ -12523,42 +12492,32 @@ class BlockMessage extends FormatInterface {
12523
12492
  if (buffer instanceof BlockMessage)
12524
12493
  return buffer;
12525
12494
  const name = 'block-message';
12526
- super(buffer, proto$4, { name });
12495
+ super(buffer, proto$5, { name });
12527
12496
  }
12528
12497
  encode(decoded) {
12529
12498
  decoded = decoded || this.decoded;
12530
12499
  const validators = [];
12531
- const transactions = [];
12532
12500
  for (const validator of decoded.validators) {
12533
12501
  if (validator instanceof ValidatorMessage)
12534
12502
  validators.push(validator.encode());
12535
12503
  else
12536
12504
  validators.push(new ValidatorMessage(validator).encode());
12537
12505
  }
12538
- for (const transaction of decoded.transactions) {
12539
- if (transaction instanceof TransactionMessage)
12540
- transactions.push(transaction.encode());
12541
- else
12542
- transactions.push(new TransactionMessage(transaction).encode());
12543
- }
12544
12506
  return super.encode({
12545
12507
  ...decoded,
12546
- validators: index$5(validators),
12547
- transactions: index$5(transactions)
12508
+ validators: index$5(validators)
12548
12509
  });
12549
12510
  }
12550
12511
  decode(encoded) {
12551
12512
  encoded = encoded || this.encoded;
12552
12513
  super.decode(encoded);
12553
12514
  // @ts-ignore
12554
- this.decoded.transactions = index$4(this.decoded.transactions).map((transaction) => new TransactionMessage(transaction).decoded);
12555
- // @ts-ignore
12556
12515
  this.decoded.validators = index$4(this.decoded.validators).map((validator) => new ValidatorMessage(validator).decoded);
12557
12516
  return this.decoded;
12558
12517
  }
12559
12518
  }
12560
12519
 
12561
- var proto$3 = {
12520
+ var proto$4 = {
12562
12521
  up: Number(),
12563
12522
  down: Number()
12564
12523
  };
@@ -12571,11 +12530,11 @@ class BWMessage extends FormatInterface {
12571
12530
  if (buffer instanceof BWMessage)
12572
12531
  return buffer;
12573
12532
  const name = 'bw-message';
12574
- super(buffer, proto$3, { name });
12533
+ super(buffer, proto$4, { name });
12575
12534
  }
12576
12535
  }
12577
12536
 
12578
- var proto$2 = {};
12537
+ var proto$3 = {};
12579
12538
 
12580
12539
  class BWRequestMessage extends FormatInterface {
12581
12540
  get messageName() {
@@ -12585,11 +12544,11 @@ class BWRequestMessage extends FormatInterface {
12585
12544
  if (buffer instanceof BWRequestMessage)
12586
12545
  return buffer;
12587
12546
  const name = 'bw-request-message';
12588
- super(buffer, proto$2, { name });
12547
+ super(buffer, proto$3, { name });
12589
12548
  }
12590
12549
  }
12591
12550
 
12592
- var proto$1 = {
12551
+ var proto$2 = {
12593
12552
  creator: String(),
12594
12553
  contract: new Uint8Array(),
12595
12554
  constructorParameters: Array()
@@ -12602,7 +12561,38 @@ class ContractMessage extends FormatInterface {
12602
12561
  constructor(buffer) {
12603
12562
  if (buffer instanceof ContractMessage)
12604
12563
  return buffer;
12605
- super(buffer, proto$1, { name: 'contract-message' });
12564
+ super(buffer, proto$2, { name: 'contract-message' });
12565
+ }
12566
+ }
12567
+
12568
+ var proto$1 = {
12569
+ timestamp: Number(),
12570
+ from: String(),
12571
+ to: String(),
12572
+ method: String(),
12573
+ params: Array(),
12574
+ signature: String(),
12575
+ 'nonce?': Number(),
12576
+ 'dependsOn?': Array(),
12577
+ 'priority?': Boolean()
12578
+ };
12579
+
12580
+ class TransactionMessage extends FormatInterface {
12581
+ get messageName() {
12582
+ return 'TransactionMessage';
12583
+ }
12584
+ constructor(buffer) {
12585
+ if (buffer instanceof TransactionMessage)
12586
+ return buffer;
12587
+ const name = 'transaction-message';
12588
+ super(buffer, proto$1, { name });
12589
+ }
12590
+ beforeHashing(decoded) {
12591
+ decoded = super.beforeHashing(decoded);
12592
+ delete decoded.signature;
12593
+ delete decoded.priority;
12594
+ delete decoded.dependsOn;
12595
+ return decoded;
12606
12596
  }
12607
12597
  }
12608
12598
 
@@ -1,5 +1,5 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-CC3grAN3.js';
2
- import './index-Bz6K16zr.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-DRoVQRWp.js';
2
+ import './index--C9N9ZXK.js';
3
3
 
4
4
  /**
5
5
  * @params {String} network
@@ -1,5 +1,5 @@
1
- import { i as inherits_browserExports, c as commonjsGlobal, g as getDefaultExportFromCjs, r as require$$3 } from './node-browser-CC3grAN3.js';
2
- import './index-Bz6K16zr.js';
1
+ import { i as inherits_browserExports, c as commonjsGlobal, g as getDefaultExportFromCjs, r as require$$3 } from './node-browser-DRoVQRWp.js';
2
+ import './index--C9N9ZXK.js';
3
3
 
4
4
  var browser$2 = {exports: {}};
5
5
 
@@ -1,5 +1,5 @@
1
- import { F as FormatInterface } from './node-browser-CC3grAN3.js';
2
- import './index-Bz6K16zr.js';
1
+ import { F as FormatInterface } from './node-browser-DRoVQRWp.js';
2
+ import './index--C9N9ZXK.js';
3
3
 
4
4
  var proto$b = {
5
5
  data: new Uint8Array(),
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface$3, C as ContractMessage, T as TransactionMessage, c as BlockMessage, d as BWMessage, e as BWRequestMessage, V as ValidatorMessage } from './index-Bz6K16zr.js';
1
+ import { F as FormatInterface$3, C as ContractMessage, T as TransactionMessage, c as BlockMessage, d as BWMessage, e as BWRequestMessage, V as ValidatorMessage } from './index--C9N9ZXK.js';
2
2
 
3
3
  var proto = {
4
4
  lastblock: Object(),
@@ -27217,7 +27217,7 @@ class Identity {
27217
27217
  this.selectedAccount = new TextDecoder().decode(selected);
27218
27218
  }
27219
27219
  else {
27220
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-G74WLzL7-d1Vs_wqe.js');
27220
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-G74WLzL7-BZ6_sy39.js');
27221
27221
  const { identity, accounts } = await importee.default(password, this.network);
27222
27222
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
27223
27223
  await globalThis.walletStore.put('version', String(1));
@@ -27409,7 +27409,7 @@ class Peernet {
27409
27409
  this.root = options.root;
27410
27410
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
27411
27411
  // FolderMessageResponse
27412
- } = await import(/* webpackChunkName: "messages" */ './messages-lWRTai7t-Cs3eU5Cb.js');
27412
+ } = await import(/* webpackChunkName: "messages" */ './messages-lWRTai7t-3F-6Qszg.js');
27413
27413
  /**
27414
27414
  * proto Object containing protos
27415
27415
  * @type {Object}
@@ -27503,7 +27503,7 @@ class Peernet {
27503
27503
  if (this.#starting || this.#started)
27504
27504
  return;
27505
27505
  this.#starting = true;
27506
- const importee = await import('./client-A009z8av-DavUUkay.js');
27506
+ const importee = await import('./client-A009z8av-Dw6NYFQr.js');
27507
27507
  /**
27508
27508
  * @access public
27509
27509
  * @type {PeernetClient}
@@ -1,2 +1,2 @@
1
- export { N as default } from './node-browser-CC3grAN3.js';
2
- import './index-Bz6K16zr.js';
1
+ export { N as default } from './node-browser-DRoVQRWp.js';
2
+ import './index--C9N9ZXK.js';
@@ -1,4 +1,4 @@
1
- import { E as EasyWorker, B as BigNumber, a as BlockMessage } from './worker-CFrwP8cD.js';
1
+ import { E as EasyWorker, B as BigNumber, a as BlockMessage } from './worker-Cqj1ERFr.js';
2
2
 
3
3
  const worker = new EasyWorker();
4
4
 
@@ -1,4 +1,4 @@
1
- import { E as EasyWorker, B as BigNumber, C as ContractMessage, T as TransactionMessage } from './worker-CFrwP8cD.js';
1
+ import { E as EasyWorker, B as BigNumber, C as ContractMessage, T as TransactionMessage } from './worker-Cqj1ERFr.js';
2
2
 
3
3
  const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
4
4
  const formatBytes = (bytes, decimals = 2) => {
@@ -150,7 +150,9 @@ const get = ({ contract, method, params }) => {
150
150
  return result
151
151
  };
152
152
 
153
- const resolveContract = (address) => askFor('contract', address);
153
+ const resolveContract = (hash) => askFor('contract', hash);
154
+
155
+ const resolveTransaction = (hash) => askFor('transaction', hash);
154
156
 
155
157
  const respond = (id, value) => {
156
158
  worker.postMessage({
@@ -306,23 +308,23 @@ _.init = async (message) => {
306
308
  );
307
309
  console.log({ blocks: message.blocks });
308
310
  if (message.blocks?.length > 0) {
309
- let pre;
310
-
311
- try {
312
- const importee = await import('url');
313
- const url = importee.default;
314
- if (url) pre = url.fileURLToPath(new URL('.', import.meta.url));
315
- } catch {
316
- // browser env
317
- pre = './';
318
- }
311
+ // let pre
312
+
313
+ // try {
314
+ // const importee = await import('url')
315
+ // const url = importee.default
316
+ // if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
317
+ // } catch {
318
+ // // browser env
319
+ // pre = './'
320
+ // }
319
321
 
320
- let _worker = await new EasyWorker(pre + './block-worker.js', {
321
- serialization: 'advanced',
322
- type: 'module'
323
- });
324
- blocks = await _worker.once(message.blocks);
325
- _worker = null;
322
+ // let _worker = await new EasyWorker(pre + './block-worker.js', {
323
+ // serialization: 'advanced',
324
+ // type: 'module'
325
+ // })
326
+ // blocks = await _worker.once(message.blocks)
327
+ // _worker = null
326
328
  // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
327
329
  // for (let i = 0; i < blocks.length; i++) {
328
330
 
@@ -333,20 +335,25 @@ _.init = async (message) => {
333
335
  // this means contracts will be restored from this state
334
336
  // this also means devs NEED to make sure the state can be restored
335
337
  // on contract deploy an error will be thrown if state wasn't recoverable
336
- if (block.index > 24) {
338
+ if (block.index >= blocks.length - 24) {
337
339
  const transactionCount = blocks[block.index - 1].transactions.length;
338
340
  latestTransactions.splice(-(transactionCount - 1), latestTransactions.length);
339
341
  }
340
342
 
341
343
  if (!block.loaded && !fromState) {
342
- const priority = block.transactions.filter((transaction) => transaction.priority);
344
+ const transactions = await Promise.all(
345
+ block.transactions.map(async (transaction) =>
346
+ new TransactionMessage(await resolveTransaction(transaction)).decode()
347
+ )
348
+ );
349
+ const priority = transactions.filter((transaction) => transaction.priority)?.sort((a, b) => a.nonce - b.nonce);
343
350
  if (priority.length > 0)
344
- await Promise.all(
345
- priority.sort((a, b) => a.nonce - b.nonce).map((transaction) => _executeTransaction(transaction))
346
- );
351
+ for (const transaction of priority) {
352
+ await _executeTransaction(transaction);
353
+ }
347
354
 
348
355
  await Promise.all(
349
- block.transactions
356
+ transactions
350
357
  .filter((transaction) => !transaction.priority)
351
358
  .map(async (transaction) => _executeTransaction(transaction))
352
359
  );
@@ -12379,37 +12379,6 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
12379
12379
  const FormatInterface = FormatInterface$1;
12380
12380
 
12381
12381
  var proto$3 = {
12382
- timestamp: Number(),
12383
- from: String(),
12384
- to: String(),
12385
- method: String(),
12386
- params: Array(),
12387
- signature: String(),
12388
- 'nonce?': Number(),
12389
- 'dependsOn?': Array(),
12390
- 'priority?': Boolean()
12391
- };
12392
-
12393
- class TransactionMessage extends FormatInterface {
12394
- get messageName() {
12395
- return 'TransactionMessage';
12396
- }
12397
- constructor(buffer) {
12398
- if (buffer instanceof TransactionMessage)
12399
- return buffer;
12400
- const name = 'transaction-message';
12401
- super(buffer, proto$3, { name });
12402
- }
12403
- beforeHashing(decoded) {
12404
- decoded = super.beforeHashing(decoded);
12405
- delete decoded.signature;
12406
- delete decoded.priority;
12407
- delete decoded.dependsOn;
12408
- return decoded;
12409
- }
12410
- }
12411
-
12412
- var proto$2 = {
12413
12382
  address: String(),
12414
12383
  reward: BigNumber.from(0)
12415
12384
  };
@@ -12422,17 +12391,17 @@ class ValidatorMessage extends FormatInterface {
12422
12391
  if (buffer instanceof ValidatorMessage)
12423
12392
  return buffer;
12424
12393
  const name = 'validator-message';
12425
- super(buffer, proto$2, { name });
12394
+ super(buffer, proto$3, { name });
12426
12395
  }
12427
12396
  }
12428
12397
 
12429
- var proto$1 = {
12398
+ var proto$2 = {
12430
12399
  index: Number(),
12431
12400
  previousHash: String(),
12432
12401
  timestamp: Number(),
12433
12402
  reward: BigNumber.from(0),
12434
12403
  fees: BigNumber.from(0),
12435
- transactions: new Uint8Array(),
12404
+ transactions: Array(),
12436
12405
  validators: new Uint8Array()
12437
12406
  };
12438
12407
 
@@ -12444,42 +12413,32 @@ class BlockMessage extends FormatInterface {
12444
12413
  if (buffer instanceof BlockMessage)
12445
12414
  return buffer;
12446
12415
  const name = 'block-message';
12447
- super(buffer, proto$1, { name });
12416
+ super(buffer, proto$2, { name });
12448
12417
  }
12449
12418
  encode(decoded) {
12450
12419
  decoded = decoded || this.decoded;
12451
12420
  const validators = [];
12452
- const transactions = [];
12453
12421
  for (const validator of decoded.validators) {
12454
12422
  if (validator instanceof ValidatorMessage)
12455
12423
  validators.push(validator.encode());
12456
12424
  else
12457
12425
  validators.push(new ValidatorMessage(validator).encode());
12458
12426
  }
12459
- for (const transaction of decoded.transactions) {
12460
- if (transaction instanceof TransactionMessage)
12461
- transactions.push(transaction.encode());
12462
- else
12463
- transactions.push(new TransactionMessage(transaction).encode());
12464
- }
12465
12427
  return super.encode({
12466
12428
  ...decoded,
12467
- validators: index$5(validators),
12468
- transactions: index$5(transactions)
12429
+ validators: index$5(validators)
12469
12430
  });
12470
12431
  }
12471
12432
  decode(encoded) {
12472
12433
  encoded = encoded || this.encoded;
12473
12434
  super.decode(encoded);
12474
12435
  // @ts-ignore
12475
- this.decoded.transactions = index$4(this.decoded.transactions).map((transaction) => new TransactionMessage(transaction).decoded);
12476
- // @ts-ignore
12477
12436
  this.decoded.validators = index$4(this.decoded.validators).map((validator) => new ValidatorMessage(validator).decoded);
12478
12437
  return this.decoded;
12479
12438
  }
12480
12439
  }
12481
12440
 
12482
- var proto = {
12441
+ var proto$1 = {
12483
12442
  creator: String(),
12484
12443
  contract: new Uint8Array(),
12485
12444
  constructorParameters: Array()
@@ -12492,7 +12451,38 @@ class ContractMessage extends FormatInterface {
12492
12451
  constructor(buffer) {
12493
12452
  if (buffer instanceof ContractMessage)
12494
12453
  return buffer;
12495
- super(buffer, proto, { name: 'contract-message' });
12454
+ super(buffer, proto$1, { name: 'contract-message' });
12455
+ }
12456
+ }
12457
+
12458
+ var proto = {
12459
+ timestamp: Number(),
12460
+ from: String(),
12461
+ to: String(),
12462
+ method: String(),
12463
+ params: Array(),
12464
+ signature: String(),
12465
+ 'nonce?': Number(),
12466
+ 'dependsOn?': Array(),
12467
+ 'priority?': Boolean()
12468
+ };
12469
+
12470
+ class TransactionMessage extends FormatInterface {
12471
+ get messageName() {
12472
+ return 'TransactionMessage';
12473
+ }
12474
+ constructor(buffer) {
12475
+ if (buffer instanceof TransactionMessage)
12476
+ return buffer;
12477
+ const name = 'transaction-message';
12478
+ super(buffer, proto, { name });
12479
+ }
12480
+ beforeHashing(decoded) {
12481
+ decoded = super.beforeHashing(decoded);
12482
+ delete decoded.signature;
12483
+ delete decoded.priority;
12484
+ delete decoded.dependsOn;
12485
+ return decoded;
12496
12486
  }
12497
12487
  }
12498
12488
 
package/exports/chain.js CHANGED
@@ -346,8 +346,8 @@ class Machine {
346
346
  break;
347
347
  }
348
348
  case 'ask': {
349
- if (data.question === 'contract') {
350
- const input = await peernet.get(data.input, 'contract');
349
+ if (data.question === 'contract' || data.question === 'transaction') {
350
+ const input = await peernet.get(data.input, data.question);
351
351
  this.worker.postMessage({ id: data.id, input });
352
352
  }
353
353
  }
@@ -1490,27 +1490,25 @@ class Chain extends VersionControl {
1490
1490
  async #addBlock(block) {
1491
1491
  const blockMessage = await new BlockMessage(block);
1492
1492
  const hash = await blockMessage.hash();
1493
- await Promise.all(blockMessage.decoded.transactions
1493
+ const transactionsMessages = await Promise.all(blockMessage.decoded.transactions
1494
1494
  // @ts-ignore
1495
- .map(async (transaction) => {
1496
- // @ts-ignore
1497
- let hash = transaction.hash;
1498
- if (!hash) {
1499
- hash = await new TransactionMessage(transaction).hash();
1500
- // @ts-ignore
1501
- transaction.hash = hash;
1502
- }
1495
+ .map(async (hash) => {
1496
+ let data;
1503
1497
  if (!(await transactionStore.has(hash))) {
1504
- transactionStore.put(hash, await blockMessage.hash());
1498
+ data = await peernet.get(hash, 'transaction');
1499
+ transactionStore.put(hash, data);
1500
+ }
1501
+ else {
1502
+ data = transactionStore.get(hash);
1505
1503
  }
1506
1504
  (await transactionPoolStore.has(hash)) && (await transactionPoolStore.delete(hash));
1507
- return transaction;
1505
+ return new TransactionMessage(data).decode();
1508
1506
  }));
1509
1507
  await globalThis.blockStore.put(hash, blockMessage.encoded);
1510
1508
  debug(`added block: ${hash}`);
1511
1509
  let promises = [];
1512
1510
  let contracts = [];
1513
- for (let transaction of blockMessage.decoded.transactions) {
1511
+ for (let transaction of transactionsMessages) {
1514
1512
  // await transactionStore.put(transaction.hash, transaction.encoded)
1515
1513
  if (!contracts.includes(transaction.to)) {
1516
1514
  contracts.push(transaction.to);
@@ -1522,7 +1520,7 @@ class Chain extends VersionControl {
1522
1520
  try {
1523
1521
  promises = await Promise.allSettled(promises);
1524
1522
  const noncesByAddress = {};
1525
- for (let transaction of blockMessage.decoded.transactions) {
1523
+ for (let transaction of transactionsMessages) {
1526
1524
  globalThis.pubsub.publish('transaction-processed', transaction);
1527
1525
  if (transaction.to === globalThis.peernet.selectedAccount)
1528
1526
  globalThis.pubsub.publish('account-transaction-processed', transaction);
@@ -1569,7 +1567,7 @@ class Chain extends VersionControl {
1569
1567
  async #handleTransaction(transaction, latestTransactions, block) {
1570
1568
  const hash = await transaction.hash();
1571
1569
  const doubleTransactions = [];
1572
- if (latestTransactions.includes(hash)) {
1570
+ if (latestTransactions.includes(hash) || transactionStore.has(hash)) {
1573
1571
  doubleTransactions.push(hash);
1574
1572
  }
1575
1573
  if (doubleTransactions.length > 0) {
@@ -1580,7 +1578,7 @@ class Chain extends VersionControl {
1580
1578
  // if (timestamp + this.#slotTime > Date.now()) {
1581
1579
  try {
1582
1580
  const result = await this.#executeTransaction({ ...transaction.decoded, hash });
1583
- block.transactions.push(transaction);
1581
+ block.transactions.push(hash);
1584
1582
  block.fees = block.fees.add(await calculateFee(transaction.decoded));
1585
1583
  await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
1586
1584
  }
@@ -1685,10 +1683,7 @@ class Chain extends VersionControl {
1685
1683
  // block.reward = block.reward.toString()
1686
1684
  // block.fees = block.fees.toString()
1687
1685
  try {
1688
- block.transactions = await Promise.all(block.transactions.map(async (transaction) => {
1689
- await globalThis.transactionPoolStore.delete(await transaction.hash());
1690
- return transaction.decoded;
1691
- }));
1686
+ await Promise.all(block.transactions.map(async (transaction) => await globalThis.transactionPoolStore.delete(transaction)));
1692
1687
  let blockMessage = await new BlockMessage(block);
1693
1688
  const hash = await blockMessage.hash();
1694
1689
  await globalThis.peernet.put(hash, blockMessage.encoded, 'block');
@@ -1,4 +1,4 @@
1
- import { E as EasyWorker, B as BigNumber, a as BlockMessage } from './worker-CFrwP8cD.js';
1
+ import { E as EasyWorker, B as BigNumber, a as BlockMessage } from './worker-Cqj1ERFr.js';
2
2
 
3
3
  const worker = new EasyWorker();
4
4
 
@@ -1,4 +1,4 @@
1
- import { E as EasyWorker, B as BigNumber, C as ContractMessage, T as TransactionMessage } from './worker-CFrwP8cD.js';
1
+ import { E as EasyWorker, B as BigNumber, C as ContractMessage, T as TransactionMessage } from './worker-Cqj1ERFr.js';
2
2
 
3
3
  const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
4
4
  const formatBytes = (bytes, decimals = 2) => {
@@ -150,7 +150,9 @@ const get = ({ contract, method, params }) => {
150
150
  return result
151
151
  };
152
152
 
153
- const resolveContract = (address) => askFor('contract', address);
153
+ const resolveContract = (hash) => askFor('contract', hash);
154
+
155
+ const resolveTransaction = (hash) => askFor('transaction', hash);
154
156
 
155
157
  const respond = (id, value) => {
156
158
  worker.postMessage({
@@ -306,23 +308,23 @@ _.init = async (message) => {
306
308
  );
307
309
  console.log({ blocks: message.blocks });
308
310
  if (message.blocks?.length > 0) {
309
- let pre;
310
-
311
- try {
312
- const importee = await import('url');
313
- const url = importee.default;
314
- if (url) pre = url.fileURLToPath(new URL('.', import.meta.url));
315
- } catch {
316
- // browser env
317
- pre = './';
318
- }
311
+ // let pre
312
+
313
+ // try {
314
+ // const importee = await import('url')
315
+ // const url = importee.default
316
+ // if (url) pre = url.fileURLToPath(new URL('.', import.meta.url))
317
+ // } catch {
318
+ // // browser env
319
+ // pre = './'
320
+ // }
319
321
 
320
- let _worker = await new EasyWorker(pre + 'block-worker.js', {
321
- serialization: 'advanced',
322
- type: 'module'
323
- });
324
- blocks = await _worker.once(message.blocks);
325
- _worker = null;
322
+ // let _worker = await new EasyWorker(pre + 'block-worker.js', {
323
+ // serialization: 'advanced',
324
+ // type: 'module'
325
+ // })
326
+ // blocks = await _worker.once(message.blocks)
327
+ // _worker = null
326
328
  // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
327
329
  // for (let i = 0; i < blocks.length; i++) {
328
330
 
@@ -333,20 +335,25 @@ _.init = async (message) => {
333
335
  // this means contracts will be restored from this state
334
336
  // this also means devs NEED to make sure the state can be restored
335
337
  // on contract deploy an error will be thrown if state wasn't recoverable
336
- if (block.index > 24) {
338
+ if (block.index >= blocks.length - 24) {
337
339
  const transactionCount = blocks[block.index - 1].transactions.length;
338
340
  latestTransactions.splice(-(transactionCount - 1), latestTransactions.length);
339
341
  }
340
342
 
341
343
  if (!block.loaded && !fromState) {
342
- const priority = block.transactions.filter((transaction) => transaction.priority);
344
+ const transactions = await Promise.all(
345
+ block.transactions.map(async (transaction) =>
346
+ new TransactionMessage(await resolveTransaction(transaction)).decode()
347
+ )
348
+ );
349
+ const priority = transactions.filter((transaction) => transaction.priority)?.sort((a, b) => a.nonce - b.nonce);
343
350
  if (priority.length > 0)
344
- await Promise.all(
345
- priority.sort((a, b) => a.nonce - b.nonce).map((transaction) => _executeTransaction(transaction))
346
- );
351
+ for (const transaction of priority) {
352
+ await _executeTransaction(transaction);
353
+ }
347
354
 
348
355
  await Promise.all(
349
- block.transactions
356
+ transactions
350
357
  .filter((transaction) => !transaction.priority)
351
358
  .map(async (transaction) => _executeTransaction(transaction))
352
359
  );
@@ -12379,37 +12379,6 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
12379
12379
  const FormatInterface = FormatInterface$1;
12380
12380
 
12381
12381
  var proto$3 = {
12382
- timestamp: Number(),
12383
- from: String(),
12384
- to: String(),
12385
- method: String(),
12386
- params: Array(),
12387
- signature: String(),
12388
- 'nonce?': Number(),
12389
- 'dependsOn?': Array(),
12390
- 'priority?': Boolean()
12391
- };
12392
-
12393
- class TransactionMessage extends FormatInterface {
12394
- get messageName() {
12395
- return 'TransactionMessage';
12396
- }
12397
- constructor(buffer) {
12398
- if (buffer instanceof TransactionMessage)
12399
- return buffer;
12400
- const name = 'transaction-message';
12401
- super(buffer, proto$3, { name });
12402
- }
12403
- beforeHashing(decoded) {
12404
- decoded = super.beforeHashing(decoded);
12405
- delete decoded.signature;
12406
- delete decoded.priority;
12407
- delete decoded.dependsOn;
12408
- return decoded;
12409
- }
12410
- }
12411
-
12412
- var proto$2 = {
12413
12382
  address: String(),
12414
12383
  reward: BigNumber.from(0)
12415
12384
  };
@@ -12422,17 +12391,17 @@ class ValidatorMessage extends FormatInterface {
12422
12391
  if (buffer instanceof ValidatorMessage)
12423
12392
  return buffer;
12424
12393
  const name = 'validator-message';
12425
- super(buffer, proto$2, { name });
12394
+ super(buffer, proto$3, { name });
12426
12395
  }
12427
12396
  }
12428
12397
 
12429
- var proto$1 = {
12398
+ var proto$2 = {
12430
12399
  index: Number(),
12431
12400
  previousHash: String(),
12432
12401
  timestamp: Number(),
12433
12402
  reward: BigNumber.from(0),
12434
12403
  fees: BigNumber.from(0),
12435
- transactions: new Uint8Array(),
12404
+ transactions: Array(),
12436
12405
  validators: new Uint8Array()
12437
12406
  };
12438
12407
 
@@ -12444,42 +12413,32 @@ class BlockMessage extends FormatInterface {
12444
12413
  if (buffer instanceof BlockMessage)
12445
12414
  return buffer;
12446
12415
  const name = 'block-message';
12447
- super(buffer, proto$1, { name });
12416
+ super(buffer, proto$2, { name });
12448
12417
  }
12449
12418
  encode(decoded) {
12450
12419
  decoded = decoded || this.decoded;
12451
12420
  const validators = [];
12452
- const transactions = [];
12453
12421
  for (const validator of decoded.validators) {
12454
12422
  if (validator instanceof ValidatorMessage)
12455
12423
  validators.push(validator.encode());
12456
12424
  else
12457
12425
  validators.push(new ValidatorMessage(validator).encode());
12458
12426
  }
12459
- for (const transaction of decoded.transactions) {
12460
- if (transaction instanceof TransactionMessage)
12461
- transactions.push(transaction.encode());
12462
- else
12463
- transactions.push(new TransactionMessage(transaction).encode());
12464
- }
12465
12427
  return super.encode({
12466
12428
  ...decoded,
12467
- validators: index$5(validators),
12468
- transactions: index$5(transactions)
12429
+ validators: index$5(validators)
12469
12430
  });
12470
12431
  }
12471
12432
  decode(encoded) {
12472
12433
  encoded = encoded || this.encoded;
12473
12434
  super.decode(encoded);
12474
12435
  // @ts-ignore
12475
- this.decoded.transactions = index$4(this.decoded.transactions).map((transaction) => new TransactionMessage(transaction).decoded);
12476
- // @ts-ignore
12477
12436
  this.decoded.validators = index$4(this.decoded.validators).map((validator) => new ValidatorMessage(validator).decoded);
12478
12437
  return this.decoded;
12479
12438
  }
12480
12439
  }
12481
12440
 
12482
- var proto = {
12441
+ var proto$1 = {
12483
12442
  creator: String(),
12484
12443
  contract: new Uint8Array(),
12485
12444
  constructorParameters: Array()
@@ -12492,7 +12451,38 @@ class ContractMessage extends FormatInterface {
12492
12451
  constructor(buffer) {
12493
12452
  if (buffer instanceof ContractMessage)
12494
12453
  return buffer;
12495
- super(buffer, proto, { name: 'contract-message' });
12454
+ super(buffer, proto$1, { name: 'contract-message' });
12455
+ }
12456
+ }
12457
+
12458
+ var proto = {
12459
+ timestamp: Number(),
12460
+ from: String(),
12461
+ to: String(),
12462
+ method: String(),
12463
+ params: Array(),
12464
+ signature: String(),
12465
+ 'nonce?': Number(),
12466
+ 'dependsOn?': Array(),
12467
+ 'priority?': Boolean()
12468
+ };
12469
+
12470
+ class TransactionMessage extends FormatInterface {
12471
+ get messageName() {
12472
+ return 'TransactionMessage';
12473
+ }
12474
+ constructor(buffer) {
12475
+ if (buffer instanceof TransactionMessage)
12476
+ return buffer;
12477
+ const name = 'transaction-message';
12478
+ super(buffer, proto, { name });
12479
+ }
12480
+ beforeHashing(decoded) {
12481
+ decoded = super.beforeHashing(decoded);
12482
+ delete decoded.signature;
12483
+ delete decoded.priority;
12484
+ delete decoded.dependsOn;
12485
+ return decoded;
12496
12486
  }
12497
12487
  }
12498
12488
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.6.12",
3
+ "version": "1.6.13",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {