dcrd/wire/msgminingstate_test.go
John C. Vernaleo 5076a00512 Initial Decred Commit.
Includes work by cjepson, ay-p, jolan, and jcv.

Initial conceptual framework by tacotime.
2016-02-07 14:00:12 -05:00

117 lines
3.7 KiB
Go

// msgminingstate_test.go
package wire_test
import (
"bytes"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/wire"
)
// TestMiningStateWire tests the MsgMiningState wire encode and decode for a sample
// message containing a fake block header and some fake vote hashes.
func TestMiningStateWire(t *testing.T) {
// Empty tx message.
sampleMSMsg := wire.NewMsgMiningState()
sampleMSMsg.ProtocolVersion = wire.ProtocolVersion
sampleMSMsg.Height = 123456
fakeBlock, _ := chainhash.NewHashFromStr("4433221144332211443322114" +
"433221144332211443322114433221144332211")
err := sampleMSMsg.AddBlockHash(fakeBlock)
if err != nil {
t.Errorf("unexpected error for AddBlockHash: %v", err.Error())
}
fakeVote1, _ := chainhash.NewHashFromStr("2222111122221111222211112" +
"222111122221111222211112222111122221111")
fakeVote2, _ := chainhash.NewHashFromStr("4444333344443333444433334" +
"444333344443333444433334444333344443333")
fakeVote3, _ := chainhash.NewHashFromStr("6666555566665555666655556" +
"666555566665555666655556666555566665555")
err = sampleMSMsg.AddVoteHash(fakeVote1)
if err != nil {
t.Errorf("unexpected error for AddVoteHash 1: %v", err.Error())
}
err = sampleMSMsg.AddVoteHash(fakeVote2)
if err != nil {
t.Errorf("unexpected error for AddVoteHash 2: %v", err.Error())
}
err = sampleMSMsg.AddVoteHash(fakeVote3)
if err != nil {
t.Errorf("unexpected error for AddVoteHash 3: %v", err.Error())
}
sampleMSMsgEncoded := []byte{
0x01, 0x00, 0x00, 0x00, // Version
0x40, 0xe2, 0x01, 0x00, // Height 0001e240 in BE
0x01,
0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44, // Dummy Block
0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44,
0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44,
0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44,
0x03, // Varint for number of votes
0x11, 0x11, 0x22, 0x22, 0x11, 0x11, 0x22, 0x22, // Dummy votes [1]
0x11, 0x11, 0x22, 0x22, 0x11, 0x11, 0x22, 0x22,
0x11, 0x11, 0x22, 0x22, 0x11, 0x11, 0x22, 0x22,
0x11, 0x11, 0x22, 0x22, 0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44, 0x33, 0x33, 0x44, 0x44, // [2]
0x33, 0x33, 0x44, 0x44, 0x33, 0x33, 0x44, 0x44,
0x33, 0x33, 0x44, 0x44, 0x33, 0x33, 0x44, 0x44,
0x33, 0x33, 0x44, 0x44, 0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66, 0x55, 0x55, 0x66, 0x66, // [3]
0x55, 0x55, 0x66, 0x66, 0x55, 0x55, 0x66, 0x66,
0x55, 0x55, 0x66, 0x66, 0x55, 0x55, 0x66, 0x66,
0x55, 0x55, 0x66, 0x66, 0x55, 0x55, 0x66, 0x66,
}
tests := []struct {
in *wire.MsgMiningState // Message to encode
out *wire.MsgMiningState // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version sample message.
{
sampleMSMsg,
sampleMSMsg,
sampleMSMsgEncoded,
wire.ProtocolVersion,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Encode the message to wire format.
var buf bytes.Buffer
err := test.in.BtcEncode(&buf, test.pver)
if err != nil {
t.Errorf("BtcEncode #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
continue
}
// Decode the message from wire format.
var msg wire.MsgMiningState
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)
continue
}
if !reflect.DeepEqual(&msg, test.out) {
t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
spew.Sdump(&msg), spew.Sdump(test.out))
continue
}
}
}