Skip to content

Commit

Permalink
all: check nil statedb
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Jul 22, 2024
1 parent 2b21070 commit 103971b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
20 changes: 15 additions & 5 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 103971b

Please sign in to comment.