blockheader_test.go raw

   1  package wire
   2  
   3  import (
   4  	"bytes"
   5  	"reflect"
   6  	"testing"
   7  	"time"
   8  
   9  	"github.com/davecgh/go-spew/spew"
  10  )
  11  
  12  // TestBlockHeader tests the BlockHeader API.
  13  func TestBlockHeader(t *testing.T) {
  14  	nonce64, e := RandomUint64()
  15  	if e != nil  {
  16  		t.Errorf("RandomUint64: Error generating nonce: %v", e)
  17  	}
  18  	nonce := uint32(nonce64)
  19  	hash := mainNetGenesisHash
  20  	merkleHash := mainNetGenesisMerkleRoot
  21  	bits := uint32(0x1d00ffff)
  22  	bh := NewBlockHeader(1, &hash, &merkleHash, bits, nonce)
  23  	// Ensure we get the same data back out.
  24  	if !bh.PrevBlock.IsEqual(&hash) {
  25  		t.Errorf("NewBlockHeader: wrong prev hash - got %v, want %v",
  26  			spew.Sprint(bh.PrevBlock), spew.Sprint(hash))
  27  	}
  28  	if !bh.MerkleRoot.IsEqual(&merkleHash) {
  29  		t.Errorf("NewBlockHeader: wrong merkle root - got %v, want %v",
  30  			spew.Sprint(bh.MerkleRoot), spew.Sprint(merkleHash))
  31  	}
  32  	if bh.Bits != bits {
  33  		t.Errorf("NewBlockHeader: wrong bits - got %v, want %v",
  34  			bh.Bits, bits)
  35  	}
  36  	if bh.Nonce != nonce {
  37  		t.Errorf("NewBlockHeader: wrong nonce - got %v, want %v",
  38  			bh.Nonce, nonce)
  39  	}
  40  }
  41  
  42  // TestBlockHeaderWire tests the BlockHeader wire encode and decode for various protocol versions.
  43  func TestBlockHeaderWire(t *testing.T) {
  44  	nonce := uint32(123123) // 0x1e0f3
  45  	pver := uint32(70001)
  46  	// baseBlockHdr is used in the various tests as a baseline BlockHeader.
  47  	bits := uint32(0x1d00ffff)
  48  	baseBlockHdr := &BlockHeader{
  49  		Version:    1,
  50  		PrevBlock:  mainNetGenesisHash,
  51  		MerkleRoot: mainNetGenesisMerkleRoot,
  52  		Timestamp:  time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
  53  		Bits:       bits,
  54  		Nonce:      nonce,
  55  	}
  56  	// baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
  57  	baseBlockHdrEncoded := []byte{
  58  		0x01, 0x00, 0x00, 0x00, // Version 1
  59  		0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
  60  		0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
  61  		0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
  62  		0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
  63  		0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
  64  		0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
  65  		0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
  66  		0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
  67  		0x29, 0xab, 0x5f, 0x49, // Timestamp
  68  		0xff, 0xff, 0x00, 0x1d, // Bits
  69  		0xf3, 0xe0, 0x01, 0x00, // Nonce
  70  	}
  71  	tests := []struct {
  72  		in   *BlockHeader    // Data to encode
  73  		out  *BlockHeader    // Expected decoded data
  74  		buf  []byte          // Wire encoding
  75  		pver uint32          // Protocol version for wire encoding
  76  		enc  MessageEncoding // Message encoding variant to use
  77  	}{
  78  		// Latest protocol version.
  79  		{
  80  			baseBlockHdr,
  81  			baseBlockHdr,
  82  			baseBlockHdrEncoded,
  83  			ProtocolVersion,
  84  			BaseEncoding,
  85  		},
  86  		// Protocol version BIP0035Version.
  87  		{
  88  			baseBlockHdr,
  89  			baseBlockHdr,
  90  			baseBlockHdrEncoded,
  91  			BIP0035Version,
  92  			BaseEncoding,
  93  		},
  94  		// Protocol version BIP0031Version.
  95  		{
  96  			baseBlockHdr,
  97  			baseBlockHdr,
  98  			baseBlockHdrEncoded,
  99  			BIP0031Version,
 100  			BaseEncoding,
 101  		},
 102  		// Protocol version NetAddressTimeVersion.
 103  		{
 104  			baseBlockHdr,
 105  			baseBlockHdr,
 106  			baseBlockHdrEncoded,
 107  			NetAddressTimeVersion,
 108  			BaseEncoding,
 109  		},
 110  		// Protocol version MultipleAddressVersion.
 111  		{
 112  			baseBlockHdr,
 113  			baseBlockHdr,
 114  			baseBlockHdrEncoded,
 115  			MultipleAddressVersion,
 116  			BaseEncoding,
 117  		},
 118  	}
 119  	t.Logf("Running %d tests", len(tests))
 120  	for i, test := range tests {
 121  		// Encode to wire format.
 122  		var buf bytes.Buffer
 123  		e := writeBlockHeader(&buf, test.pver, test.in)
 124  		if e != nil  {
 125  			t.Errorf("writeBlockHeader #%d error %v", i, e)
 126  			continue
 127  		}
 128  		if !bytes.Equal(buf.Bytes(), test.buf) {
 129  			t.Errorf("writeBlockHeader #%d\n got: %s want: %s", i,
 130  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
 131  			continue
 132  		}
 133  		buf.Reset()
 134  		e = test.in.BtcEncode(&buf, pver, 0)
 135  		if e != nil  {
 136  			t.Errorf("BtcEncode #%d error %v", i, e)
 137  			continue
 138  		}
 139  		if !bytes.Equal(buf.Bytes(), test.buf) {
 140  			t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
 141  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
 142  			continue
 143  		}
 144  		// Decode the block header from wire format.
 145  		var bh BlockHeader
 146  		rbuf := bytes.NewReader(test.buf)
 147  		e = readBlockHeader(rbuf, test.pver, &bh)
 148  		if e != nil  {
 149  			t.Errorf("readBlockHeader #%d error %v", i, e)
 150  			continue
 151  		}
 152  		if !reflect.DeepEqual(&bh, test.out) {
 153  			t.Errorf("readBlockHeader #%d\n got: %s want: %s", i,
 154  				spew.Sdump(&bh), spew.Sdump(test.out))
 155  			continue
 156  		}
 157  		rbuf = bytes.NewReader(test.buf)
 158  		e = bh.BtcDecode(rbuf, pver, test.enc)
 159  		if e != nil  {
 160  			t.Errorf("BtcDecode #%d error %v", i, e)
 161  			continue
 162  		}
 163  		if !reflect.DeepEqual(&bh, test.out) {
 164  			t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
 165  				spew.Sdump(&bh), spew.Sdump(test.out))
 166  			continue
 167  		}
 168  	}
 169  }
 170  
 171  // TestBlockHeaderSerialize tests BlockHeader serialize and deserialize.
 172  func TestBlockHeaderSerialize(t *testing.T) {
 173  	nonce := uint32(123123) // 0x1e0f3
 174  	// baseBlockHdr is used in the various tests as a baseline BlockHeader.
 175  	bits := uint32(0x1d00ffff)
 176  	baseBlockHdr := &BlockHeader{
 177  		Version:    1,
 178  		PrevBlock:  mainNetGenesisHash,
 179  		MerkleRoot: mainNetGenesisMerkleRoot,
 180  		Timestamp:  time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
 181  		Bits:       bits,
 182  		Nonce:      nonce,
 183  	}
 184  	// baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
 185  	baseBlockHdrEncoded := []byte{
 186  		0x01, 0x00, 0x00, 0x00, // Version 1
 187  		0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
 188  		0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
 189  		0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
 190  		0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
 191  		0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
 192  		0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
 193  		0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
 194  		0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
 195  		0x29, 0xab, 0x5f, 0x49, // Timestamp
 196  		0xff, 0xff, 0x00, 0x1d, // Bits
 197  		0xf3, 0xe0, 0x01, 0x00, // Nonce
 198  	}
 199  	tests := []struct {
 200  		in  *BlockHeader // Data to encode
 201  		out *BlockHeader // Expected decoded data
 202  		buf []byte       // Serialized data
 203  	}{
 204  		{
 205  			baseBlockHdr,
 206  			baseBlockHdr,
 207  			baseBlockHdrEncoded,
 208  		},
 209  	}
 210  	t.Logf("Running %d tests", len(tests))
 211  	for i, test := range tests {
 212  		// Serialize the block header.
 213  		var buf bytes.Buffer
 214  		e := test.in.Serialize(&buf)
 215  		if e != nil  {
 216  			t.Errorf("Serialize #%d error %v", i, e)
 217  			continue
 218  		}
 219  		if !bytes.Equal(buf.Bytes(), test.buf) {
 220  			t.Errorf("Serialize #%d\n got: %s want: %s", i,
 221  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
 222  			continue
 223  		}
 224  		// Deserialize the block header.
 225  		var bh BlockHeader
 226  		rbuf := bytes.NewReader(test.buf)
 227  		e = bh.Deserialize(rbuf)
 228  		if e != nil  {
 229  			t.Errorf("Deserialize #%d error %v", i, e)
 230  			continue
 231  		}
 232  		if !reflect.DeepEqual(&bh, test.out) {
 233  			t.Errorf("Deserialize #%d\n got: %s want: %s", i,
 234  				spew.Sdump(&bh), spew.Sdump(test.out))
 235  			continue
 236  		}
 237  	}
 238  }
 239