github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/netsync/consensusmgr/consensus_msg_test.go (about)

     1  package consensusmgr
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  	"github.com/tendermint/go-wire"
     9  
    10  	"github.com/bytom/bytom/protocol/bc"
    11  	"github.com/bytom/bytom/protocol/bc/types"
    12  )
    13  
    14  var _ = wire.RegisterInterface(
    15  	struct{ ConsensusMessage }{},
    16  	wire.ConcreteType{O: &BlockVerificationMsg{}, Byte: blockSignatureByte},
    17  	wire.ConcreteType{O: &BlockProposeMsg{}, Byte: blockProposeByte},
    18  )
    19  
    20  func TestDecodeMessage(t *testing.T) {
    21  	testCases := []struct {
    22  		msg     ConsensusMessage
    23  		msgType byte
    24  	}{
    25  		{
    26  			msg: &BlockVerificationMsg{
    27  				SourceHash: bc.Hash{V0: 1, V1: 1, V2: 1, V3: 1},
    28  				TargetHash: bc.Hash{V0: 2, V1: 2, V2: 2, V3: 2},
    29  				Signature:  []byte{0x00},
    30  				PubKey:     []byte{0x01},
    31  			},
    32  			msgType: blockSignatureByte,
    33  		},
    34  		{
    35  			msg: &BlockProposeMsg{
    36  				RawBlock: []byte{0x01, 0x02},
    37  			},
    38  			msgType: blockProposeByte,
    39  		},
    40  	}
    41  	for i, c := range testCases {
    42  		binMsg := wire.BinaryBytes(struct{ ConsensusMessage }{c.msg})
    43  		gotMsgType, gotMsg, err := decodeMessage(binMsg)
    44  		if err != nil {
    45  			t.Fatalf("index:%d decode Message err %s", i, err)
    46  		}
    47  		if gotMsgType != c.msgType {
    48  			t.Fatalf("index:%d decode Message type err. got:%d want:%d", i, gotMsgType, c.msg)
    49  		}
    50  		if !reflect.DeepEqual(gotMsg, c.msg) {
    51  			t.Fatalf("index:%d decode Message err. got:%s\n want:%s", i, spew.Sdump(gotMsg), spew.Sdump(c.msg))
    52  		}
    53  	}
    54  }
    55  
    56  func TestBlockVerificationBroadcastMsg(t *testing.T) {
    57  	blockSignMsg := &BlockVerificationMsg{
    58  		SourceHash: bc.Hash{V0: 1, V1: 1, V2: 1, V3: 1},
    59  		TargetHash: bc.Hash{V0: 2, V1: 2, V2: 2, V3: 2},
    60  		Signature:  []byte{0x00},
    61  		PubKey:     []byte{0x01},
    62  	}
    63  	verificationBroadcastMsg := NewBroadcastMsg(NewBlockVerificationMsg(blockSignMsg.SourceHash, blockSignMsg.TargetHash, blockSignMsg.PubKey, blockSignMsg.Signature), consensusChannel)
    64  
    65  	binMsg := wire.BinaryBytes(verificationBroadcastMsg.GetMsg())
    66  	gotMsgType, gotMsg, err := decodeMessage(binMsg)
    67  	if err != nil {
    68  		t.Fatalf("decode Message err %s", err)
    69  	}
    70  	if gotMsgType != blockSignatureByte {
    71  		t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockSignatureByte)
    72  	}
    73  	if !reflect.DeepEqual(gotMsg, blockSignMsg) {
    74  		t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockSignMsg))
    75  	}
    76  }
    77  
    78  func TestBlockProposeBroadcastMsg(t *testing.T) {
    79  	blockProposeMsg, _ := NewBlockProposeMsg(testBlock)
    80  
    81  	proposeBroadcastMsg := NewBroadcastMsg(blockProposeMsg, consensusChannel)
    82  
    83  	binMsg := wire.BinaryBytes(proposeBroadcastMsg.GetMsg())
    84  	gotMsgType, gotMsg, err := decodeMessage(binMsg)
    85  	if err != nil {
    86  		t.Fatalf("decode Message err %s", err)
    87  	}
    88  	if gotMsgType != blockProposeByte {
    89  		t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockProposeByte)
    90  	}
    91  	if !reflect.DeepEqual(gotMsg, blockProposeMsg) {
    92  		t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockProposeMsg))
    93  	}
    94  }
    95  
    96  var testBlock = &types.Block{
    97  	BlockHeader: types.BlockHeader{
    98  		Version:   1,
    99  		Height:    0,
   100  		Timestamp: 1528945000,
   101  		BlockCommitment: types.BlockCommitment{
   102  			TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
   103  		},
   104  		SupLinks: types.SupLinks{},
   105  	},
   106  }
   107  
   108  func TestBlockProposeMsg(t *testing.T) {
   109  	blockMsg, err := NewBlockProposeMsg(testBlock)
   110  	if err != nil {
   111  		t.Fatalf("create new mine block msg err:%s", err)
   112  	}
   113  
   114  	gotBlock, err := blockMsg.(*BlockProposeMsg).GetProposeBlock()
   115  	if err != nil {
   116  		t.Fatalf("got block err:%s", err)
   117  	}
   118  
   119  	if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
   120  		t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
   121  	}
   122  
   123  	wantString := "{block_height: 0, block_hash: 3ce98dfffbd0e10c318f167696603b23173b3ec86e7868c8fa65be76edefc67e}"
   124  	if blockMsg.String() != wantString {
   125  		t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
   126  	}
   127  
   128  	blockMsg.(*BlockProposeMsg).RawBlock[1] = blockMsg.(*BlockProposeMsg).RawBlock[1] + 0x1
   129  	_, err = blockMsg.(*BlockProposeMsg).GetProposeBlock()
   130  	if err == nil {
   131  		t.Fatalf("get mine block err")
   132  	}
   133  
   134  	wantString = "{err: wrong message}"
   135  	if blockMsg.String() != wantString {
   136  		t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
   137  	}
   138  }
   139  
   140  func TestBlockVerificationMsg(t *testing.T) {
   141  	msg := &BlockVerificationMsg{
   142  		SourceHash: bc.Hash{V0: 1, V1: 1, V2: 1, V3: 1},
   143  		TargetHash: bc.Hash{V0: 2, V1: 2, V2: 2, V3: 2},
   144  		Signature:  []byte{0x00},
   145  		PubKey:     []byte{0x01},
   146  	}
   147  	gotMsg := NewBlockVerificationMsg(msg.SourceHash, msg.TargetHash, msg.PubKey, msg.Signature)
   148  
   149  	if !reflect.DeepEqual(gotMsg, msg) {
   150  		t.Fatalf("test block verification message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(msg))
   151  	}
   152  	wantString := "{sourceHash:0000000000000001000000000000000100000000000000010000000000000001,targetHash:0000000000000002000000000000000200000000000000020000000000000002,signature:00,pubkey:01}"
   153  	if gotMsg.String() != wantString {
   154  		t.Fatalf("test block verification message err. got string:%s\n want string:%s", gotMsg.String(), wantString)
   155  	}
   156  }