@leofcoin/chain 1.1.11 → 1.1.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.
Files changed (35) hide show
  1. package/{dist/workers/block-worker.js → block-worker.js} +0 -0
  2. package/demo/chain.browser.js +50 -51
  3. package/demo/node.browser.js +6 -13
  4. package/demo/pako.browser.js +670 -501
  5. package/demo/peernet-swarm.browser.js +8 -12
  6. package/demo/workers/block-worker.js +1 -2
  7. package/demo/workers/machine-worker.js +2 -4
  8. package/demo/workers/pool-worker.js +1 -2
  9. package/demo/workers/transaction-worker.js +1 -2
  10. package/dist/browser/workers/block-worker.js +1 -2
  11. package/dist/browser/workers/machine-worker.js +2 -4
  12. package/dist/browser/workers/pool-worker.js +1 -2
  13. package/dist/browser/workers/transaction-worker.js +1 -2
  14. package/dist/chain.browser.js +50 -51
  15. package/dist/chain.js +49 -49
  16. package/dist/module/chain.js +49 -49
  17. package/dist/module/workers/machine-worker.js +2 -2
  18. package/dist/node.browser.js +6 -13
  19. package/dist/pako.browser.js +670 -501
  20. package/dist/peernet-swarm.browser.js +8 -12
  21. package/dist/workers/machine-worker.js +1 -1
  22. package/package.json +4 -2
  23. package/rollup.config.js +1 -1
  24. package/src/chain.js +42 -46
  25. package/src/machine.js +6 -2
  26. package/test/chain.js +33 -18
  27. package/demo/865.machine-worker.js +0 -10
  28. package/demo/chain.js +0 -1209
  29. package/demo/machine-worker.js +0 -17624
  30. package/demo/node.js +0 -1
  31. package/demo/workers/workers/865.js +0 -10
  32. package/demo/workers/workers/block-worker.js +0 -13200
  33. package/demo/workers/workers/machine-worker.js +0 -13904
  34. package/demo/workers/workers/pool-worker.js +0 -8504
  35. package/demo/workers/workers/transaction-worker.js +0 -8496
@@ -56165,8 +56165,7 @@ module.exports = JSON.parse('{"2.16.840.1.101.3.4.1.1":"aes-128-ecb","2.16.840.1
56165
56165
  /******/ script.parentNode && script.parentNode.removeChild(script);
56166
56166
  /******/ doneFns && doneFns.forEach(function(fn) { return fn(event); });
56167
56167
  /******/ if(prev) return prev(event);
56168
- /******/ }
56169
- /******/ ;
56168
+ /******/ };
56170
56169
  /******/ var timeout = setTimeout(onScriptComplete.bind(null, undefined, { type: 'timeout', target: script }), 120000);
56171
56170
  /******/ script.onerror = onScriptComplete.bind(null, script.onerror);
56172
56171
  /******/ script.onload = onScriptComplete.bind(null, script.onload);
@@ -58570,7 +58569,7 @@ class Machine {
58570
58569
  return this.#init()
58571
58570
  }
58572
58571
 
58573
- #createMessage(sender = peernet.id) {
58572
+ #createMessage(sender = peernet.selectedAccount) {
58574
58573
  return {
58575
58574
  sender,
58576
58575
  call: this.execute,
@@ -58595,7 +58594,7 @@ class Machine {
58595
58594
  break
58596
58595
  case 'machine-ready':
58597
58596
  this.lastBlock = data.lastBlock;
58598
- pubsub.publish('machine.ready');
58597
+ pubsub.publish('machine.ready', true);
58599
58598
  break
58600
58599
  case 'response':
58601
58600
  pubsub.publish(data.id, data.value);
@@ -58716,6 +58715,10 @@ class Machine {
58716
58715
  return contractStore.delete(hash)
58717
58716
  }
58718
58717
 
58718
+ /**
58719
+ *
58720
+ * @returns Promise
58721
+ */
58719
58722
  async deleteAll() {
58720
58723
  let hashes = await contractStore.get();
58721
58724
  hashes = Object.keys(hashes).map(hash => this.delete(hash));
@@ -58950,7 +58953,7 @@ class Chain {
58950
58953
  console.log('epoch');
58951
58954
  const validators = await this.staticCall(addresses.validators, 'validators');
58952
58955
  console.log(validators);
58953
- if (!validators[peernet.id]?.active) return
58956
+ if (!validators[peernet.selectedAccount]?.active) return
58954
58957
 
58955
58958
  const start = new Date().getTime();
58956
58959
  try {
@@ -59036,8 +59039,7 @@ class Chain {
59036
59039
  }, {index: 0, hash: '0x0'});
59037
59040
  // get lastblock
59038
59041
  if (promises.hash && promises.hash !== '0x0') {
59039
- let localBlock = await peernet.get(promises.hash);
59040
- console.log({localBlock});
59042
+ await peernet.get(promises.hash);
59041
59043
  }
59042
59044
 
59043
59045
 
@@ -59052,22 +59054,7 @@ class Chain {
59052
59054
 
59053
59055
  this.#machine = await new Machine();
59054
59056
  this.utils = { BigNumber: BigNumber, formatUnits: formatUnits, parseUnits: parseUnits };
59055
-
59056
- await peernet.addRequestHandler('bw-request-message', () => {
59057
-
59058
- return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
59059
- });
59060
-
59061
- await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
59062
-
59063
- peernet.subscribe('add-block', this.#addBlock.bind(this));
59064
-
59065
- peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
59066
-
59067
- peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
59068
-
59069
- pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
59070
-
59057
+
59071
59058
  try {
59072
59059
  let localBlock;
59073
59060
  try {
@@ -59077,17 +59064,11 @@ class Chain {
59077
59064
  localBlock = await chainStore.get('lastBlock');
59078
59065
  }
59079
59066
  localBlock = new TextDecoder().decode(localBlock);
59080
-
59081
59067
  if (localBlock && localBlock !== '0x0') {
59082
- localBlock = await peernet.get(localBlock);
59068
+ localBlock = await peernet.get(localBlock, 'block');
59083
59069
  localBlock = await new BlockMessage(localBlock);
59084
59070
  this.#lastBlock = {...localBlock.decoded, hash: await localBlock.hash};
59085
- } else if (this.#machine.lastBlock?.hash) {
59086
- // todo remove when network is running
59087
- // recovering chain (not needed if multiple peers are online)
59088
- this.#lastBlock = this.#machine.lastBlock;
59089
- await chainStore.put('lastBlock', this.#lastBlock.hash);
59090
- } else {
59071
+ } else {
59091
59072
  await this.#sync();
59092
59073
  }
59093
59074
  } catch (e) {
@@ -59096,8 +59077,25 @@ class Chain {
59096
59077
 
59097
59078
  // this.#setup()
59098
59079
  }
59080
+
59081
+ await peernet.addRequestHandler('bw-request-message', () => {
59082
+
59083
+ return new BWMessage(peernet.client.bw) || { up: 0, down: 0 }
59084
+ });
59085
+
59086
+ await peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
59087
+
59088
+ peernet.subscribe('add-block', this.#addBlock.bind(this));
59089
+
59090
+ peernet.subscribe('add-transaction', this.#addTransaction.bind(this));
59091
+
59092
+ peernet.subscribe('validator:timeout', this.#validatorTimeout.bind(this));
59093
+
59094
+ pubsub.subscribe('peer:connected', this.#peerConnected.bind(this));
59095
+
59096
+
59099
59097
  // load local blocks
59100
- if (peernet.connections?.length > 1) await this.resolveBlocks();
59098
+ await this.resolveBlocks();
59101
59099
  return this
59102
59100
  }
59103
59101
 
@@ -59115,7 +59113,7 @@ class Chain {
59115
59113
  response = await new globalThis.peernet.protos['peernet-response'](response);
59116
59114
  let lastBlock = response.decoded.response;
59117
59115
 
59118
- if (!this.lastBlock || this.lastBlock.index < lastBlock.index) {
59116
+ if (!this.lastBlock || Number(this.lastBlock.index) < Number(lastBlock.index)) {
59119
59117
  // TODO: check if valid
59120
59118
  const localIndex = this.lastBlock ? this.lastBlock.index : 0;
59121
59119
  const index = lastBlock.index;
@@ -59157,7 +59155,8 @@ async resolveBlock(hash) {
59157
59155
  if (!await peernet.has(hash, 'block')) await peernet.put(hash, block.encoded, 'block');
59158
59156
  const size = block.encoded.length || block.encoded.byteLength;
59159
59157
  block = {...block.decoded, hash};
59160
- this.#blocks[block.index - 1] = block;
59158
+ if (this.#blocks[block.index]) throw `invalid block ${hash} @${block.index}`
59159
+ this.#blocks[block.index] = block;
59161
59160
  console.log(`loaded block: ${hash} @${block.index} ${formatBytes(size)}`);
59162
59161
  if (block.previousHash !== '0x0') {
59163
59162
  return this.resolveBlock(block.previousHash)
@@ -59166,11 +59165,11 @@ async resolveBlock(hash) {
59166
59165
 
59167
59166
  async resolveBlocks() {
59168
59167
  try {
59169
-
59170
59168
  const localBlock = await chainStore.get('lastBlock');
59171
59169
  const hash = new TextDecoder().decode(localBlock);
59170
+
59172
59171
  if (hash && hash !== '0x0')
59173
- await this.resolveBlock(localBlock);
59172
+ await this.resolveBlock(hash);
59174
59173
  this.#lastBlock = this.#blocks[this.#blocks.length - 1];
59175
59174
  await this.#loadBlocks(this.#blocks);
59176
59175
  } catch (e) {
@@ -59227,7 +59226,7 @@ async resolveBlock(hash) {
59227
59226
  }
59228
59227
 
59229
59228
  async #addBlock(block) {
59230
- console.log(block);
59229
+ // console.log(block);
59231
59230
  const blockMessage = await new BlockMessage(new Uint8Array(Object.values(block)));
59232
59231
  // if (!Buffer.isBuffer(block)) block = Buffer.from(block, 'hex')
59233
59232
  // const transactionJob = async transaction => {
@@ -59253,7 +59252,7 @@ async resolveBlock(hash) {
59253
59252
 
59254
59253
  await blockStore.put(hash, blockMessage.encoded);
59255
59254
 
59256
- if (this.lastBlock.index < blockMessage.decoded.index) this.#updateState(blockMessage);
59255
+ if (this.lastBlock.index < blockMessage.decoded.index) await this.#updateState(blockMessage);
59257
59256
  debug(`added block: ${hash}`);
59258
59257
  let promises = [];
59259
59258
  let contracts = [];
@@ -59292,7 +59291,7 @@ async resolveBlock(hash) {
59292
59291
 
59293
59292
 
59294
59293
 
59295
- async participate() {
59294
+ async participate(address) {
59296
59295
  // TODO: validate participant
59297
59296
  // hold min amount of 50k ART for 7 days
59298
59297
  // lock the 50k
@@ -59300,7 +59299,7 @@ async resolveBlock(hash) {
59300
59299
  // peerReputation(peerId)
59301
59300
  // {bandwith: {up, down}, uptime}
59302
59301
  this.participating = true;
59303
- if (!await this.staticCall(addresses.validators, 'has', [peernet.id])) await this.createTransactionFrom(peernet.id, addresses.validators, 'addValidator', [peernet.id]);
59302
+ if (!await this.staticCall(addresses.validators, 'has', [address])) await this.createTransactionFrom(address, addresses.validators, 'addValidator', [address]);
59304
59303
  if (await this.hasTransactionToHandle() && !this.#runningEpoch) await this.#runEpoch();
59305
59304
 
59306
59305
  // const runEpoch = () => setTimeout(async () => {
@@ -59389,7 +59388,7 @@ async resolveBlock(hash) {
59389
59388
  const node = await peernet.prepareMessage(validator, data.encoded);
59390
59389
  try {
59391
59390
  const bw = await peer.request(node.encoded);
59392
- console.log(bw);
59391
+ console.log({bw});
59393
59392
  block.validators.push({
59394
59393
  address: validator,
59395
59394
  bw: bw.up + bw.down
@@ -59398,9 +59397,9 @@ async resolveBlock(hash) {
59398
59397
 
59399
59398
  }
59400
59399
 
59401
- } else if (peernet.id === validator) {
59400
+ } else if (peernet.selectedAccount === validator) {
59402
59401
  block.validators.push({
59403
- address: peernet.id,
59402
+ address: peernet.selectedAccount,
59404
59403
  bw: peernet.bw.up + peernet.bw.down
59405
59404
  });
59406
59405
 
@@ -59449,8 +59448,8 @@ async resolveBlock(hash) {
59449
59448
  const hash = await blockMessage.hash;
59450
59449
 
59451
59450
 
59452
- await blockStore.put(hash, blockMessage.encoded);
59453
- this.#updateState(blockMessage);
59451
+ await peernet.put(hash, blockMessage.encoded, 'block');
59452
+ await this.#updateState(blockMessage);
59454
59453
  debug(`created block: ${hash}`);
59455
59454
 
59456
59455
  peernet.publish('add-block', blockMessage.encoded);
@@ -59526,7 +59525,7 @@ async resolveBlock(hash) {
59526
59525
  * @param {Number} nonce - total transaction count [optional]
59527
59526
  */
59528
59527
  async createTransaction(to, method, params, nonce, signature) {
59529
- return this.createTransactionFrom(peernet.id, to, method, params, nonce)
59528
+ return this.createTransactionFrom(peernet.selectedAccount, to, method, params, nonce)
59530
59529
  }
59531
59530
 
59532
59531
 
@@ -59578,7 +59577,7 @@ async #signTransaction (transaction, wallet) {
59578
59577
  * @returns {Object} transaction
59579
59578
  */
59580
59579
  async createRawTransaction(transaction) {
59581
- if (!transaction.from) transaction.from = peernet.id;
59580
+ if (!transaction.from) transaction.from = peernet.selectedAccount;
59582
59581
  transaction.timestamp = Date.now();
59583
59582
 
59584
59583
  if (transaction.nonce === undefined) {
@@ -59654,21 +59653,21 @@ async #signTransaction (transaction, wallet) {
59654
59653
  * @param {String} contract - a contract string (see plugins/deployContract)
59655
59654
  */
59656
59655
  async deployContract(contract, params = []) {
59657
- globalThis.msg = {sender: peernet.id, call: this.call};
59656
+ globalThis.msg = {sender: peernet.selectedAccount, call: this.call};
59658
59657
 
59659
59658
  const hash = await this.createContractAddress(creator, contract, params);
59660
59659
  console.log(hash);
59661
59660
  try {
59662
- const tx = await this.createTransactionFrom(peernet.id, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
59661
+ const tx = await this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'deployContract', [hash, creator, contract, constructorParameters]);
59663
59662
  } catch (e) {
59664
59663
  throw e
59665
59664
  }
59666
59665
  return this.#machine.addContract(message)
59667
59666
  }
59668
59667
 
59669
- #createMessage(sender = peernet.id) {
59668
+ #createMessage(sender = peernet.selectedAccount) {
59670
59669
  return {
59671
- sender: peernet.id,
59670
+ sender,
59672
59671
  call: this.call,
59673
59672
  staticCall: this.staticCall,
59674
59673
  delegate: this.delegate,
@@ -8341,8 +8341,7 @@ module.exports = function (value) {
8341
8341
  /******/ script.parentNode && script.parentNode.removeChild(script);
8342
8342
  /******/ doneFns && doneFns.forEach(function(fn) { return fn(event); });
8343
8343
  /******/ if(prev) return prev(event);
8344
- /******/ }
8345
- /******/ ;
8344
+ /******/ };
8346
8345
  /******/ var timeout = setTimeout(onScriptComplete.bind(null, undefined, { type: 'timeout', target: script }), 120000);
8347
8346
  /******/ script.onerror = onScriptComplete.bind(null, script.onerror);
8348
8347
  /******/ script.onload = onScriptComplete.bind(null, script.onload);
@@ -9133,7 +9132,8 @@ class Peernet {
9133
9132
  this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
9134
9133
  let accounts = await walletStore.get('accounts');
9135
9134
  accounts = new TextDecoder().decode(accounts);
9136
-
9135
+ const selected = await walletStore.get('selected-account');
9136
+ globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
9137
9137
 
9138
9138
  // fixing account issue (string while needs to be a JSON)
9139
9139
  // TODO: remove when on mainnet
@@ -9151,15 +9151,17 @@ class Peernet {
9151
9151
 
9152
9152
  await walletStore.put('version', String(1));
9153
9153
  await walletStore.put('accounts', JSON.stringify(accounts));
9154
+ await walletStore.put('selected-account', accounts[0][1]);
9154
9155
  await walletStore.put('identity', JSON.stringify(identity));
9155
9156
 
9157
+ globalThis.peernet.selectedAccount = accounts[0][1];
9156
9158
  this.id = identity.walletId;
9157
9159
  }
9158
9160
  this._peerHandler = new PeerDiscovery(this.id);
9159
9161
  this.peerId = this.id;
9160
9162
 
9161
9163
  pubsub.subscribe('peer:connected', async (peer) => {
9162
- console.log(peer);
9164
+ // console.log(peer);
9163
9165
  // console.log({connected: peer.id, as: this._getPeerId(peer.id) });
9164
9166
  // peer.on('peernet.data', async (message) => {
9165
9167
  // const id = message.id
@@ -9269,7 +9271,6 @@ class Peernet {
9269
9271
  const walk = async peer => {
9270
9272
  const node = await this.prepareMessage(data);
9271
9273
  let result = await peer.request(node.encoded);
9272
- console.log({result});
9273
9274
  result = new Uint8Array(Object.values(result));
9274
9275
  const proto = await protoFor(result);
9275
9276
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
@@ -9616,14 +9617,6 @@ class Peernet {
9616
9617
  get Buffer() {
9617
9618
  return Buffer
9618
9619
  }
9619
- // async block(index) {
9620
- // const _values = []
9621
- // for (const peer of this.peers) {
9622
- // const value = await peer.request({type: 'block', index})
9623
- // console.log(value);
9624
- // }
9625
- //
9626
- // }
9627
9620
  }
9628
9621
  globalThis.Peernet = Peernet;
9629
9622