github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/core/message_set_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  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/consensus/istanbul"
    25  	"github.com/ethereum/go-ethereum/rlp"
    26  )
    27  
    28  func TestMessageSetWithSubject(t *testing.T) {
    29  	valSet := newTestValidatorSet(4)
    30  
    31  	ms := newMessageSet(valSet)
    32  
    33  	view := &istanbul.View{
    34  		Round:    new(big.Int),
    35  		Sequence: new(big.Int),
    36  	}
    37  
    38  	sub := &istanbul.Subject{
    39  		View:   view,
    40  		Digest: common.BytesToHash([]byte("1234567890")),
    41  	}
    42  
    43  	rawSub, err := rlp.EncodeToBytes(sub)
    44  	if err != nil {
    45  		t.Errorf("error mismatch: have %v, want nil", err)
    46  	}
    47  
    48  	msg := &istanbul.Message{
    49  		Code:    istanbul.MsgPrepare,
    50  		Msg:     rawSub,
    51  		Address: valSet.GetByIndex(0).Address(),
    52  	}
    53  
    54  	err = ms.Add(msg)
    55  	if err != nil {
    56  		t.Errorf("error mismatch: have %v, want nil", err)
    57  	}
    58  
    59  	err = ms.Add(msg)
    60  	if err != nil {
    61  		t.Errorf("error mismatch: have %v, want nil", err)
    62  	}
    63  
    64  	if ms.Size() != 1 {
    65  		t.Errorf("the size of message set mismatch: have %v, want 1", ms.Size())
    66  	}
    67  }