Nonce calculated without counting pendig tx
Hi, I'm working with web3j v1.1.2 I created the Smart Contract class from the SolidityFunctionWrapperGenerator.
The problem is that if I call, before a block is mined, more than one time a function that generates a transaction, the nonce is always the same, so only the first transaction will be valid, all the other one will have the same nonce and will be rejected.
The geth client manages this situation calculating the nonce with getTransactionCount(pending)
In this way, even if you create more than one transaction before a block is mined, all transactions are valid because have an increasing nonce.
I suggest to change the code of the class: ManagedTransaction and the method getNonce
From:
protected BigInteger getNonce(String address) throws InterruptedException, ExecutionException {
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
address, DefaultBlockParameterName.LATEST).sendAsync().get();
return ethGetTransactionCount.getTransactionCount();
}
To:
protected BigInteger getNonce(String address) throws InterruptedException, ExecutionException {
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
address, DefaultBlockParameterName.PENDING).sendAsync().get();
return ethGetTransactionCount.getTransactionCount();
}
Issue #43 already covered this, and a change with a fast transaction manager has been submitted, which should support multiple transactions. Should be present in web3j 1.2.0
@ferOnti - thanks for testing so promptly.
This suggestion does make sense. From what @ferOnti stated it won't solve pending transaction issues, but it's probably to use the pending block instead of latest.
@fraspadafora - anything else to add re: your experience with it?
I'm open to PRs with the changes too :)
The problem is that if you start multiple transactions without waiting that the node accepts and inlcudes them in the pending transactions pool, you will have the problem @ferOnti showed in the log.
The solutions can be two:
- local nonce as you are implementing in the HQTransactionManager
- transaction creation must be sequential, every transaction has to wait the previous transaction is included in the pending transaction pool.
My concern about the first solution is that if I create a transaction from another client with the same account, the nonce in the HQTransactionManager will not be updated.
Fixed.