github.com/pfcoder/quorum@v2.0.3-0.20180501191142-d4a1b0958135+incompatible/consensus/istanbul/backend/handler_test.go (about) 1 // Copyright 2015 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 backend 18 19 import ( 20 "testing" 21 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/consensus/istanbul" 24 "github.com/ethereum/go-ethereum/p2p" 25 "github.com/ethereum/go-ethereum/rlp" 26 lru "github.com/hashicorp/golang-lru" 27 ) 28 29 func TestIstanbulMessage(t *testing.T) { 30 _, backend := newBlockChain(1) 31 32 // generate one msg 33 data := []byte("data1") 34 hash := istanbul.RLPHash(data) 35 msg := makeMsg(istanbulMsg, data) 36 addr := common.StringToAddress("address") 37 38 // 1. this message should not be in cache 39 // for peers 40 if _, ok := backend.recentMessages.Get(addr); ok { 41 t.Fatalf("the cache of messages for this peer should be nil") 42 } 43 44 // for self 45 if _, ok := backend.knownMessages.Get(hash); ok { 46 t.Fatalf("the cache of messages should be nil") 47 } 48 49 // 2. this message should be in cache after we handle it 50 _, err := backend.HandleMsg(addr, msg) 51 if err != nil { 52 t.Fatalf("handle message failed: %v", err) 53 } 54 // for peers 55 if ms, ok := backend.recentMessages.Get(addr); ms == nil || !ok { 56 t.Fatalf("the cache of messages for this peer cannot be nil") 57 } else if m, ok := ms.(*lru.ARCCache); !ok { 58 t.Fatalf("the cache of messages for this peer cannot be casted") 59 } else if _, ok := m.Get(hash); !ok { 60 t.Fatalf("the cache of messages for this peer cannot be found") 61 } 62 63 // for self 64 if _, ok := backend.knownMessages.Get(hash); !ok { 65 t.Fatalf("the cache of messages cannot be found") 66 } 67 } 68 69 func makeMsg(msgcode uint64, data interface{}) p2p.Msg { 70 size, r, _ := rlp.EncodeToReader(data) 71 return p2p.Msg{Code: msgcode, Size: uint32(size), Payload: r} 72 }