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 : /**
# 16 : * A flag that is ORed into the protocol version to designate that a transaction
# 17 : * should be (un)serialized without witness data.
# 18 : * Make sure that this does not collide with any of the values in `version.h`
# 19 : * or with `ADDRv2_FORMAT`.
# 20 : */
# 21 : static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
# 22 :
# 23 : /** An outpoint - a combination of a transaction hash and an index n into its vout */
# 24 : class COutPoint
# 25 : {
# 26 : public:
# 27 : uint256 hash;
# 28 : uint32_t n;
# 29 :
# 30 : static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
# 31 :
# 32 2771422 : COutPoint(): n(NULL_INDEX) { }
# 33 1599377 : COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
# 34 :
# 35 42494742 : SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }
# 36 :
# 37 7507 : void SetNull() { hash.SetNull(); n = NULL_INDEX; }
# 38 874197 : bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
# 39 :
# 40 : friend bool operator<(const COutPoint& a, const COutPoint& b)
# 41 197022768 : {
# 42 197022768 : int cmp = a.hash.Compare(b.hash);
# 43 197022768 : return cmp < 0 || (cmp == 0 && a.n < b.n);
# 44 197022768 : }
# 45 :
# 46 : friend bool operator==(const COutPoint& a, const COutPoint& b)
# 47 4150859 : {
# 48 4150859 : return (a.hash == b.hash && a.n == b.n);
# 49 4150859 : }
# 50 :
# 51 : friend bool operator!=(const COutPoint& a, const COutPoint& b)
# 52 3 : {
# 53 3 : return !(a == b);
# 54 3 : }
# 55 :
# 56 : std::string ToString() const;
# 57 : };
# 58 :
# 59 : /** An input of a transaction. It contains the location of the previous
# 60 : * transaction's output that it claims and a signature that matches the
# 61 : * output's public key.
# 62 : */
# 63 : class CTxIn
# 64 : {
# 65 : public:
# 66 : COutPoint prevout;
# 67 : CScript scriptSig;
# 68 : uint32_t nSequence;
# 69 : CScriptWitness scriptWitness; //!< Only serialized through CTransaction
# 70 :
# 71 : /* Setting nSequence to this value for every input in a transaction
# 72 : * disables nLockTime. */
# 73 : static const uint32_t SEQUENCE_FINAL = 0xffffffff;
# 74 :
# 75 : /* Below flags apply in the context of BIP 68*/
# 76 : /* If this flag set, CTxIn::nSequence is NOT interpreted as a
# 77 : * relative lock-time. */
# 78 : static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
# 79 :
# 80 : /* If CTxIn::nSequence encodes a relative lock-time and this flag
# 81 : * is set, the relative lock-time has units of 512 seconds,
# 82 : * otherwise it specifies blocks with a granularity of 1. */
# 83 : static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
# 84 :
# 85 : /* If CTxIn::nSequence encodes a relative lock-time, this mask is
# 86 : * applied to extract that lock-time from the sequence field. */
# 87 : static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
# 88 :
# 89 : /* In order to use the same number of bits to encode roughly the
# 90 : * same wall-clock duration, and because blocks are naturally
# 91 : * limited to occur every 600s on average, the minimum granularity
# 92 : * for time-based relative lock-time is fixed at 512 seconds.
# 93 : * Converting from CTxIn::nSequence to seconds is performed by
# 94 : * multiplying by 512 = 2^9, or equivalently shifting up by
# 95 : * 9 bits. */
# 96 : static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
# 97 :
# 98 : CTxIn()
# 99 1753633 : {
# 100 1753633 : nSequence = SEQUENCE_FINAL;
# 101 1753633 : }
# 102 :
# 103 : explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
# 104 : CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
# 105 :
# 106 1427358 : SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
# 107 :
# 108 : friend bool operator==(const CTxIn& a, const CTxIn& b)
# 109 96 : {
# 110 96 : return (a.prevout == b.prevout &&
# 111 96 : a.scriptSig == b.scriptSig &&
# 112 96 : a.nSequence == b.nSequence);
# 113 96 : }
# 114 :
# 115 : friend bool operator!=(const CTxIn& a, const CTxIn& b)
# 116 0 : {
# 117 0 : return !(a == b);
# 118 0 : }
# 119 :
# 120 : std::string ToString() const;
# 121 : };
# 122 :
# 123 : /** An output of a transaction. It contains the public key that the next input
# 124 : * must be able to sign with to claim it.
# 125 : */
# 126 : class CTxOut
# 127 : {
# 128 : public:
# 129 : CAmount nValue;
# 130 : CScript scriptPubKey;
# 131 :
# 132 : CTxOut()
# 133 66098385 : {
# 134 66098385 : SetNull();
# 135 66098385 : }
# 136 :
# 137 : CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
# 138 :
# 139 29398408 : SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
# 140 :
# 141 : void SetNull()
# 142 66354982 : {
# 143 66354982 : nValue = -1;
# 144 66354982 : scriptPubKey.clear();
# 145 66354982 : }
# 146 :
# 147 : bool IsNull() const
# 148 35084025 : {
# 149 35084025 : return (nValue == -1);
# 150 35084025 : }
# 151 :
# 152 : friend bool operator==(const CTxOut& a, const CTxOut& b)
# 153 671836 : {
# 154 671836 : return (a.nValue == b.nValue &&
# 155 671836 : a.scriptPubKey == b.scriptPubKey);
# 156 671836 : }
# 157 :
# 158 : friend bool operator!=(const CTxOut& a, const CTxOut& b)
# 159 830 : {
# 160 830 : return !(a == b);
# 161 830 : }
# 162 :
# 163 : std::string ToString() const;
# 164 : };
# 165 :
# 166 : struct CMutableTransaction;
# 167 :
# 168 : /**
# 169 : * Basic transaction serialization format:
# 170 : * - int32_t nVersion
# 171 : * - std::vector<CTxIn> vin
# 172 : * - std::vector<CTxOut> vout
# 173 : * - uint32_t nLockTime
# 174 : *
# 175 : * Extended transaction serialization format:
# 176 : * - int32_t nVersion
# 177 : * - unsigned char dummy = 0x00
# 178 : * - unsigned char flags (!= 0)
# 179 : * - std::vector<CTxIn> vin
# 180 : * - std::vector<CTxOut> vout
# 181 : * - if (flags & 1):
# 182 : * - CTxWitness wit;
# 183 : * - uint32_t nLockTime
# 184 : */
# 185 : template<typename Stream, typename TxType>
# 186 4886 : inline void UnserializeTransaction(TxType& tx, Stream& s) {
# 187 4886 : const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
# 188 4886 :
# 189 4886 : s >> tx.nVersion;
# 190 4886 : unsigned char flags = 0;
# 191 4886 : tx.vin.clear();
# 192 4886 : tx.vout.clear();
# 193 4886 : /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
# 194 4886 : s >> tx.vin;
# 195 4886 : if (tx.vin.size() == 0 && fAllowWitness) {
# 196 1158 : /* We read a dummy or an empty vin. */
# 197 1158 : s >> flags;
# 198 1158 : if (flags != 0) {
# 199 1152 : s >> tx.vin;
# 200 1152 : s >> tx.vout;
# 201 1152 : }
# 202 3728 : } else {
# 203 3728 : /* We read a non-empty vin. Assume a normal vout follows. */
# 204 3728 : s >> tx.vout;
# 205 3728 : }
# 206 4886 : if ((flags & 1) && fAllowWitness) {
# 207 1152 : /* The witness flag is present, and we support witnesses. */
# 208 1152 : flags ^= 1;
# 209 11410 : for (size_t i = 0; i < tx.vin.size(); i++) {
# 210 10258 : s >> tx.vin[i].scriptWitness.stack;
# 211 10258 : }
# 212 1152 : if (!tx.HasWitness()) {
# 213 0 : /* It's illegal to encode witnesses when all witness stacks are empty. */
# 214 0 : throw std::ios_base::failure("Superfluous witness record");
# 215 0 : }
# 216 4886 : }
# 217 4886 : if (flags) {
# 218 0 : /* Unknown flag in the serialization */
# 219 0 : throw std::ios_base::failure("Unknown transaction optional data");
# 220 0 : }
# 221 4886 : s >> tx.nLockTime;
# 222 4886 : }
# 223 :
# 224 : template<typename Stream, typename TxType>
# 225 1312215 : inline void SerializeTransaction(const TxType& tx, Stream& s) {
# 226 1312215 : const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
# 227 1312215 :
# 228 1312215 : s << tx.nVersion;
# 229 1312215 : unsigned char flags = 0;
# 230 1312215 : // Consistency check
# 231 1312215 : if (fAllowWitness) {
# 232 96021 : /* Check whether witnesses need to be serialized. */
# 233 96021 : if (tx.HasWitness()) {
# 234 9060 : flags |= 1;
# 235 9060 : }
# 236 96021 : }
# 237 1312215 : if (flags) {
# 238 9060 : /* Use extended format in case witnesses are to be serialized. */
# 239 9060 : std::vector<CTxIn> vinDummy;
# 240 9060 : s << vinDummy;
# 241 9060 : s << flags;
# 242 9060 : }
# 243 1312215 : s << tx.vin;
# 244 1312215 : s << tx.vout;
# 245 1312215 : if (flags & 1) {
# 246 36226 : for (size_t i = 0; i < tx.vin.size(); i++) {
# 247 27166 : s << tx.vin[i].scriptWitness.stack;
# 248 27166 : }
# 249 9060 : }
# 250 1312215 : s << tx.nLockTime;
# 251 1312215 : }
# 252 :
# 253 :
# 254 : /** The basic transaction that is broadcasted on the network and contained in
# 255 : * blocks. A transaction can contain multiple inputs and outputs.
# 256 : */
# 257 : class CTransaction
# 258 : {
# 259 : public:
# 260 : // Default transaction version.
# 261 : static const int32_t CURRENT_VERSION=2;
# 262 :
# 263 : // Changing the default transaction version requires a two step process: first
# 264 : // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
# 265 : // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
# 266 : // MAX_STANDARD_VERSION will be equal.
# 267 : static const int32_t MAX_STANDARD_VERSION=2;
# 268 :
# 269 : // The local variables are made const to prevent unintended modification
# 270 : // without updating the cached hash value. However, CTransaction is not
# 271 : // actually immutable; deserialization and assignment are implemented,
# 272 : // and bypass the constness. This is safe, as they update the entire
# 273 : // structure, including the hash.
# 274 : const std::vector<CTxIn> vin;
# 275 : const std::vector<CTxOut> vout;
# 276 : const int32_t nVersion;
# 277 : const uint32_t nLockTime;
# 278 :
# 279 : private:
# 280 : /** Memory only. */
# 281 : const uint256 hash;
# 282 : const uint256 m_witness_hash;
# 283 :
# 284 : uint256 ComputeHash() const;
# 285 : uint256 ComputeWitnessHash() const;
# 286 :
# 287 : public:
# 288 : /** Construct a CTransaction that qualifies as IsNull() */
# 289 : CTransaction();
# 290 :
# 291 : /** Convert a CMutableTransaction into a CTransaction. */
# 292 : explicit CTransaction(const CMutableTransaction &tx);
# 293 : CTransaction(CMutableTransaction &&tx);
# 294 :
# 295 : template <typename Stream>
# 296 1073017 : inline void Serialize(Stream& s) const {
# 297 1073017 : SerializeTransaction(*this, s);
# 298 1073017 : }
# 299 :
# 300 : /** This deserializing constructor is provided instead of an Unserialize method.
# 301 : * Unserialize is not possible, since it would require overwriting const fields. */
# 302 : template <typename Stream>
# 303 4821 : CTransaction(deserialize_type, Stream& s) : CTransaction(CMutableTransaction(deserialize, s)) {}
# 304 :
# 305 10 : bool IsNull() const {
# 306 10 : return vin.empty() && vout.empty();
# 307 10 : }
# 308 :
# 309 377607417 : const uint256& GetHash() const { return hash; }
# 310 577137 : const uint256& GetWitnessHash() const { return m_witness_hash; };
# 311 :
# 312 : // Return sum of txouts.
# 313 : CAmount GetValueOut() const;
# 314 :
# 315 : /**
# 316 : * Get the total transaction size in bytes, including witness data.
# 317 : * "Total Size" defined in BIP141 and BIP144.
# 318 : * @return Total transaction size in bytes
# 319 : */
# 320 : unsigned int GetTotalSize() const;
# 321 :
# 322 : bool IsCoinBase() const
# 323 1048905 : {
# 324 1048905 : return (vin.size() == 1 && vin[0].prevout.IsNull());
# 325 1048905 : }
# 326 :
# 327 : friend bool operator==(const CTransaction& a, const CTransaction& b)
# 328 18 : {
# 329 18 : return a.hash == b.hash;
# 330 18 : }
# 331 :
# 332 : friend bool operator!=(const CTransaction& a, const CTransaction& b)
# 333 0 : {
# 334 0 : return a.hash != b.hash;
# 335 0 : }
# 336 :
# 337 : std::string ToString() const;
# 338 :
# 339 : bool HasWitness() const
# 340 975318 : {
# 341 1858943 : for (size_t i = 0; i < vin.size(); i++) {
# 342 895883 : if (!vin[i].scriptWitness.IsNull()) {
# 343 12258 : return true;
# 344 12258 : }
# 345 895883 : }
# 346 975318 : return false;
# 347 975318 : }
# 348 : };
# 349 :
# 350 : /** A mutable version of CTransaction. */
# 351 : struct CMutableTransaction
# 352 : {
# 353 : std::vector<CTxIn> vin;
# 354 : std::vector<CTxOut> vout;
# 355 : int32_t nVersion;
# 356 : uint32_t nLockTime;
# 357 :
# 358 : CMutableTransaction();
# 359 : explicit CMutableTransaction(const CTransaction& tx);
# 360 :
# 361 : template <typename Stream>
# 362 239198 : inline void Serialize(Stream& s) const {
# 363 239198 : SerializeTransaction(*this, s);
# 364 239198 : }
# 365 :
# 366 :
# 367 : template <typename Stream>
# 368 4886 : inline void Unserialize(Stream& s) {
# 369 4886 : UnserializeTransaction(*this, s);
# 370 4886 : }
# 371 :
# 372 : template <typename Stream>
# 373 4821 : CMutableTransaction(deserialize_type, Stream& s) {
# 374 4821 : Unserialize(s);
# 375 4821 : }
# 376 :
# 377 : /** Compute the hash of this CMutableTransaction. This is computed on the
# 378 : * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
# 379 : */
# 380 : uint256 GetHash() const;
# 381 :
# 382 : bool HasWitness() const
# 383 3946 : {
# 384 6452 : for (size_t i = 0; i < vin.size(); i++) {
# 385 3984 : if (!vin[i].scriptWitness.IsNull()) {
# 386 1478 : return true;
# 387 1478 : }
# 388 3984 : }
# 389 3946 : return false;
# 390 3946 : }
# 391 : };
# 392 :
# 393 : typedef std::shared_ptr<const CTransaction> CTransactionRef;
# 394 0 : static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
# 395 456027 : template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
# 396 :
# 397 : #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
|