eth: handle nil blocks and state when pending is not available (#1053)

* eth: handle nil blocks and state when pending is not available

* internal/ethapi: handle nil state and err in send condtional tx api
This commit is contained in:
Manav Darji 2023-10-20 15:22:05 +05:30 committed by GitHub
parent e1898d5aea
commit 260deb873d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 1 deletions

View file

@ -277,6 +277,9 @@ func (api *DebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
// both the pending block as well as the pending state from // both the pending block as well as the pending state from
// the miner and operate on those // the miner and operate on those
_, stateDb := api.eth.miner.Pending() _, stateDb := api.eth.miner.Pending()
if stateDb == nil {
return state.Dump{}, errors.New("pending state is not available")
}
return stateDb.RawDump(opts), nil return stateDb.RawDump(opts), nil
} }
@ -385,6 +388,9 @@ func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hex
// both the pending block as well as the pending state from // both the pending block as well as the pending state from
// the miner and operate on those // the miner and operate on those
_, stateDb = api.eth.miner.Pending() _, stateDb = api.eth.miner.Pending()
if stateDb == nil {
return state.IteratorDump{}, errors.New("pending state is not available")
}
} else { } else {
var header *types.Header var header *types.Header
if number == rpc.LatestBlockNumber { if number == rpc.LatestBlockNumber {

View file

@ -68,6 +68,9 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
// Pending block is only known by the miner // Pending block is only known by the miner
if number == rpc.PendingBlockNumber { if number == rpc.PendingBlockNumber {
block := b.eth.miner.PendingBlock() block := b.eth.miner.PendingBlock()
if block == nil {
return nil, errors.New("pending block is not available")
}
return block.Header(), nil return block.Header(), nil
} }
// Otherwise resolve and return the block // Otherwise resolve and return the block
@ -136,6 +139,9 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
// Pending block is only known by the miner // Pending block is only known by the miner
if number == rpc.PendingBlockNumber { if number == rpc.PendingBlockNumber {
block := b.eth.miner.PendingBlock() block := b.eth.miner.PendingBlock()
if block == nil {
return nil, errors.New("pending block is not available")
}
return block, nil return block, nil
} }
// Otherwise resolve and return the block // Otherwise resolve and return the block
@ -217,6 +223,9 @@ func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.B
// Pending state is only known by the miner // Pending state is only known by the miner
if number == rpc.PendingBlockNumber { if number == rpc.PendingBlockNumber {
block, state := b.eth.miner.Pending() block, state := b.eth.miner.Pending()
if block == nil || state == nil {
return nil, nil, errors.New("pending state is not available")
}
return state, block.Header(), nil return state, block.Header(), nil
} }
// Otherwise resolve the block number and return its state // Otherwise resolve the block number and return its state

View file

@ -328,6 +328,9 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*typ
// pendingLogs returns the logs matching the filter criteria within the pending block. // pendingLogs returns the logs matching the filter criteria within the pending block.
func (f *Filter) pendingLogs() ([]*types.Log, error) { func (f *Filter) pendingLogs() ([]*types.Log, error) {
block, receipts := f.sys.backend.PendingBlockAndReceipts() block, receipts := f.sys.backend.PendingBlockAndReceipts()
if block == nil || receipts == nil {
return nil, errors.New("pending block is not available")
}
if bloomFilter(block.Bloom(), f.addresses, f.topics) { if bloomFilter(block.Bloom(), f.addresses, f.topics) {
var unfiltered []*types.Log var unfiltered []*types.Log
for _, r := range receipts { for _, r := range receipts {

View file

@ -74,7 +74,11 @@ func (api *BorAPI) SendRawTransactionConditional(ctx context.Context, input hexu
} }
currentHeader := api.b.CurrentHeader() currentHeader := api.b.CurrentHeader()
currentState, _, _ := api.b.StateAndHeaderByNumber(ctx, rpc.BlockNumber(currentHeader.Number.Int64())) currentState, _, err := api.b.StateAndHeaderByNumber(ctx, rpc.BlockNumber(currentHeader.Number.Int64()))
if currentState == nil || err != nil {
return common.Hash{}, err
}
// check block number range // check block number range
if err := currentHeader.ValidateBlockNumberOptions4337(options.BlockNumberMin, options.BlockNumberMax); err != nil { if err := currentHeader.ValidateBlockNumberOptions4337(options.BlockNumberMin, options.BlockNumberMax); err != nil {