@leofcoin/chain 1.6.12 → 1.6.14

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
  }
@@ -4879,7 +4879,7 @@ class VersionControl extends State {
4879
4879
  constructor(config) {
4880
4880
  super(config);
4881
4881
  }
4882
- #currentVersion = '1.2.0';
4882
+ #currentVersion = '1.2.1';
4883
4883
  async #setCurrentVersion() {
4884
4884
  this.version = this.#currentVersion;
4885
4885
  await globalThis.chainStore.put('version', this.version);
@@ -4892,15 +4892,16 @@ class VersionControl extends State {
4892
4892
  console.log(this.version, this.#currentVersion);
4893
4893
  /**
4894
4894
  * protocol version control!
4895
- * note v1 and 1.1 delete everything because of big changes, this is not what we want in the future
4896
- * in the future we want newer nodes to handle the new changes and still confirm old version transactions
4897
- * unless there is a security issue!
4895
+ * Note that before v0.2.0 everything gets deleted because of big changes,
4896
+ * this is not what we want in the future.
4897
+ * In the future we want newer nodes to handle the new changes and still confirm old version transactions
4898
+ * Unless there is a security issue!
4899
+ * But for now the protocoll isn't finished enough and still has to much breaking changes.
4898
4900
  */
4899
- if (semver$1.compare('1.1.1', this.version) === 1) {
4901
+ if (semver$1.compare('1.2.0', this.version) === 1) {
4900
4902
  await this.clearAll();
4901
4903
  }
4902
4904
  if (semver$1.compare(this.#currentVersion, this.version) === 1) {
4903
- // await this.clearAll()
4904
4905
  await this.#setCurrentVersion();
4905
4906
  }
4906
4907
  // if (version)
@@ -5123,27 +5124,25 @@ class Chain extends VersionControl {
5123
5124
  async #addBlock(block) {
5124
5125
  const blockMessage = await new BlockMessage(block);
5125
5126
  const hash = await blockMessage.hash();
5126
- await Promise.all(blockMessage.decoded.transactions
5127
+ const transactionsMessages = await Promise.all(blockMessage.decoded.transactions
5127
5128
  // @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
- }
5129
+ .map(async (hash) => {
5130
+ let data;
5136
5131
  if (!(await transactionStore.has(hash))) {
5137
- transactionStore.put(hash, await blockMessage.hash());
5132
+ data = await peernet.get(hash, 'transaction');
5133
+ transactionStore.put(hash, data);
5134
+ }
5135
+ else {
5136
+ data = transactionStore.get(hash);
5138
5137
  }
5139
5138
  (await transactionPoolStore.has(hash)) && (await transactionPoolStore.delete(hash));
5140
- return transaction;
5139
+ return new TransactionMessage(data).decode();
5141
5140
  }));
5142
5141
  await globalThis.blockStore.put(hash, blockMessage.encoded);
5143
5142
  debug(`added block: ${hash}`);
5144
5143
  let promises = [];
5145
5144
  let contracts = [];
5146
- for (let transaction of blockMessage.decoded.transactions) {
5145
+ for (let transaction of transactionsMessages) {
5147
5146
  // await transactionStore.put(transaction.hash, transaction.encoded)
5148
5147
  if (!contracts.includes(transaction.to)) {
5149
5148
  contracts.push(transaction.to);
@@ -5155,7 +5154,7 @@ class Chain extends VersionControl {
5155
5154
  try {
5156
5155
  promises = await Promise.allSettled(promises);
5157
5156
  const noncesByAddress = {};
5158
- for (let transaction of blockMessage.decoded.transactions) {
5157
+ for (let transaction of transactionsMessages) {
5159
5158
  globalThis.pubsub.publish('transaction-processed', transaction);
5160
5159
  if (transaction.to === globalThis.peernet.selectedAccount)
5161
5160
  globalThis.pubsub.publish('account-transaction-processed', transaction);
@@ -5202,7 +5201,7 @@ class Chain extends VersionControl {
5202
5201
  async #handleTransaction(transaction, latestTransactions, block) {
5203
5202
  const hash = await transaction.hash();
5204
5203
  const doubleTransactions = [];
5205
- if (latestTransactions.includes(hash)) {
5204
+ if (latestTransactions.includes(hash) || transactionStore.has(hash)) {
5206
5205
  doubleTransactions.push(hash);
5207
5206
  }
5208
5207
  if (doubleTransactions.length > 0) {
@@ -5213,7 +5212,7 @@ class Chain extends VersionControl {
5213
5212
  // if (timestamp + this.#slotTime > Date.now()) {
5214
5213
  try {
5215
5214
  const result = await this.#executeTransaction({ ...transaction.decoded, hash });
5216
- block.transactions.push(transaction);
5215
+ block.transactions.push(hash);
5217
5216
  block.fees = block.fees.add(await calculateFee(transaction.decoded));
5218
5217
  await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
5219
5218
  }
@@ -5318,10 +5317,7 @@ class Chain extends VersionControl {
5318
5317
  // block.reward = block.reward.toString()
5319
5318
  // block.fees = block.fees.toString()
5320
5319
  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
- }));
5320
+ await Promise.all(block.transactions.map(async (transaction) => await globalThis.transactionPoolStore.delete(transaction)));
5325
5321
  let blockMessage = await new BlockMessage(block);
5326
5322
  const hash = await blockMessage.hash();
5327
5323
  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
  }
@@ -1246,7 +1246,7 @@ class VersionControl extends State {
1246
1246
  constructor(config) {
1247
1247
  super(config);
1248
1248
  }
1249
- #currentVersion = '1.2.0';
1249
+ #currentVersion = '1.2.1';
1250
1250
  async #setCurrentVersion() {
1251
1251
  this.version = this.#currentVersion;
1252
1252
  await globalThis.chainStore.put('version', this.version);
@@ -1259,15 +1259,16 @@ class VersionControl extends State {
1259
1259
  console.log(this.version, this.#currentVersion);
1260
1260
  /**
1261
1261
  * protocol version control!
1262
- * note v1 and 1.1 delete everything because of big changes, this is not what we want in the future
1263
- * in the future we want newer nodes to handle the new changes and still confirm old version transactions
1264
- * unless there is a security issue!
1262
+ * Note that before v0.2.0 everything gets deleted because of big changes,
1263
+ * this is not what we want in the future.
1264
+ * In the future we want newer nodes to handle the new changes and still confirm old version transactions
1265
+ * Unless there is a security issue!
1266
+ * But for now the protocoll isn't finished enough and still has to much breaking changes.
1265
1267
  */
1266
- if (semver.compare('1.1.1', this.version) === 1) {
1268
+ if (semver.compare('1.2.0', this.version) === 1) {
1267
1269
  await this.clearAll();
1268
1270
  }
1269
1271
  if (semver.compare(this.#currentVersion, this.version) === 1) {
1270
- // await this.clearAll()
1271
1272
  await this.#setCurrentVersion();
1272
1273
  }
1273
1274
  // if (version)
@@ -1490,27 +1491,25 @@ class Chain extends VersionControl {
1490
1491
  async #addBlock(block) {
1491
1492
  const blockMessage = await new BlockMessage(block);
1492
1493
  const hash = await blockMessage.hash();
1493
- await Promise.all(blockMessage.decoded.transactions
1494
+ const transactionsMessages = await Promise.all(blockMessage.decoded.transactions
1494
1495
  // @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
- }
1496
+ .map(async (hash) => {
1497
+ let data;
1503
1498
  if (!(await transactionStore.has(hash))) {
1504
- transactionStore.put(hash, await blockMessage.hash());
1499
+ data = await peernet.get(hash, 'transaction');
1500
+ transactionStore.put(hash, data);
1501
+ }
1502
+ else {
1503
+ data = transactionStore.get(hash);
1505
1504
  }
1506
1505
  (await transactionPoolStore.has(hash)) && (await transactionPoolStore.delete(hash));
1507
- return transaction;
1506
+ return new TransactionMessage(data).decode();
1508
1507
  }));
1509
1508
  await globalThis.blockStore.put(hash, blockMessage.encoded);
1510
1509
  debug(`added block: ${hash}`);
1511
1510
  let promises = [];
1512
1511
  let contracts = [];
1513
- for (let transaction of blockMessage.decoded.transactions) {
1512
+ for (let transaction of transactionsMessages) {
1514
1513
  // await transactionStore.put(transaction.hash, transaction.encoded)
1515
1514
  if (!contracts.includes(transaction.to)) {
1516
1515
  contracts.push(transaction.to);
@@ -1522,7 +1521,7 @@ class Chain extends VersionControl {
1522
1521
  try {
1523
1522
  promises = await Promise.allSettled(promises);
1524
1523
  const noncesByAddress = {};
1525
- for (let transaction of blockMessage.decoded.transactions) {
1524
+ for (let transaction of transactionsMessages) {
1526
1525
  globalThis.pubsub.publish('transaction-processed', transaction);
1527
1526
  if (transaction.to === globalThis.peernet.selectedAccount)
1528
1527
  globalThis.pubsub.publish('account-transaction-processed', transaction);
@@ -1569,7 +1568,7 @@ class Chain extends VersionControl {
1569
1568
  async #handleTransaction(transaction, latestTransactions, block) {
1570
1569
  const hash = await transaction.hash();
1571
1570
  const doubleTransactions = [];
1572
- if (latestTransactions.includes(hash)) {
1571
+ if (latestTransactions.includes(hash) || transactionStore.has(hash)) {
1573
1572
  doubleTransactions.push(hash);
1574
1573
  }
1575
1574
  if (doubleTransactions.length > 0) {
@@ -1580,7 +1579,7 @@ class Chain extends VersionControl {
1580
1579
  // if (timestamp + this.#slotTime > Date.now()) {
1581
1580
  try {
1582
1581
  const result = await this.#executeTransaction({ ...transaction.decoded, hash });
1583
- block.transactions.push(transaction);
1582
+ block.transactions.push(hash);
1584
1583
  block.fees = block.fees.add(await calculateFee(transaction.decoded));
1585
1584
  await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
1586
1585
  }
@@ -1685,10 +1684,7 @@ class Chain extends VersionControl {
1685
1684
  // block.reward = block.reward.toString()
1686
1685
  // block.fees = block.fees.toString()
1687
1686
  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
- }));
1687
+ await Promise.all(block.transactions.map(async (transaction) => await globalThis.transactionPoolStore.delete(transaction)));
1692
1688
  let blockMessage = await new BlockMessage(block);
1693
1689
  const hash = await blockMessage.hash();
1694
1690
  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.14",
4
4
  "description": "Official javascript implementation",
5
5
  "private": false,
6
6
  "exports": {