Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nonce value is same when using two instance for two chains #4500

Closed
1 task done
ujjwalguptaofficial opened this issue Oct 26, 2021 · 6 comments
Closed
1 task done
Assignees
Labels
1.x 1.0 related issues Bug Addressing a bug Investigate

Comments

@ujjwalguptaofficial
Copy link

ujjwalguptaofficial commented Oct 26, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

If i have two web3js instance with two different chains and i call getTransactionCount then value is being copied from first call.

const web3Parent = new Web3(new HDWalletProvider(privateKey, rpc.parent))
 const web3Child = new Web3(new HDWalletProvider(privateKey, rpc.child))



 const nonceChild = await web3Child.eth.getTransactionCount(from, 'pending');
  console.log('nonce', nonceChild); //11840

  const nonceParent = await web3Parent.eth.getTransactionCount(from, 'pending');
  console.log('nonce', nonceParent); // 11840

If i call getTransactionCount on parent first then value from parent will be returned in child.

Expected Behavior

value should be different on each chain.

i guess this is because of caching somewhere

Steps to Reproduce

 const web3Parent = new Web3(new HDWalletProvider(privateKey, rpc.parent))
  const web3Child = new Web3(new HDWalletProvider(privateKey, rpc.child))



  let nonce = await web3Child.eth.getTransactionCount(from, 'pending');
  console.log('nonce', nonce);

  nonce = await web3Parent.eth.getTransactionCount(from, 'pending');
  console.log('nonce', nonce);

Here is reproducible repo - https://github.com/ujjwalguptaofficial/web3-nonce-issue

Web3.js Version

1.6.0

Environment

  • Operating System:
  • Browser:
  • Node.js Version:
  • NPM Version:

Anything Else?

No response

@ujjwalguptaofficial ujjwalguptaofficial added the Bug Addressing a bug label Oct 26, 2021
@ujjwalguptaofficial
Copy link
Author

have created a reproducible repo - https://github.com/ujjwalguptaofficial/web3-nonce-issue

@jdevcs jdevcs added the 1.x 1.0 related issues label Nov 16, 2021
@spacesailor24
Copy link
Contributor

@ujjwalguptaofficial Just wanted to let you know that I'm starting work on this now, apologies that it's been pending for so long

Thank you for bringing this to our attention, and thank you especially for taking the time to make a reproducible repo!

@spacesailor24
Copy link
Contributor

In my testing, this bug only seems to appear when both requests are sent with pending block tag e.g.:

web3.eth.getTransactionCount(from, 'pending');
web3.eth.getTransactionCount(from2, 'pending');

Yields duplicate count from first request

web3.eth.getTransactionCount(from, 'pending');
web3.eth.getTransactionCount(from2, 'latest'); // This could be anything besides 'pending', also order of calls doesn't matter

Yields correct respective counts

@spacesailor24
Copy link
Contributor

This seems to be an issue with Web3 being instantiated with multiple times with @truffle/hdwallet-provider

Instantiating two instance like so:

const web3Parent = new Web3(rpc.parent)
const web3Child = new Web3(rpc.child)

Yields the correct result

@spacesailor24
Copy link
Contributor

@ujjwalguptaofficial I can't provide you more information on this (can't seem to find an issue in Truffle repo for it, even though there's a comment about it), but it seems setting the option shareNonce to false will fix this issue:

const web3Parent = new Web3(new HDWalletProvider(
        privateKey,
        rpc.parent,
        0, // addressIndex
        1, // numberOfAddresses
        false //shareNonce
))
const web3Child = new Web3(new HDWalletProvider(privateKey, rpc.child))

For readability sake, this can be written as:

const web3Parent = new Web3(new HDWalletProvider({
        privateKeys: [privateKey],
        providerOrUrl: rpc.parent,
        shareNonce: false
}))
const web3Child = new Web3(new HDWalletProvider(privateKey, rpc.child))

Both of the above will result in the correct nonces being returned

I'm going to close this issue as it's not related to Web3.js

@ujjwalguptaofficial
Copy link
Author

thanks @spacesailor24 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug Investigate
Projects
None yet
Development

No branches or pull requests

4 participants