quorum/consensus/istanbul/backend/handler_test.go

73 lines
2.3 KiB
Go
Raw Normal View History

// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package backend
import (
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/istanbul"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
lru "github.com/hashicorp/golang-lru"
)
func TestIstanbulMessage(t *testing.T) {
_, backend := newBlockChain(1)
// generate one msg
data := []byte("data1")
hash := istanbul.RLPHash(data)
msg := makeMsg(istanbulMsg, data)
addr := common.StringToAddress("address")
// 1. this message should not be in cache
// for peers
if _, ok := backend.recentMessages.Get(addr); ok {
t.Fatalf("the cache of messages for this peer should be nil")
}
// for self
if _, ok := backend.knownMessages.Get(hash); ok {
t.Fatalf("the cache of messages should be nil")
}
// 2. this message should be in cache after we handle it
_, err := backend.HandleMsg(addr, msg)
if err != nil {
t.Fatalf("handle message failed: %v", err)
}
// for peers
if ms, ok := backend.recentMessages.Get(addr); ms == nil || !ok {
t.Fatalf("the cache of messages for this peer cannot be nil")
} else if m, ok := ms.(*lru.ARCCache); !ok {
t.Fatalf("the cache of messages for this peer cannot be casted")
} else if _, ok := m.Get(hash); !ok {
t.Fatalf("the cache of messages for this peer cannot be found")
}
// for self
if _, ok := backend.knownMessages.Get(hash); !ok {
t.Fatalf("the cache of messages cannot be found")
}
}
func makeMsg(msgcode uint64, data interface{}) p2p.Msg {
size, r, _ := rlp.EncodeToReader(data)
return p2p.Msg{Code: msgcode, Size: uint32(size), Payload: r}
}