1 // Copyright (c) 2013-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
13 "github.com/davecgh/go-spew/spew"
16 // TestBlockHeader tests the BlockHeader API.
17 func TestBlockHeader(t *testing.T) {
18 nonce64, err := RandomUint64()
20 t.Errorf("RandomUint64: Error generating nonce: %v", err)
22 nonce := uint32(nonce64)
24 hash := mainNetGenesisHash
25 merkleHash := mainNetGenesisMerkleRoot
26 bits := uint32(0x1d00ffff)
27 bh := NewBlockHeader(1, &hash, &merkleHash, bits, nonce)
29 // Ensure we get the same data back out.
30 if !bh.PrevBlock.IsEqual(&hash) {
31 t.Errorf("NewBlockHeader: wrong prev hash - got %v, want %v",
32 spew.Sprint(bh.PrevBlock), spew.Sprint(hash))
34 if !bh.MerkleRoot.IsEqual(&merkleHash) {
35 t.Errorf("NewBlockHeader: wrong merkle root - got %v, want %v",
36 spew.Sprint(bh.MerkleRoot), spew.Sprint(merkleHash))
39 t.Errorf("NewBlockHeader: wrong bits - got %v, want %v",
42 if bh.Nonce != nonce {
43 t.Errorf("NewBlockHeader: wrong nonce - got %v, want %v",
48 // TestBlockHeaderWire tests the BlockHeader wire encode and decode for various
50 func TestBlockHeaderWire(t *testing.T) {
51 nonce := uint32(123123) // 0x1e0f3
54 // baseBlockHdr is used in the various tests as a baseline BlockHeader.
55 bits := uint32(0x1d00ffff)
56 baseBlockHdr := &BlockHeader{
58 PrevBlock: mainNetGenesisHash,
59 MerkleRoot: mainNetGenesisMerkleRoot,
60 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
65 // baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
66 baseBlockHdrEncoded := []byte{
67 0x01, 0x00, 0x00, 0x00, // Version 1
68 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
69 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
70 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
71 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
72 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
73 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
74 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
75 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
76 0x29, 0xab, 0x5f, 0x49, // Timestamp
77 0xff, 0xff, 0x00, 0x1d, // Bits
78 0xf3, 0xe0, 0x01, 0x00, // Nonce
82 in *BlockHeader // Data to encode
83 out *BlockHeader // Expected decoded data
84 buf []byte // Wire encoding
85 pver uint32 // Protocol version for wire encoding
86 enc MessageEncoding // Message encoding variant to use
88 // Latest protocol version.
97 // Protocol version BIP0035Version.
106 // Protocol version BIP0031Version.
115 // Protocol version NetAddressTimeVersion.
120 NetAddressTimeVersion,
124 // Protocol version MultipleAddressVersion.
129 MultipleAddressVersion,
134 t.Logf("Running %d tests", len(tests))
135 for i, test := range tests {
136 // Encode to wire format.
138 err := writeBlockHeader(&buf, test.pver, test.in)
140 t.Errorf("writeBlockHeader #%d error %v", i, err)
143 if !bytes.Equal(buf.Bytes(), test.buf) {
144 t.Errorf("writeBlockHeader #%d\n got: %s want: %s", i,
145 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
150 err = test.in.BtcEncode(&buf, pver, 0)
152 t.Errorf("BtcEncode #%d error %v", i, err)
155 if !bytes.Equal(buf.Bytes(), test.buf) {
156 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
157 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
161 // Decode the block header from wire format.
163 rbuf := bytes.NewReader(test.buf)
164 err = readBlockHeader(rbuf, test.pver, &bh)
166 t.Errorf("readBlockHeader #%d error %v", i, err)
169 if !reflect.DeepEqual(&bh, test.out) {
170 t.Errorf("readBlockHeader #%d\n got: %s want: %s", i,
171 spew.Sdump(&bh), spew.Sdump(test.out))
175 rbuf = bytes.NewReader(test.buf)
176 err = bh.BtcDecode(rbuf, pver, test.enc)
178 t.Errorf("BtcDecode #%d error %v", i, err)
181 if !reflect.DeepEqual(&bh, test.out) {
182 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
183 spew.Sdump(&bh), spew.Sdump(test.out))
189 // TestBlockHeaderSerialize tests BlockHeader serialize and deserialize.
190 func TestBlockHeaderSerialize(t *testing.T) {
191 nonce := uint32(123123) // 0x1e0f3
193 // baseBlockHdr is used in the various tests as a baseline BlockHeader.
194 bits := uint32(0x1d00ffff)
195 baseBlockHdr := &BlockHeader{
197 PrevBlock: mainNetGenesisHash,
198 MerkleRoot: mainNetGenesisMerkleRoot,
199 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
204 // baseBlockHdrEncoded is the wire encoded bytes of baseBlockHdr.
205 baseBlockHdrEncoded := []byte{
206 0x01, 0x00, 0x00, 0x00, // Version 1
207 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
208 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
209 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
210 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
211 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
212 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
213 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
214 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
215 0x29, 0xab, 0x5f, 0x49, // Timestamp
216 0xff, 0xff, 0x00, 0x1d, // Bits
217 0xf3, 0xe0, 0x01, 0x00, // Nonce
221 in *BlockHeader // Data to encode
222 out *BlockHeader // Expected decoded data
223 buf []byte // Serialized data
232 t.Logf("Running %d tests", len(tests))
233 for i, test := range tests {
234 // Serialize the block header.
236 err := test.in.Serialize(&buf)
238 t.Errorf("Serialize #%d error %v", i, err)
241 if !bytes.Equal(buf.Bytes(), test.buf) {
242 t.Errorf("Serialize #%d\n got: %s want: %s", i,
243 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
247 // Deserialize the block header.
249 rbuf := bytes.NewReader(test.buf)
250 err = bh.Deserialize(rbuf)
252 t.Errorf("Deserialize #%d error %v", i, err)
255 if !reflect.DeepEqual(&bh, test.out) {
256 t.Errorf("Deserialize #%d\n got: %s want: %s", i,
257 spew.Sdump(&bh), spew.Sdump(test.out))