mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
set finalized block hash in SimulatedBeacon forkchoice state upon chain rewind. move calculation of finalized block hash into its own function
This commit is contained in:
parent
998cec05af
commit
d853c71504
1 changed files with 22 additions and 5 deletions
|
|
@ -146,7 +146,8 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal) error {
|
||||||
|
|
||||||
// Reset to CurrentBlock in case of the chain was rewound
|
// Reset to CurrentBlock in case of the chain was rewound
|
||||||
if headerHash := c.eth.BlockChain().CurrentBlock().Hash(); c.curForkchoiceState.HeadBlockHash != headerHash {
|
if headerHash := c.eth.BlockChain().CurrentBlock().Hash(); c.curForkchoiceState.HeadBlockHash != headerHash {
|
||||||
c.setCurrentState(headerHash, headerHash)
|
finalizedHash := c.finalizedBlockHash(c.eth.BlockChain().CurrentBlock().Number.Uint64())
|
||||||
|
c.setCurrentState(headerHash, *finalizedHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
fcResponse, err := c.engineAPI.ForkchoiceUpdatedV2(c.curForkchoiceState, &engine.PayloadAttributes{
|
fcResponse, err := c.engineAPI.ForkchoiceUpdatedV2(c.curForkchoiceState, &engine.PayloadAttributes{
|
||||||
|
|
@ -171,13 +172,12 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal) error {
|
||||||
if payload.Number%devEpochLength == 0 {
|
if payload.Number%devEpochLength == 0 {
|
||||||
finalizedHash = payload.BlockHash
|
finalizedHash = payload.BlockHash
|
||||||
} else {
|
} else {
|
||||||
number := (payload.Number - 1) / devEpochLength * devEpochLength
|
if fh := c.finalizedBlockHash(payload.Number); fh == nil {
|
||||||
if block := c.eth.BlockChain().GetBlockByNumber(number); block == nil {
|
|
||||||
// Chain rewound after the ForkchoiceUpdate
|
// Chain rewound after the ForkchoiceUpdate
|
||||||
log.Warn("Finalized block not found, skipping", "block", number)
|
log.Warn("Finalized block not found, skipping", "block", payload.Number)
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
finalizedHash = block.Hash()
|
finalizedHash = *fh
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -240,6 +240,23 @@ func (c *SimulatedBeacon) loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// finalizedBlockHash returns the block hash of the finalized block corresponding to the given number
|
||||||
|
// or nil if doesn't exist in the chain.
|
||||||
|
func (c *SimulatedBeacon) finalizedBlockHash(number uint64) *common.Hash {
|
||||||
|
var finalizedNumber uint64
|
||||||
|
if number%devEpochLength == 0 {
|
||||||
|
finalizedNumber = number
|
||||||
|
} else {
|
||||||
|
finalizedNumber = (number - 1) / devEpochLength * devEpochLength
|
||||||
|
}
|
||||||
|
|
||||||
|
if finalizedBlock := c.eth.BlockChain().GetBlockByNumber(finalizedNumber); finalizedBlock != nil {
|
||||||
|
fh := finalizedBlock.Hash()
|
||||||
|
return &fh
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// setCurrentState sets the current forkchoice state
|
// setCurrentState sets the current forkchoice state
|
||||||
func (c *SimulatedBeacon) setCurrentState(headHash, finalizedHash common.Hash) {
|
func (c *SimulatedBeacon) setCurrentState(headHash, finalizedHash common.Hash) {
|
||||||
c.curForkchoiceState = engine.ForkchoiceStateV1{
|
c.curForkchoiceState = engine.ForkchoiceStateV1{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue