// FetchTxAllBySha returns several pieces of data regarding the given sha. func (db *SqliteDb) FetchTxAllBySha(txsha *btcwire.ShaHash) (rtx *btcwire.MsgTx, rtxbuf []byte, rpver uint32, rblksha *btcwire.ShaHash, err error) { // Check Tx cache if txc, ok := db.fetchTxCache(txsha); ok { return txc.tx, txc.txbuf, txc.pver, &txc.blksha, nil } // If not cached load it bidx, toff, tlen, err := db.FetchLocationBySha(txsha) if err != nil { log.Warnf("unable to find location of origin tx %v", txsha) return } blksha, err := db.FetchBlockShaByHeight(bidx) if err != nil { log.Warnf("block idx lookup %v to %v", bidx, err) return } log.Tracef("transaction %v is at block %v %v tx %v", txsha, blksha, bidx, toff) blk, err := db.FetchBlockBySha(blksha) if err != nil { log.Warnf("unable to fetch block %v %v ", bidx, &blksha) return } blkbuf, pver, err := blk.Bytes() if err != nil { log.Warnf("unable to decode block %v %v", bidx, &blksha) return } txbuf := make([]byte, tlen) copy(txbuf[:], blkbuf[toff:toff+tlen]) rbuf := bytes.NewBuffer(txbuf) var tx btcwire.MsgTx err = tx.BtcDecode(rbuf, pver) if err != nil { log.Warnf("unable to decode tx block %v %v txoff %v txlen %v", bidx, &blksha, toff, tlen) return } // Shove data into TxCache // XXX - var txc txCacheObj txc.sha = *txsha txc.tx = &tx txc.txbuf = txbuf txc.pver = pver txc.blksha = *blksha db.insertTxCache(&txc) return &tx, txbuf, pver, blksha, nil }
// TestTxOverflowErrors performs tests to ensure deserializing transactions // which are intentionally crafted to use large values for the variable number // of inputs and outputs are handled properly. This could otherwise potentially // be used as an attack vector. func TestTxOverflowErrors(t *testing.T) { // Use protocol version 70001 and transaction version 1 specifically // here instead of the latest values because the test data is using // bytes encoded with those versions. pver := uint32(70001) txVer := uint32(1) tests := []struct { buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding version uint32 // Transaction version err error // Expected error }{ // Transaction that claims to have ~uint64(0) inputs. { []byte{ 0x00, 0x00, 0x00, 0x01, // Version 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for number of input transactions }, pver, txVer, &btcwire.MessageError{}, }, // Transaction that claims to have ~uint64(0) outputs. { []byte{ 0x00, 0x00, 0x00, 0x01, // Version 0x00, // Varint for number of input transactions 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for number of output transactions }, pver, txVer, &btcwire.MessageError{}, }, // Transaction that has an input with a signature script that // claims to have ~uint64(0) length. { []byte{ 0x00, 0x00, 0x00, 0x01, // Version 0x01, // Varint for number of input transactions 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash 0xff, 0xff, 0xff, 0xff, // Prevous output index 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for length of signature script }, pver, txVer, &btcwire.MessageError{}, }, // Transaction that has an output with a public key script // that claims to have ~uint64(0) length. { []byte{ 0x00, 0x00, 0x00, 0x01, // Version 0x01, // Varint for number of input transactions 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash 0xff, 0xff, 0xff, 0xff, // Prevous output index 0x00, // Varint for length of signature script 0xff, 0xff, 0xff, 0xff, // Sequence 0x01, // Varint for number of output transactions 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Varint for length of public key script }, pver, txVer, &btcwire.MessageError{}, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Decode from wire format. var msg btcwire.MsgTx r := bytes.NewReader(test.buf) err := msg.BtcDecode(r, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.err) { t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", i, err, reflect.TypeOf(test.err)) continue } // Decode from wire format. r = bytes.NewReader(test.buf) err = msg.Deserialize(r) if reflect.TypeOf(err) != reflect.TypeOf(test.err) { t.Errorf("Deserialize #%d wrong error got: %v, want: %v", i, err, reflect.TypeOf(test.err)) continue } } }
// TestTxWireErrors performs negative tests against wire encode and decode // of MsgTx to confirm error paths work correctly. func TestTxWireErrors(t *testing.T) { // Use protocol version 60002 specifically here instead of the latest // because the test data is using bytes encoded with that protocol // version. pver := uint32(60002) tests := []struct { in *btcwire.MsgTx // Value to encode buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ // Force error in version. {multiTx, multiTxEncoded, pver, 0, io.ErrShortWrite, io.EOF}, // Force error in number of transaction inputs. {multiTx, multiTxEncoded, pver, 4, io.ErrShortWrite, io.EOF}, // Force error in transaction input previous block hash. {multiTx, multiTxEncoded, pver, 5, io.ErrShortWrite, io.EOF}, // Force error in transaction input previous block hash. {multiTx, multiTxEncoded, pver, 5, io.ErrShortWrite, io.EOF}, // Force error in transaction input previous block output index. {multiTx, multiTxEncoded, pver, 37, io.ErrShortWrite, io.EOF}, // Force error in transaction input signature script length. {multiTx, multiTxEncoded, pver, 41, io.ErrShortWrite, io.EOF}, // Force error in transaction input signature script. {multiTx, multiTxEncoded, pver, 42, io.ErrShortWrite, io.EOF}, // Force error in transaction input sequence. {multiTx, multiTxEncoded, pver, 49, io.ErrShortWrite, io.EOF}, // Force error in number of transaction outputs. {multiTx, multiTxEncoded, pver, 53, io.ErrShortWrite, io.EOF}, // Force error in transaction output value. {multiTx, multiTxEncoded, pver, 54, io.ErrShortWrite, io.EOF}, // Force error in transaction output pk script length. {multiTx, multiTxEncoded, pver, 62, io.ErrShortWrite, io.EOF}, // Force error in transaction output pk script. {multiTx, multiTxEncoded, pver, 63, io.ErrShortWrite, io.EOF}, // Force error in transaction output lock time. {multiTx, multiTxEncoded, pver, 130, io.ErrShortWrite, io.EOF}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. w := newFixedWriter(test.max) err := test.in.BtcEncode(w, test.pver) if err != test.writeErr { t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", i, err, test.writeErr) continue } // Decode from wire format. var msg btcwire.MsgTx r := newFixedReader(test.max, test.buf) err = msg.BtcDecode(r, test.pver) if err != test.readErr { t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", i, err, test.readErr) continue } } }
// TestTxWire tests the MsgTx wire encode and decode for various numbers // of transaction inputs and outputs and protocol versions. func TestTxWire(t *testing.T) { // Empty tx message. noTx := btcwire.NewMsgTx() noTx.Version = 1 noTxEncoded := []byte{ 0x01, 0x00, 0x00, 0x00, // Version 0x00, // Varint for number of input transactions 0x00, // Varint for number of output transactions 0x00, 0x00, 0x00, 0x00, // Lock time } tests := []struct { in *btcwire.MsgTx // Message to encode out *btcwire.MsgTx // Expected decoded message buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding }{ // Latest protocol version with no transactions. { noTx, noTx, noTxEncoded, btcwire.ProtocolVersion, }, // Latest protocol version with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.ProtocolVersion, }, // Protocol version BIP0035Version with no transactions. { noTx, noTx, noTxEncoded, btcwire.BIP0035Version, }, // Protocol version BIP0035Version with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.BIP0035Version, }, // Protocol version BIP0031Version with no transactions. { noTx, noTx, noTxEncoded, btcwire.BIP0031Version, }, // Protocol version BIP0031Version with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.BIP0031Version, }, // Protocol version NetAddressTimeVersion with no transactions. { noTx, noTx, noTxEncoded, btcwire.NetAddressTimeVersion, }, // Protocol version NetAddressTimeVersion with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.NetAddressTimeVersion, }, // Protocol version MultipleAddressVersion with no transactions. { noTx, noTx, noTxEncoded, btcwire.MultipleAddressVersion, }, // Protocol version MultipleAddressVersion with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.MultipleAddressVersion, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode the message to wire format. var buf bytes.Buffer err := test.in.BtcEncode(&buf, test.pver) if err != nil { t.Errorf("BtcEncode #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("BtcEncode #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Decode the message from wire format. var msg btcwire.MsgTx rbuf := bytes.NewReader(test.buf) err = msg.BtcDecode(rbuf, test.pver) if err != nil { t.Errorf("BtcDecode #%d error %v", i, err) continue } if !reflect.DeepEqual(&msg, test.out) { t.Errorf("BtcDecode #%d\n got: %s want: %s", i, spew.Sdump(&msg), spew.Sdump(test.out)) continue } } }
// TestTxWire tests the MsgTx wire encode and decode for various numbers // of transaction inputs and outputs and protocol versions. func TestTxWire(t *testing.T) { // Previous transaction output point for coinbase to test. prevOutIndex := uint32(0xffffffff) prevOut := btcwire.NewOutPoint(&btcwire.ShaHash{}, prevOutIndex) // Transaction input to test. sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62} txIn := btcwire.NewTxIn(prevOut, sigScript) txIn.Sequence = 0xffffffff // Transaction output to test. txValue := int64(5000000000) pkScript := []byte{ 0x41, // OP_DATA_65 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 0xa6, // 65-byte signature 0xac, // OP_CHECKSIG } txOut := btcwire.NewTxOut(txValue, pkScript) // Empty tx message. noTx := btcwire.NewMsgTx() noTx.Version = 1 noTxEncoded := []byte{ 0x01, 0x00, 0x00, 0x00, // Version 0x00, // Varint for number of input transactions 0x00, // Varint for number of output transactions 0x00, 0x00, 0x00, 0x00, // Lock time } multiTx := btcwire.NewMsgTx() multiTx.Version = 1 multiTx.AddTxIn(txIn) multiTx.AddTxOut(txOut) multiTx.LockTime = 0 multiTxEncoded := []byte{ 0x01, 0x00, 0x00, 0x00, // Version 0x01, // Varint for number of input transactions 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash 0xff, 0xff, 0xff, 0xff, // Prevous output index 0x07, // Varint for length of signature script 0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62, // Signature script 0xff, 0xff, 0xff, 0xff, // Sequence 0x01, // Varint for number of output transactions 0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount 0x43, // Varint for length of pk script 0x41, // OP_DATA_65 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 0xa6, // 65-byte signature 0xac, // OP_CHECKSIG 0x00, 0x00, 0x00, 0x00, // Lock time } tests := []struct { in *btcwire.MsgTx // Message to encode out *btcwire.MsgTx // Expected decoded message buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding }{ // Latest protocol version with no transactions. { noTx, noTx, noTxEncoded, btcwire.ProtocolVersion, }, // Latest protocol version with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.ProtocolVersion, }, // Protocol version BIP0035Version with no transactions. { noTx, noTx, noTxEncoded, btcwire.BIP0035Version, }, // Protocol version BIP0035Version with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.BIP0035Version, }, // Protocol version BIP0031Version with no transactions. { noTx, noTx, noTxEncoded, btcwire.BIP0031Version, }, // Protocol version BIP0031Version with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.BIP0031Version, }, // Protocol version NetAddressTimeVersion with no transactions. { noTx, noTx, noTxEncoded, btcwire.NetAddressTimeVersion, }, // Protocol version NetAddressTimeVersion with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.NetAddressTimeVersion, }, // Protocol version MultipleAddressVersion with no transactions. { noTx, noTx, noTxEncoded, btcwire.MultipleAddressVersion, }, // Protocol version MultipleAddressVersion with multiple transactions. { multiTx, multiTx, multiTxEncoded, btcwire.MultipleAddressVersion, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode the message to wire format. var buf bytes.Buffer err := test.in.BtcEncode(&buf, test.pver) if err != nil { t.Errorf("BtcEncode #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("BtcEncode #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Decode the message from wire format. var msg btcwire.MsgTx rbuf := bytes.NewBuffer(test.buf) err = msg.BtcDecode(rbuf, test.pver) if err != nil { t.Errorf("BtcDecode #%d error %v", i, err) continue } if !reflect.DeepEqual(&msg, test.out) { t.Errorf("BtcDecode #%d\n got: %s want: %s", i, spew.Sdump(&msg), spew.Sdump(test.out)) continue } } }