7 "github.com/davecgh/go-spew/spew"
8 "github.com/tendermint/go-wire"
10 "github.com/bytom/bytom/protocol/bc"
11 "github.com/bytom/bytom/protocol/bc/types"
14 var _ = wire.RegisterInterface(
15 struct{ ConsensusMessage }{},
16 wire.ConcreteType{O: &BlockVerificationMsg{}, Byte: blockSignatureByte},
17 wire.ConcreteType{O: &BlockProposeMsg{}, Byte: blockProposeByte},
20 func TestDecodeMessage(t *testing.T) {
21 testCases := []struct {
26 msg: &BlockVerificationMsg{
28 SourceHash: bc.Hash{V0: 1, V1: 1, V2: 1, V3: 1},
30 TargetHash: bc.Hash{V0: 2, V1: 2, V2: 2, V3: 2},
31 Signature: []byte{0x00},
34 msgType: blockSignatureByte,
37 msg: &BlockProposeMsg{
38 RawBlock: []byte{0x01, 0x02},
40 msgType: blockProposeByte,
43 for i, c := range testCases {
44 binMsg := wire.BinaryBytes(struct{ ConsensusMessage }{c.msg})
45 gotMsgType, gotMsg, err := decodeMessage(binMsg)
47 t.Fatalf("index:%d decode Message err %s", i, err)
49 if gotMsgType != c.msgType {
50 t.Fatalf("index:%d decode Message type err. got:%d want:%d", i, gotMsgType, c.msg)
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))
58 func TestBlockVerificationBroadcastMsg(t *testing.T) {
59 blockSignMsg := &BlockVerificationMsg{
61 SourceHash: bc.Hash{V0: 1, V1: 1, V2: 1, V3: 1},
63 TargetHash: bc.Hash{V0: 2, V1: 2, V2: 2, V3: 2},
64 Signature: []byte{0x00},
67 verificationBroadcastMsg := NewBroadcastMsg(NewBlockVerificationMsg(blockSignMsg.SourceHeight, blockSignMsg.TargetHeight, blockSignMsg.SourceHash, blockSignMsg.TargetHash, blockSignMsg.PubKey, blockSignMsg.Signature), consensusChannel)
69 binMsg := wire.BinaryBytes(verificationBroadcastMsg.GetMsg())
70 gotMsgType, gotMsg, err := decodeMessage(binMsg)
72 t.Fatalf("decode Message err %s", err)
74 if gotMsgType != blockSignatureByte {
75 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockSignatureByte)
77 if !reflect.DeepEqual(gotMsg, blockSignMsg) {
78 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockSignMsg))
82 func TestBlockProposeBroadcastMsg(t *testing.T) {
83 blockProposeMsg, _ := NewBlockProposeMsg(testBlock)
85 proposeBroadcastMsg := NewBroadcastMsg(blockProposeMsg, consensusChannel)
87 binMsg := wire.BinaryBytes(proposeBroadcastMsg.GetMsg())
88 gotMsgType, gotMsg, err := decodeMessage(binMsg)
90 t.Fatalf("decode Message err %s", err)
92 if gotMsgType != blockProposeByte {
93 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockProposeByte)
95 if !reflect.DeepEqual(gotMsg, blockProposeMsg) {
96 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockProposeMsg))
100 var testBlock = &types.Block{
101 BlockHeader: types.BlockHeader{
104 Timestamp: 1528945000,
105 BlockCommitment: types.BlockCommitment{
106 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
108 SupLinks: types.SupLinks{},
112 func TestBlockProposeMsg(t *testing.T) {
113 blockMsg, err := NewBlockProposeMsg(testBlock)
115 t.Fatalf("create new mine block msg err:%s", err)
118 gotBlock, err := blockMsg.(*BlockProposeMsg).GetProposeBlock()
120 t.Fatalf("got block err:%s", err)
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))
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)
132 blockMsg.(*BlockProposeMsg).RawBlock[1] = blockMsg.(*BlockProposeMsg).RawBlock[1] + 0x1
133 _, err = blockMsg.(*BlockProposeMsg).GetProposeBlock()
135 t.Fatalf("get mine block err")
138 wantString = "{err: wrong message}"
139 if blockMsg.String() != wantString {
140 t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
144 func TestBlockVerificationMsg(t *testing.T) {
145 msg := &BlockVerificationMsg{
147 SourceHash: bc.Hash{V0: 1, V1: 1, V2: 1, V3: 1},
149 TargetHash: bc.Hash{V0: 2, V1: 2, V2: 2, V3: 2},
150 Signature: []byte{0x00},
151 PubKey: []byte{0x01},
153 gotMsg := NewBlockVerificationMsg(msg.SourceHeight, msg.TargetHeight, msg.SourceHash, msg.TargetHash, msg.PubKey, msg.Signature)
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))
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)