2016-08-08 18:42:54 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-08-08 18:42:54 +02:00
|
|
|
package wire
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-07-03 02:43:33 +02:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestBlockHeader tests the BlockHeader API.
|
|
|
|
func TestBlockHeader(t *testing.T) {
|
2016-08-08 18:42:54 +02:00
|
|
|
nonce64, err := RandomUint64()
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("RandomUint64: Error generating nonce: %v", err)
|
|
|
|
}
|
|
|
|
nonce := uint32(nonce64)
|
|
|
|
|
2014-05-28 07:26:47 +02:00
|
|
|
hash := mainNetGenesisHash
|
|
|
|
merkleHash := mainNetGenesisMerkleRoot
|
2013-05-08 21:31:00 +02:00
|
|
|
bits := uint32(0x1d00ffff)
|
2016-08-29 00:51:55 +02:00
|
|
|
bh := NewBlockHeader(1, &hash, &merkleHash, bits, nonce)
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
// Ensure we get the same data back out.
|
|
|
|
if !bh.PrevBlock.IsEqual(&hash) {
|
|
|
|
t.Errorf("NewBlockHeader: wrong prev hash - got %v, want %v",
|
|
|
|
spew.Sprint(bh.PrevBlock), spew.Sprint(hash))
|
|
|
|
}
|
|
|
|
if !bh.MerkleRoot.IsEqual(&merkleHash) {
|
|
|
|
t.Errorf("NewBlockHeader: wrong merkle root - got %v, want %v",
|
|
|
|
spew.Sprint(bh.MerkleRoot), spew.Sprint(merkleHash))
|
|
|
|
}
|
|
|
|
if bh.Bits != bits {
|
|
|
|
t.Errorf("NewBlockHeader: wrong bits - got %v, want %v",
|
|
|
|
bh.Bits, bits)
|
|
|
|
}
|
|
|
|
if bh.Nonce != nonce {
|
|
|
|
t.Errorf("NewBlockHeader: wrong nonce - got %v, want %v",
|
|
|
|
bh.Nonce, nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBlockHeaderWire tests the BlockHeader wire encode and decode for various
|
|
|
|
// protocol versions.
|
|
|
|
func TestBlockHeaderWire(t *testing.T) {
|
|
|
|
nonce := uint32(123123) // 0x1e0f3
|
2015-07-09 17:50:03 +02:00
|
|
|
pver := uint32(70001)
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
|
|
|
|
bits := uint32(0x1d00ffff)
|
2016-08-08 18:42:54 +02:00
|
|
|
baseBlockHdr := &BlockHeader{
|
2013-05-08 21:31:00 +02:00
|
|
|
Version: 1,
|
2014-05-28 07:26:47 +02:00
|
|
|
PrevBlock: mainNetGenesisHash,
|
|
|
|
MerkleRoot: mainNetGenesisMerkleRoot,
|
2013-05-08 21:31:00 +02:00
|
|
|
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
|
|
|
Bits: bits,
|
|
|
|
Nonce: nonce,
|
|
|
|
}
|
|
|
|
|
|
|
|
// baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
|
|
|
|
baseBlockHdrEncoded := []byte{
|
|
|
|
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
|
|
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
|
|
|
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
|
|
|
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
|
|
|
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
|
|
|
|
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
|
|
|
|
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
|
|
|
|
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
|
|
|
|
0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
|
|
|
|
0x29, 0xab, 0x5f, 0x49, // Timestamp
|
|
|
|
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
|
|
0xf3, 0xe0, 0x01, 0x00, // Nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2016-08-08 18:42:54 +02:00
|
|
|
in *BlockHeader // Data to encode
|
|
|
|
out *BlockHeader // Expected decoded data
|
|
|
|
buf []byte // Wire encoding
|
|
|
|
pver uint32 // Protocol version for wire encoding
|
2013-05-08 21:31:00 +02:00
|
|
|
}{
|
|
|
|
// Latest protocol version.
|
|
|
|
{
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
ProtocolVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0035Version.
|
|
|
|
{
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
BIP0035Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0031Version.
|
|
|
|
{
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
BIP0031Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version NetAddressTimeVersion.
|
|
|
|
{
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
NetAddressTimeVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version MultipleAddressVersion.
|
|
|
|
{
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
MultipleAddressVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
// Encode to wire format.
|
|
|
|
var buf bytes.Buffer
|
2016-08-08 18:42:54 +02:00
|
|
|
err := writeBlockHeader(&buf, test.pver, test.in)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("writeBlockHeader #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
|
|
t.Errorf("writeBlockHeader #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-07-09 17:50:03 +02:00
|
|
|
buf.Reset()
|
|
|
|
err = test.in.BtcEncode(&buf, 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
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// Decode the block header from wire format.
|
2016-08-08 18:42:54 +02:00
|
|
|
var bh BlockHeader
|
2014-06-05 06:39:03 +02:00
|
|
|
rbuf := bytes.NewReader(test.buf)
|
2016-08-08 18:42:54 +02:00
|
|
|
err = readBlockHeader(rbuf, test.pver, &bh)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("readBlockHeader #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&bh, test.out) {
|
|
|
|
t.Errorf("readBlockHeader #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(&bh), spew.Sdump(test.out))
|
|
|
|
continue
|
|
|
|
}
|
2015-07-09 17:50:03 +02:00
|
|
|
|
|
|
|
rbuf = bytes.NewReader(test.buf)
|
|
|
|
err = bh.BtcDecode(rbuf, pver)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("BtcDecode #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&bh, test.out) {
|
|
|
|
t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(&bh), spew.Sdump(test.out))
|
|
|
|
continue
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-19 03:31:08 +01:00
|
|
|
|
|
|
|
// TestBlockHeaderSerialize tests BlockHeader serialize and deserialize.
|
|
|
|
func TestBlockHeaderSerialize(t *testing.T) {
|
|
|
|
nonce := uint32(123123) // 0x1e0f3
|
|
|
|
|
|
|
|
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
|
|
|
|
bits := uint32(0x1d00ffff)
|
2016-08-08 18:42:54 +02:00
|
|
|
baseBlockHdr := &BlockHeader{
|
2014-01-19 03:31:08 +01:00
|
|
|
Version: 1,
|
2014-05-28 07:26:47 +02:00
|
|
|
PrevBlock: mainNetGenesisHash,
|
|
|
|
MerkleRoot: mainNetGenesisMerkleRoot,
|
2014-01-19 03:31:08 +01:00
|
|
|
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
|
|
|
Bits: bits,
|
|
|
|
Nonce: nonce,
|
|
|
|
}
|
|
|
|
|
|
|
|
// baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
|
|
|
|
baseBlockHdrEncoded := []byte{
|
|
|
|
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
|
|
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
|
|
|
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
|
|
|
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
|
|
|
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
|
|
|
|
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
|
|
|
|
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
|
|
|
|
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
|
|
|
|
0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
|
|
|
|
0x29, 0xab, 0x5f, 0x49, // Timestamp
|
|
|
|
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
|
|
0xf3, 0xe0, 0x01, 0x00, // Nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2016-08-08 18:42:54 +02:00
|
|
|
in *BlockHeader // Data to encode
|
|
|
|
out *BlockHeader // Expected decoded data
|
|
|
|
buf []byte // Serialized data
|
2014-01-19 03:31:08 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdr,
|
|
|
|
baseBlockHdrEncoded,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
// Serialize the block header.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := test.in.Serialize(&buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Serialize #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
|
|
t.Errorf("Serialize #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the block header.
|
2016-08-08 18:42:54 +02:00
|
|
|
var bh BlockHeader
|
2014-06-05 06:39:03 +02:00
|
|
|
rbuf := bytes.NewReader(test.buf)
|
2014-01-19 03:31:08 +01:00
|
|
|
err = bh.Deserialize(rbuf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Deserialize #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&bh, test.out) {
|
|
|
|
t.Errorf("Deserialize #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(&bh), spew.Sdump(test.out))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|