fix: Eliminate local transaction nonce cache. #4839
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WARNING: i'm not experienced in golang, and i don't know the bee internals either. this commit requires review by someone who understands the codebase.
Checklist
Description
Stop keeping the next nonce in our local state. Instead we always calculate it from the blockchain nonce and the local list of pending transactions.
Motivation and Context (Optional)
Fixes issue #4826 where the local nonce cache was out of sync with the blockchain for an unknown reason. Because of that no transactions got incorporated into the blockchain. This made it impossible to unstake or to participation in the reserve incentives, probably forever (until fixed).
It only became apparent when i tried to unstake my nodes at the v2.2.0 upgrade, and some of them got stuck. It also explains why i stopped seeing rewards on my nodes.
With this commit on top of v2.1.0 i've successfully unstaked a previously hung node.
And an eternal guiding principle: do not optimize any program until it works as specified. And the only exception is when it's so slow that it substantially hinders development.
A potential unaddressed issue
If a third party creates a transaction, then the bee's transactions may get stuck. There should be a check that the blockchain nonce is lower than the lowest nonce in our pending tx list. If not, then the nonce of the pending tx'es should be updated.