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