github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/consensus/istanbul/ibft/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/kisexp/xdchain/common" 27 "github.com/kisexp/xdchain/consensus/istanbul" 28 ibfttypes "github.com/kisexp/xdchain/consensus/istanbul/ibft/types" 29 "github.com/kisexp/xdchain/core/types" 30 elog "github.com/kisexp/xdchain/log" 31 ) 32 33 func makeBlock(number int64) *types.Block { 34 header := &types.Header{ 35 Difficulty: big.NewInt(0), 36 Number: big.NewInt(number), 37 GasLimit: 0, 38 GasUsed: 0, 39 Time: 0, 40 } 41 block := &types.Block{} 42 return block.WithSeal(header) 43 } 44 45 func newTestProposal() istanbul.Proposal { 46 return makeBlock(1) 47 } 48 49 func TestNewRequest(t *testing.T) { 50 testLogger.SetHandler(elog.StdoutHandler) 51 52 N := uint64(4) 53 F := uint64(1) 54 55 sys := NewTestSystemWithBackend(N, F) 56 57 close := sys.Run(true) 58 defer close() 59 60 request1 := makeBlock(1) 61 sys.backends[0].NewRequest(request1) 62 63 <-time.After(1 * time.Second) 64 65 request2 := makeBlock(2) 66 sys.backends[0].NewRequest(request2) 67 68 <-time.After(1 * time.Second) 69 70 for _, backend := range sys.backends { 71 if len(backend.committedMsgs) != 2 { 72 t.Fatalf("the number of executed requests mismatch: have %v, want 2", len(backend.committedMsgs)) 73 } 74 if !reflect.DeepEqual(request1.Number(), backend.committedMsgs[0].commitProposal.Number()) { 75 t.Errorf("the number of requests mismatch: have %v, want %v", request1.Number(), backend.committedMsgs[0].commitProposal.Number()) 76 } 77 if !reflect.DeepEqual(request2.Number(), backend.committedMsgs[1].commitProposal.Number()) { 78 t.Errorf("the number of requests mismatch: have %v, want %v", request2.Number(), backend.committedMsgs[1].commitProposal.Number()) 79 } 80 } 81 } 82 83 func TestQuorumSize(t *testing.T) { 84 N := uint64(4) 85 F := uint64(1) 86 87 sys := NewTestSystemWithBackend(N, F) 88 backend := sys.backends[0] 89 c := backend.engine 90 91 valSet := c.valSet 92 for i := 1; i <= 1000; i++ { 93 valSet.AddValidator(common.StringToAddress(fmt.Sprint(i))) 94 if 2*c.QuorumSize() <= (valSet.Size()+valSet.F()) || 2*c.QuorumSize() > (valSet.Size()+valSet.F()+2) { 95 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()) 96 } 97 } 98 } 99 100 func TestNilCommittedSealWithEmptyProposal(t *testing.T) { 101 N := uint64(4) 102 F := uint64(1) 103 104 sys := NewTestSystemWithBackend(N, F) 105 backend := sys.backends[0] 106 c := backend.engine 107 // Set the current round state with an empty proposal 108 preprepare := &istanbul.Preprepare{ 109 View: c.currentView(), 110 } 111 c.current.SetPreprepare(preprepare) 112 113 // Create a Commit message 114 subject := &istanbul.Subject{ 115 View: c.currentView(), 116 Digest: common.StringToHash("1234567890"), 117 } 118 subjectPayload, err := ibfttypes.Encode(subject) 119 if err != nil { 120 t.Errorf("problem with encoding: %v", err) 121 } 122 msg := &ibfttypes.Message{ 123 Code: ibfttypes.MsgCommit, 124 Msg: subjectPayload, 125 } 126 127 c.finalizeMessage(msg) 128 129 if msg.CommittedSeal != nil { 130 t.Errorf("Unexpected committed seal: %s", msg.CommittedSeal) 131 } 132 }