Skip to content

Commit

Permalink
feat: support for new node feature next-nonce of release 6.2.0 (#1299)
Browse files Browse the repository at this point in the history
* feat: select nonce using next-nonce api from node v6.2.0 release

* refactor: fix nonce strategy to continuity

* feat: allow nonce strategy selection

* docs(nonce): add nonce strategy selection to tx options

* refactor(nonce): get rid of getAccount request

* fix: missing nonce field

* refactor: determine nonce only when not supplied
  • Loading branch information
subhod-i authored Oct 20, 2021
1 parent 3121885 commit e40b046
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NODE_TAG=v6.1.0
NODE_TAG=v6.2.0
COMPILER_TAG=v6.0.0
7 changes: 6 additions & 1 deletion docs/transaction-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ These options are common and can be provided to every tx-type:
- You can specify the account that should be used to sign a transaction.
- Note:
- The account needs to be provided to the SDK instance in order to be used for signing.
- `nonce` (default: current nonce of the account obtained via node API + 1)
- `nonce` (default: obtain nonce of the account via node API)
- The default behavior might cause problems if you perform many transactions in a short period of time.
- You might want to implement your own nonce management and provide the nonce "manually".
- 2 different strategies to use in order to determine the next nonce, See option `strategy` to learn more.
- `strategy` (default: `max`)
- The strategy to obtain next nonce for an account via node API
- If set to `max`, then the greatest nonce seen in the account or currently in the transaction pool is incremented with 1 and returned.
If the strategy is set to `continuity`, then transactions in the mempool are checked if there are gaps - missing nonces that prevent transactions with greater nonces to get included
- `ttl` (default: `0`)
- Should be set if you want the transaction to be only valid until a certain block height is reached.
- `fee` (default: calculated for each tx-type)
Expand Down
2 changes: 1 addition & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const Node = AsyncInit.compose({
}
})

const NODE_GE_VERSION = '6.0.0'
const NODE_GE_VERSION = '6.2.0'
const NODE_LT_VERSION = '7.0.0'

export default Node
10 changes: 2 additions & 8 deletions src/tx/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,8 @@ async function getAccountNonce (accountId, nonce) {
* @param {Object} params Object which contains all tx data
* @return {Object} { ttl, nonce, fee } Object with account nonce, absolute ttl and transaction fee
*/
async function prepareTxParams (txType, { senderId, nonce: n, ttl: t, fee: f, gas, absoluteTtl, vsn }) {
const account = await this.getAccount(senderId).catch(e => ({ nonce: 0 }))
// Is GA account
if (account.contractId) {
n = 0
} else {
n = n || (account.nonce + 1)
}
async function prepareTxParams (txType, { senderId, nonce: n, ttl: t, fee: f, gas, absoluteTtl, vsn, strategy }) {
n = n || (await this.api.getAccountNextNonce(senderId, { strategy }).catch(e => ({ nextNonce: 1 }))).nextNonce
const ttl = await calculateTtl.call(this, t, !absoluteTtl)
const fee = calculateFee(f, txType, { showWarning: this.showWarning, gas, params: R.merge(R.last(arguments), { nonce: n, ttl }), vsn })
return { fee, ttl, nonce: n }
Expand Down

0 comments on commit e40b046

Please sign in to comment.