github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/dbft/core/message_set_test.go (about)

     1  package core
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/quickchainproject/quickchain/common"
     8  	"github.com/quickchainproject/quickchain/consensus/dbft"
     9  	"github.com/quickchainproject/quickchain/rlp"
    10  )
    11  
    12  func TestMessageSetWithPreprepare(t *testing.T) {
    13  	valSet := newTestValidatorSet(4)
    14  
    15  	ms := newMessageSet(valSet)
    16  
    17  	view := &bft.View{
    18  		Round:    new(big.Int),
    19  		Sequence: new(big.Int),
    20  	}
    21  	pp := &bft.Preprepare{
    22  		View:     view,
    23  		Proposal: makeBlock(1),
    24  	}
    25  
    26  	rawPP, err := rlp.EncodeToBytes(pp)
    27  	if err != nil {
    28  		t.Errorf("error mismatch: have %v, want nil", err)
    29  	}
    30  	msg := &message{
    31  		Code:    msgPreprepare,
    32  		Msg:     rawPP,
    33  		Address: valSet.GetProposer().Address(),
    34  	}
    35  
    36  	err = ms.Add(msg)
    37  	if err != nil {
    38  		t.Errorf("error mismatch: have %v, want nil", err)
    39  	}
    40  
    41  	err = ms.Add(msg)
    42  	if err != nil {
    43  		t.Errorf("error mismatch: have %v, want nil", err)
    44  	}
    45  
    46  	if ms.Size() != 1 {
    47  		t.Errorf("the size of message set mismatch: have %v, want 1", ms.Size())
    48  	}
    49  }
    50  
    51  func TestMessageSetWithSubject(t *testing.T) {
    52  	valSet := newTestValidatorSet(4)
    53  
    54  	ms := newMessageSet(valSet)
    55  
    56  	view := &bft.View{
    57  		Round:    new(big.Int),
    58  		Sequence: new(big.Int),
    59  	}
    60  
    61  	sub := &bft.Subject{
    62  		View:   view,
    63  		Digest: common.StringToHash("1234567890"),
    64  	}
    65  
    66  	rawSub, err := rlp.EncodeToBytes(sub)
    67  	if err != nil {
    68  		t.Errorf("error mismatch: have %v, want nil", err)
    69  	}
    70  
    71  	msg := &message{
    72  		Code:    msgPrepare,
    73  		Msg:     rawSub,
    74  		Address: valSet.GetProposer().Address(),
    75  	}
    76  
    77  	err = ms.Add(msg)
    78  	if err != nil {
    79  		t.Errorf("error mismatch: have %v, want nil", err)
    80  	}
    81  
    82  	err = ms.Add(msg)
    83  	if err != nil {
    84  		t.Errorf("error mismatch: have %v, want nil", err)
    85  	}
    86  
    87  	if ms.Size() != 1 {
    88  		t.Errorf("the size of message set mismatch: have %v, want 1", ms.Size())
    89  	}
    90  }