github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgtx_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 "fmt" 11 "io" 12 "reflect" 13 "testing" 14 15 "github.com/dashpay/godash/wire" 16 "github.com/davecgh/go-spew/spew" 17 ) 18 19 // TestTx tests the MsgTx API. 20 func TestTx(t *testing.T) { 21 pver := wire.ProtocolVersion 22 23 // Block 100000 hash. 24 hashStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506" 25 hash, err := wire.NewShaHashFromStr(hashStr) 26 if err != nil { 27 t.Errorf("NewShaHashFromStr: %v", err) 28 } 29 30 // Ensure the command is expected value. 31 wantCmd := "tx" 32 msg := wire.NewMsgTx() 33 if cmd := msg.Command(); cmd != wantCmd { 34 t.Errorf("NewMsgAddr: wrong command - got %v want %v", 35 cmd, wantCmd) 36 } 37 38 // Ensure max payload is expected value for latest protocol version. 39 // Num addresses (varInt) + max allowed addresses. 40 wantPayload := uint32(1000 * 1000) 41 maxPayload := msg.MaxPayloadLength(pver) 42 if maxPayload != wantPayload { 43 t.Errorf("MaxPayloadLength: wrong max payload length for "+ 44 "protocol version %d - got %v, want %v", pver, 45 maxPayload, wantPayload) 46 } 47 48 // Ensure we get the same transaction output point data back out. 49 // NOTE: This is a block hash and made up index, but we're only 50 // testing package functionality. 51 prevOutIndex := uint32(1) 52 prevOut := wire.NewOutPoint(hash, prevOutIndex) 53 if !prevOut.Hash.IsEqual(hash) { 54 t.Errorf("NewOutPoint: wrong hash - got %v, want %v", 55 spew.Sprint(&prevOut.Hash), spew.Sprint(hash)) 56 } 57 if prevOut.Index != prevOutIndex { 58 t.Errorf("NewOutPoint: wrong index - got %v, want %v", 59 prevOut.Index, prevOutIndex) 60 } 61 prevOutStr := fmt.Sprintf("%s:%d", hash.String(), prevOutIndex) 62 if s := prevOut.String(); s != prevOutStr { 63 t.Errorf("OutPoint.String: unexpected result - got %v, "+ 64 "want %v", s, prevOutStr) 65 } 66 67 // Ensure we get the same transaction input back out. 68 sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62} 69 txIn := wire.NewTxIn(prevOut, sigScript) 70 if !reflect.DeepEqual(&txIn.PreviousOutPoint, prevOut) { 71 t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v", 72 spew.Sprint(&txIn.PreviousOutPoint), 73 spew.Sprint(prevOut)) 74 } 75 if !bytes.Equal(txIn.SignatureScript, sigScript) { 76 t.Errorf("NewTxIn: wrong signature script - got %v, want %v", 77 spew.Sdump(txIn.SignatureScript), 78 spew.Sdump(sigScript)) 79 } 80 81 // Ensure we get the same transaction output back out. 82 txValue := int64(5000000000) 83 pkScript := []byte{ 84 0x41, // OP_DATA_65 85 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 86 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 87 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 88 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 89 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 90 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 91 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 92 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 93 0xa6, // 65-byte signature 94 0xac, // OP_CHECKSIG 95 } 96 txOut := wire.NewTxOut(txValue, pkScript) 97 if txOut.Value != txValue { 98 t.Errorf("NewTxOut: wrong pk script - got %v, want %v", 99 txOut.Value, txValue) 100 101 } 102 if !bytes.Equal(txOut.PkScript, pkScript) { 103 t.Errorf("NewTxOut: wrong pk script - got %v, want %v", 104 spew.Sdump(txOut.PkScript), 105 spew.Sdump(pkScript)) 106 } 107 108 // Ensure transaction inputs are added properly. 109 msg.AddTxIn(txIn) 110 if !reflect.DeepEqual(msg.TxIn[0], txIn) { 111 t.Errorf("AddTxIn: wrong transaction input added - got %v, want %v", 112 spew.Sprint(msg.TxIn[0]), spew.Sprint(txIn)) 113 } 114 115 // Ensure transaction outputs are added properly. 116 msg.AddTxOut(txOut) 117 if !reflect.DeepEqual(msg.TxOut[0], txOut) { 118 t.Errorf("AddTxIn: wrong transaction output added - got %v, want %v", 119 spew.Sprint(msg.TxOut[0]), spew.Sprint(txOut)) 120 } 121 122 // Ensure the copy produced an identical transaction message. 123 newMsg := msg.Copy() 124 if !reflect.DeepEqual(newMsg, msg) { 125 t.Errorf("Copy: mismatched tx messages - got %v, want %v", 126 spew.Sdump(newMsg), spew.Sdump(msg)) 127 } 128 129 return 130 } 131 132 // TestTxSha tests the ability to generate the hash of a transaction accurately. 133 func TestTxSha(t *testing.T) { 134 // Hash of first transaction from block 113875. 135 hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86" 136 wantHash, err := wire.NewShaHashFromStr(hashStr) 137 if err != nil { 138 t.Errorf("NewShaHashFromStr: %v", err) 139 return 140 } 141 142 // First transaction from block 113875. 143 msgTx := wire.NewMsgTx() 144 txIn := wire.TxIn{ 145 PreviousOutPoint: wire.OutPoint{ 146 Hash: wire.ShaHash{}, 147 Index: 0xffffffff, 148 }, 149 SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}, 150 Sequence: 0xffffffff, 151 } 152 txOut := wire.TxOut{ 153 Value: 5000000000, 154 PkScript: []byte{ 155 0x41, // OP_DATA_65 156 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 157 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 158 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 159 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 160 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 161 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 162 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 163 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 164 0xa6, // 65-byte signature 165 0xac, // OP_CHECKSIG 166 }, 167 } 168 msgTx.AddTxIn(&txIn) 169 msgTx.AddTxOut(&txOut) 170 msgTx.LockTime = 0 171 172 // Ensure the hash produced is expected. 173 txHash := msgTx.TxSha() 174 if !txHash.IsEqual(wantHash) { 175 t.Errorf("TxSha: wrong hash - got %v, want %v", 176 spew.Sprint(txHash), spew.Sprint(wantHash)) 177 } 178 } 179 180 // TestTxWire tests the MsgTx wire encode and decode for various numbers 181 // of transaction inputs and outputs and protocol versions. 182 func TestTxWire(t *testing.T) { 183 // Empty tx message. 184 noTx := wire.NewMsgTx() 185 noTx.Version = 1 186 noTxEncoded := []byte{ 187 0x01, 0x00, 0x00, 0x00, // Version 188 0x00, // Varint for number of input transactions 189 0x00, // Varint for number of output transactions 190 0x00, 0x00, 0x00, 0x00, // Lock time 191 } 192 193 tests := []struct { 194 in *wire.MsgTx // Message to encode 195 out *wire.MsgTx // Expected decoded message 196 buf []byte // Wire encoding 197 pver uint32 // Protocol version for wire encoding 198 }{ 199 // Latest protocol version with no transactions. 200 { 201 noTx, 202 noTx, 203 noTxEncoded, 204 wire.ProtocolVersion, 205 }, 206 207 // Latest protocol version with multiple transactions. 208 { 209 multiTx, 210 multiTx, 211 multiTxEncoded, 212 wire.ProtocolVersion, 213 }, 214 215 // Protocol version BIP0035Version with no transactions. 216 { 217 noTx, 218 noTx, 219 noTxEncoded, 220 wire.BIP0035Version, 221 }, 222 223 // Protocol version BIP0035Version with multiple transactions. 224 { 225 multiTx, 226 multiTx, 227 multiTxEncoded, 228 wire.BIP0035Version, 229 }, 230 231 // Protocol version BIP0031Version with no transactions. 232 { 233 noTx, 234 noTx, 235 noTxEncoded, 236 wire.BIP0031Version, 237 }, 238 239 // Protocol version BIP0031Version with multiple transactions. 240 { 241 multiTx, 242 multiTx, 243 multiTxEncoded, 244 wire.BIP0031Version, 245 }, 246 247 // Protocol version NetAddressTimeVersion with no transactions. 248 { 249 noTx, 250 noTx, 251 noTxEncoded, 252 wire.NetAddressTimeVersion, 253 }, 254 255 // Protocol version NetAddressTimeVersion with multiple transactions. 256 { 257 multiTx, 258 multiTx, 259 multiTxEncoded, 260 wire.NetAddressTimeVersion, 261 }, 262 263 // Protocol version MultipleAddressVersion with no transactions. 264 { 265 noTx, 266 noTx, 267 noTxEncoded, 268 wire.MultipleAddressVersion, 269 }, 270 271 // Protocol version MultipleAddressVersion with multiple transactions. 272 { 273 multiTx, 274 multiTx, 275 multiTxEncoded, 276 wire.MultipleAddressVersion, 277 }, 278 } 279 280 t.Logf("Running %d tests", len(tests)) 281 for i, test := range tests { 282 // Encode the message to wire format. 283 var buf bytes.Buffer 284 err := test.in.BtcEncode(&buf, test.pver) 285 if err != nil { 286 t.Errorf("BtcEncode #%d error %v", i, err) 287 continue 288 } 289 if !bytes.Equal(buf.Bytes(), test.buf) { 290 t.Errorf("BtcEncode #%d\n got: %s want: %s", i, 291 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) 292 continue 293 } 294 295 // Decode the message from wire format. 296 var msg wire.MsgTx 297 rbuf := bytes.NewReader(test.buf) 298 err = msg.BtcDecode(rbuf, test.pver) 299 if err != nil { 300 t.Errorf("BtcDecode #%d error %v", i, err) 301 continue 302 } 303 if !reflect.DeepEqual(&msg, test.out) { 304 t.Errorf("BtcDecode #%d\n got: %s want: %s", i, 305 spew.Sdump(&msg), spew.Sdump(test.out)) 306 continue 307 } 308 } 309 } 310 311 // TestTxWireErrors performs negative tests against wire encode and decode 312 // of MsgTx to confirm error paths work correctly. 313 func TestTxWireErrors(t *testing.T) { 314 // Use protocol version 60002 specifically here instead of the latest 315 // because the test data is using bytes encoded with that protocol 316 // version. 317 pver := uint32(60002) 318 319 tests := []struct { 320 in *wire.MsgTx // Value to encode 321 buf []byte // Wire encoding 322 pver uint32 // Protocol version for wire encoding 323 max int // Max size of fixed buffer to induce errors 324 writeErr error // Expected write error 325 readErr error // Expected read error 326 }{ 327 // Force error in version. 328 {multiTx, multiTxEncoded, pver, 0, io.ErrShortWrite, io.EOF}, 329 // Force error in number of transaction inputs. 330 {multiTx, multiTxEncoded, pver, 4, io.ErrShortWrite, io.EOF}, 331 // Force error in transaction input previous block hash. 332 {multiTx, multiTxEncoded, pver, 5, io.ErrShortWrite, io.EOF}, 333 // Force error in transaction input previous block output index. 334 {multiTx, multiTxEncoded, pver, 37, io.ErrShortWrite, io.EOF}, 335 // Force error in transaction input signature script length. 336 {multiTx, multiTxEncoded, pver, 41, io.ErrShortWrite, io.EOF}, 337 // Force error in transaction input signature script. 338 {multiTx, multiTxEncoded, pver, 42, io.ErrShortWrite, io.EOF}, 339 // Force error in transaction input sequence. 340 {multiTx, multiTxEncoded, pver, 49, io.ErrShortWrite, io.EOF}, 341 // Force error in number of transaction outputs. 342 {multiTx, multiTxEncoded, pver, 53, io.ErrShortWrite, io.EOF}, 343 // Force error in transaction output value. 344 {multiTx, multiTxEncoded, pver, 54, io.ErrShortWrite, io.EOF}, 345 // Force error in transaction output pk script length. 346 {multiTx, multiTxEncoded, pver, 62, io.ErrShortWrite, io.EOF}, 347 // Force error in transaction output pk script. 348 {multiTx, multiTxEncoded, pver, 63, io.ErrShortWrite, io.EOF}, 349 // Force error in transaction output lock time. 350 {multiTx, multiTxEncoded, pver, 206, io.ErrShortWrite, io.EOF}, 351 } 352 353 t.Logf("Running %d tests", len(tests)) 354 for i, test := range tests { 355 // Encode to wire format. 356 w := newFixedWriter(test.max) 357 err := test.in.BtcEncode(w, test.pver) 358 if err != test.writeErr { 359 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", 360 i, err, test.writeErr) 361 continue 362 } 363 364 // Decode from wire format. 365 var msg wire.MsgTx 366 r := newFixedReader(test.max, test.buf) 367 err = msg.BtcDecode(r, test.pver) 368 if err != test.readErr { 369 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", 370 i, err, test.readErr) 371 continue 372 } 373 } 374 } 375 376 // TestTxSerialize tests MsgTx serialize and deserialize. 377 func TestTxSerialize(t *testing.T) { 378 noTx := wire.NewMsgTx() 379 noTx.Version = 1 380 noTxEncoded := []byte{ 381 0x01, 0x00, 0x00, 0x00, // Version 382 0x00, // Varint for number of input transactions 383 0x00, // Varint for number of output transactions 384 0x00, 0x00, 0x00, 0x00, // Lock time 385 } 386 387 tests := []struct { 388 in *wire.MsgTx // Message to encode 389 out *wire.MsgTx // Expected decoded message 390 buf []byte // Serialized data 391 pkScriptLocs []int // Expected output script locations 392 }{ 393 // No transactions. 394 { 395 noTx, 396 noTx, 397 noTxEncoded, 398 nil, 399 }, 400 401 // Multiple transactions. 402 { 403 multiTx, 404 multiTx, 405 multiTxEncoded, 406 multiTxPkScriptLocs, 407 }, 408 } 409 410 t.Logf("Running %d tests", len(tests)) 411 for i, test := range tests { 412 // Serialize the transaction. 413 var buf bytes.Buffer 414 err := test.in.Serialize(&buf) 415 if err != nil { 416 t.Errorf("Serialize #%d error %v", i, err) 417 continue 418 } 419 if !bytes.Equal(buf.Bytes(), test.buf) { 420 t.Errorf("Serialize #%d\n got: %s want: %s", i, 421 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) 422 continue 423 } 424 425 // Deserialize the transaction. 426 var tx wire.MsgTx 427 rbuf := bytes.NewReader(test.buf) 428 err = tx.Deserialize(rbuf) 429 if err != nil { 430 t.Errorf("Deserialize #%d error %v", i, err) 431 continue 432 } 433 if !reflect.DeepEqual(&tx, test.out) { 434 t.Errorf("Deserialize #%d\n got: %s want: %s", i, 435 spew.Sdump(&tx), spew.Sdump(test.out)) 436 continue 437 } 438 439 // Ensure the public key script locations are accurate. 440 pkScriptLocs := test.in.PkScriptLocs() 441 if !reflect.DeepEqual(pkScriptLocs, test.pkScriptLocs) { 442 t.Errorf("PkScriptLocs #%d\n got: %s want: %s", i, 443 spew.Sdump(pkScriptLocs), 444 spew.Sdump(test.pkScriptLocs)) 445 continue 446 } 447 for j, loc := range pkScriptLocs { 448 wantPkScript := test.in.TxOut[j].PkScript 449 gotPkScript := test.buf[loc : loc+len(wantPkScript)] 450 if !bytes.Equal(gotPkScript, wantPkScript) { 451 t.Errorf("PkScriptLocs #%d:%d\n unexpected "+ 452 "script got: %s want: %s", i, j, 453 spew.Sdump(gotPkScript), 454 spew.Sdump(wantPkScript)) 455 } 456 } 457 } 458 } 459 460 // TestTxSerializeErrors performs negative tests against wire encode and decode 461 // of MsgTx to confirm error paths work correctly. 462 func TestTxSerializeErrors(t *testing.T) { 463 tests := []struct { 464 in *wire.MsgTx // Value to encode 465 buf []byte // Serialized data 466 max int // Max size of fixed buffer to induce errors 467 writeErr error // Expected write error 468 readErr error // Expected read error 469 }{ 470 // Force error in version. 471 {multiTx, multiTxEncoded, 0, io.ErrShortWrite, io.EOF}, 472 // Force error in number of transaction inputs. 473 {multiTx, multiTxEncoded, 4, io.ErrShortWrite, io.EOF}, 474 // Force error in transaction input previous block hash. 475 {multiTx, multiTxEncoded, 5, io.ErrShortWrite, io.EOF}, 476 // Force error in transaction input previous block output index. 477 {multiTx, multiTxEncoded, 37, io.ErrShortWrite, io.EOF}, 478 // Force error in transaction input signature script length. 479 {multiTx, multiTxEncoded, 41, io.ErrShortWrite, io.EOF}, 480 // Force error in transaction input signature script. 481 {multiTx, multiTxEncoded, 42, io.ErrShortWrite, io.EOF}, 482 // Force error in transaction input sequence. 483 {multiTx, multiTxEncoded, 49, io.ErrShortWrite, io.EOF}, 484 // Force error in number of transaction outputs. 485 {multiTx, multiTxEncoded, 53, io.ErrShortWrite, io.EOF}, 486 // Force error in transaction output value. 487 {multiTx, multiTxEncoded, 54, io.ErrShortWrite, io.EOF}, 488 // Force error in transaction output pk script length. 489 {multiTx, multiTxEncoded, 62, io.ErrShortWrite, io.EOF}, 490 // Force error in transaction output pk script. 491 {multiTx, multiTxEncoded, 63, io.ErrShortWrite, io.EOF}, 492 // Force error in transaction output lock time. 493 {multiTx, multiTxEncoded, 206, io.ErrShortWrite, io.EOF}, 494 } 495 496 t.Logf("Running %d tests", len(tests)) 497 for i, test := range tests { 498 // Serialize the transaction. 499 w := newFixedWriter(test.max) 500 err := test.in.Serialize(w) 501 if err != test.writeErr { 502 t.Errorf("Serialize #%d wrong error got: %v, want: %v", 503 i, err, test.writeErr) 504 continue 505 } 506 507 // Deserialize the transaction. 508 var tx wire.MsgTx 509 r := newFixedReader(test.max, test.buf) 510 err = tx.Deserialize(r) 511 if err != test.readErr { 512 t.Errorf("Deserialize #%d wrong error got: %v, want: %v", 513 i, err, test.readErr) 514 continue 515 } 516 } 517 } 518 519 // TestTxOverflowErrors performs tests to ensure deserializing transactions 520 // which are intentionally crafted to use large values for the variable number 521 // of inputs and outputs are handled properly. This could otherwise potentially 522 // be used as an attack vector. 523 func TestTxOverflowErrors(t *testing.T) { 524 // Use protocol version 70001 and transaction version 1 specifically 525 // here instead of the latest values because the test data is using 526 // bytes encoded with those versions. 527 pver := uint32(70001) 528 txVer := uint32(1) 529 530 tests := []struct { 531 buf []byte // Wire encoding 532 pver uint32 // Protocol version for wire encoding 533 version uint32 // Transaction version 534 err error // Expected error 535 }{ 536 // Transaction that claims to have ~uint64(0) inputs. 537 { 538 []byte{ 539 0x00, 0x00, 0x00, 0x01, // Version 540 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 541 0xff, // Varint for number of input transactions 542 }, pver, txVer, &wire.MessageError{}, 543 }, 544 545 // Transaction that claims to have ~uint64(0) outputs. 546 { 547 []byte{ 548 0x00, 0x00, 0x00, 0x01, // Version 549 0x00, // Varint for number of input transactions 550 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 551 0xff, // Varint for number of output transactions 552 }, pver, txVer, &wire.MessageError{}, 553 }, 554 555 // Transaction that has an input with a signature script that 556 // claims to have ~uint64(0) length. 557 { 558 []byte{ 559 0x00, 0x00, 0x00, 0x01, // Version 560 0x01, // Varint for number of input transactions 561 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 562 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 563 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 564 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash 565 0xff, 0xff, 0xff, 0xff, // Prevous output index 566 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 567 0xff, // Varint for length of signature script 568 }, pver, txVer, &wire.MessageError{}, 569 }, 570 571 // Transaction that has an output with a public key script 572 // that claims to have ~uint64(0) length. 573 { 574 []byte{ 575 0x00, 0x00, 0x00, 0x01, // Version 576 0x01, // Varint for number of input transactions 577 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 578 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 579 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 580 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash 581 0xff, 0xff, 0xff, 0xff, // Prevous output index 582 0x00, // Varint for length of signature script 583 0xff, 0xff, 0xff, 0xff, // Sequence 584 0x01, // Varint for number of output transactions 585 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount 586 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 587 0xff, // Varint for length of public key script 588 }, pver, txVer, &wire.MessageError{}, 589 }, 590 } 591 592 t.Logf("Running %d tests", len(tests)) 593 for i, test := range tests { 594 // Decode from wire format. 595 var msg wire.MsgTx 596 r := bytes.NewReader(test.buf) 597 err := msg.BtcDecode(r, test.pver) 598 if reflect.TypeOf(err) != reflect.TypeOf(test.err) { 599 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", 600 i, err, reflect.TypeOf(test.err)) 601 continue 602 } 603 604 // Decode from wire format. 605 r = bytes.NewReader(test.buf) 606 err = msg.Deserialize(r) 607 if reflect.TypeOf(err) != reflect.TypeOf(test.err) { 608 t.Errorf("Deserialize #%d wrong error got: %v, want: %v", 609 i, err, reflect.TypeOf(test.err)) 610 continue 611 } 612 } 613 } 614 615 // TestTxSerializeSize performs tests to ensure the serialize size for various 616 // transactions is accurate. 617 func TestTxSerializeSize(t *testing.T) { 618 // Empty tx message. 619 noTx := wire.NewMsgTx() 620 noTx.Version = 1 621 622 tests := []struct { 623 in *wire.MsgTx // Tx to encode 624 size int // Expected serialized size 625 }{ 626 // No inputs or outpus. 627 {noTx, 10}, 628 629 // Transcaction with an input and an output. 630 {multiTx, 210}, 631 } 632 633 t.Logf("Running %d tests", len(tests)) 634 for i, test := range tests { 635 serializedSize := test.in.SerializeSize() 636 if serializedSize != test.size { 637 t.Errorf("MsgTx.SerializeSize: #%d got: %d, want: %d", i, 638 serializedSize, test.size) 639 continue 640 } 641 } 642 } 643 644 // multiTx is a MsgTx with an input and output and used in various tests. 645 var multiTx = &wire.MsgTx{ 646 Version: 1, 647 TxIn: []*wire.TxIn{ 648 { 649 PreviousOutPoint: wire.OutPoint{ 650 Hash: wire.ShaHash{}, 651 Index: 0xffffffff, 652 }, 653 SignatureScript: []byte{ 654 0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62, 655 }, 656 Sequence: 0xffffffff, 657 }, 658 }, 659 TxOut: []*wire.TxOut{ 660 { 661 Value: 0x12a05f200, 662 PkScript: []byte{ 663 0x41, // OP_DATA_65 664 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 665 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 666 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 667 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 668 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 669 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 670 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 671 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 672 0xa6, // 65-byte signature 673 0xac, // OP_CHECKSIG 674 }, 675 }, 676 { 677 Value: 0x5f5e100, 678 PkScript: []byte{ 679 0x41, // OP_DATA_65 680 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 681 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 682 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 683 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 684 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 685 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 686 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 687 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 688 0xa6, // 65-byte signature 689 0xac, // OP_CHECKSIG 690 }, 691 }, 692 }, 693 LockTime: 0, 694 } 695 696 // multiTxEncoded is the wire encoded bytes for multiTx using protocol version 697 // 60002 and is used in the various tests. 698 var multiTxEncoded = []byte{ 699 0x01, 0x00, 0x00, 0x00, // Version 700 0x01, // Varint for number of input transactions 701 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 702 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 703 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 704 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash 705 0xff, 0xff, 0xff, 0xff, // Prevous output index 706 0x07, // Varint for length of signature script 707 0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62, // Signature script 708 0xff, 0xff, 0xff, 0xff, // Sequence 709 0x02, // Varint for number of output transactions 710 0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount 711 0x43, // Varint for length of pk script 712 0x41, // OP_DATA_65 713 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 714 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 715 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 716 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 717 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 718 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 719 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 720 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 721 0xa6, // 65-byte signature 722 0xac, // OP_CHECKSIG 723 0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00, // Transaction amount 724 0x43, // Varint for length of pk script 725 0x41, // OP_DATA_65 726 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 727 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 728 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 729 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 730 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 731 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 732 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 733 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 734 0xa6, // 65-byte signature 735 0xac, // OP_CHECKSIG 736 0x00, 0x00, 0x00, 0x00, // Lock time 737 } 738 739 // multiTxPkScriptLocs is the location information for the public key scripts 740 // located in multiTx. 741 var multiTxPkScriptLocs = []int{63, 139}