Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Conversation

@halfalicious
Copy link
Contributor

Fix #5863

The old check was restrictive - it prohibited the same account from sending multiple transactions before a new block was mined, because the check required that the new tx nonce match the nonce retrieved from the state in the most recent block in the local chain. The new check is more flexible - it requires that new tx nonces be greater than or equal to the required nonce.

These changes also add a test which verifies the new behavior.

The old check was restrictive - it prohibited the same account from sending multiple transactions before a new block was mined, because the
check required that the new tx nonce match the nonce retrieved from the (state in the) most recent block in the local chain. The new check is more flexible - it requires that
new tx nonces be greater than or equal to the required nonce.

These changes also add a new test which verifies the new behavior.
throw;
}
if (m_t.nonce() != nonceReq)
if (m_t.nonce() < nonceReq)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is going to break consensus, allowing the blocks with transactions in incorrect order, so this won't do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@winsvega do we have any blockchain tests with blocks containing transactions with invalid nonces?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gumb0 Ah you're correct, I had thought that the Executive was only invoked when submitting txs but it looks like it's invoked when executing txs as a part of importing txs into a block: Block::sync->Block::execute->State::execute->State::executeTransaction->Executive::initialize

...and when importing blocks into the chain:

BlockChain::sync -> BlockChain::import -> Block::enactOn -> Block::enact -> Block::execute -> State::execute -> State::executeTransaction -> Executive::initialize

We definitely don't want to change the nonce check in the Executive since that's consensus-critical code. Thanks for catching this.

Ignore invalid nonce errors at the initial check and let TQ correctly order transactions with future nonces.
@gumb0 gumb0 changed the title Update transaction nonce check in Executive Allow submitting several transaction from the same sender Dec 6, 2019
@gumb0
Copy link
Member

gumb0 commented Dec 6, 2019

A couple of unit tests still fails

BOOST_REQUIRE(!txHash.empty());

auto invalidNonce =
jsToU256(rpcClient->eth_getTransactionCount(toJS(senderAddress), "latest")) - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without mining previously sent transaction, getTransactionCount here returned 0, then u256 underflowed, and invalidNonce was max

I replaced it with just using 0 as invalid nonce

Copy link
Contributor Author

@halfalicious halfalicious Dec 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice catch! And can we make the invalidNonce const?

BOOST_REQUIRE(!signedTx["raw"].empty());

BOOST_CHECK_EQUAL(sendingRawShouldFail(signedTx["raw"].asString()),
"Same transaction already exists in the pending transaction queue.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really difficult now to hit this error, because we check the nonce now against pending block, and the first transaction is added to the pending block faster than we manage to send the second, duplicate one.
So sending the duplicate transaction still fails, but with "Invalid nonce" error.

I haven't found anything better to do, than remove this test.
(changing the expected error would work, but then it would be kind of the same test as eth_sendRawTransaction_errorInvalidNonce above)

@codecov-io
Copy link

Codecov Report

Merging #5864 into master will decrease coverage by 0.02%.
The diff coverage is 96.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5864      +/-   ##
==========================================
- Coverage   64.03%   64.01%   -0.03%     
==========================================
  Files         364      364              
  Lines       30970    30994      +24     
  Branches     3434     3435       +1     
==========================================
+ Hits        19832    19840       +8     
- Misses       9913     9925      +12     
- Partials     1225     1229       +4

@winsvega
Copy link
Contributor

winsvega commented Dec 7, 2019

With this PR I am able to import multiple transactions from same address for blockchain test generation.
Thanks!

@gumb0 gumb0 merged commit cb8580f into master Dec 9, 2019
@gumb0 gumb0 deleted the tx-nonce branch December 9, 2019 13:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid transaction nonce when import transaction of the same origin

5 participants