From 103971bd9ad8b2870b51f602a8d2dfe31f25fba7 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 13 Jul 2024 12:02:18 +0800 Subject: [PATCH] all: check nil statedb --- core/blockchain.go | 2 ++ eth/api_backend.go | 20 +++++++++++++++----- internal/ethapi/api.go | 8 ++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 927e4628df1a..69f21fc14050 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2578,6 +2578,8 @@ func (bc *BlockChain) UpdateM1() error { if err != nil { return err } + } else if stateDB == nil { + return errors.New("nil stateDB in UpdateM1") } else { candidates = state.GetCandidates(stateDB) } diff --git a/eth/api_backend.go b/eth/api_backend.go index 161ee5ddfb4f..1a9ebc417091 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -20,7 +20,6 @@ import ( "context" "encoding/json" "errors" - "fmt" "math/big" "os" "path/filepath" @@ -443,7 +442,11 @@ func (b *EthApiBackend) GetVotersRewards(masternodeAddr common.Address) map[comm state, err := chain.StateAt(lastCheckpointBlock.Root()) if err != nil { - fmt.Println("ERROR Trying to getting state at", lastCheckpointNumber, " Error ", err) + log.Error("fail to get state in GetVotersRewards", "lastCheckpointNumber", lastCheckpointNumber, "err", err) + return nil + } + if state == nil { + log.Error("fail to get state in GetVotersRewards", "lastCheckpointNumber", lastCheckpointNumber) return nil } @@ -503,7 +506,11 @@ func (b *EthApiBackend) GetVotersCap(checkpoint *big.Int, masterAddr common.Addr state, err := chain.StateAt(checkpointBlock.Root()) if err != nil { - fmt.Println("ERROR Trying to getting state at", checkpoint, " Error ", err) + log.Error("fail to get state in GetVotersCap", "checkpoint", checkpoint, "err", err) + return nil + } + if state != nil { + log.Error("fail to get state in GetVotersCap", "checkpoint", checkpoint) return nil } @@ -533,9 +540,12 @@ func (b *EthApiBackend) GetEpochDuration() *big.Int { func (b *EthApiBackend) GetMasternodesCap(checkpoint uint64) map[common.Address]*big.Int { checkpointBlock := b.eth.blockchain.GetBlockByNumber(checkpoint) state, err := b.eth.blockchain.StateAt(checkpointBlock.Root()) - if err != nil { - fmt.Println("ERROR Trying to getting state at", checkpoint, " Error ", err) + log.Error("fail to get state in GetMasternodesCap", "checkpoint", checkpoint, "err", err) + return nil + } + if state == nil { + log.Error("fail to get state in GetMasternodesCap", "checkpoint", checkpoint) return nil } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 27e217e912bc..6a0b3d171093 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -897,6 +897,10 @@ func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAd result[fieldSuccess] = false return result, err } + if statedb == nil { + result[fieldSuccess] = false + return result, errors.New("nil statedb in GetCandidateStatus") + } candidatesAddresses := state.GetCandidates(statedb) candidates = make([]utils.Masternode, 0, len(candidatesAddresses)) for _, address := range candidatesAddresses { @@ -1052,6 +1056,10 @@ func (s *PublicBlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.Epoch result[fieldSuccess] = false return result, err } + if statedb == nil { + result[fieldSuccess] = false + return result, errors.New("nil statedb in GetCandidates") + } candidatesAddresses := state.GetCandidates(statedb) candidates = make([]utils.Masternode, 0, len(candidatesAddresses)) for _, address := range candidatesAddresses {