github.com/Night-mk/quorum@v21.1.0+incompatible/consensus/istanbul/core/request_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 "reflect" 22 "sync" 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/event" 29 "github.com/ethereum/go-ethereum/log" 30 "gopkg.in/karalabe/cookiejar.v2/collections/prque" 31 ) 32 33 func TestCheckRequestMsg(t *testing.T) { 34 c := &core{ 35 state: StateAcceptRequest, 36 current: newRoundState(&istanbul.View{ 37 Sequence: big.NewInt(1), 38 Round: big.NewInt(0), 39 }, newTestValidatorSet(4), common.Hash{}, nil, nil, nil), 40 } 41 42 // invalid request 43 err := c.checkRequestMsg(nil) 44 if err != errInvalidMessage { 45 t.Errorf("error mismatch: have %v, want %v", err, errInvalidMessage) 46 } 47 r := &istanbul.Request{ 48 Proposal: nil, 49 } 50 err = c.checkRequestMsg(r) 51 if err != errInvalidMessage { 52 t.Errorf("error mismatch: have %v, want %v", err, errInvalidMessage) 53 } 54 55 // old request 56 r = &istanbul.Request{ 57 Proposal: makeBlock(0), 58 } 59 err = c.checkRequestMsg(r) 60 if err != errOldMessage { 61 t.Errorf("error mismatch: have %v, want %v", err, errOldMessage) 62 } 63 64 // future request 65 r = &istanbul.Request{ 66 Proposal: makeBlock(2), 67 } 68 err = c.checkRequestMsg(r) 69 if err != errFutureMessage { 70 t.Errorf("error mismatch: have %v, want %v", err, errFutureMessage) 71 } 72 73 // current request 74 r = &istanbul.Request{ 75 Proposal: makeBlock(1), 76 } 77 err = c.checkRequestMsg(r) 78 if err != nil { 79 t.Errorf("error mismatch: have %v, want nil", err) 80 } 81 } 82 83 func TestStoreRequestMsg(t *testing.T) { 84 backend := &testSystemBackend{ 85 events: new(event.TypeMux), 86 } 87 c := &core{ 88 logger: log.New("backend", "test", "id", 0), 89 backend: backend, 90 state: StateAcceptRequest, 91 current: newRoundState(&istanbul.View{ 92 Sequence: big.NewInt(0), 93 Round: big.NewInt(0), 94 }, newTestValidatorSet(4), common.Hash{}, nil, nil, nil), 95 pendingRequests: prque.New(), 96 pendingRequestsMu: new(sync.Mutex), 97 } 98 requests := []istanbul.Request{ 99 { 100 Proposal: makeBlock(1), 101 }, 102 { 103 Proposal: makeBlock(2), 104 }, 105 { 106 Proposal: makeBlock(3), 107 }, 108 } 109 110 c.storeRequestMsg(&requests[1]) 111 c.storeRequestMsg(&requests[0]) 112 c.storeRequestMsg(&requests[2]) 113 if c.pendingRequests.Size() != len(requests) { 114 t.Errorf("the size of pending requests mismatch: have %v, want %v", c.pendingRequests.Size(), len(requests)) 115 } 116 117 c.current.sequence = big.NewInt(3) 118 119 c.subscribeEvents() 120 defer c.unsubscribeEvents() 121 122 c.processPendingRequests() 123 124 const timeoutDura = 2 * time.Second 125 timeout := time.NewTimer(timeoutDura) 126 select { 127 case ev := <-c.events.Chan(): 128 e, ok := ev.Data.(istanbul.RequestEvent) 129 if !ok { 130 t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data)) 131 } 132 if e.Proposal.Number().Cmp(requests[2].Proposal.Number()) != 0 { 133 t.Errorf("the number of proposal mismatch: have %v, want %v", e.Proposal.Number(), requests[2].Proposal.Number()) 134 } 135 case <-timeout.C: 136 t.Error("unexpected timeout occurs") 137 } 138 }