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},
58 Signature: []byte{0x00},
61 signatureBroadcastMsg := NewBroadcastMsg(NewBlockSignatureMsg(bc.NewHash(blockSignMsg.BlockHash), blockSignMsg.Signature, blockSignMsg.PubKey), consensusChannel)
63 binMsg := wire.BinaryBytes(signatureBroadcastMsg.GetMsg())
64 gotMsgType, gotMsg, err := decodeMessage(binMsg)
66 t.Fatalf("decode Message err %s", err)
68 if gotMsgType != blockSignatureByte {
69 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockSignatureByte)
71 if !reflect.DeepEqual(gotMsg, blockSignMsg) {
72 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockSignMsg))
76 func TestBlockProposeBroadcastMsg(t *testing.T) {
77 blockProposeMsg, _ := NewBlockProposeMsg(testBlock)
79 proposeBroadcastMsg := NewBroadcastMsg(blockProposeMsg, consensusChannel)
81 binMsg := wire.BinaryBytes(proposeBroadcastMsg.GetMsg())
82 gotMsgType, gotMsg, err := decodeMessage(binMsg)
84 t.Fatalf("decode Message err %s", err)
86 if gotMsgType != blockProposeByte {
87 t.Fatalf("decode Message type err. got:%d want:%d", gotMsgType, blockProposeByte)
89 if !reflect.DeepEqual(gotMsg, blockProposeMsg) {
90 t.Fatalf("decode Message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(blockProposeMsg))
94 var testBlock = &types.Block{
95 BlockHeader: types.BlockHeader{
98 Timestamp: 1528945000,
99 BlockCommitment: types.BlockCommitment{
100 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)},
101 TransactionStatusHash: bc.Hash{V0: uint64(0x55)},
106 func TestBlockProposeMsg(t *testing.T) {
107 blockMsg, err := NewBlockProposeMsg(testBlock)
109 t.Fatalf("create new mine block msg err:%s", err)
112 gotBlock, err := blockMsg.(*BlockProposeMsg).GetProposeBlock()
114 t.Fatalf("got block err:%s", err)
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))
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)
126 blockMsg.(*BlockProposeMsg).RawBlock[1] = blockMsg.(*BlockProposeMsg).RawBlock[1] + 0x1
127 _, err = blockMsg.(*BlockProposeMsg).GetProposeBlock()
129 t.Fatalf("get mine block err")
132 wantString = "{err: wrong message}"
133 if blockMsg.String() != wantString {
134 t.Errorf("block msg test err. got:%s want:%s", blockMsg.String(), wantString)
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},
144 gotMsg := NewBlockSignatureMsg(bc.NewHash(msg.BlockHash), msg.Signature, msg.PubKey)
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))
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)