github.com/btcsuite/btcd@v0.24.0/wire/msgtx.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  	"errors"
     9  	"fmt"
    10  	"io"
    11  	"strconv"
    12  	"strings"
    13  
    14  	"github.com/btcsuite/btcd/chaincfg/chainhash"
    15  )
    16  
    17  const (
    18  	// TxVersion is the current latest supported transaction version.
    19  	TxVersion = 1
    20  
    21  	// MaxTxInSequenceNum is the maximum sequence number the sequence field
    22  	// of a transaction input can be.
    23  	MaxTxInSequenceNum uint32 = 0xffffffff
    24  
    25  	// MaxPrevOutIndex is the maximum index the index field of a previous
    26  	// outpoint can be.
    27  	MaxPrevOutIndex uint32 = 0xffffffff
    28  
    29  	// SequenceLockTimeDisabled is a flag that if set on a transaction
    30  	// input's sequence number, the sequence number will not be interpreted
    31  	// as a relative locktime.
    32  	SequenceLockTimeDisabled = 1 << 31
    33  
    34  	// SequenceLockTimeIsSeconds is a flag that if set on a transaction
    35  	// input's sequence number, the relative locktime has units of 512
    36  	// seconds.
    37  	SequenceLockTimeIsSeconds = 1 << 22
    38  
    39  	// SequenceLockTimeMask is a mask that extracts the relative locktime
    40  	// when masked against the transaction input sequence number.
    41  	SequenceLockTimeMask = 0x0000ffff
    42  
    43  	// SequenceLockTimeGranularity is the defined time based granularity
    44  	// for seconds-based relative time locks. When converting from seconds
    45  	// to a sequence number, the value is right shifted by this amount,
    46  	// therefore the granularity of relative time locks in 512 or 2^9
    47  	// seconds. Enforced relative lock times are multiples of 512 seconds.
    48  	SequenceLockTimeGranularity = 9
    49  
    50  	// defaultTxInOutAlloc is the default size used for the backing array for
    51  	// transaction inputs and outputs.  The array will dynamically grow as needed,
    52  	// but this figure is intended to provide enough space for the number of
    53  	// inputs and outputs in a typical transaction without needing to grow the
    54  	// backing array multiple times.
    55  	defaultTxInOutAlloc = 15
    56  
    57  	// minTxInPayload is the minimum payload size for a transaction input.
    58  	// PreviousOutPoint.Hash + PreviousOutPoint.Index 4 bytes + Varint for
    59  	// SignatureScript length 1 byte + Sequence 4 bytes.
    60  	minTxInPayload = 9 + chainhash.HashSize
    61  
    62  	// maxTxInPerMessage is the maximum number of transactions inputs that
    63  	// a transaction which fits into a message could possibly have.
    64  	maxTxInPerMessage = (MaxMessagePayload / minTxInPayload) + 1
    65  
    66  	// MinTxOutPayload is the minimum payload size for a transaction output.
    67  	// Value 8 bytes + Varint for PkScript length 1 byte.
    68  	MinTxOutPayload = 9
    69  
    70  	// maxTxOutPerMessage is the maximum number of transactions outputs that
    71  	// a transaction which fits into a message could possibly have.
    72  	maxTxOutPerMessage = (MaxMessagePayload / MinTxOutPayload) + 1
    73  
    74  	// minTxPayload is the minimum payload size for a transaction.  Note
    75  	// that any realistically usable transaction must have at least one
    76  	// input or output, but that is a rule enforced at a higher layer, so
    77  	// it is intentionally not included here.
    78  	// Version 4 bytes + Varint number of transaction inputs 1 byte + Varint
    79  	// number of transaction outputs 1 byte + LockTime 4 bytes + min input
    80  	// payload + min output payload.
    81  	minTxPayload = 10
    82  
    83  	// freeListMaxScriptSize is the size of each buffer in the free list
    84  	// that	is used for deserializing scripts from the wire before they are
    85  	// concatenated into a single contiguous buffers.  This value was chosen
    86  	// because it is slightly more than twice the size of the vast majority
    87  	// of all "standard" scripts.  Larger scripts are still deserialized
    88  	// properly as the free list will simply be bypassed for them.
    89  	freeListMaxScriptSize = 512
    90  
    91  	// freeListMaxItems is the number of buffers to keep in the free list
    92  	// to use for script deserialization.  This value allows up to 100
    93  	// scripts per transaction being simultaneously deserialized by 125
    94  	// peers.  Thus, the peak usage of the free list is 12,500 * 512 =
    95  	// 6,400,000 bytes.
    96  	freeListMaxItems = 125
    97  
    98  	// maxWitnessItemsPerInput is the maximum number of witness items to
    99  	// be read for the witness data for a single TxIn. This number is
   100  	// derived using a possible lower bound for the encoding of a witness
   101  	// item: 1 byte for length + 1 byte for the witness item itself, or two
   102  	// bytes. This value is then divided by the currently allowed maximum
   103  	// "cost" for a transaction. We use this for an upper bound for the
   104  	// buffer and consensus makes sure that the weight of a transaction
   105  	// cannot be more than 4000000.
   106  	maxWitnessItemsPerInput = 4_000_000
   107  
   108  	// maxWitnessItemSize is the maximum allowed size for an item within
   109  	// an input's witness data. This value is bounded by the largest
   110  	// possible block size, post segwit v1 (taproot).
   111  	maxWitnessItemSize = 4_000_000
   112  )
   113  
   114  // TxFlagMarker is the first byte of the FLAG field in a bitcoin tx
   115  // message. It allows decoders to distinguish a regular serialized
   116  // transaction from one that would require a different parsing logic.
   117  //
   118  // Position of FLAG in a bitcoin tx message:
   119  //
   120  //	┌─────────┬────────────────────┬─────────────┬─────┐
   121  //	│ VERSION │ FLAG               │ TX-IN-COUNT │ ... │
   122  //	│ 4 bytes │ 2 bytes (optional) │ varint      │     │
   123  //	└─────────┴────────────────────┴─────────────┴─────┘
   124  //
   125  // Zooming into the FLAG field:
   126  //
   127  //	┌── FLAG ─────────────┬────────┐
   128  //	│ TxFlagMarker (0x00) │ TxFlag │
   129  //	│ 1 byte              │ 1 byte │
   130  //	└─────────────────────┴────────┘
   131  const TxFlagMarker = 0x00
   132  
   133  // TxFlag is the second byte of the FLAG field in a bitcoin tx message.
   134  // It indicates the decoding logic to use in the transaction parser, if
   135  // TxFlagMarker is detected in the tx message.
   136  //
   137  // As of writing this, only the witness flag (0x01) is supported, but may be
   138  // extended in the future to accommodate auxiliary non-committed fields.
   139  type TxFlag = byte
   140  
   141  const (
   142  	// WitnessFlag is a flag specific to witness encoding. If the TxFlagMarker
   143  	// is encountered followed by the WitnessFlag, then it indicates a
   144  	// transaction has witness data. This allows decoders to distinguish a
   145  	// serialized transaction with witnesses from a legacy one.
   146  	WitnessFlag TxFlag = 0x01
   147  )
   148  
   149  const scriptSlabSize = 1 << 22
   150  
   151  type scriptSlab [scriptSlabSize]byte
   152  
   153  // scriptFreeList defines a free list of byte slices (up to the maximum number
   154  // defined by the freeListMaxItems constant) that have a cap according to the
   155  // freeListMaxScriptSize constant.  It is used to provide temporary buffers for
   156  // deserializing scripts in order to greatly reduce the number of allocations
   157  // required.
   158  //
   159  // The caller can obtain a buffer from the free list by calling the Borrow
   160  // function and should return it via the Return function when done using it.
   161  type scriptFreeList chan *scriptSlab
   162  
   163  // Borrow returns a byte slice from the free list with a length according the
   164  // provided size.  A new buffer is allocated if there are any items available.
   165  //
   166  // When the size is larger than the max size allowed for items on the free list
   167  // a new buffer of the appropriate size is allocated and returned.  It is safe
   168  // to attempt to return said buffer via the Return function as it will be
   169  // ignored and allowed to go the garbage collector.
   170  func (c scriptFreeList) Borrow() *scriptSlab {
   171  	var buf *scriptSlab
   172  	select {
   173  	case buf = <-c:
   174  	default:
   175  		buf = new(scriptSlab)
   176  	}
   177  	return buf
   178  }
   179  
   180  // Return puts the provided byte slice back on the free list when it has a cap
   181  // of the expected length.  The buffer is expected to have been obtained via
   182  // the Borrow function.  Any slices that are not of the appropriate size, such
   183  // as those whose size is greater than the largest allowed free list item size
   184  // are simply ignored so they can go to the garbage collector.
   185  func (c scriptFreeList) Return(buf *scriptSlab) {
   186  	// Return the buffer to the free list when it's not full.  Otherwise let
   187  	// it be garbage collected.
   188  	select {
   189  	case c <- buf:
   190  	default:
   191  		// Let it go to the garbage collector.
   192  	}
   193  }
   194  
   195  // Create the concurrent safe free list to use for script deserialization.  As
   196  // previously described, this free list is maintained to significantly reduce
   197  // the number of allocations.
   198  var scriptPool = make(scriptFreeList, freeListMaxItems)
   199  
   200  // OutPoint defines a bitcoin data type that is used to track previous
   201  // transaction outputs.
   202  type OutPoint struct {
   203  	Hash  chainhash.Hash
   204  	Index uint32
   205  }
   206  
   207  // NewOutPoint returns a new bitcoin transaction outpoint point with the
   208  // provided hash and index.
   209  func NewOutPoint(hash *chainhash.Hash, index uint32) *OutPoint {
   210  	return &OutPoint{
   211  		Hash:  *hash,
   212  		Index: index,
   213  	}
   214  }
   215  
   216  // NewOutPointFromString returns a new bitcoin transaction outpoint parsed from
   217  // the provided string, which should be in the format "hash:index".
   218  func NewOutPointFromString(outpoint string) (*OutPoint, error) {
   219  	parts := strings.Split(outpoint, ":")
   220  	if len(parts) != 2 {
   221  		return nil, errors.New("outpoint should be of the form txid:index")
   222  	}
   223  	hash, err := chainhash.NewHashFromStr(parts[0])
   224  	if err != nil {
   225  		return nil, err
   226  	}
   227  
   228  	outputIndex, err := strconv.ParseUint(parts[1], 10, 32)
   229  	if err != nil {
   230  		return nil, fmt.Errorf("invalid output index: %v", err)
   231  	}
   232  
   233  	return &OutPoint{
   234  		Hash:  *hash,
   235  		Index: uint32(outputIndex),
   236  	}, nil
   237  }
   238  
   239  // String returns the OutPoint in the human-readable form "hash:index".
   240  func (o OutPoint) String() string {
   241  	// Allocate enough for hash string, colon, and 10 digits.  Although
   242  	// at the time of writing, the number of digits can be no greater than
   243  	// the length of the decimal representation of maxTxOutPerMessage, the
   244  	// maximum message payload may increase in the future and this
   245  	// optimization may go unnoticed, so allocate space for 10 decimal
   246  	// digits, which will fit any uint32.
   247  	buf := make([]byte, 2*chainhash.HashSize+1, 2*chainhash.HashSize+1+10)
   248  	copy(buf, o.Hash.String())
   249  	buf[2*chainhash.HashSize] = ':'
   250  	buf = strconv.AppendUint(buf, uint64(o.Index), 10)
   251  	return string(buf)
   252  }
   253  
   254  // TxIn defines a bitcoin transaction input.
   255  type TxIn struct {
   256  	PreviousOutPoint OutPoint
   257  	SignatureScript  []byte
   258  	Witness          TxWitness
   259  	Sequence         uint32
   260  }
   261  
   262  // SerializeSize returns the number of bytes it would take to serialize the
   263  // the transaction input.
   264  func (t *TxIn) SerializeSize() int {
   265  	// Outpoint Hash 32 bytes + Outpoint Index 4 bytes + Sequence 4 bytes +
   266  	// serialized varint size for the length of SignatureScript +
   267  	// SignatureScript bytes.
   268  	return 40 + VarIntSerializeSize(uint64(len(t.SignatureScript))) +
   269  		len(t.SignatureScript)
   270  }
   271  
   272  // NewTxIn returns a new bitcoin transaction input with the provided
   273  // previous outpoint point and signature script with a default sequence of
   274  // MaxTxInSequenceNum.
   275  func NewTxIn(prevOut *OutPoint, signatureScript []byte, witness [][]byte) *TxIn {
   276  	return &TxIn{
   277  		PreviousOutPoint: *prevOut,
   278  		SignatureScript:  signatureScript,
   279  		Witness:          witness,
   280  		Sequence:         MaxTxInSequenceNum,
   281  	}
   282  }
   283  
   284  // TxWitness defines the witness for a TxIn. A witness is to be interpreted as
   285  // a slice of byte slices, or a stack with one or many elements.
   286  type TxWitness [][]byte
   287  
   288  // SerializeSize returns the number of bytes it would take to serialize the
   289  // transaction input's witness.
   290  func (t TxWitness) SerializeSize() int {
   291  	// A varint to signal the number of elements the witness has.
   292  	n := VarIntSerializeSize(uint64(len(t)))
   293  
   294  	// For each element in the witness, we'll need a varint to signal the
   295  	// size of the element, then finally the number of bytes the element
   296  	// itself comprises.
   297  	for _, witItem := range t {
   298  		n += VarIntSerializeSize(uint64(len(witItem)))
   299  		n += len(witItem)
   300  	}
   301  
   302  	return n
   303  }
   304  
   305  // TxOut defines a bitcoin transaction output.
   306  type TxOut struct {
   307  	Value    int64
   308  	PkScript []byte
   309  }
   310  
   311  // SerializeSize returns the number of bytes it would take to serialize the
   312  // the transaction output.
   313  func (t *TxOut) SerializeSize() int {
   314  	// Value 8 bytes + serialized varint size for the length of PkScript +
   315  	// PkScript bytes.
   316  	return 8 + VarIntSerializeSize(uint64(len(t.PkScript))) + len(t.PkScript)
   317  }
   318  
   319  // NewTxOut returns a new bitcoin transaction output with the provided
   320  // transaction value and public key script.
   321  func NewTxOut(value int64, pkScript []byte) *TxOut {
   322  	return &TxOut{
   323  		Value:    value,
   324  		PkScript: pkScript,
   325  	}
   326  }
   327  
   328  // MsgTx implements the Message interface and represents a bitcoin tx message.
   329  // It is used to deliver transaction information in response to a getdata
   330  // message (MsgGetData) for a given transaction.
   331  //
   332  // Use the AddTxIn and AddTxOut functions to build up the list of transaction
   333  // inputs and outputs.
   334  type MsgTx struct {
   335  	Version  int32
   336  	TxIn     []*TxIn
   337  	TxOut    []*TxOut
   338  	LockTime uint32
   339  }
   340  
   341  // AddTxIn adds a transaction input to the message.
   342  func (msg *MsgTx) AddTxIn(ti *TxIn) {
   343  	msg.TxIn = append(msg.TxIn, ti)
   344  }
   345  
   346  // AddTxOut adds a transaction output to the message.
   347  func (msg *MsgTx) AddTxOut(to *TxOut) {
   348  	msg.TxOut = append(msg.TxOut, to)
   349  }
   350  
   351  // TxHash generates the Hash for the transaction.
   352  func (msg *MsgTx) TxHash() chainhash.Hash {
   353  	return chainhash.DoubleHashRaw(msg.SerializeNoWitness)
   354  }
   355  
   356  // WitnessHash generates the hash of the transaction serialized according to
   357  // the new witness serialization defined in BIP0141 and BIP0144. The final
   358  // output is used within the Segregated Witness commitment of all the witnesses
   359  // within a block. If a transaction has no witness data, then the witness hash,
   360  // is the same as its txid.
   361  func (msg *MsgTx) WitnessHash() chainhash.Hash {
   362  	if msg.HasWitness() {
   363  		return chainhash.DoubleHashRaw(msg.Serialize)
   364  	}
   365  
   366  	return msg.TxHash()
   367  }
   368  
   369  // Copy creates a deep copy of a transaction so that the original does not get
   370  // modified when the copy is manipulated.
   371  func (msg *MsgTx) Copy() *MsgTx {
   372  	// Create new tx and start by copying primitive values and making space
   373  	// for the transaction inputs and outputs.
   374  	newTx := MsgTx{
   375  		Version:  msg.Version,
   376  		TxIn:     make([]*TxIn, 0, len(msg.TxIn)),
   377  		TxOut:    make([]*TxOut, 0, len(msg.TxOut)),
   378  		LockTime: msg.LockTime,
   379  	}
   380  
   381  	// Deep copy the old TxIn data.
   382  	for _, oldTxIn := range msg.TxIn {
   383  		// Deep copy the old previous outpoint.
   384  		oldOutPoint := oldTxIn.PreviousOutPoint
   385  		newOutPoint := OutPoint{}
   386  		newOutPoint.Hash.SetBytes(oldOutPoint.Hash[:])
   387  		newOutPoint.Index = oldOutPoint.Index
   388  
   389  		// Deep copy the old signature script.
   390  		var newScript []byte
   391  		oldScript := oldTxIn.SignatureScript
   392  		oldScriptLen := len(oldScript)
   393  		if oldScriptLen > 0 {
   394  			newScript = make([]byte, oldScriptLen)
   395  			copy(newScript, oldScript[:oldScriptLen])
   396  		}
   397  
   398  		// Create new txIn with the deep copied data.
   399  		newTxIn := TxIn{
   400  			PreviousOutPoint: newOutPoint,
   401  			SignatureScript:  newScript,
   402  			Sequence:         oldTxIn.Sequence,
   403  		}
   404  
   405  		// If the transaction is witnessy, then also copy the
   406  		// witnesses.
   407  		if len(oldTxIn.Witness) != 0 {
   408  			// Deep copy the old witness data.
   409  			newTxIn.Witness = make([][]byte, len(oldTxIn.Witness))
   410  			for i, oldItem := range oldTxIn.Witness {
   411  				newItem := make([]byte, len(oldItem))
   412  				copy(newItem, oldItem)
   413  				newTxIn.Witness[i] = newItem
   414  			}
   415  		}
   416  
   417  		// Finally, append this fully copied txin.
   418  		newTx.TxIn = append(newTx.TxIn, &newTxIn)
   419  	}
   420  
   421  	// Deep copy the old TxOut data.
   422  	for _, oldTxOut := range msg.TxOut {
   423  		// Deep copy the old PkScript
   424  		var newScript []byte
   425  		oldScript := oldTxOut.PkScript
   426  		oldScriptLen := len(oldScript)
   427  		if oldScriptLen > 0 {
   428  			newScript = make([]byte, oldScriptLen)
   429  			copy(newScript, oldScript[:oldScriptLen])
   430  		}
   431  
   432  		// Create new txOut with the deep copied data and append it to
   433  		// new Tx.
   434  		newTxOut := TxOut{
   435  			Value:    oldTxOut.Value,
   436  			PkScript: newScript,
   437  		}
   438  		newTx.TxOut = append(newTx.TxOut, &newTxOut)
   439  	}
   440  
   441  	return &newTx
   442  }
   443  
   444  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
   445  // This is part of the Message interface implementation.
   446  // See Deserialize for decoding transactions stored to disk, such as in a
   447  // database, as opposed to decoding transactions from the wire.
   448  func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
   449  	buf := binarySerializer.Borrow()
   450  	defer binarySerializer.Return(buf)
   451  
   452  	sbuf := scriptPool.Borrow()
   453  	defer scriptPool.Return(sbuf)
   454  
   455  	err := msg.btcDecode(r, pver, enc, buf, sbuf[:])
   456  	return err
   457  }
   458  
   459  func (msg *MsgTx) btcDecode(r io.Reader, pver uint32, enc MessageEncoding,
   460  	buf, sbuf []byte) error {
   461  
   462  	if _, err := io.ReadFull(r, buf[:4]); err != nil {
   463  		return err
   464  	}
   465  	msg.Version = int32(littleEndian.Uint32(buf[:4]))
   466  
   467  	count, err := ReadVarIntBuf(r, pver, buf)
   468  	if err != nil {
   469  		return err
   470  	}
   471  
   472  	// A count of zero (meaning no TxIn's to the uninitiated) means that the
   473  	// value is a TxFlagMarker, and hence indicates the presence of a flag.
   474  	var flag [1]TxFlag
   475  	if count == TxFlagMarker && enc == WitnessEncoding {
   476  		// The count varint was in fact the flag marker byte. Next, we need to
   477  		// read the flag value, which is a single byte.
   478  		if _, err = io.ReadFull(r, flag[:]); err != nil {
   479  			return err
   480  		}
   481  
   482  		// At the moment, the flag MUST be WitnessFlag (0x01). In the future
   483  		// other flag types may be supported.
   484  		if flag[0] != WitnessFlag {
   485  			str := fmt.Sprintf("witness tx but flag byte is %x", flag)
   486  			return messageError("MsgTx.BtcDecode", str)
   487  		}
   488  
   489  		// With the Segregated Witness specific fields decoded, we can
   490  		// now read in the actual txin count.
   491  		count, err = ReadVarIntBuf(r, pver, buf)
   492  		if err != nil {
   493  			return err
   494  		}
   495  	}
   496  
   497  	// Prevent more input transactions than could possibly fit into a
   498  	// message.  It would be possible to cause memory exhaustion and panics
   499  	// without a sane upper bound on this count.
   500  	if count > uint64(maxTxInPerMessage) {
   501  		str := fmt.Sprintf("too many input transactions to fit into "+
   502  			"max message size [count %d, max %d]", count,
   503  			maxTxInPerMessage)
   504  		return messageError("MsgTx.BtcDecode", str)
   505  	}
   506  
   507  	// Deserialize the inputs.
   508  	var totalScriptSize uint64
   509  	txIns := make([]TxIn, count)
   510  	msg.TxIn = make([]*TxIn, count)
   511  	for i := uint64(0); i < count; i++ {
   512  		// The pointer is set now in case a script buffer is borrowed
   513  		// and needs to be returned to the pool on error.
   514  		ti := &txIns[i]
   515  		msg.TxIn[i] = ti
   516  		err = readTxInBuf(r, pver, msg.Version, ti, buf, sbuf)
   517  		if err != nil {
   518  			return err
   519  		}
   520  		totalScriptSize += uint64(len(ti.SignatureScript))
   521  		sbuf = sbuf[len(ti.SignatureScript):]
   522  	}
   523  
   524  	count, err = ReadVarIntBuf(r, pver, buf)
   525  	if err != nil {
   526  		return err
   527  	}
   528  
   529  	// Prevent more output transactions than could possibly fit into a
   530  	// message.  It would be possible to cause memory exhaustion and panics
   531  	// without a sane upper bound on this count.
   532  	if count > uint64(maxTxOutPerMessage) {
   533  		str := fmt.Sprintf("too many output transactions to fit into "+
   534  			"max message size [count %d, max %d]", count,
   535  			maxTxOutPerMessage)
   536  		return messageError("MsgTx.BtcDecode", str)
   537  	}
   538  
   539  	// Deserialize the outputs.
   540  	txOuts := make([]TxOut, count)
   541  	msg.TxOut = make([]*TxOut, count)
   542  	for i := uint64(0); i < count; i++ {
   543  		// The pointer is set now in case a script buffer is borrowed
   544  		// and needs to be returned to the pool on error.
   545  		to := &txOuts[i]
   546  		msg.TxOut[i] = to
   547  		err = readTxOutBuf(r, pver, msg.Version, to, buf, sbuf)
   548  		if err != nil {
   549  			return err
   550  		}
   551  		totalScriptSize += uint64(len(to.PkScript))
   552  		sbuf = sbuf[len(to.PkScript):]
   553  	}
   554  
   555  	// If the transaction's flag byte isn't 0x00 at this point, then one or
   556  	// more of its inputs has accompanying witness data.
   557  	if flag[0] != 0 && enc == WitnessEncoding {
   558  		for _, txin := range msg.TxIn {
   559  			// For each input, the witness is encoded as a stack
   560  			// with one or more items. Therefore, we first read a
   561  			// varint which encodes the number of stack items.
   562  			witCount, err := ReadVarIntBuf(r, pver, buf)
   563  			if err != nil {
   564  				return err
   565  			}
   566  
   567  			// Prevent a possible memory exhaustion attack by
   568  			// limiting the witCount value to a sane upper bound.
   569  			if witCount > maxWitnessItemsPerInput {
   570  				str := fmt.Sprintf("too many witness items to fit "+
   571  					"into max message size [count %d, max %d]",
   572  					witCount, maxWitnessItemsPerInput)
   573  				return messageError("MsgTx.BtcDecode", str)
   574  			}
   575  
   576  			// Then for witCount number of stack items, each item
   577  			// has a varint length prefix, followed by the witness
   578  			// item itself.
   579  			txin.Witness = make([][]byte, witCount)
   580  			for j := uint64(0); j < witCount; j++ {
   581  				txin.Witness[j], err = readScriptBuf(
   582  					r, pver, buf, sbuf, maxWitnessItemSize,
   583  					"script witness item",
   584  				)
   585  				if err != nil {
   586  					return err
   587  				}
   588  				totalScriptSize += uint64(len(txin.Witness[j]))
   589  				sbuf = sbuf[len(txin.Witness[j]):]
   590  			}
   591  		}
   592  	}
   593  
   594  	if _, err := io.ReadFull(r, buf[:4]); err != nil {
   595  		return err
   596  	}
   597  	msg.LockTime = littleEndian.Uint32(buf[:4])
   598  
   599  	// Create a single allocation to house all of the scripts and set each
   600  	// input signature script and output public key script to the
   601  	// appropriate subslice of the overall contiguous buffer.  Then, return
   602  	// each individual script buffer back to the pool so they can be reused
   603  	// for future deserializations.  This is done because it significantly
   604  	// reduces the number of allocations the garbage collector needs to
   605  	// track, which in turn improves performance and drastically reduces the
   606  	// amount of runtime overhead that would otherwise be needed to keep
   607  	// track of millions of small allocations.
   608  	//
   609  	// NOTE: It is no longer valid to call the returnScriptBuffers closure
   610  	// after these blocks of code run because it is already done and the
   611  	// scripts in the transaction inputs and outputs no longer point to the
   612  	// buffers.
   613  	var offset uint64
   614  	scripts := make([]byte, totalScriptSize)
   615  	for i := 0; i < len(msg.TxIn); i++ {
   616  		// Copy the signature script into the contiguous buffer at the
   617  		// appropriate offset.
   618  		signatureScript := msg.TxIn[i].SignatureScript
   619  		copy(scripts[offset:], signatureScript)
   620  
   621  		// Reset the signature script of the transaction input to the
   622  		// slice of the contiguous buffer where the script lives.
   623  		scriptSize := uint64(len(signatureScript))
   624  		end := offset + scriptSize
   625  		msg.TxIn[i].SignatureScript = scripts[offset:end:end]
   626  		offset += scriptSize
   627  
   628  		for j := 0; j < len(msg.TxIn[i].Witness); j++ {
   629  			// Copy each item within the witness stack for this
   630  			// input into the contiguous buffer at the appropriate
   631  			// offset.
   632  			witnessElem := msg.TxIn[i].Witness[j]
   633  			copy(scripts[offset:], witnessElem)
   634  
   635  			// Reset the witness item within the stack to the slice
   636  			// of the contiguous buffer where the witness lives.
   637  			witnessElemSize := uint64(len(witnessElem))
   638  			end := offset + witnessElemSize
   639  			msg.TxIn[i].Witness[j] = scripts[offset:end:end]
   640  			offset += witnessElemSize
   641  		}
   642  	}
   643  	for i := 0; i < len(msg.TxOut); i++ {
   644  		// Copy the public key script into the contiguous buffer at the
   645  		// appropriate offset.
   646  		pkScript := msg.TxOut[i].PkScript
   647  		copy(scripts[offset:], pkScript)
   648  
   649  		// Reset the public key script of the transaction output to the
   650  		// slice of the contiguous buffer where the script lives.
   651  		scriptSize := uint64(len(pkScript))
   652  		end := offset + scriptSize
   653  		msg.TxOut[i].PkScript = scripts[offset:end:end]
   654  		offset += scriptSize
   655  	}
   656  
   657  	return nil
   658  }
   659  
   660  // Deserialize decodes a transaction from r into the receiver using a format
   661  // that is suitable for long-term storage such as a database while respecting
   662  // the Version field in the transaction.  This function differs from BtcDecode
   663  // in that BtcDecode decodes from the bitcoin wire protocol as it was sent
   664  // across the network.  The wire encoding can technically differ depending on
   665  // the protocol version and doesn't even really need to match the format of a
   666  // stored transaction at all.  As of the time this comment was written, the
   667  // encoded transaction is the same in both instances, but there is a distinct
   668  // difference and separating the two allows the API to be flexible enough to
   669  // deal with changes.
   670  func (msg *MsgTx) Deserialize(r io.Reader) error {
   671  	// At the current time, there is no difference between the wire encoding
   672  	// at protocol version 0 and the stable long-term storage format.  As
   673  	// a result, make use of BtcDecode.
   674  	return msg.BtcDecode(r, 0, WitnessEncoding)
   675  }
   676  
   677  // DeserializeNoWitness decodes a transaction from r into the receiver, where
   678  // the transaction encoding format within r MUST NOT utilize the new
   679  // serialization format created to encode transaction bearing witness data
   680  // within inputs.
   681  func (msg *MsgTx) DeserializeNoWitness(r io.Reader) error {
   682  	return msg.BtcDecode(r, 0, BaseEncoding)
   683  }
   684  
   685  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
   686  // This is part of the Message interface implementation.
   687  // See Serialize for encoding transactions to be stored to disk, such as in a
   688  // database, as opposed to encoding transactions for the wire.
   689  func (msg *MsgTx) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
   690  	buf := binarySerializer.Borrow()
   691  	defer binarySerializer.Return(buf)
   692  
   693  	err := msg.btcEncode(w, pver, enc, buf)
   694  	return err
   695  }
   696  
   697  func (msg *MsgTx) btcEncode(w io.Writer, pver uint32, enc MessageEncoding,
   698  	buf []byte) error {
   699  
   700  	littleEndian.PutUint32(buf[:4], uint32(msg.Version))
   701  	if _, err := w.Write(buf[:4]); err != nil {
   702  		return err
   703  	}
   704  
   705  	// If the encoding version is set to WitnessEncoding, and the Flags
   706  	// field for the MsgTx aren't 0x00, then this indicates the transaction
   707  	// is to be encoded using the new witness inclusionary structure
   708  	// defined in BIP0144.
   709  	doWitness := enc == WitnessEncoding && msg.HasWitness()
   710  	if doWitness {
   711  		// After the transaction's Version field, we include two additional
   712  		// bytes specific to the witness encoding. This byte sequence is known
   713  		// as a flag. The first byte is a marker byte (TxFlagMarker) and the
   714  		// second one is the flag value to indicate presence of witness data.
   715  		if _, err := w.Write([]byte{TxFlagMarker, WitnessFlag}); err != nil {
   716  			return err
   717  		}
   718  	}
   719  
   720  	count := uint64(len(msg.TxIn))
   721  	err := WriteVarIntBuf(w, pver, count, buf)
   722  	if err != nil {
   723  		return err
   724  	}
   725  
   726  	for _, ti := range msg.TxIn {
   727  		err = writeTxInBuf(w, pver, msg.Version, ti, buf)
   728  		if err != nil {
   729  			return err
   730  		}
   731  	}
   732  
   733  	count = uint64(len(msg.TxOut))
   734  	err = WriteVarIntBuf(w, pver, count, buf)
   735  	if err != nil {
   736  		return err
   737  	}
   738  
   739  	for _, to := range msg.TxOut {
   740  		err = WriteTxOutBuf(w, pver, msg.Version, to, buf)
   741  		if err != nil {
   742  			return err
   743  		}
   744  	}
   745  
   746  	// If this transaction is a witness transaction, and the witness
   747  	// encoded is desired, then encode the witness for each of the inputs
   748  	// within the transaction.
   749  	if doWitness {
   750  		for _, ti := range msg.TxIn {
   751  			err = writeTxWitnessBuf(w, pver, msg.Version, ti.Witness, buf)
   752  			if err != nil {
   753  				return err
   754  			}
   755  		}
   756  	}
   757  
   758  	littleEndian.PutUint32(buf[:4], msg.LockTime)
   759  	_, err = w.Write(buf[:4])
   760  	return err
   761  }
   762  
   763  // HasWitness returns false if none of the inputs within the transaction
   764  // contain witness data, true false otherwise.
   765  func (msg *MsgTx) HasWitness() bool {
   766  	for _, txIn := range msg.TxIn {
   767  		if len(txIn.Witness) != 0 {
   768  			return true
   769  		}
   770  	}
   771  
   772  	return false
   773  }
   774  
   775  // Serialize encodes the transaction to w using a format that suitable for
   776  // long-term storage such as a database while respecting the Version field in
   777  // the transaction.  This function differs from BtcEncode in that BtcEncode
   778  // encodes the transaction to the bitcoin wire protocol in order to be sent
   779  // across the network.  The wire encoding can technically differ depending on
   780  // the protocol version and doesn't even really need to match the format of a
   781  // stored transaction at all.  As of the time this comment was written, the
   782  // encoded transaction is the same in both instances, but there is a distinct
   783  // difference and separating the two allows the API to be flexible enough to
   784  // deal with changes.
   785  func (msg *MsgTx) Serialize(w io.Writer) error {
   786  	// At the current time, there is no difference between the wire encoding
   787  	// at protocol version 0 and the stable long-term storage format.  As
   788  	// a result, make use of BtcEncode.
   789  	//
   790  	// Passing a encoding type of WitnessEncoding to BtcEncode for MsgTx
   791  	// indicates that the transaction's witnesses (if any) should be
   792  	// serialized according to the new serialization structure defined in
   793  	// BIP0144.
   794  	return msg.BtcEncode(w, 0, WitnessEncoding)
   795  }
   796  
   797  // SerializeNoWitness encodes the transaction to w in an identical manner to
   798  // Serialize, however even if the source transaction has inputs with witness
   799  // data, the old serialization format will still be used.
   800  func (msg *MsgTx) SerializeNoWitness(w io.Writer) error {
   801  	return msg.BtcEncode(w, 0, BaseEncoding)
   802  }
   803  
   804  // baseSize returns the serialized size of the transaction without accounting
   805  // for any witness data.
   806  func (msg *MsgTx) baseSize() int {
   807  	// Version 4 bytes + LockTime 4 bytes + Serialized varint size for the
   808  	// number of transaction inputs and outputs.
   809  	n := 8 + VarIntSerializeSize(uint64(len(msg.TxIn))) +
   810  		VarIntSerializeSize(uint64(len(msg.TxOut)))
   811  
   812  	for _, txIn := range msg.TxIn {
   813  		n += txIn.SerializeSize()
   814  	}
   815  
   816  	for _, txOut := range msg.TxOut {
   817  		n += txOut.SerializeSize()
   818  	}
   819  
   820  	return n
   821  }
   822  
   823  // SerializeSize returns the number of bytes it would take to serialize the
   824  // the transaction.
   825  func (msg *MsgTx) SerializeSize() int {
   826  	n := msg.baseSize()
   827  
   828  	if msg.HasWitness() {
   829  		// The marker, and flag fields take up two additional bytes.
   830  		n += 2
   831  
   832  		// Additionally, factor in the serialized size of each of the
   833  		// witnesses for each txin.
   834  		for _, txin := range msg.TxIn {
   835  			n += txin.Witness.SerializeSize()
   836  		}
   837  	}
   838  
   839  	return n
   840  }
   841  
   842  // SerializeSizeStripped returns the number of bytes it would take to serialize
   843  // the transaction, excluding any included witness data.
   844  func (msg *MsgTx) SerializeSizeStripped() int {
   845  	return msg.baseSize()
   846  }
   847  
   848  // Command returns the protocol command string for the message.  This is part
   849  // of the Message interface implementation.
   850  func (msg *MsgTx) Command() string {
   851  	return CmdTx
   852  }
   853  
   854  // MaxPayloadLength returns the maximum length the payload can be for the
   855  // receiver.  This is part of the Message interface implementation.
   856  func (msg *MsgTx) MaxPayloadLength(pver uint32) uint32 {
   857  	return MaxBlockPayload
   858  }
   859  
   860  // PkScriptLocs returns a slice containing the start of each public key script
   861  // within the raw serialized transaction.  The caller can easily obtain the
   862  // length of each script by using len on the script available via the
   863  // appropriate transaction output entry.
   864  func (msg *MsgTx) PkScriptLocs() []int {
   865  	numTxOut := len(msg.TxOut)
   866  	if numTxOut == 0 {
   867  		return nil
   868  	}
   869  
   870  	// The starting offset in the serialized transaction of the first
   871  	// transaction output is:
   872  	//
   873  	// Version 4 bytes + serialized varint size for the number of
   874  	// transaction inputs and outputs + serialized size of each transaction
   875  	// input.
   876  	n := 4 + VarIntSerializeSize(uint64(len(msg.TxIn))) +
   877  		VarIntSerializeSize(uint64(numTxOut))
   878  
   879  	// If this transaction has a witness input, the an additional two bytes
   880  	// for the marker, and flag byte need to be taken into account.
   881  	if len(msg.TxIn) > 0 && msg.TxIn[0].Witness != nil {
   882  		n += 2
   883  	}
   884  
   885  	for _, txIn := range msg.TxIn {
   886  		n += txIn.SerializeSize()
   887  	}
   888  
   889  	// Calculate and set the appropriate offset for each public key script.
   890  	pkScriptLocs := make([]int, numTxOut)
   891  	for i, txOut := range msg.TxOut {
   892  		// The offset of the script in the transaction output is:
   893  		//
   894  		// Value 8 bytes + serialized varint size for the length of
   895  		// PkScript.
   896  		n += 8 + VarIntSerializeSize(uint64(len(txOut.PkScript)))
   897  		pkScriptLocs[i] = n
   898  		n += len(txOut.PkScript)
   899  	}
   900  
   901  	return pkScriptLocs
   902  }
   903  
   904  // NewMsgTx returns a new bitcoin tx message that conforms to the Message
   905  // interface.  The return instance has a default version of TxVersion and there
   906  // are no transaction inputs or outputs.  Also, the lock time is set to zero
   907  // to indicate the transaction is valid immediately as opposed to some time in
   908  // future.
   909  func NewMsgTx(version int32) *MsgTx {
   910  	return &MsgTx{
   911  		Version: version,
   912  		TxIn:    make([]*TxIn, 0, defaultTxInOutAlloc),
   913  		TxOut:   make([]*TxOut, 0, defaultTxInOutAlloc),
   914  	}
   915  }
   916  
   917  // readOutPointBuf reads the next sequence of bytes from r as an OutPoint.
   918  //
   919  // If b is non-nil, the provided buffer will be used for serializing small
   920  // values.  Otherwise a buffer will be drawn from the binarySerializer's pool
   921  // and return when the method finishes.
   922  //
   923  // NOTE: b MUST either be nil or at least an 8-byte slice.
   924  func readOutPointBuf(r io.Reader, pver uint32, version int32, op *OutPoint,
   925  	buf []byte) error {
   926  
   927  	_, err := io.ReadFull(r, op.Hash[:])
   928  	if err != nil {
   929  		return err
   930  	}
   931  
   932  	if _, err := io.ReadFull(r, buf[:4]); err != nil {
   933  		return err
   934  	}
   935  	op.Index = littleEndian.Uint32(buf[:4])
   936  
   937  	return nil
   938  }
   939  
   940  // WriteOutPoint encodes op to the bitcoin protocol encoding for an OutPoint to
   941  // w.
   942  func WriteOutPoint(w io.Writer, pver uint32, version int32, op *OutPoint) error {
   943  	buf := binarySerializer.Borrow()
   944  	defer binarySerializer.Return(buf)
   945  
   946  	err := writeOutPointBuf(w, pver, version, op, buf)
   947  	return err
   948  }
   949  
   950  // writeOutPointBuf encodes op to the bitcoin protocol encoding for an OutPoint
   951  // to w.
   952  //
   953  // If b is non-nil, the provided buffer will be used for serializing small
   954  // values.  Otherwise a buffer will be drawn from the binarySerializer's pool
   955  // and return when the method finishes.
   956  //
   957  // NOTE: b MUST either be nil or at least an 8-byte slice.
   958  func writeOutPointBuf(w io.Writer, pver uint32, version int32, op *OutPoint,
   959  	buf []byte) error {
   960  
   961  	_, err := w.Write(op.Hash[:])
   962  	if err != nil {
   963  		return err
   964  	}
   965  
   966  	littleEndian.PutUint32(buf[:4], op.Index)
   967  	_, err = w.Write(buf[:4])
   968  	return err
   969  }
   970  
   971  // readScript reads a variable length byte array that represents a transaction
   972  // script.  It is encoded as a varInt containing the length of the array
   973  // followed by the bytes themselves.  An error is returned if the length is
   974  // greater than the passed maxAllowed parameter which helps protect against
   975  // memory exhaustion attacks and forced panics through malformed messages.  The
   976  // fieldName parameter is only used for the error message so it provides more
   977  // context in the error.
   978  //
   979  // If b is non-nil, the provided buffer will be used for serializing small
   980  // values.  Otherwise a buffer will be drawn from the binarySerializer's pool
   981  // and return when the method finishes.
   982  //
   983  // NOTE: b MUST either be nil or at least an 8-byte slice.
   984  func readScriptBuf(r io.Reader, pver uint32, buf, s []byte,
   985  	maxAllowed uint32, fieldName string) ([]byte, error) {
   986  
   987  	count, err := ReadVarIntBuf(r, pver, buf)
   988  	if err != nil {
   989  		return nil, err
   990  	}
   991  
   992  	// Prevent byte array larger than the max message size.  It would
   993  	// be possible to cause memory exhaustion and panics without a sane
   994  	// upper bound on this count.
   995  	if count > uint64(maxAllowed) {
   996  		str := fmt.Sprintf("%s is larger than the max allowed size "+
   997  			"[count %d, max %d]", fieldName, count, maxAllowed)
   998  		return nil, messageError("readScript", str)
   999  	}
  1000  
  1001  	_, err = io.ReadFull(r, s[:count])
  1002  	if err != nil {
  1003  		return nil, err
  1004  	}
  1005  	return s[:count], nil
  1006  }
  1007  
  1008  // readTxInBuf reads the next sequence of bytes from r as a transaction input
  1009  // (TxIn).
  1010  //
  1011  // If b is non-nil, the provided buffer will be used for serializing small
  1012  // values.  Otherwise a buffer will be drawn from the binarySerializer's pool
  1013  // and return when the method finishes.
  1014  //
  1015  // NOTE: b MUST either be nil or at least an 8-byte slice.
  1016  func readTxInBuf(r io.Reader, pver uint32, version int32, ti *TxIn,
  1017  	buf, s []byte) error {
  1018  
  1019  	err := readOutPointBuf(r, pver, version, &ti.PreviousOutPoint, buf)
  1020  	if err != nil {
  1021  		return err
  1022  	}
  1023  
  1024  	ti.SignatureScript, err = readScriptBuf(r, pver, buf, s, MaxMessagePayload,
  1025  		"transaction input signature script")
  1026  	if err != nil {
  1027  		return err
  1028  	}
  1029  
  1030  	if _, err := io.ReadFull(r, buf[:4]); err != nil {
  1031  		return err
  1032  	}
  1033  
  1034  	ti.Sequence = littleEndian.Uint32(buf[:4])
  1035  
  1036  	return nil
  1037  }
  1038  
  1039  // writeTxInBuf encodes ti to the bitcoin protocol encoding for a transaction
  1040  // input (TxIn) to w. If b is non-nil, the provided buffer will be used for
  1041  // serializing small values. Otherwise a buffer will be drawn from the
  1042  // binarySerializer's pool and return when the method finishes.
  1043  func writeTxInBuf(w io.Writer, pver uint32, version int32, ti *TxIn,
  1044  	buf []byte) error {
  1045  
  1046  	err := writeOutPointBuf(w, pver, version, &ti.PreviousOutPoint, buf)
  1047  	if err != nil {
  1048  		return err
  1049  	}
  1050  
  1051  	err = WriteVarBytesBuf(w, pver, ti.SignatureScript, buf)
  1052  	if err != nil {
  1053  		return err
  1054  	}
  1055  
  1056  	littleEndian.PutUint32(buf[:4], ti.Sequence)
  1057  	_, err = w.Write(buf[:4])
  1058  
  1059  	return err
  1060  }
  1061  
  1062  // ReadTxOut reads the next sequence of bytes from r as a transaction output
  1063  // (TxOut).
  1064  func ReadTxOut(r io.Reader, pver uint32, version int32, to *TxOut) error {
  1065  	var s scriptSlab
  1066  
  1067  	buf := binarySerializer.Borrow()
  1068  	defer binarySerializer.Return(buf)
  1069  
  1070  	err := readTxOutBuf(r, pver, version, to, buf, s[:])
  1071  	return err
  1072  }
  1073  
  1074  // readTxOutBuf reads the next sequence of bytes from r as a transaction output
  1075  // (TxOut). If b is non-nil, the provided buffer will be used for serializing
  1076  // small values. Otherwise a buffer will be drawn from the binarySerializer's
  1077  // pool and return when the method finishes.
  1078  func readTxOutBuf(r io.Reader, pver uint32, version int32, to *TxOut,
  1079  	buf, s []byte) error {
  1080  
  1081  	_, err := io.ReadFull(r, buf)
  1082  	if err != nil {
  1083  		return err
  1084  	}
  1085  	to.Value = int64(littleEndian.Uint64(buf))
  1086  
  1087  	to.PkScript, err = readScriptBuf(
  1088  		r, pver, buf, s, MaxMessagePayload,
  1089  		"transaction output public key script",
  1090  	)
  1091  	return err
  1092  }
  1093  
  1094  // WriteTxOut encodes to into the bitcoin protocol encoding for a transaction
  1095  // output (TxOut) to w.
  1096  //
  1097  // NOTE: This function is exported in order to allow txscript to compute the
  1098  // new sighashes for witness transactions (BIP0143).
  1099  func WriteTxOut(w io.Writer, pver uint32, version int32, to *TxOut) error {
  1100  	buf := binarySerializer.Borrow()
  1101  	defer binarySerializer.Return(buf)
  1102  
  1103  	err := WriteTxOutBuf(w, pver, version, to, buf)
  1104  	return err
  1105  }
  1106  
  1107  // WriteTxOutBuf encodes to into the bitcoin protocol encoding for a transaction
  1108  // output (TxOut) to w. If b is non-nil, the provided buffer will be used for
  1109  // serializing small values. Otherwise a buffer will be drawn from the
  1110  // binarySerializer's pool and return when the method finishes.
  1111  //
  1112  // NOTE: This function is exported in order to allow txscript to compute the
  1113  // new sighashes for witness transactions (BIP0143).
  1114  func WriteTxOutBuf(w io.Writer, pver uint32, version int32, to *TxOut,
  1115  	buf []byte) error {
  1116  
  1117  	littleEndian.PutUint64(buf, uint64(to.Value))
  1118  	_, err := w.Write(buf)
  1119  	if err != nil {
  1120  		return err
  1121  	}
  1122  
  1123  	return WriteVarBytesBuf(w, pver, to.PkScript, buf)
  1124  }
  1125  
  1126  // writeTxWitnessBuf encodes the bitcoin protocol encoding for a transaction
  1127  // input's witness into to w. If b is non-nil, the provided buffer will be used
  1128  // for serializing small values. Otherwise a buffer will be drawn from the
  1129  // binarySerializer's pool and return when the method finishes.
  1130  func writeTxWitnessBuf(w io.Writer, pver uint32, version int32, wit [][]byte,
  1131  	buf []byte) error {
  1132  
  1133  	err := WriteVarIntBuf(w, pver, uint64(len(wit)), buf)
  1134  	if err != nil {
  1135  		return err
  1136  	}
  1137  	for _, item := range wit {
  1138  		err = WriteVarBytesBuf(w, pver, item, buf)
  1139  		if err != nil {
  1140  			return err
  1141  		}
  1142  	}
  1143  
  1144  	return nil
  1145  }