OSDN Git Service

Del BlockSignatureMsg unused Height field (#293)
[bytom/vapor.git] / netsync / consensusmgr / consensus_msg_test.go
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/vapor/protocol/bc"
11         "github.com/vapor/protocol/bc/types"
12 )
13
14 var _ = wire.RegisterInterface(
15         struct{ ConsensusMessage }{},
16         wire.ConcreteType{O: &BlockSignatureMsg{}, 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: &BlockSignatureMsg{
27                                 BlockHash: [32]byte{0x01},
28                                 Signature: []byte{0x00},
29                                 PubKey:    []byte{0x01},
30                         },
31                         msgType: blockSignatureByte,
32                 },
33                 {
34                         msg: &BlockProposeMsg{
35                                 RawBlock: []byte{0x01, 0x02},
36                         },
37                         msgType: blockProposeByte,
38                 },
39         }
40         for i, c := range testCases {
41                 binMsg := wire.BinaryBytes(struct{ ConsensusMessage }{c.msg})
42                 gotMsgType, gotMsg, err := decodeMessage(binMsg)
43                 if err != nil {
44                         t.Fatalf("index:%d decode Message err %s", i, err)
45                 }
46                 if gotMsgType != c.msgType {
47                         t.Fatalf("index:%d decode Message type err. got:%d want:%d", i, gotMsgType, c.msg)
48                 }
49                 if !reflect.DeepEqual(gotMsg, c.msg) {
50                         t.Fatalf("index:%d decode Message err. got:%s\n want:%s", i, spew.Sdump(gotMsg), spew.Sdump(c.msg))
51                 }
52         }
53 }
54
55 func TestBlockSignBroadcastMsg(t *testing.T) {
56         blockSignMsg := &BlockSignatureMsg{
57                 BlockHash: [32]byte{0x01},
58                 Signature: []byte{0x00},
59                 PubKey:    []byte{0x01},
60         }
61         signatureBroadcastMsg := NewBroadcastMsg(NewBlockSignatureMsg(bc.NewHash(blockSignMsg.BlockHash), blockSignMsg.Signature, blockSignMsg.PubKey), consensusChannel)
62
63         binMsg := wire.BinaryBytes(signatureBroadcastMsg.GetMsg())
64         gotMsgType, gotMsg, err := decodeMessage(binMsg)
65         if err != nil {
66                 t.Fatalf("decode Message err %s", err)
67         }
68         if gotMsgType != blockSignatureByte {
69                 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockSignatureByte)
70         }
71         if !reflect.DeepEqual(gotMsg, blockSignMsg) {
72                 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockSignMsg))
73         }
74 }
75
76 func TestBlockProposeBroadcastMsg(t *testing.T) {
77         blockProposeMsg, _ := NewBlockProposeMsg(testBlock)
78
79         proposeBroadcastMsg := NewBroadcastMsg(blockProposeMsg, consensusChannel)
80
81         binMsg := wire.BinaryBytes(proposeBroadcastMsg.GetMsg())
82         gotMsgType, gotMsg, err := decodeMessage(binMsg)
83         if err != nil {
84                 t.Fatalf("decode Message err %s", err)
85         }
86         if gotMsgType != blockProposeByte {
87                 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockProposeByte)
88         }
89         if !reflect.DeepEqual(gotMsg, blockProposeMsg) {
90                 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockProposeMsg))
91         }
92 }
93
94 var testBlock = &types.Block{
95         BlockHeader: types.BlockHeader{
96                 Version:   1,
97                 Height:    0,
98                 Timestamp: 1528945000,
99                 BlockCommitment: types.BlockCommitment{
100                         TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
101                         TransactionStatusHash:  bc.Hash{V0: uint64(0x55)},
102                 },
103         },
104 }
105
106 func TestBlockProposeMsg(t *testing.T) {
107         blockMsg, err := NewBlockProposeMsg(testBlock)
108         if err != nil {
109                 t.Fatalf("create new mine block msg err:%s", err)
110         }
111
112         gotBlock, err := blockMsg.(*BlockProposeMsg).GetProposeBlock()
113         if err != nil {
114                 t.Fatalf("got block err:%s", err)
115         }
116
117         if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
118                 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
119         }
120
121         wantString := "{block_height: 0, block_hash: f59514e2541488a38bc2667940bc2c24027e4a3a371d884b55570d036997bb57}"
122         if blockMsg.String() != wantString {
123                 t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
124         }
125
126         blockMsg.(*BlockProposeMsg).RawBlock[1] = blockMsg.(*BlockProposeMsg).RawBlock[1] + 0x1
127         _, err = blockMsg.(*BlockProposeMsg).GetProposeBlock()
128         if err == nil {
129                 t.Fatalf("get mine block err")
130         }
131
132         wantString = "{err: wrong message}"
133         if blockMsg.String() != wantString {
134                 t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
135         }
136 }
137
138 func TestBlockSignatureMsg(t *testing.T) {
139         msg := &BlockSignatureMsg{
140                 BlockHash: [32]byte{0x01},
141                 Signature: []byte{0x00},
142                 PubKey:    []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
143         }
144         gotMsg := NewBlockSignatureMsg(bc.NewHash(msg.BlockHash), msg.Signature, msg.PubKey)
145
146         if !reflect.DeepEqual(gotMsg, msg) {
147                 t.Fatalf("test block signature message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(msg))
148         }
149         wantString := "{block_hash: 0100000000000000000000000000000000000000000000000000000000000000,signature:00,pubkey:01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}"
150         if gotMsg.String() != wantString {
151                 t.Fatalf("test block signature message err. got string:%s\n want string:%s", gotMsg.String(), wantString)
152         }
153 }