@leofcoin/chain 1.4.9 → 1.4.11

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.4.9",
3
+ "version": "1.4.11",
4
4
  "description": "Official javascript implementation",
5
5
  "main": "./dist/node.js",
6
6
  "module": "./dist/chain.esm",
package/src/chain.js CHANGED
@@ -382,6 +382,7 @@ async resolveBlock(hash) {
382
382
  pubsub.publish(`transaction.completed.${hash}`, {status: 'fulfilled', hash})
383
383
  return result || 'no state change'
384
384
  } catch (error) {
385
+ console.log(error);
385
386
  pubsub.publish(`transaction.completed.${hash}`, {status: 'fail', hash, error: error})
386
387
  throw error
387
388
  }
@@ -458,7 +459,7 @@ async resolveBlock(hash) {
458
459
  // vote for transactions
459
460
  if (await transactionPoolStore.size() === 0) return;
460
461
 
461
- let transactions = await transactionPoolStore.values(limit)
462
+ let transactions = await transactionPoolStore.values(this.transactionLimit)
462
463
 
463
464
  if (Object.keys(transactions)?.length === 0 ) return
464
465
 
@@ -469,18 +470,18 @@ async resolveBlock(hash) {
469
470
  }
470
471
 
471
472
  // exclude failing tx
472
- transactions = await this.getTransactions(transactions.slice(0, transactions.length < 1800 ? transactions.length : 1800))
473
+ transactions = await this.promiseTransactions(transactions)
473
474
 
474
475
  transactions = transactions.sort((a, b) => a.nonce - b.nonce)
475
476
  for (let transaction of transactions) {
477
+ const hash = await transaction.hash()
476
478
  try {
477
- await this.#executeTransaction(transaction)
478
- block.transactions.push(transaction)
479
- block.fees += Number(calculateFee(transaction))
480
- await accountsStore.put(transaction.from, new TextEncoder().encode(String(transaction.nonce)))
479
+ await this.#executeTransaction({...transaction.decoded, hash})
480
+ block.transactions.push({hash, ...transaction.decoded})
481
+ block.fees += Number(calculateFee(transaction.decoded))
482
+ await accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)))
481
483
  } catch {
482
- transaction = await new TransactionMessage(transaction)
483
- await transactionPoolStore.delete(await transaction.hash())
484
+ await transactionPoolStore.delete(hash)
484
485
  }
485
486
  }
486
487
  // don't add empty block
package/src/machine.js CHANGED
@@ -1,13 +1,8 @@
1
1
  import { contractFactory, nativeToken, validators, nameService } from '@leofcoin/addresses'
2
- import randombytes from 'randombytes'
3
- import { join, dirname } from 'node:path'
2
+ import { randombytes } from '@leofcoin/crypto'
4
3
  import EasyWorker from '@vandeurenglenn/easy-worker'
5
4
  import { ContractMessage } from '@leofcoin/messages'
6
5
  // import State from './state'
7
- import { fileURLToPath } from 'node:url';
8
-
9
- const __filename = fileURLToPath(import.meta.url);
10
- const __dirname = dirname(__filename);
11
6
 
12
7
  export default class Machine {
13
8
  #contracts = {}
package/src/protocol.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export default class Protocol {
2
2
  limit = 1800
3
+ transactionLimit = 1800
3
4
  }
@@ -64,7 +64,7 @@ export default class Transaction extends Protocol {
64
64
  * @returns {Number} nonce
65
65
  */
66
66
  async #getNonceFallback(address) {
67
- let transactions = await transactionPoolStore.values()
67
+ let transactions = await transactionPoolStore.values(this.transactionLimit)
68
68
  transactions = await this.promiseTransactions(transactions)
69
69
  transactions = transactions.filter(tx => tx.decoded.from === address)
70
70
  transactions = await this.promiseTransactionsContent(transactions)
@@ -158,7 +158,7 @@ export default class Transaction extends Protocol {
158
158
  identity = JSON.parse(new TextDecoder().decode(identity))
159
159
  const wallet = new MultiWallet(peernet.network)
160
160
  await wallet.recover(identity.mnemonic)
161
- const account = wallet.account(0).external(0)
161
+ const account = await wallet.account(0).external(0)
162
162
  transaction.signature = await this.#signTransaction(transaction, account)
163
163
  transaction.signature = bs32.encode(transaction.signature)
164
164
  return transaction
@@ -208,7 +208,7 @@ export default class Transaction extends Protocol {
208
208
  const message = await new TransactionMessage(transaction)
209
209
 
210
210
  let data
211
- const wait = () => new Promise(async (resolve, reject) => {
211
+ const wait = new Promise(async (resolve, reject) => {
212
212
  if (pubsub.subscribers[`transaction.completed.${await message.hash()}`]) {
213
213
  const result = pubsub.subscribers[`transaction.completed.${await message.hash()}`].value
214
214
  result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error})
package/test/chain.js CHANGED
@@ -51,6 +51,9 @@ const job = async () => {
51
51
  // return
52
52
  let nonce = await chain.getNonce(peernet.selectedAccount)
53
53
  console.log({nonce});
54
+ let balances = await chain.balances
55
+ console.log(`balance for ${Object.keys(balances)[0]}:${chain.utils.formatUnits(balances[Object.keys(balances)[0]]).toString()}`);
56
+ console.log(`balance for ${Object.keys(balances)[1]}:${chain.utils.formatUnits(balances[Object.keys(balances)[1]]).toString()}`);
54
57
  // return
55
58
  let promises = []
56
59
  // nonce += 1
@@ -60,14 +63,11 @@ const job = async () => {
60
63
  promises.push(chain.createTransaction(chain.nativeToken, 'transfer', [peernet.selectedAccount, '6zqut21djrRNJAniaTByovGhnBGs5h9wfkP35mzjZkEBZwnQVo', chain.utils.parseUnits('100').toString()], nonce))
61
64
  }
62
65
  promises = await Promise.allSettled(promises)
63
- promises = await Promise.allSettled(promises.map(({value}) => value.wait()))
64
- promises.forEach((item, i) => {
65
- if (item.reason) console.log(item.reason);
66
- });
66
+ promises = await Promise.allSettled(promises.map(({value}) => value.wait))
67
67
 
68
68
  console.log(`${(new Date().getTime() - start) / 1000} s`);
69
69
 
70
- let balances = await chain.balances
70
+ balances = await chain.balances
71
71
  console.log(`balance for ${Object.keys(balances)[0]}:${chain.utils.formatUnits(balances[Object.keys(balances)[0]]).toString()}`);
72
72
  console.log(`balance for ${Object.keys(balances)[1]}:${chain.utils.formatUnits(balances[Object.keys(balances)[1]]).toString()}`);
73
73
  // return
@@ -82,7 +82,7 @@ const job = async () => {
82
82
  promises.push(chain.createTransaction(chain.nativeToken, 'transfer', [peernet.selectedAccount, '6zqut21djrRNJAniaTByovGhnBGs5h9wfkP35mzjZkEBZwnQVo', chain.utils.parseUnits('100').toString()], nonce))
83
83
  }
84
84
  promises = await Promise.allSettled(promises)
85
- promises = await Promise.allSettled(promises.map(({value}) => value.wait()))
85
+ promises = await Promise.allSettled(promises.map(({value}) => value.wait))
86
86
  balances = await chain.balances
87
87
  console.log(`balance for ${Object.keys(balances)[0]}:${chain.utils.formatUnits(balances[Object.keys(balances)[0]]).toString()}`);
88
88
  console.log(`balance for ${Object.keys(balances)[1]}:${chain.utils.formatUnits(balances[Object.keys(balances)[1]]).toString()}`);
@@ -90,14 +90,14 @@ const job = async () => {
90
90
  promises = []
91
91
 
92
92
  // nonce += 1
93
- for (let i = 0; i < 100; i++) {
93
+ for (let i = 0; i < 1000; i++) {
94
94
  // contract , method, from, to, amount, (optional) nonce
95
95
 
96
96
  nonce += 1
97
97
  promises.push(chain.createTransaction(chain.nativeToken, 'transfer', [peernet.selectedAccount, '6zqut21djrRNJAniaTByovGhnBGs5h9wfkP35mzjZkEBZwnQVo', chain.utils.parseUnits('100').toString()], nonce))
98
98
  }
99
99
  promises = await Promise.allSettled(promises)
100
- promises = await Promise.allSettled(promises.map(({value}) => value.wait()))
100
+ promises = await Promise.allSettled(promises.map(({value}) => value.wait))
101
101
  balances = await chain.balances
102
102
  console.log(`balance for ${Object.keys(balances)[0]}:${chain.utils.formatUnits(balances[Object.keys(balances)[0]]).toString()}`);
103
103
  console.log(`balance for ${Object.keys(balances)[1]}:${chain.utils.formatUnits(balances[Object.keys(balances)[1]]).toString()}`);