github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/consensus/istanbul/ibft/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/kisexp/xdchain/common"
    24  	"github.com/kisexp/xdchain/consensus/istanbul"
    25  	ibfttypes "github.com/kisexp/xdchain/consensus/istanbul/ibft/types"
    26  	"github.com/kisexp/xdchain/rlp"
    27  )
    28  
    29  func TestMessageSetWithPreprepare(t *testing.T) {
    30  	valSet := newTestValidatorSet(4)
    31  
    32  	ms := newMessageSet(valSet)
    33  
    34  	view := &istanbul.View{
    35  		Round:    new(big.Int),
    36  		Sequence: new(big.Int),
    37  	}
    38  	pp := &istanbul.Preprepare{
    39  		View:     view,
    40  		Proposal: makeBlock(1),
    41  	}
    42  
    43  	rawPP, err := rlp.EncodeToBytes(pp)
    44  	if err != nil {
    45  		t.Errorf("error mismatch: have %v, want nil", err)
    46  	}
    47  	msg := &ibfttypes.Message{
    48  		Code:    ibfttypes.MsgPreprepare,
    49  		Msg:     rawPP,
    50  		Address: valSet.GetProposer().Address(),
    51  	}
    52  
    53  	err = ms.Add(msg)
    54  	if err != nil {
    55  		t.Errorf("error mismatch: have %v, want nil", err)
    56  	}
    57  
    58  	err = ms.Add(msg)
    59  	if err != nil {
    60  		t.Errorf("error mismatch: have %v, want nil", err)
    61  	}
    62  
    63  	if ms.Size() != 1 {
    64  		t.Errorf("the size of message set mismatch: have %v, want 1", ms.Size())
    65  	}
    66  }
    67  
    68  func TestMessageSetWithSubject(t *testing.T) {
    69  	valSet := newTestValidatorSet(4)
    70  
    71  	ms := newMessageSet(valSet)
    72  
    73  	view := &istanbul.View{
    74  		Round:    new(big.Int),
    75  		Sequence: new(big.Int),
    76  	}
    77  
    78  	sub := &istanbul.Subject{
    79  		View:   view,
    80  		Digest: common.StringToHash("1234567890"),
    81  	}
    82  
    83  	rawSub, err := rlp.EncodeToBytes(sub)
    84  	if err != nil {
    85  		t.Errorf("error mismatch: have %v, want nil", err)
    86  	}
    87  
    88  	msg := &ibfttypes.Message{
    89  		Code:    ibfttypes.MsgPrepare,
    90  		Msg:     rawSub,
    91  		Address: valSet.GetProposer().Address(),
    92  	}
    93  
    94  	err = ms.Add(msg)
    95  	if err != nil {
    96  		t.Errorf("error mismatch: have %v, want nil", err)
    97  	}
    98  
    99  	err = ms.Add(msg)
   100  	if err != nil {
   101  		t.Errorf("error mismatch: have %v, want nil", err)
   102  	}
   103  
   104  	if ms.Size() != 1 {
   105  		t.Errorf("the size of message set mismatch: have %v, want 1", ms.Size())
   106  	}
   107  }