Skip to content

Commit

Permalink
Fix getCandidateStatus API (#663)
Browse files Browse the repository at this point in the history
* Fix getCandidateStatus API

* sync vote message and fix test

* update function name

---------

Co-authored-by: Liam Lai <liam@home>
  • Loading branch information
2 people authored and wanwiset25 committed Nov 13, 2024
1 parent 032f96d commit ef5975b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
15 changes: 15 additions & 0 deletions consensus/tests/engine_v2_tests/forensics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
)

func TestProcessQcShallSetForensicsCommittedQc(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")

blockchain, _, currentBlock, signer, signFn, _ := PrepareXDCTestBlockChainForV2Engine(t, 905, params.TestXDPoSMockChainConfig, nil)
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2

Expand Down Expand Up @@ -92,6 +94,8 @@ func TestProcessQcShallSetForensicsCommittedQc(t *testing.T) {
}

func TestSetCommittedQCsInOrder(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")

blockchain, _, currentBlock, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 905, params.TestXDPoSMockChainConfig, nil)
forensics := blockchain.Engine().(*XDPoS.XDPoS).EngineV2.GetForensicsFaker()

Expand All @@ -118,6 +122,8 @@ func TestSetCommittedQCsInOrder(t *testing.T) {

// Happty path
func TestForensicsMonitoring(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")

blockchain, _, currentBlock, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 915, params.TestXDPoSMockChainConfig, nil)
forensics := blockchain.Engine().(*XDPoS.XDPoS).EngineV2.GetForensicsFaker()
var decodedCurrentblockExtraField types.ExtraFields_v2
Expand All @@ -140,6 +146,7 @@ func TestForensicsMonitoring(t *testing.T) {
}

func TestForensicsMonitoringNotOnSameChainButHaveSameRoundQC(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")
var numOfForks = new(int)
*numOfForks = 10
var forkRoundDifference = new(int)
Expand Down Expand Up @@ -199,6 +206,8 @@ func TestForensicsMonitoringNotOnSameChainButHaveSameRoundQC(t *testing.T) {
}

func TestForensicsMonitoringNotOnSameChainDoNotHaveSameRoundQC(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")

var numOfForks = new(int)
*numOfForks = 10
var forkRoundDifference = new(int)
Expand Down Expand Up @@ -260,6 +269,8 @@ func TestForensicsMonitoringNotOnSameChainDoNotHaveSameRoundQC(t *testing.T) {

// "prone to attack" test where the "across epoch" field is true
func TestForensicsAcrossEpoch(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")

var numOfForks = new(int)
*numOfForks = 10
var forkRoundDifference = new(int)
Expand Down Expand Up @@ -322,6 +333,8 @@ func TestForensicsAcrossEpoch(t *testing.T) {
}

func TestVoteEquivocationSameRound(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")

var numOfForks = new(int)
*numOfForks = 1
blockchain, _, currentBlock, signer, signFn, currentForkBlock := PrepareXDCTestBlockChainForV2Engine(t, 901, params.TestXDPoSMockChainConfig, &ForkedBlockOptions{numOfForkedBlocks: numOfForks})
Expand Down Expand Up @@ -388,6 +401,8 @@ func TestVoteEquivocationSameRound(t *testing.T) {
}

func TestVoteEquivocationDifferentRound(t *testing.T) {
t.Skip("Skipping this test for now as we disable forensics")

var numOfForks = new(int)
*numOfForks = 10
var forkRoundDifference = new(int)
Expand Down
4 changes: 2 additions & 2 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ func (b *EthApiBackend) GetVotersRewards(masternodeAddr common.Address) map[comm
// calculate for 2 epochs ago
currentCheckpointNumber, _, err := engine.GetCurrentEpochSwitchBlock(chain, block.Number())
if err != nil {
log.Error("[GetVotersRewards] Fail to get GetCurrentEpochSwitchBlock for current checkpoint block", "block", block)
log.Error("[GetVotersRewards] Fail to get GetCurrentEpochSwitchBlock for current checkpoint block", "block", block.Number(), "err", err)
}
lastCheckpointNumber, _, err := engine.GetCurrentEpochSwitchBlock(chain, big.NewInt(int64(currentCheckpointNumber-1)))
if err != nil {
log.Error("[GetVotersRewards] Fail to get GetCurrentEpochSwitchBlock for last checkpoint block", "block", block)
log.Error("[GetVotersRewards] Fail to get GetCurrentEpochSwitchBlock for last checkpoint block", "block", block.Number(), "err", err)
}

lastCheckpointBlock := chain.GetBlockByNumber(lastCheckpointNumber)
Expand Down
3 changes: 1 addition & 2 deletions eth/bft/bft_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ func (b *Bfter) Vote(peer string, vote *types.Vote) error {
return err
}

b.broadcastCh <- vote

if verified {
b.broadcastCh <- vote
err = b.consensus.voteHandler(b.blockChainReader, vote)
if err != nil {
if _, ok := err.(*utils.ErrIncomingMessageRoundTooFarFromCurrentRound); ok {
Expand Down
6 changes: 3 additions & 3 deletions eth/bft/bft_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestBoardcastButNotProcessDisqualifiedVotes(t *testing.T) {
tester.bfter.Vote(peerID, &vote)

time.Sleep(50 * time.Millisecond)
if int(handlerCounter) != targetVotes || int(broadcastCounter) != 1 {
if int(handlerCounter) != targetVotes || int(broadcastCounter) != 0 {
t.Fatalf("count mismatch: have %v on handler, %v on broadcast, want %v", handlerCounter, broadcastCounter, targetVotes)
}
}
Expand All @@ -171,7 +171,7 @@ func TestBoardcastButNotProcessDisqualifiedTimeout(t *testing.T) {
tester.bfter.Timeout(peerID, &timeout)

time.Sleep(50 * time.Millisecond)
if int(handlerCounter) != targetTimeout || int(broadcastCounter) != 1 {
if int(handlerCounter) != targetTimeout || int(broadcastCounter) != 0 {
t.Fatalf("count mismatch: have %v on handler, %v on broadcast, want %v", handlerCounter, broadcastCounter, targetTimeout)
}
}
Expand All @@ -198,7 +198,7 @@ func TestBoardcastButNotProcessDisqualifiedSyncInfo(t *testing.T) {
tester.bfter.SyncInfo(peerID, &syncInfo)

time.Sleep(50 * time.Millisecond)
if int(handlerCounter) != targetSyncInfo || int(broadcastCounter) != 1 {
if int(handlerCounter) != targetSyncInfo || int(broadcastCounter) != 0 {
t.Fatalf("count mismatch: have %v on handler, %v on broadcast, want %v", handlerCounter, broadcastCounter, targetSyncInfo)
}
}
Expand Down
28 changes: 17 additions & 11 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAd
epochConfig := s.b.ChainConfig().XDPoS.Epoch

// checkpoint block
checkpointNumber, epochNumber = s.GetPreviousCheckpointFromEpoch(ctx, epoch)
checkpointNumber, epochNumber = s.GetCheckpointFromEpoch(ctx, epoch)
result[fieldEpoch] = epochNumber.Int64()

block, err = s.b.BlockByNumber(ctx, checkpointNumber)
Expand Down Expand Up @@ -1036,7 +1036,7 @@ func (s *PublicBlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.Epoch
}
epochConfig := s.b.ChainConfig().XDPoS.Epoch

checkpointNumber, epochNumber = s.GetPreviousCheckpointFromEpoch(ctx, epoch)
checkpointNumber, epochNumber = s.GetCheckpointFromEpoch(ctx, epoch)
result[fieldEpoch] = epochNumber.Int64()

block, err = s.b.BlockByNumber(ctx, checkpointNumber)
Expand Down Expand Up @@ -1184,25 +1184,31 @@ func (s *PublicBlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.Epoch
return result, nil
}

// GetPreviousCheckpointFromEpoch returns header of the previous checkpoint
func (s *PublicBlockChainAPI) GetPreviousCheckpointFromEpoch(ctx context.Context, epochNum rpc.EpochNumber) (rpc.BlockNumber, rpc.EpochNumber) {
// GetCheckpointFromEpoch returns header of the previous checkpoint
func (s *PublicBlockChainAPI) GetCheckpointFromEpoch(ctx context.Context, epochNum rpc.EpochNumber) (rpc.BlockNumber, rpc.EpochNumber) {
var checkpointNumber uint64
epoch := s.b.ChainConfig().XDPoS.Epoch

if epochNum == rpc.LatestEpochNumber {
blockNumer := s.b.CurrentBlock().Number().Uint64()
diff := blockNumer % epoch
// checkpoint number
checkpointNumber = blockNumer - diff
epochNum = rpc.EpochNumber(checkpointNumber / epoch)
if diff > 0 {
epochNum += 1
blockNumer := s.b.CurrentBlock().Number()
if engine, ok := s.b.GetEngine().(*XDPoS.XDPoS); ok {
var err error
var currentEpoch uint64
checkpointNumber, currentEpoch, err = engine.GetCurrentEpochSwitchBlock(s.chainReader, blockNumer)
if err != nil {
log.Error("[GetCheckpointFromEpoch] Fail to get GetCurrentEpochSwitchBlock for current checkpoint block", "block", blockNumer, "err", err)
return 0, epochNum
}

epochNum = rpc.EpochNumber(currentEpoch)
}
} else if epochNum < 2 {
checkpointNumber = 0
} else {
// TODO this checkpointNumber needs to be recalculated for v2 blocks
checkpointNumber = epoch * (uint64(epochNum) - 1)
}

return rpc.BlockNumber(checkpointNumber), epochNum
}

Expand Down

0 comments on commit ef5975b

Please sign in to comment.