In context of https://github.com/ledgerwatch/erigon/issues/5694, this PR
adds some fixes and improvement in the mining flow. Also, a relevant
change in txpool (present in erigon-lib) is made here:
https://github.com/ledgerwatch/erigon-lib/pull/737
#### Changes in triggering mining in `startMining()`
The mining module didn't honour the block time as a simple 3 second
timer and a notifier from txpool was used to trigger mining. This would
cause inconsistencies, at least with the bor consensus. Hence, a geth
like approach is used instead for simplicity. A new head channel
subscription is added in the `startMining()` loop which would notify the
addition of new block. Hence, this would make sure that the block time
is being honoured. Moreover, the fixed 3 second timer is replaced by the
`miner.recommit` value set using flags.
#### Changes in the arrangement of calls made post mining
When all the mining stages are completed, erigon writes all the data in
a cache. It then processes the block through all the stages as it would
process a block received from P2P. In this case, some of the stages
aren't really required. Like the block header and body download stage is
not required as the block was mined locally. Even execution stage is not
required as it already went through it in the mining stages.
Now, we encountered an issue where the chain was halted and kept mining
the same block again and again (liveness issue). The root cause is
because of an error in a stage of it's parent block. This stage turns
out to be the 4th stage which is "Block body download" stage. This stage
tries to download the block body from peers using the headers. As, we
mined this block locally we don't really need to download anything (or
process anything again). Hence, it reaches out to the cache which we
store for the block body.
Interestingly that cache turned out to be empty for some blocks. This
was because post mining, before adding block header and body to a cache,
we call the broadcast method which starts the staged sync. So,
technically it’s a bit uncertain at any stage if the block header and
body has been written or not.(see
[this](https://github.com/ledgerwatch/erigon/blob/devel/eth/backend.go#L553-L572)).
To achieve complete certainty, we rearranged the calls with the write to
cache being called first and broadcast next. This pretty much solves the
issue as now we’re sure that we’d always have a block body in the cache
when we reach the body download stage.
#### Misc changes
This PR also adds some logs in bor consensus.
BaseFee is required in AuRa headers when
[EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) is activated.
Also:
- Basic AuRa header verification
- Extract some common RLP methods
- Tiny log clean-up
This fixes the following panic for Gnosis Chain on the validator switch
at block 9186425:
```
panic: method 'getValidators' not found
goroutine 90 [running]:
github.com/ledgerwatch/erigon/consensus/aura.(*ValidatorSafeContract).getListSyscall(0x14000ed9358, 0xd40004bf620)
github.com/ledgerwatch/erigon/consensus/aura/validators.go:634 +0x258
github.com/ledgerwatch/erigon/consensus/aura.(*ValidatorSafeContract).epochSet(0x16?, 0x20?, 0x8c2c79, {0xd4002d77180, 0x25f, 0x25f}, 0x11400fac7ee8?)
github.com/ledgerwatch/erigon/consensus/aura/validators.go:453 +0xdc
github.com/ledgerwatch/erigon/consensus/aura.(*ValidatorContract).epochSet(0x140006ae980?, 0x38?, 0x6f9d00000000c28e?, {0xd4002d77180?, 0x108acc108?, 0x40?}, 0x14000618000?)
```
Previously "in-memory" MDBX instances for fork validation and mining
were created inside `os.TempDir()`. We should create them inside
Erigon's datadir so that the file permissions and the disk are the same
as for the main database.
Prerequisite: https://github.com/ledgerwatch/erigon-lib/pull/676.
* Test GnosisGenesisStateRoot
* Delete obsolete allocations
* SysCallContract shouldn't increase nonce of SystemAddress
* Max gas limit in SysCallContract
* Restore error swallowing for Bor
* General cleanup and fixes
* Cleaning part 2
* cleanup part 3
* Refactored staterunner to have a handler function
* Fixed linting
* Cleanup part 4
* Changed the initialization of the require package and updated .golangci.yml
* Fixed lint errors from golangci-lint
* deleted state_recon_1.go
* fixed build error
* Added ReadHeaderTimeouts for httpServer
* made changes to golangci
* Reset golangci.yml
* Consensus tests update 11
* Add GrayGlacier fork
* Wire currentRandom from tests into PREVRANDAO
* Serenity engine
* Support insertion of PoS blocks in MockSentry
* Introduce marshallTypedTransactionsAsRlpStrings arg into (*Block) RawBody()
* Revert "Introduce marshallTypedTransactionsAsRlpStrings arg into (*Block) RawBody()"
This reverts commit 903fca572be03c7de33318ce177a03a4be34927b.
* Post-merge fix
* Don't wait for Beacon Chain in tests
* Skip powToPosBlockRejection transition test
* ForkChoice in insertPoSBlocks
* Add withPosDownloader arg to MockWithEverything in order to fix TestPoSDownloader
* evm t8n tool to use ExecuteBlockEphemerally api (#4512)
* fix to set V, R, S in legacy transaction
* fix to dump post-execution alloc for evm t8n
* close tx in evm t8n
* populate current difficulty and gas used in output result
- update the ExecutionResult to include corresponding info (like
Difficulty/GasUsed)
* initial attempt at migrating 'evm t8n' to use ExecuteBlockEphemerally
* using ExecutionResult in ExecuteBlockEphemerally
* bypass validations and integrate with EphemeralExecResult
* fixing output of 'evm t8n'
- remaining bits are "stateRoot" in results.txt and "balance" field for one account in
alloc.txt (for testdata=1)
* get ExecuteBlockEphemerally to accept getTracer lambda
* fix build failure
* test cases for evm t8n
* more test cases for evm t8n
* fix stateRoot computation in evm t8n
* remove reward argument, as EBE itself takes care of it
* final cleanups for migration to using ExecuteBlockEphemerally
* change EBEforBSC to match EBE
* fix linter issues
* manually revert an unwanted diff
* avoid calculating ReceiptHash twice
* linter check
* minor correction
* remove unnecessary logic in EBEforBsc
* fix integration tests
* fix build
* Revert "evm t8n tool to use ExecuteBlockEphemerally api (#4512)"
This reverts commit db93d2ea37.
* Fix compilation
Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
* fix to set V, R, S in legacy transaction
* fix to dump post-execution alloc for evm t8n
* close tx in evm t8n
* populate current difficulty and gas used in output result
- update the ExecutionResult to include corresponding info (like
Difficulty/GasUsed)
* initial attempt at migrating 'evm t8n' to use ExecuteBlockEphemerally
* using ExecutionResult in ExecuteBlockEphemerally
* bypass validations and integrate with EphemeralExecResult
* fixing output of 'evm t8n'
- remaining bits are "stateRoot" in results.txt and "balance" field for one account in
alloc.txt (for testdata=1)
* get ExecuteBlockEphemerally to accept getTracer lambda
* fix build failure
* test cases for evm t8n
* more test cases for evm t8n
* fix stateRoot computation in evm t8n
* remove reward argument, as EBE itself takes care of it
* final cleanups for migration to using ExecuteBlockEphemerally
* change EBEforBSC to match EBE
* fix linter issues
* manually revert an unwanted diff
* avoid calculating ReceiptHash twice
* linter check
* minor correction
* remove unnecessary logic in EBEforBsc
* Configure consensus engine with context of stage if engine will do async work
* Change API to make setting of context for AsyncEngine multithreaded-safe
* Ensure lock gets inherited by reference
* Fix linter errors
* Print more info on ErrUnauthorizedSigner
* Authorize Clique even when wrapped into Serenity
* Revert "Print more info on ErrUnauthorizedSigner"
This reverts commit b0ab83ffbed45d6beeb8ba30a6ea576b2003ab26.
* Authorize Parlia even when wrapped into Serenity
* consensus/clique: remove race condition
* consensus/clique: fix one more signer data race
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
Co-authored-by: Harry Kalodner <harry.kalodner@gmail.com>
Co-authored-by: Gary Rong <garyrong0905@gmail.com>