eth: fix crash on querying finalized block (#27162)

eth: fix crash on querying nil finalized block
This commit is contained in:
Sina Mahmoodi 2023-04-25 15:15:43 +02:00 committed by GitHub
parent 2f98dd3838
commit b1113aa07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -134,6 +134,9 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
return nil, errors.New("'finalized' tag not supported on pre-merge network") return nil, errors.New("'finalized' tag not supported on pre-merge network")
} }
header := b.eth.blockchain.CurrentFinalBlock() header := b.eth.blockchain.CurrentFinalBlock()
if header == nil {
return nil, errors.New("finalized block not found")
}
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
} }
if number == rpc.SafeBlockNumber { if number == rpc.SafeBlockNumber {
@ -141,6 +144,9 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
return nil, errors.New("'safe' tag not supported on pre-merge network") return nil, errors.New("'safe' tag not supported on pre-merge network")
} }
header := b.eth.blockchain.CurrentSafeBlock() header := b.eth.blockchain.CurrentSafeBlock()
if header == nil {
return nil, errors.New("safe block not found")
}
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
} }
return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil