github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/consensus/podc/core/roundchange_test.go (about) 1 // Copyright 2017 AMIS Technologies 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/consensus/podc" 25 "github.com/ethereum/go-ethereum/consensus/podc/validator" 26 ) 27 28 func TestRoundChangeSet(t *testing.T) { 29 vset := validator.NewSet(generateValidators(4), podc.RoundRobin) 30 rc := newRoundChangeSet(vset) 31 32 view := &podc.View{ 33 Sequence: big.NewInt(1), 34 Round: big.NewInt(1), 35 } 36 r := &roundChange{ 37 Round: view.Round, 38 Sequence: view.Sequence, 39 Digest: common.Hash{}, 40 } 41 m, _ := Encode(r) 42 43 // Test Add() 44 // Add message from all validators 45 for i, v := range vset.List() { 46 msg := &message{ 47 Code: msgRoundChange, 48 Msg: m, 49 Address: v.Address(), 50 } 51 rc.Add(view.Round, msg) 52 if rc.roundChanges[view.Round.Uint64()].Size() != i+1 { 53 t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), i+1) 54 } 55 } 56 57 // Add message again from all validators, but the size should be the same 58 for _, v := range vset.List() { 59 msg := &message{ 60 Code: msgRoundChange, 61 Msg: m, 62 Address: v.Address(), 63 } 64 rc.Add(view.Round, msg) 65 if rc.roundChanges[view.Round.Uint64()].Size() != vset.Size() { 66 t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), vset.Size()) 67 } 68 } 69 70 // Test MaxRound() 71 for i := 0; i < 10; i++ { 72 maxRound := rc.MaxRound(i) 73 if i <= vset.Size() { 74 if maxRound == nil || maxRound.Cmp(view.Round) != 0 { 75 t.Errorf("max round mismatch: have %v, want %v", maxRound, view.Round) 76 } 77 } else if maxRound != nil { 78 t.Errorf("max round mismatch: have %v, want nil", maxRound) 79 } 80 } 81 82 // Test Clear() 83 for i := int64(0); i < 2; i++ { 84 rc.Clear(big.NewInt(i)) 85 if rc.roundChanges[view.Round.Uint64()].Size() != vset.Size() { 86 t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), vset.Size()) 87 } 88 } 89 rc.Clear(big.NewInt(2)) 90 if rc.roundChanges[view.Round.Uint64()] != nil { 91 t.Errorf("the change messages mismatch: have %v, want nil", rc.roundChanges[view.Round.Uint64()]) 92 } 93 }