-
Notifications
You must be signed in to change notification settings - Fork 207
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
Remove unnecessary statedb copies and move the prepare inside Finalise #1673
Conversation
…e(). This call needs to be done during both block construction and block verification. It was previously only done during verification, but this didn't cause problems because of a statedb .Copy() during consutrction, in between transaction processing and the Finalize(). This commit moves it to the right place, ensuring it's done in both scenarios without relying on unrelated code.
The one in block.go is superfluous now that the `Prepare()` has been moved into `Finalize()`. The one in backend.go was always superfluous, as the statedb instance there is a fresh one obtained a few lines above.
0cf5c99
to
515f3d7
Compare
This reduces duplication and ensures the same logic is used in all cases.
515f3d7
to
dcce39f
Compare
Codecov Report
@@ Coverage Diff @@
## master #1673 +/- ##
==========================================
+ Coverage 56.06% 56.07% +0.01%
==========================================
Files 605 606 +1
Lines 80269 80261 -8
==========================================
+ Hits 45001 45010 +9
+ Misses 31744 31726 -18
- Partials 3524 3525 +1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
… Finalise (#1673)" This reverts commit 8b8e717. The combination of v1.10.7 and the reverted commit caused an issue in the e2e governance test. The test failed when it was looking for a specific event that is produced during the "block transaction" performed at the end of the block. I'm not sure whey the update to v1.10.7 caused this change to no longer work.
Background
Logs emitted by the EVM are recorded using
StateDB
'sAddLog()
method, which records on the log the transaction hash, transaction index, and block hash, based on the fieldsthash
,txindex
, andbhash
. These fields are set usingStateDB
'sPrepare()
method, which must be called before processing EVM calls. For example, it is called before processing a transaction, both during block construction and during block verification.In Celo, we have some contract calls done outside of any transaction, as for example the gas price minimum update, epoch rewards, and validator elections. Any logs from these calls are added to a special "block receipt" (see #1628). The way we look up these logs is using
state.GetLogs(common.Hash{})
(i.e. looking up logs with zero as their transaction hash). Therefore, before making these EVM calls we need to ensure thatstate.thash
is set to zero using a call toPrepare()
.We do this during block verification:
celo-blockchain/core/state_processor.go
Lines 95 to 96 in 765e371
But we don't do it during block construction:
celo-blockchain/consensus/istanbul/backend/engine.go
Lines 502 to 504 in 765e371
This has not led to problems because was a
StateDB.Copy()
call in between transaction processing and callingFinalizeAndAssemble()
, and thethash
(and related) fields are not copied in.Copy()
, so instead the copy hasthash
set to the zero value, which is what thePrepare()
should be setting it to. This call to.Copy()
was originally there from upstream for unrelated reasons, and kept during the miner refactor (#1545) as it seemed to be necessary. Without it,thash
would be the hash of the block's last transaction (if there were any), and as a result the system call logs would get assigned this incorrect hash is their index, which would lead to their not being found bystate.GetLogs(common.Hash{})
and to no block receipt being added. In contrast, during block verification it would be added, leading to a mismatch so that the block is judged invalid.Description
This PR moves the pre-
Finalize()
Prepare()
calls to insideFinalize()
. This is desirable for two reasons:Prepare()
preparesstatedb
for the EVM calls which follow, and so it belongs together with the code responsible for those calls.Other changes
state.Copy()
call which was previously making up for the missingPrepare()
call. It is now superfluous.state.Copy()
call (can be seen to be superfluous by the fact that it's called right below where the statedb instance is first initialized, and so we knows it's a fresh instance that no one else has a reference to. This call is atcelo-blockchain/consensus/istanbul/backend/backend.go
Lines 600 to 601 in 0390b45
Tested
.Copy()
no longer happensBAD BLOCK
errors.