From 8000ce3c936880088ecc601fb7db7af916a873bb Mon Sep 17 00:00:00 2001 From: AnilChinchawale Date: Tue, 26 Mar 2019 14:46:39 +0530 Subject: [PATCH] move m2 for unit test --- consensus/XDPoS/XDPoS.go | 2 +- consensus/XDPoS/XDPoS_test.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/consensus/XDPoS/XDPoS.go b/consensus/XDPoS/XDPoS.go index 3c03b02119..f386dce0f9 100644 --- a/consensus/XDPoS/XDPoS.go +++ b/consensus/XDPoS/XDPoS.go @@ -1149,7 +1149,7 @@ func getM1M2(masternodes []common.Address, validators []int64, currentHeader *ty if maxMNs > 0 { isForked := config.IsTIPRandomize(currentHeader.Number) if isForked { - moveM2 = (currentHeader.Number.Uint64() % config.XDPoS.Epoch) / uint64(maxMNs) + moveM2 = ((currentHeader.Number.Uint64() % config.XDPoS.Epoch) / uint64(maxMNs)) % uint64(maxMNs) } for i, m1 := range masternodes { m2Index := uint64(validators[i] % int64(maxMNs)) diff --git a/consensus/XDPoS/XDPoS_test.go b/consensus/XDPoS/XDPoS_test.go index ebb6b332d0..a99c90e8eb 100644 --- a/consensus/XDPoS/XDPoS_test.go +++ b/consensus/XDPoS/XDPoS_test.go @@ -1,9 +1,9 @@ package XDPoS import ( - "testing" - "math/big" "fmt" + "math/big" + "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -21,27 +21,30 @@ func TestGetM1M2FromCheckpointHeader(t *testing.T) { 1, 0, } - epoch := int64(900) + epoch := uint64(900) config := ¶ms.ChainConfig{ XDPoS: ¶ms.XDPoSConfig{ Epoch: uint64(epoch), }, } //try from block 900 to 909 - for i:=int64(0); i<9; i++ { + for i := uint64(3410001); i < 3411027; i++ { + currentNumber := int64(i) currentHeader := &types.Header{ - Number: big.NewInt(epoch+i), + Number: big.NewInt(currentNumber), } m1m2, moveM2, err := getM1M2(masternodes, validators, currentHeader, config) if err != nil { t.Error("can't get m1m2", "err", err) } fmt.Printf("block: %v, moveM2: %v\n", currentHeader.Number.Int64(), moveM2) - for _,k := range masternodes { + for _, k := range masternodes { fmt.Printf("m1: %v - m2: %v\n", k.Str(), m1m2[k].Str()) } - if moveM2 != uint64(i/3) { //3 = len(masternodes) - t.Error("wrong moveM2", "want", uint64(i/3), "have", moveM2) + maxMNs := len(masternodes) + testMoveM2 := ((uint64(currentNumber) % config.XDPoS.Epoch) / uint64(maxMNs)) % uint64(maxMNs) + if moveM2 != testMoveM2 { //3 = len(masternodes) + t.Error("wrong moveM2", "currentNumber", currentNumber, "want", testMoveM2, "have", moveM2) } } }