github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/consensus/istanbul/core/core_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 "github.com/ethereum/go-ethereum/common" 21 "math/big" 22 "reflect" 23 "testing" 24 "time" 25 26 "github.com/ethereum/go-ethereum/consensus/istanbul" 27 "github.com/ethereum/go-ethereum/core/types" 28 elog "github.com/ethereum/go-ethereum/log" 29 ) 30 31 func makeBlock(number int64) *types.Block { 32 header := &types.Header{ 33 Difficulty: big.NewInt(0), 34 Number: big.NewInt(number), 35 GasLimit: 0, 36 GasUsed: 0, 37 Time: big.NewInt(0), 38 } 39 block := &types.Block{} 40 return block.WithSeal(header) 41 } 42 43 func newTestProposal() istanbul.Proposal { 44 return makeBlock(1) 45 } 46 47 func TestNewRequest(t *testing.T) { 48 testLogger.SetHandler(elog.StdoutHandler) 49 50 N := uint64(4) 51 F := uint64(1) 52 53 sys := NewTestSystemWithBackend(N, F) 54 55 close := sys.Run(true) 56 defer close() 57 58 request1 := makeBlock(1) 59 sys.backends[0].NewRequest(request1) 60 61 select { 62 case <-time.After(1 * time.Second): 63 } 64 65 request2 := makeBlock(2) 66 sys.backends[0].NewRequest(request2) 67 68 select { 69 case <-time.After(1 * time.Second): 70 } 71 72 for _, backend := range sys.backends { 73 if len(backend.committedMsgs) != 2 { 74 t.Errorf("the number of executed requests mismatch: have %v, want 2", len(backend.committedMsgs)) 75 } 76 if !reflect.DeepEqual(request1.Number(), backend.committedMsgs[0].commitProposal.Number()) { 77 t.Errorf("the number of requests mismatch: have %v, want %v", request1.Number(), backend.committedMsgs[0].commitProposal.Number()) 78 } 79 if !reflect.DeepEqual(request2.Number(), backend.committedMsgs[1].commitProposal.Number()) { 80 t.Errorf("the number of requests mismatch: have %v, want %v", request2.Number(), backend.committedMsgs[1].commitProposal.Number()) 81 } 82 } 83 } 84 85 func TestQuorumSize(t *testing.T) { 86 N := uint64(4) 87 F := uint64(1) 88 89 sys := NewTestSystemWithBackend(N, F) 90 backend := sys.backends[0] 91 c := backend.engine.(*core) 92 93 valSet := c.valSet 94 for i := 1; i <= 1000; i++ { 95 valSet.AddValidator(common.StringToAddress(string(i))) 96 if 2*c.QuorumSize() <= (valSet.Size()+valSet.F()) || 2*c.QuorumSize() > (valSet.Size()+valSet.F()+2) { 97 t.Errorf("quorumSize constraint failed, expected value (2*QuorumSize > Size+F && 2*QuorumSize <= Size+F+2) to be:%v, got: %v, for size: %v", true, false, valSet.Size()) 98 } 99 } 100 }