Skip to content

Commit

Permalink
Merge pull request #127 from XinFinOrg/fix-wrong-GetPreviousCheckpoin…
Browse files Browse the repository at this point in the history
…tFromEpoch-logic

fix: Fix the wrong logic in GetPreviousCheckpointFromEpoch where we w…
  • Loading branch information
wjrjerome authored Jan 15, 2024
2 parents c283020 + 5791c53 commit 5c031cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 5 additions & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.
}
// Otherwise resolve the block number and return its state
header, err := b.HeaderByNumber(ctx, blockNr)
if header == nil || err != nil {
if err != nil {
log.Error("[StateAndHeaderByNumber] Error while when loading header by blockNum", "blockNr", blockNr, "err", err)
return nil, nil, err
}
if header == nil {
return nil, nil, errors.New("header not found")
}
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
return stateDb, header, err
}
Expand Down
18 changes: 8 additions & 10 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ func (s *PublicBlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.Epoch
block, err = s.b.BlockByNumber(ctx, checkpointNumber)
if err != nil || block == nil { // || checkpointNumber == 0 {
result[fieldSuccess] = false
log.Error("[GetCandidates] Error or block not found", "checkpointNumber", checkpointNumber)
return result, err
}

Expand All @@ -898,6 +899,7 @@ func (s *PublicBlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.Epoch
statedb, _, err := s.b.StateAndHeaderByNumber(ctx, checkpointNumber)
if err != nil {
result[fieldSuccess] = false
log.Error("[GetCandidates] Error when load statedb", "checkpointNumber", checkpointNumber, "err", err)
return result, err
}
candidatesAddresses := state.GetCandidates(statedb)
Expand All @@ -910,7 +912,7 @@ func (s *PublicBlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.Epoch
}

if err != nil || len(candidates) == 0 {
log.Debug("Candidates list cannot be found", "len(candidates)", len(candidates), "err", err)
log.Error("Candidates list cannot be found", "len(candidates)", len(candidates), "err", err)
result[fieldSuccess] = false
return result, err
}
Expand Down Expand Up @@ -976,21 +978,17 @@ func (s *PublicBlockChainAPI) GetPreviousCheckpointFromEpoch(ctx context.Context
var checkpointNumber uint64

if engine, ok := s.b.GetEngine().(*XDPoS.XDPoS); ok {
currentCheckpointNumber, epochNumber, err := engine.GetCurrentEpochSwitchBlock(s.chainReader, s.b.CurrentBlock().Number())
if err != nil {
log.Error("[GetPreviousCheckpointFromEpoch] Error while trying to get current epoch switch block information", "Block", s.b.CurrentBlock(), "Error", err)
}
if epochNum == rpc.LatestEpochNumber {
currentCheckpointNumber, epochNumber, err := engine.GetCurrentEpochSwitchBlock(s.chainReader, s.b.CurrentBlock().Number())
if err != nil {
log.Error("[GetPreviousCheckpointFromEpoch] Error while trying to get current epoch switch block information", "Block", s.b.CurrentBlock(), "Error", err)
}
checkpointNumber = currentCheckpointNumber
epochNum = rpc.EpochNumber(epochNumber)
} else if epochNum < 2 {
checkpointNumber = 0
} else {
blockNumberBeforeCurrentEpochSwitch := currentCheckpointNumber - 1
checkpointNumber, _, err = engine.GetCurrentEpochSwitchBlock(s.chainReader, big.NewInt(int64(blockNumberBeforeCurrentEpochSwitch)))
if err != nil {
log.Error("[GetPreviousCheckpointFromEpoch] Error while trying to get last epoch switch block information", "Number", blockNumberBeforeCurrentEpochSwitch, "Error", err)
}
checkpointNumber = s.b.ChainConfig().XDPoS.Epoch * (uint64(epochNum) - 1)
}
return rpc.BlockNumber(checkpointNumber), epochNum
} else {
Expand Down

0 comments on commit 5c031cb

Please sign in to comment.