OSDN Git Service

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