Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2019 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 <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 : 6539521 : COutPoint(): n(NULL_INDEX) { }
# 35 : 15777592 : COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
# 36 : :
# 37 : 47447053 : SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }
# 38 : :
# 39 : 24044 : void SetNull() { hash.SetNull(); n = NULL_INDEX; }
# 40 : 12804807 : bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
# 41 : :
# 42 : : friend bool operator<(const COutPoint& a, const COutPoint& b)
# 43 : 290985207 : {
# 44 : 290985207 : int cmp = a.hash.Compare(b.hash);
# 45 : 290985207 : return cmp < 0 || (cmp == 0 && a.n < b.n);
# 46 : 290985207 : }
# 47 : :
# 48 : : friend bool operator==(const COutPoint& a, const COutPoint& b)
# 49 : 38567503 : {
# 50 : 38567503 : return (a.hash == b.hash && a.n == b.n);
# 51 : 38567503 : }
# 52 : :
# 53 : : friend bool operator!=(const COutPoint& a, const COutPoint& b)
# 54 : 13 : {
# 55 : 13 : return !(a == b);
# 56 : 13 : }
# 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 : : /* Setting nSequence to this value for every input in a transaction
# 74 : : * disables nLockTime. */
# 75 : : static const uint32_t SEQUENCE_FINAL = 0xffffffff;
# 76 : :
# 77 : : /* Below flags apply in the context of BIP 68*/
# 78 : : /* If this flag set, CTxIn::nSequence is NOT interpreted as a
# 79 : : * relative lock-time. */
# 80 : : static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
# 81 : :
# 82 : : /* If CTxIn::nSequence encodes a relative lock-time and this flag
# 83 : : * is set, the relative lock-time has units of 512 seconds,
# 84 : : * otherwise it specifies blocks with a granularity of 1. */
# 85 : : static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
# 86 : :
# 87 : : /* If CTxIn::nSequence encodes a relative lock-time, this mask is
# 88 : : * applied to extract that lock-time from the sequence field. */
# 89 : : static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
# 90 : :
# 91 : : /* In order to use the same number of bits to encode roughly the
# 92 : : * same wall-clock duration, and because blocks are naturally
# 93 : : * limited to occur every 600s on average, the minimum granularity
# 94 : : * for time-based relative lock-time is fixed at 512 seconds.
# 95 : : * Converting from CTxIn::nSequence to seconds is performed by
# 96 : : * multiplying by 512 = 2^9, or equivalently shifting up by
# 97 : : * 9 bits. */
# 98 : : static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
# 99 : :
# 100 : : CTxIn()
# 101 : 4644495 : {
# 102 : 4644495 : nSequence = SEQUENCE_FINAL;
# 103 : 4644495 : }
# 104 : :
# 105 : : explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
# 106 : : CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
# 107 : :
# 108 : 4492100 : SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
# 109 : :
# 110 : : friend bool operator==(const CTxIn& a, const CTxIn& b)
# 111 : 146 : {
# 112 : 146 : return (a.prevout == b.prevout &&
# 113 : 98 : a.scriptSig == b.scriptSig &&
# 114 : 98 : a.nSequence == b.nSequence);
# 115 : 146 : }
# 116 : :
# 117 : : friend bool operator!=(const CTxIn& a, const CTxIn& b)
# 118 : 0 : {
# 119 : 0 : return !(a == b);
# 120 : 0 : }
# 121 : :
# 122 : : std::string ToString() const;
# 123 : : };
# 124 : :
# 125 : : /** An output of a transaction. It contains the public key that the next input
# 126 : : * must be able to sign with to claim it.
# 127 : : */
# 128 : : class CTxOut
# 129 : : {
# 130 : : public:
# 131 : : CAmount nValue;
# 132 : : CScript scriptPubKey;
# 133 : :
# 134 : : CTxOut()
# 135 : 81991451 : {
# 136 : 81991451 : SetNull();
# 137 : 81991451 : }
# 138 : :
# 139 : : CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
# 140 : :
# 141 : 39321930 : SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
# 142 : :
# 143 : : void SetNull()
# 144 : 87395241 : {
# 145 : 87395241 : nValue = -1;
# 146 : 87395241 : scriptPubKey.clear();
# 147 : 87395241 : }
# 148 : :
# 149 : : bool IsNull() const
# 150 : 76652727 : {
# 151 : 76652727 : return (nValue == -1);
# 152 : 76652727 : }
# 153 : :
# 154 : : friend bool operator==(const CTxOut& a, const CTxOut& b)
# 155 : 725683 : {
# 156 : 725683 : return (a.nValue == b.nValue &&
# 157 : 725683 : a.scriptPubKey == b.scriptPubKey);
# 158 : 725683 : }
# 159 : :
# 160 : : friend bool operator!=(const CTxOut& a, const CTxOut& b)
# 161 : 19469 : {
# 162 : 19469 : return !(a == b);
# 163 : 19469 : }
# 164 : :
# 165 : : std::string ToString() const;
# 166 : : };
# 167 : :
# 168 : : struct CMutableTransaction;
# 169 : :
# 170 : : /**
# 171 : : * Basic transaction serialization format:
# 172 : : * - int32_t nVersion
# 173 : : * - std::vector<CTxIn> vin
# 174 : : * - std::vector<CTxOut> vout
# 175 : : * - uint32_t nLockTime
# 176 : : *
# 177 : : * Extended transaction serialization format:
# 178 : : * - int32_t nVersion
# 179 : : * - unsigned char dummy = 0x00
# 180 : : * - unsigned char flags (!= 0)
# 181 : : * - std::vector<CTxIn> vin
# 182 : : * - std::vector<CTxOut> vout
# 183 : : * - if (flags & 1):
# 184 : : * - CTxWitness wit;
# 185 : : * - uint32_t nLockTime
# 186 : : */
# 187 : : template<typename Stream, typename TxType>
# 188 : 216547 : inline void UnserializeTransaction(TxType& tx, Stream& s) {
# 189 : 216547 : const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
# 190 : 216547 :
# 191 : 216547 : s >> tx.nVersion;
# 192 : 216547 : unsigned char flags = 0;
# 193 : 216547 : tx.vin.clear();
# 194 : 216547 : tx.vout.clear();
# 195 : : /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
# 196 : 216547 : s >> tx.vin;
# 197 : 216547 : if (tx.vin.size() == 0 && fAllowWitness) {
# 198 : : /* We read a dummy or an empty vin. */
# 199 : 126985 : s >> flags;
# 200 : 126985 : if (flags != 0) {
# 201 : 126977 : s >> tx.vin;
# 202 : 126977 : s >> tx.vout;
# 203 : 126977 : }
# 204 : 89562 : } else {
# 205 : : /* We read a non-empty vin. Assume a normal vout follows. */
# 206 : 89562 : s >> tx.vout;
# 207 : 89562 : }
# 208 : 216547 : if ((flags & 1) && fAllowWitness) {
# 209 : : /* The witness flag is present, and we support witnesses. */
# 210 : 126976 : flags ^= 1;
# 211 : 325904 : for (size_t i = 0; i < tx.vin.size(); i++) {
# 212 : 198928 : s >> tx.vin[i].scriptWitness.stack;
# 213 : 198928 : }
# 214 : 126976 : if (!tx.HasWitness()) {
# 215 : : /* It's illegal to encode witnesses when all witness stacks are empty. */
# 216 : 2 : throw std::ios_base::failure("Superfluous witness record");
# 217 : 2 : }
# 218 : 216545 : }
# 219 : 216545 : if (flags) {
# 220 : : /* Unknown flag in the serialization */
# 221 : 2 : throw std::ios_base::failure("Unknown transaction optional data");
# 222 : 2 : }
# 223 : 216543 : s >> tx.nLockTime;
# 224 : 216543 : }
# 225 : :
# 226 : : template<typename Stream, typename TxType>
# 227 : 3090761 : inline void SerializeTransaction(const TxType& tx, Stream& s) {
# 228 : 3090761 : const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
# 229 : 3090761 :
# 230 : 3090761 : s << tx.nVersion;
# 231 : 3090761 : unsigned char flags = 0;
# 232 : : // Consistency check
# 233 : 3090761 : if (fAllowWitness) {
# 234 : : /* Check whether witnesses need to be serialized. */
# 235 : 902787 : if (tx.HasWitness()) {
# 236 : 460996 : flags |= 1;
# 237 : 460996 : }
# 238 : 902787 : }
# 239 : 3090761 : if (flags) {
# 240 : : /* Use extended format in case witnesses are to be serialized. */
# 241 : 460996 : std::vector<CTxIn> vinDummy;
# 242 : 460996 : s << vinDummy;
# 243 : 460996 : s << flags;
# 244 : 460996 : }
# 245 : 3090761 : s << tx.vin;
# 246 : 3090761 : s << tx.vout;
# 247 : 3090761 : if (flags & 1) {
# 248 : 1201151 : for (size_t i = 0; i < tx.vin.size(); i++) {
# 249 : 740155 : s << tx.vin[i].scriptWitness.stack;
# 250 : 740155 : }
# 251 : 460996 : }
# 252 : 3090761 : s << tx.nLockTime;
# 253 : 3090761 : }
# 254 : :
# 255 : :
# 256 : : /** The basic transaction that is broadcasted on the network and contained in
# 257 : : * blocks. A transaction can contain multiple inputs and outputs.
# 258 : : */
# 259 : : class CTransaction
# 260 : : {
# 261 : : public:
# 262 : : // Default transaction version.
# 263 : : static const int32_t CURRENT_VERSION=2;
# 264 : :
# 265 : : // Changing the default transaction version requires a two step process: first
# 266 : : // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
# 267 : : // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
# 268 : : // MAX_STANDARD_VERSION will be equal.
# 269 : : static const int32_t MAX_STANDARD_VERSION=2;
# 270 : :
# 271 : : // The local variables are made const to prevent unintended modification
# 272 : : // without updating the cached hash value. However, CTransaction is not
# 273 : : // actually immutable; deserialization and assignment are implemented,
# 274 : : // and bypass the constness. This is safe, as they update the entire
# 275 : : // structure, including the hash.
# 276 : : const std::vector<CTxIn> vin;
# 277 : : const std::vector<CTxOut> vout;
# 278 : : const int32_t nVersion;
# 279 : : const uint32_t nLockTime;
# 280 : :
# 281 : : private:
# 282 : : /** Memory only. */
# 283 : : const uint256 hash;
# 284 : : const uint256 m_witness_hash;
# 285 : :
# 286 : : uint256 ComputeHash() const;
# 287 : : uint256 ComputeWitnessHash() const;
# 288 : :
# 289 : : public:
# 290 : : /** Construct a CTransaction that qualifies as IsNull() */
# 291 : : CTransaction();
# 292 : :
# 293 : : /** Convert a CMutableTransaction into a CTransaction. */
# 294 : : explicit CTransaction(const CMutableTransaction &tx);
# 295 : : CTransaction(CMutableTransaction &&tx);
# 296 : :
# 297 : : template <typename Stream>
# 298 : 2851028 : inline void Serialize(Stream& s) const {
# 299 : 2851028 : SerializeTransaction(*this, s);
# 300 : 2851028 : }
# 301 : :
# 302 : : /** This deserializing constructor is provided instead of an Unserialize method.
# 303 : : * Unserialize is not possible, since it would require overwriting const fields. */
# 304 : : template <typename Stream>
# 305 : 206023 : CTransaction(deserialize_type, Stream& s) : CTransaction(CMutableTransaction(deserialize, s)) {}
# 306 : :
# 307 : 9592 : bool IsNull() const {
# 308 : 9592 : return vin.empty() && vout.empty();
# 309 : 9592 : }
# 310 : :
# 311 : 2307410785 : const uint256& GetHash() const { return hash; }
# 312 : 6671412 : const uint256& GetWitnessHash() const { return m_witness_hash; };
# 313 : :
# 314 : : // Return sum of txouts.
# 315 : : CAmount GetValueOut() const;
# 316 : :
# 317 : : /**
# 318 : : * Get the total transaction size in bytes, including witness data.
# 319 : : * "Total Size" defined in BIP141 and BIP144.
# 320 : : * @return Total transaction size in bytes
# 321 : : */
# 322 : : unsigned int GetTotalSize() const;
# 323 : :
# 324 : : bool IsCoinBase() const
# 325 : 18272736 : {
# 326 : 18272736 : return (vin.size() == 1 && vin[0].prevout.IsNull());
# 327 : 18272736 : }
# 328 : :
# 329 : : friend bool operator==(const CTransaction& a, const CTransaction& b)
# 330 : 2574 : {
# 331 : 2574 : return a.hash == b.hash;
# 332 : 2574 : }
# 333 : :
# 334 : : friend bool operator!=(const CTransaction& a, const CTransaction& b)
# 335 : 48 : {
# 336 : 48 : return a.hash != b.hash;
# 337 : 48 : }
# 338 : :
# 339 : : std::string ToString() const;
# 340 : :
# 341 : : bool HasWitness() const
# 342 : 2352461 : {
# 343 : 3911615 : for (size_t i = 0; i < vin.size(); i++) {
# 344 : 2242914 : if (!vin[i].scriptWitness.IsNull()) {
# 345 : 683760 : return true;
# 346 : 683760 : }
# 347 : 2242914 : }
# 348 : 1668701 : return false;
# 349 : 2352461 : }
# 350 : : };
# 351 : :
# 352 : : /** A mutable version of CTransaction. */
# 353 : : struct CMutableTransaction
# 354 : : {
# 355 : : std::vector<CTxIn> vin;
# 356 : : std::vector<CTxOut> vout;
# 357 : : int32_t nVersion;
# 358 : : uint32_t nLockTime;
# 359 : :
# 360 : : CMutableTransaction();
# 361 : : explicit CMutableTransaction(const CTransaction& tx);
# 362 : :
# 363 : : template <typename Stream>
# 364 : 239734 : inline void Serialize(Stream& s) const {
# 365 : 239734 : SerializeTransaction(*this, s);
# 366 : 239734 : }
# 367 : :
# 368 : :
# 369 : : template <typename Stream>
# 370 : 216548 : inline void Unserialize(Stream& s) {
# 371 : 216548 : UnserializeTransaction(*this, s);
# 372 : 216548 : }
# 373 : :
# 374 : : template <typename Stream>
# 375 : 206023 : CMutableTransaction(deserialize_type, Stream& s) {
# 376 : 206023 : Unserialize(s);
# 377 : 206023 : }
# 378 : :
# 379 : : /** Compute the hash of this CMutableTransaction. This is computed on the
# 380 : : * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
# 381 : : */
# 382 : : uint256 GetHash() const;
# 383 : :
# 384 : : bool HasWitness() const
# 385 : 129783 : {
# 386 : 132832 : for (size_t i = 0; i < vin.size(); i++) {
# 387 : 130360 : if (!vin[i].scriptWitness.IsNull()) {
# 388 : 127311 : return true;
# 389 : 127311 : }
# 390 : 130360 : }
# 391 : 2472 : return false;
# 392 : 129783 : }
# 393 : : };
# 394 : :
# 395 : : typedef std::shared_ptr<const CTransaction> CTransactionRef;
# 396 : 89 : static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
# 397 : 698651 : template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
# 398 : :
# 399 : : /** A generic txid reference (txid or wtxid). */
# 400 : : class GenTxid
# 401 : : {
# 402 : : const bool m_is_wtxid;
# 403 : : const uint256 m_hash;
# 404 : : public:
# 405 : 2245781 : GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
# 406 : 2300342 : bool IsWtxid() const { return m_is_wtxid; }
# 407 : 2397653 : const uint256& GetHash() const { return m_hash; }
# 408 : 0 : friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
# 409 : 0 : 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); }
# 410 : : };
# 411 : :
# 412 : : #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
|