Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2021 The Bitcoin Core developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
# 7 : : #define BITCOIN_PRIMITIVES_TRANSACTION_H
# 8 : :
# 9 : : #include <stdint.h>
# 10 : : #include <consensus/amount.h>
# 11 : : #include <script/script.h>
# 12 : : #include <serialize.h>
# 13 : : #include <uint256.h>
# 14 : :
# 15 : : #include <tuple>
# 16 : :
# 17 : : /**
# 18 : : * A flag that is ORed into the protocol version to designate that a transaction
# 19 : : * should be (un)serialized without witness data.
# 20 : : * Make sure that this does not collide with any of the values in `version.h`
# 21 : : * or with `ADDRV2_FORMAT`.
# 22 : : */
# 23 : : static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
# 24 : :
# 25 : : /** An outpoint - a combination of a transaction hash and an index n into its vout */
# 26 : : class COutPoint
# 27 : : {
# 28 : : public:
# 29 : : uint256 hash;
# 30 : : uint32_t n;
# 31 : :
# 32 : : static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
# 33 : :
# 34 : 6877328 : COutPoint(): n(NULL_INDEX) { }
# 35 : 41049756 : COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
# 36 : :
# 37 : 50634871 : SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }
# 38 : :
# 39 : 31280 : void SetNull() { hash.SetNull(); n = NULL_INDEX; }
# 40 [ + + ][ + + ]: 29043246 : bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
# 41 : :
# 42 : : friend bool operator<(const COutPoint& a, const COutPoint& b)
# 43 : 472247926 : {
# 44 : 472247926 : int cmp = a.hash.Compare(b.hash);
# 45 [ + + ][ + + ]: 472247926 : return cmp < 0 || (cmp == 0 && a.n < b.n);
# [ + + ]
# 46 : 472247926 : }
# 47 : :
# 48 : : friend bool operator==(const COutPoint& a, const COutPoint& b)
# 49 : 99778765 : {
# 50 [ + + ][ + + ]: 99778765 : return (a.hash == b.hash && a.n == b.n);
# 51 : 99778765 : }
# 52 : :
# 53 : : friend bool operator!=(const COutPoint& a, const COutPoint& b)
# 54 : 0 : {
# 55 : 0 : return !(a == b);
# 56 : 0 : }
# 57 : :
# 58 : : std::string ToString() const;
# 59 : : };
# 60 : :
# 61 : : /** An input of a transaction. It contains the location of the previous
# 62 : : * transaction's output that it claims and a signature that matches the
# 63 : : * output's public key.
# 64 : : */
# 65 : : class CTxIn
# 66 : : {
# 67 : : public:
# 68 : : COutPoint prevout;
# 69 : : CScript scriptSig;
# 70 : : uint32_t nSequence;
# 71 : : CScriptWitness scriptWitness; //!< Only serialized through CTransaction
# 72 : :
# 73 : : /**
# 74 : : * Setting nSequence to this value for every input in a transaction
# 75 : : * disables nLockTime/IsFinalTx().
# 76 : : * It fails OP_CHECKLOCKTIMEVERIFY/CheckLockTime() for any input that has
# 77 : : * it set (BIP 65).
# 78 : : * It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).
# 79 : : */
# 80 : : static const uint32_t SEQUENCE_FINAL = 0xffffffff;
# 81 : : /**
# 82 : : * This is the maximum sequence number that enables both nLockTime and
# 83 : : * OP_CHECKLOCKTIMEVERIFY (BIP 65).
# 84 : : * It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).
# 85 : : */
# 86 : : static const uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1};
# 87 : :
# 88 : : // Below flags apply in the context of BIP 68. BIP 68 requires the tx
# 89 : : // version to be set to 2, or higher.
# 90 : : /**
# 91 : : * If this flag is set, CTxIn::nSequence is NOT interpreted as a
# 92 : : * relative lock-time.
# 93 : : * It skips SequenceLocks() for any input that has it set (BIP 68).
# 94 : : * It fails OP_CHECKSEQUENCEVERIFY/CheckSequence() for any input that has
# 95 : : * it set (BIP 112).
# 96 : : */
# 97 : : static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
# 98 : :
# 99 : : /**
# 100 : : * If CTxIn::nSequence encodes a relative lock-time and this flag
# 101 : : * is set, the relative lock-time has units of 512 seconds,
# 102 : : * otherwise it specifies blocks with a granularity of 1. */
# 103 : : static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
# 104 : :
# 105 : : /**
# 106 : : * If CTxIn::nSequence encodes a relative lock-time, this mask is
# 107 : : * applied to extract that lock-time from the sequence field. */
# 108 : : static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
# 109 : :
# 110 : : /**
# 111 : : * In order to use the same number of bits to encode roughly the
# 112 : : * same wall-clock duration, and because blocks are naturally
# 113 : : * limited to occur every 600s on average, the minimum granularity
# 114 : : * for time-based relative lock-time is fixed at 512 seconds.
# 115 : : * Converting from CTxIn::nSequence to seconds is performed by
# 116 : : * multiplying by 512 = 2^9, or equivalently shifting up by
# 117 : : * 9 bits. */
# 118 : : static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
# 119 : :
# 120 : : CTxIn()
# 121 : 5845914 : {
# 122 : 5845914 : nSequence = SEQUENCE_FINAL;
# 123 : 5845914 : }
# 124 : :
# 125 : : explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
# 126 : : CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
# 127 : :
# 128 : 5948052 : SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
# 129 : :
# 130 : : friend bool operator==(const CTxIn& a, const CTxIn& b)
# 131 : 146 : {
# 132 [ + + ]: 146 : return (a.prevout == b.prevout &&
# 133 [ + - ]: 146 : a.scriptSig == b.scriptSig &&
# 134 [ + - ]: 146 : a.nSequence == b.nSequence);
# 135 : 146 : }
# 136 : :
# 137 : : friend bool operator!=(const CTxIn& a, const CTxIn& b)
# 138 : 0 : {
# 139 : 0 : return !(a == b);
# 140 : 0 : }
# 141 : :
# 142 : : std::string ToString() const;
# 143 : : };
# 144 : :
# 145 : : /** An output of a transaction. It contains the public key that the next input
# 146 : : * must be able to sign with to claim it.
# 147 : : */
# 148 : : class CTxOut
# 149 : : {
# 150 : : public:
# 151 : : CAmount nValue;
# 152 : : CScript scriptPubKey;
# 153 : :
# 154 : : CTxOut()
# 155 : 102506082 : {
# 156 : 102506082 : SetNull();
# 157 : 102506082 : }
# 158 : :
# 159 : : CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
# 160 : :
# 161 : 43195240 : SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
# 162 : :
# 163 : : void SetNull()
# 164 : 116714996 : {
# 165 : 116714996 : nValue = -1;
# 166 : 116714996 : scriptPubKey.clear();
# 167 : 116714996 : }
# 168 : :
# 169 : : bool IsNull() const
# 170 : 150295453 : {
# 171 : 150295453 : return (nValue == -1);
# 172 : 150295453 : }
# 173 : :
# 174 : : friend bool operator==(const CTxOut& a, const CTxOut& b)
# 175 : 700082 : {
# 176 [ + - ]: 700082 : return (a.nValue == b.nValue &&
# 177 [ + - ]: 700082 : a.scriptPubKey == b.scriptPubKey);
# 178 : 700082 : }
# 179 : :
# 180 : : friend bool operator!=(const CTxOut& a, const CTxOut& b)
# 181 : 21075 : {
# 182 : 21075 : return !(a == b);
# 183 : 21075 : }
# 184 : :
# 185 : : std::string ToString() const;
# 186 : : };
# 187 : :
# 188 : : struct CMutableTransaction;
# 189 : :
# 190 : : /**
# 191 : : * Basic transaction serialization format:
# 192 : : * - int32_t nVersion
# 193 : : * - std::vector<CTxIn> vin
# 194 : : * - std::vector<CTxOut> vout
# 195 : : * - uint32_t nLockTime
# 196 : : *
# 197 : : * Extended transaction serialization format:
# 198 : : * - int32_t nVersion
# 199 : : * - unsigned char dummy = 0x00
# 200 : : * - unsigned char flags (!= 0)
# 201 : : * - std::vector<CTxIn> vin
# 202 : : * - std::vector<CTxOut> vout
# 203 : : * - if (flags & 1):
# 204 : : * - CScriptWitness scriptWitness; (deserialized into CTxIn)
# 205 : : * - uint32_t nLockTime
# 206 : : */
# 207 : : template<typename Stream, typename TxType>
# 208 : 293451 : inline void UnserializeTransaction(TxType& tx, Stream& s) {
# 209 : 293451 : const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
# 210 : :
# 211 : 293451 : s >> tx.nVersion;
# 212 : 293451 : unsigned char flags = 0;
# 213 : 293451 : tx.vin.clear();
# 214 : 293451 : tx.vout.clear();
# 215 : : /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
# 216 : 293451 : s >> tx.vin;
# 217 [ + + ][ + + ]: 293451 : if (tx.vin.size() == 0 && fAllowWitness) {
# [ + + ][ + + ]
# [ + + ][ - + ]
# [ + - ][ + - ]
# [ + - ][ + + ]
# [ + + ][ # # ]
# 218 : : /* We read a dummy or an empty vin. */
# 219 : 225749 : s >> flags;
# 220 [ + - ][ + - ]: 225749 : if (flags != 0) {
# [ + - ][ + + ]
# [ + - ][ # # ]
# 221 : 225737 : s >> tx.vin;
# 222 : 225737 : s >> tx.vout;
# 223 : 225737 : }
# 224 : 225749 : } else {
# 225 : : /* We read a non-empty vin. Assume a normal vout follows. */
# 226 : 67702 : s >> tx.vout;
# 227 : 67702 : }
# 228 [ + + ][ + + ]: 293451 : if ((flags & 1) && fAllowWitness) {
# [ + + ][ + + ]
# [ + + ][ - + ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# 229 : : /* The witness flag is present, and we support witnesses. */
# 230 : 225490 : flags ^= 1;
# 231 [ # # ][ + + ]: 554573 : for (size_t i = 0; i < tx.vin.size(); i++) {
# [ + + ][ + + ]
# [ + + ][ + + ]
# 232 : 329083 : s >> tx.vin[i].scriptWitness.stack;
# 233 : 329083 : }
# 234 [ + + ][ - + ]: 225490 : if (!tx.HasWitness()) {
# [ - + ][ - + ]
# [ # # ][ - + ]
# 235 : : /* It's illegal to encode witnesses when all witness stacks are empty. */
# 236 : 6 : throw std::ios_base::failure("Superfluous witness record");
# 237 : 6 : }
# 238 : 225490 : }
# 239 [ - + ][ - + ]: 293445 : if (flags) {
# [ - + ][ - + ]
# [ + + ][ - + ]
# 240 : : /* Unknown flag in the serialization */
# 241 : 2 : throw std::ios_base::failure("Unknown transaction optional data");
# 242 : 2 : }
# 243 : 293443 : s >> tx.nLockTime;
# 244 : 293443 : }
# 245 : :
# 246 : : template<typename Stream, typename TxType>
# 247 : 3592799 : inline void SerializeTransaction(const TxType& tx, Stream& s) {
# 248 : 3592799 : const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
# 249 : :
# 250 : 3592799 : s << tx.nVersion;
# 251 : 3592799 : unsigned char flags = 0;
# 252 : : // Consistency check
# 253 [ + + ][ + + ]: 3592799 : if (fAllowWitness) {
# [ + + ][ + + ]
# [ + - ][ - + ]
# [ + + ][ + + ]
# [ - + ][ + - ]
# 254 : : /* Check whether witnesses need to be serialized. */
# 255 [ - + ][ + + ]: 1088498 : if (tx.HasWitness()) {
# [ + + ][ # # ]
# [ + + ][ + + ]
# [ + + ][ - + ]
# [ + - ][ # # ]
# 256 : 864888 : flags |= 1;
# 257 : 864888 : }
# 258 : 1088498 : }
# 259 [ + + ][ - + ]: 3592799 : if (flags) {
# [ - + ][ + + ]
# [ - + ][ + + ]
# [ + + ][ + + ]
# [ - + ][ + + ]
# 260 : : /* Use extended format in case witnesses are to be serialized. */
# 261 : 864888 : std::vector<CTxIn> vinDummy;
# 262 : 864888 : s << vinDummy;
# 263 : 864888 : s << flags;
# 264 : 864888 : }
# 265 : 3592799 : s << tx.vin;
# 266 : 3592799 : s << tx.vout;
# 267 [ + + ][ - + ]: 3592799 : if (flags & 1) {
# [ + + ][ - + ]
# [ + + ][ - + ]
# [ + + ][ + + ]
# [ + + ][ - + ]
# 268 [ + + ][ # # ]: 2174052 : for (size_t i = 0; i < tx.vin.size(); i++) {
# [ # # ][ + + ]
# [ + + ][ + + ]
# [ # # ][ + + ]
# [ # # ][ + + ]
# 269 : 1309164 : s << tx.vin[i].scriptWitness.stack;
# 270 : 1309164 : }
# 271 : 864888 : }
# 272 : 3592799 : s << tx.nLockTime;
# 273 : 3592799 : }
# 274 : :
# 275 : :
# 276 : : /** The basic transaction that is broadcasted on the network and contained in
# 277 : : * blocks. A transaction can contain multiple inputs and outputs.
# 278 : : */
# 279 : : class CTransaction
# 280 : : {
# 281 : : public:
# 282 : : // Default transaction version.
# 283 : : static const int32_t CURRENT_VERSION=2;
# 284 : :
# 285 : : // The local variables are made const to prevent unintended modification
# 286 : : // without updating the cached hash value. However, CTransaction is not
# 287 : : // actually immutable; deserialization and assignment are implemented,
# 288 : : // and bypass the constness. This is safe, as they update the entire
# 289 : : // structure, including the hash.
# 290 : : const std::vector<CTxIn> vin;
# 291 : : const std::vector<CTxOut> vout;
# 292 : : const int32_t nVersion;
# 293 : : const uint32_t nLockTime;
# 294 : :
# 295 : : private:
# 296 : : /** Memory only. */
# 297 : : const uint256 hash;
# 298 : : const uint256 m_witness_hash;
# 299 : :
# 300 : : uint256 ComputeHash() const;
# 301 : : uint256 ComputeWitnessHash() const;
# 302 : :
# 303 : : public:
# 304 : : /** Convert a CMutableTransaction into a CTransaction. */
# 305 : : explicit CTransaction(const CMutableTransaction& tx);
# 306 : : CTransaction(CMutableTransaction&& tx);
# 307 : :
# 308 : : template <typename Stream>
# 309 : 3191956 : inline void Serialize(Stream& s) const {
# 310 : 3191956 : SerializeTransaction(*this, s);
# 311 : 3191956 : }
# 312 : :
# 313 : : /** This deserializing constructor is provided instead of an Unserialize method.
# 314 : : * Unserialize is not possible, since it would require overwriting const fields. */
# 315 : : template <typename Stream>
# 316 : 272560 : CTransaction(deserialize_type, Stream& s) : CTransaction(CMutableTransaction(deserialize, s)) {}
# 317 : :
# 318 : 14553 : bool IsNull() const {
# 319 [ - + ][ # # ]: 14553 : return vin.empty() && vout.empty();
# 320 : 14553 : }
# 321 : :
# 322 : 553335219 : const uint256& GetHash() const { return hash; }
# 323 : 6622911 : const uint256& GetWitnessHash() const { return m_witness_hash; };
# 324 : :
# 325 : : // Return sum of txouts.
# 326 : : CAmount GetValueOut() const;
# 327 : :
# 328 : : /**
# 329 : : * Get the total transaction size in bytes, including witness data.
# 330 : : * "Total Size" defined in BIP141 and BIP144.
# 331 : : * @return Total transaction size in bytes
# 332 : : */
# 333 : : unsigned int GetTotalSize() const;
# 334 : :
# 335 : : bool IsCoinBase() const
# 336 : 37432732 : {
# 337 [ + + ][ + + ]: 37432732 : return (vin.size() == 1 && vin[0].prevout.IsNull());
# 338 : 37432732 : }
# 339 : :
# 340 : : friend bool operator==(const CTransaction& a, const CTransaction& b)
# 341 : 3429 : {
# 342 : 3429 : return a.hash == b.hash;
# 343 : 3429 : }
# 344 : :
# 345 : : friend bool operator!=(const CTransaction& a, const CTransaction& b)
# 346 : 78 : {
# 347 : 78 : return a.hash != b.hash;
# 348 : 78 : }
# 349 : :
# 350 : : std::string ToString() const;
# 351 : :
# 352 : : bool HasWitness() const
# 353 : 2484034 : {
# 354 [ + + ]: 3958746 : for (size_t i = 0; i < vin.size(); i++) {
# 355 [ + + ]: 2735192 : if (!vin[i].scriptWitness.IsNull()) {
# 356 : 1260480 : return true;
# 357 : 1260480 : }
# 358 : 2735192 : }
# 359 : 1223554 : return false;
# 360 : 2484034 : }
# 361 : : };
# 362 : :
# 363 : : /** A mutable version of CTransaction. */
# 364 : : struct CMutableTransaction
# 365 : : {
# 366 : : std::vector<CTxIn> vin;
# 367 : : std::vector<CTxOut> vout;
# 368 : : int32_t nVersion;
# 369 : : uint32_t nLockTime;
# 370 : :
# 371 : : CMutableTransaction();
# 372 : : explicit CMutableTransaction(const CTransaction& tx);
# 373 : :
# 374 : : template <typename Stream>
# 375 : 400842 : inline void Serialize(Stream& s) const {
# 376 : 400842 : SerializeTransaction(*this, s);
# 377 : 400842 : }
# 378 : :
# 379 : :
# 380 : : template <typename Stream>
# 381 : 293451 : inline void Unserialize(Stream& s) {
# 382 : 293451 : UnserializeTransaction(*this, s);
# 383 : 293451 : }
# 384 : :
# 385 : : template <typename Stream>
# 386 : 272560 : CMutableTransaction(deserialize_type, Stream& s) {
# 387 : 272560 : Unserialize(s);
# 388 : 272560 : }
# 389 : :
# 390 : : /** Compute the hash of this CMutableTransaction. This is computed on the
# 391 : : * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
# 392 : : */
# 393 : : uint256 GetHash() const;
# 394 : :
# 395 : : bool HasWitness() const
# 396 : 228372 : {
# 397 [ + + ]: 232012 : for (size_t i = 0; i < vin.size(); i++) {
# 398 [ + + ]: 229541 : if (!vin[i].scriptWitness.IsNull()) {
# 399 : 225901 : return true;
# 400 : 225901 : }
# 401 : 229541 : }
# 402 : 2471 : return false;
# 403 : 228372 : }
# 404 : : };
# 405 : :
# 406 : : typedef std::shared_ptr<const CTransaction> CTransactionRef;
# 407 : 547221 : template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
# 408 : :
# 409 : : /** A generic txid reference (txid or wtxid). */
# 410 : : class GenTxid
# 411 : : {
# 412 : : bool m_is_wtxid;
# 413 : : uint256 m_hash;
# 414 : 293087 : GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
# 415 : :
# 416 : : public:
# 417 : 156379 : static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
# 418 : 136708 : static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
# 419 : 407228 : bool IsWtxid() const { return m_is_wtxid; }
# 420 : 512959 : const uint256& GetHash() const { return m_hash; }
# 421 [ + - ][ + - ]: 26398 : friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
# 422 : 4160 : friend bool operator<(const GenTxid& a, const GenTxid& b) { return std::tie(a.m_is_wtxid, a.m_hash) < std::tie(b.m_is_wtxid, b.m_hash); }
# 423 : : };
# 424 : :
# 425 : : #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
|