github.com/ConsenSys/Quorum@v20.10.0+incompatible/consensus/istanbul/core/message_set_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/rlp" 26 ) 27 28 func TestMessageSetWithPreprepare(t *testing.T) { 29 valSet := newTestValidatorSet(4) 30 31 ms := newMessageSet(valSet) 32 33 view := &istanbul.View{ 34 Round: new(big.Int), 35 Sequence: new(big.Int), 36 } 37 pp := &istanbul.Preprepare{ 38 View: view, 39 Proposal: makeBlock(1), 40 } 41 42 rawPP, err := rlp.EncodeToBytes(pp) 43 if err != nil { 44 t.Errorf("error mismatch: have %v, want nil", err) 45 } 46 msg := &message{ 47 Code: msgPreprepare, 48 Msg: rawPP, 49 Address: valSet.GetProposer().Address(), 50 } 51 52 err = ms.Add(msg) 53 if err != nil { 54 t.Errorf("error mismatch: have %v, want nil", err) 55 } 56 57 err = ms.Add(msg) 58 if err != nil { 59 t.Errorf("error mismatch: have %v, want nil", err) 60 } 61 62 if ms.Size() != 1 { 63 t.Errorf("the size of message set mismatch: have %v, want 1", ms.Size()) 64 } 65 } 66 67 func TestMessageSetWithSubject(t *testing.T) { 68 valSet := newTestValidatorSet(4) 69 70 ms := newMessageSet(valSet) 71 72 view := &istanbul.View{ 73 Round: new(big.Int), 74 Sequence: new(big.Int), 75 } 76 77 sub := &istanbul.Subject{ 78 View: view, 79 Digest: common.StringToHash("1234567890"), 80 } 81 82 rawSub, err := rlp.EncodeToBytes(sub) 83 if err != nil { 84 t.Errorf("error mismatch: have %v, want nil", err) 85 } 86 87 msg := &message{ 88 Code: msgPrepare, 89 Msg: rawSub, 90 Address: valSet.GetProposer().Address(), 91 } 92 93 err = ms.Add(msg) 94 if err != nil { 95 t.Errorf("error mismatch: have %v, want nil", err) 96 } 97 98 err = ms.Add(msg) 99 if err != nil { 100 t.Errorf("error mismatch: have %v, want nil", err) 101 } 102 103 if ms.Size() != 1 { 104 t.Errorf("the size of message set mismatch: have %v, want 1", ms.Size()) 105 } 106 }