core, light: handle the nil return value of GetBlockNumber, fix #517

This commit is contained in:
JukLee0ira 2025-01-23 12:56:34 +08:00
parent bc2f68d70b
commit 197291443f
2 changed files with 10 additions and 2 deletions

View file

@ -891,7 +891,11 @@ func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
// GetBlockByHash retrieves a block from the database by hash, caching it if found.
func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block {
return bc.GetBlock(hash, *bc.hc.GetBlockNumber(hash))
number := bc.hc.GetBlockNumber(hash)
if number == nil {
return nil
}
return bc.GetBlock(hash, *number)
}
// GetBlockByNumber retrieves a block from the database by number, caching it

View file

@ -218,7 +218,11 @@ func (lc *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.Bod
if cached, ok := lc.bodyCache.Get(hash); ok && cached != nil {
return cached, nil
}
body, err := GetBody(ctx, lc.odr, hash, *lc.hc.GetBlockNumber(hash))
number := lc.hc.GetBlockNumber(hash)
if number == nil {
return nil, errors.New("unknown block")
}
body, err := GetBody(ctx, lc.odr, hash, *number)
if err != nil {
return nil, err
}