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