7 "github.com/davecgh/go-spew/spew"
8 "github.com/tendermint/go-wire"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
14 var _ = wire.RegisterInterface(
15 struct{ ConsensusMessage }{},
16 wire.ConcreteType{O: &BlockSignatureMsg{}, Byte: blockSignatureByte},
17 wire.ConcreteType{O: &BlockProposeMsg{}, Byte: blockProposeByte},
20 func TestDecodeMessage(t *testing.T) {
21 testCases := []struct {
26 msg: &BlockSignatureMsg{
27 BlockHash: [32]byte{0x01},
28 Signature: []byte{0x00},
31 msgType: blockSignatureByte,
34 msg: &BlockProposeMsg{
35 RawBlock: []byte{0x01, 0x02},
37 msgType: blockProposeByte,
40 for i, c := range testCases {
41 binMsg := wire.BinaryBytes(struct{ ConsensusMessage }{c.msg})
42 gotMsgType, gotMsg, err := decodeMessage(binMsg)
44 t.Fatalf("index:%d decode Message err %s", i, err)
46 if gotMsgType != c.msgType {
47 t.Fatalf("index:%d decode Message type err. got:%d want:%d", i, gotMsgType, c.msg)
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))
55 func TestBlockSignBroadcastMsg(t *testing.T) {
56 blockSignMsg := &BlockSignatureMsg{
57 BlockHash: [32]byte{0x01},
59 Signature: []byte{0x00},
62 signatureBroadcastMsg := NewBroadcastMsg(NewBlockSignatureMsg(bc.NewHash(blockSignMsg.BlockHash), blockSignMsg.Height, blockSignMsg.Signature, blockSignMsg.PubKey), consensusChannel)
64 binMsg := wire.BinaryBytes(signatureBroadcastMsg.GetMsg())
65 gotMsgType, gotMsg, err := decodeMessage(binMsg)
67 t.Fatalf("decode Message err %s", err)
69 if gotMsgType != blockSignatureByte {
70 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockSignatureByte)
72 if !reflect.DeepEqual(gotMsg, blockSignMsg) {
73 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockSignMsg))
77 func TestBlockProposeBroadcastMsg(t *testing.T) {
78 blockProposeMsg, _ := NewBlockProposeMsg(testBlock)
80 proposeBroadcastMsg := NewBroadcastMsg(blockProposeMsg, consensusChannel)
82 binMsg := wire.BinaryBytes(proposeBroadcastMsg.GetMsg())
83 gotMsgType, gotMsg, err := decodeMessage(binMsg)
85 t.Fatalf("decode Message err %s", err)
87 if gotMsgType != blockProposeByte {
88 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockProposeByte)
90 if !reflect.DeepEqual(gotMsg, blockProposeMsg) {
91 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockProposeMsg))
95 var testBlock = &types.Block{
96 BlockHeader: types.BlockHeader{
99 Timestamp: 1528945000,
100 BlockCommitment: types.BlockCommitment{
101 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
102 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
107 func TestBlockProposeMsg(t *testing.T) {
108 blockMsg, err := NewBlockProposeMsg(testBlock)
110 t.Fatalf("create new mine block msg err:%s", err)
113 gotBlock, err := blockMsg.(*BlockProposeMsg).GetProposeBlock()
115 t.Fatalf("got block err:%s", err)
118 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) {
119 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader))
122 wantString := "{block_height: 0, block_hash: f59514e2541488a38bc2667940bc2c24027e4a3a371d884b55570d036997bb57}"
123 if blockMsg.String() != wantString {
124 t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
127 blockMsg.(*BlockProposeMsg).RawBlock[1] = blockMsg.(*BlockProposeMsg).RawBlock[1] + 0x1
128 _, err = blockMsg.(*BlockProposeMsg).GetProposeBlock()
130 t.Fatalf("get mine block err")
133 wantString = "{err: wrong message}"
134 if blockMsg.String() != wantString {
135 t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
139 func TestBlockSignatureMsg(t *testing.T) {
140 msg := &BlockSignatureMsg{
141 BlockHash: [32]byte{0x01},
143 Signature: []byte{0x00},
144 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},
146 gotMsg := NewBlockSignatureMsg(bc.NewHash(msg.BlockHash), msg.Height, msg.Signature, msg.PubKey)
148 if !reflect.DeepEqual(gotMsg, msg) {
149 t.Fatalf("test block signature message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(msg))
151 wantString := "{block_hash: 0100000000000000000000000000000000000000000000000000000000000000,block_height:100,signature:00,pubkey:01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}"
152 if gotMsg.String() != wantString {
153 t.Fatalf("test block signature message err. got string:%s\n want string:%s", gotMsg.String(), wantString)