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:
Jared Wasinger 2023-08-25 00:45:13 +07:00
parent 998cec05af
commit d853c71504

View file

@ -146,7 +146,8 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal) error {
// Reset to CurrentBlock in case of the chain was rewound
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{
@ -171,13 +172,12 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal) error {
if payload.Number%devEpochLength == 0 {
finalizedHash = payload.BlockHash
} else {
number := (payload.Number - 1) / devEpochLength * devEpochLength
if block := c.eth.BlockChain().GetBlockByNumber(number); block == nil {
if fh := c.finalizedBlockHash(payload.Number); fh == nil {
// 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
} 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
func (c *SimulatedBeacon) setCurrentState(headHash, finalizedHash common.Hash) {
c.curForkchoiceState = engine.ForkchoiceStateV1{