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