github.com/ConsenSys/Quorum@v20.10.0+incompatible/consensus/istanbul/core/roundchange_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 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/istanbul" 25 "github.com/ethereum/go-ethereum/consensus/istanbul/validator" 26 ) 27 28 func TestRoundChangeSet(t *testing.T) { 29 vset := validator.NewSet(generateValidators(4), istanbul.RoundRobin) 30 rc := newRoundChangeSet(vset) 31 32 view := &istanbul.View{ 33 Sequence: big.NewInt(1), 34 Round: big.NewInt(1), 35 } 36 r := &istanbul.Subject{ 37 View: view, 38 Digest: common.Hash{}, 39 } 40 m, _ := Encode(r) 41 42 // Test Add() 43 // Add message from all validators 44 for i, v := range vset.List() { 45 msg := &message{ 46 Code: msgRoundChange, 47 Msg: m, 48 Address: v.Address(), 49 } 50 rc.Add(view.Round, msg) 51 if rc.roundChanges[view.Round.Uint64()].Size() != i+1 { 52 t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), i+1) 53 } 54 } 55 56 // Add message again from all validators, but the size should be the same 57 for _, v := range vset.List() { 58 msg := &message{ 59 Code: msgRoundChange, 60 Msg: m, 61 Address: v.Address(), 62 } 63 rc.Add(view.Round, msg) 64 if rc.roundChanges[view.Round.Uint64()].Size() != vset.Size() { 65 t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), vset.Size()) 66 } 67 } 68 69 // Test MaxRound() 70 for i := 0; i < 10; i++ { 71 maxRound := rc.MaxRound(i) 72 if i <= vset.Size() { 73 if maxRound == nil || maxRound.Cmp(view.Round) != 0 { 74 t.Errorf("max round mismatch: have %v, want %v", maxRound, view.Round) 75 } 76 } else if maxRound != nil { 77 t.Errorf("max round mismatch: have %v, want nil", maxRound) 78 } 79 } 80 81 // Test Clear() 82 for i := int64(0); i < 2; i++ { 83 rc.Clear(big.NewInt(i)) 84 if rc.roundChanges[view.Round.Uint64()].Size() != vset.Size() { 85 t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), vset.Size()) 86 } 87 } 88 rc.Clear(big.NewInt(2)) 89 if rc.roundChanges[view.Round.Uint64()] != nil { 90 t.Errorf("the change messages mismatch: have %v, want nil", rc.roundChanges[view.Round.Uint64()]) 91 } 92 }