github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/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/p2p/enode"
    26  	"github.com/ethereum/go-ethereum/rlp"
    27  	lru "github.com/hashicorp/golang-lru"
    28  )
    29  
    30  type MockPeer struct{}
    31  
    32  func (p *MockPeer) Send(msgcode uint64, data interface{}) error {
    33  	return nil
    34  }
    35  
    36  func (p *MockPeer) Node() *enode.Node {
    37  	return nil
    38  }
    39  
    40  func TestIstanbulMessage(t *testing.T) {
    41  	_, backend := newBlockChain(1, true)
    42  
    43  	// generate one msg
    44  	data := []byte("data1")
    45  	hash := istanbul.RLPHash(data)
    46  	msg := makeMsg(istanbulConsensusMsg, data)
    47  	addr := common.BytesToAddress([]byte("address"))
    48  
    49  	// 1. this message should not be in cache
    50  	// for peers
    51  	if _, ok := backend.recentMessages.Get(addr); ok {
    52  		t.Fatalf("the cache of messages for this peer should be nil")
    53  	}
    54  
    55  	// for self
    56  	if _, ok := backend.knownMessages.Get(hash); ok {
    57  		t.Fatalf("the cache of messages should be nil")
    58  	}
    59  
    60  	// 2. this message should be in cache after we handle it
    61  	_, err := backend.HandleMsg(addr, msg, &MockPeer{})
    62  	if err != nil {
    63  		t.Fatalf("handle message failed: %v", err)
    64  	}
    65  	// for peers
    66  	if ms, ok := backend.recentMessages.Get(addr); ms == nil || !ok {
    67  		t.Fatalf("the cache of messages for this peer cannot be nil")
    68  	} else if m, ok := ms.(*lru.ARCCache); !ok {
    69  		t.Fatalf("the cache of messages for this peer cannot be casted")
    70  	} else if _, ok := m.Get(hash); !ok {
    71  		t.Fatalf("the cache of messages for this peer cannot be found")
    72  	}
    73  
    74  	// for self
    75  	if _, ok := backend.knownMessages.Get(hash); !ok {
    76  		t.Fatalf("the cache of messages cannot be found")
    77  	}
    78  }
    79  
    80  func makeMsg(msgcode uint64, data interface{}) p2p.Msg {
    81  	size, r, _ := rlp.EncodeToReader(data)
    82  	return p2p.Msg{Code: msgcode, Size: uint32(size), Payload: r}
    83  }