Changeset View
Changeset View
Standalone View
Standalone View
src/primitives/transaction.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_PRIMITIVES_TRANSACTION_H | #ifndef BITCOIN_PRIMITIVES_TRANSACTION_H | ||||
#define BITCOIN_PRIMITIVES_TRANSACTION_H | #define BITCOIN_PRIMITIVES_TRANSACTION_H | ||||
#include "amount.h" | #include "amount.h" | ||||
#include "script/script.h" | #include "script/script.h" | ||||
#include "serialize.h" | #include "serialize.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include "boost/serialization/strong_typedef.hpp" | |||||
static const int SERIALIZE_TRANSACTION = 0x00; | static const int SERIALIZE_TRANSACTION = 0x00; | ||||
/** A unspentid is either: | |||||
* The hash of a v1 or v2 transaction or | |||||
* The double sha256 of all transaction data *except the inputs* | |||||
* | |||||
* It is used as reference to a transaction in an Outpoint. */ | |||||
class unspentid_t : public uint256 { | |||||
public: | |||||
unspentid_t() {} | |||||
explicit unspentid_t(const uint256 &b) : uint256(b) {} | |||||
}; | |||||
/** A txhash is the double sha256 hash of the full transaction data */ | |||||
class txhash_t : public uint256 { | |||||
public: | |||||
txhash_t() {} | |||||
explicit txhash_t(const uint256 &b) : uint256(b) {} | |||||
}; | |||||
/** An outpoint - a combination of a transaction hash and an index n into its | /** An outpoint - a combination of a transaction hash and an index n into its | ||||
* vout */ | * vout */ | ||||
class COutPoint { | class COutPoint { | ||||
public: | public: | ||||
uint256 hash; | unspentid_t unspentid; | ||||
uint32_t n; | uint32_t n; | ||||
COutPoint() { SetNull(); } | COutPoint() { SetNull(); } | ||||
COutPoint(uint256 hashIn, uint32_t nIn) { | COutPoint(unspentid_t unspentidIn, uint32_t nIn) { | ||||
hash = hashIn; | unspentid = unspentidIn; | ||||
n = nIn; | n = nIn; | ||||
} | } | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(hash); | READWRITE(unspentid); | ||||
READWRITE(n); | READWRITE(n); | ||||
} | } | ||||
void SetNull() { | void SetNull() { | ||||
hash.SetNull(); | unspentid.SetNull(); | ||||
n = (uint32_t)-1; | n = (uint32_t)-1; | ||||
} | } | ||||
bool IsNull() const { return (hash.IsNull() && n == (uint32_t)-1); } | bool IsNull() const { return (unspentid.IsNull() && n == (uint32_t)-1); } | ||||
friend bool operator<(const COutPoint &a, const COutPoint &b) { | friend bool operator<(const COutPoint &a, const COutPoint &b) { | ||||
int cmp = a.hash.Compare(b.hash); | int cmp = a.unspentid.Compare(b.unspentid); | ||||
return cmp < 0 || (cmp == 0 && a.n < b.n); | return cmp < 0 || (cmp == 0 && a.n < b.n); | ||||
} | } | ||||
friend bool operator==(const COutPoint &a, const COutPoint &b) { | friend bool operator==(const COutPoint &a, const COutPoint &b) { | ||||
return (a.hash == b.hash && a.n == b.n); | return (a.unspentid == b.unspentid && a.n == b.n); | ||||
} | } | ||||
friend bool operator!=(const COutPoint &a, const COutPoint &b) { | friend bool operator!=(const COutPoint &a, const COutPoint &b) { | ||||
return !(a == b); | return !(a == b); | ||||
} | } | ||||
std::string ToString() const; | std::string ToString() const; | ||||
}; | }; | ||||
Show All 34 Lines | public: | ||||
* multiplying by 512 = 2^9, or equivalently shifting up by | * multiplying by 512 = 2^9, or equivalently shifting up by | ||||
* 9 bits. */ | * 9 bits. */ | ||||
static const int SEQUENCE_LOCKTIME_GRANULARITY = 9; | static const int SEQUENCE_LOCKTIME_GRANULARITY = 9; | ||||
CTxIn() { nSequence = SEQUENCE_FINAL; } | CTxIn() { nSequence = SEQUENCE_FINAL; } | ||||
explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn = CScript(), | explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn = CScript(), | ||||
uint32_t nSequenceIn = SEQUENCE_FINAL); | uint32_t nSequenceIn = SEQUENCE_FINAL); | ||||
CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn = CScript(), | CTxIn(unspentid_t unspentid, uint32_t nOut, CScript scriptSigIn = CScript(), | ||||
uint32_t nSequenceIn = SEQUENCE_FINAL); | uint32_t nSequenceIn = SEQUENCE_FINAL); | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(prevout); | READWRITE(prevout); | ||||
READWRITE(*(CScriptBase *)(&scriptSig)); | READWRITE(*(CScriptBase *)(&scriptSig)); | ||||
▲ Show 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | public: | ||||
// structure, including the hash. | // structure, including the hash. | ||||
const int32_t nVersion; | const int32_t nVersion; | ||||
const std::vector<CTxIn> vin; | const std::vector<CTxIn> vin; | ||||
const std::vector<CTxOut> vout; | const std::vector<CTxOut> vout; | ||||
const uint32_t nLockTime; | const uint32_t nLockTime; | ||||
private: | private: | ||||
/** Memory only. */ | /** Memory only. */ | ||||
const uint256 hash; | const txhash_t hash; | ||||
const unspentid_t unspentid; | |||||
uint256 ComputeHash() const; | txhash_t ComputeHash() const; | ||||
unspentid_t Computeunspentid() const; | |||||
public: | public: | ||||
/** Construct a CTransaction that qualifies as IsNull() */ | /** Construct a CTransaction that qualifies as IsNull() */ | ||||
CTransaction(); | CTransaction(); | ||||
/** Convert a CMutableTransaction into a CTransaction. */ | /** Convert a CMutableTransaction into a CTransaction. */ | ||||
CTransaction(const CMutableTransaction &tx); | CTransaction(const CMutableTransaction &tx); | ||||
CTransaction(CMutableTransaction &&tx); | CTransaction(CMutableTransaction &&tx); | ||||
template <typename Stream> inline void Serialize(Stream &s) const { | template <typename Stream> inline void Serialize(Stream &s) const { | ||||
SerializeTransaction(*this, s); | SerializeTransaction(*this, s); | ||||
} | } | ||||
/** This deserializing constructor is provided instead of an Unserialize | /** This deserializing constructor is provided instead of an Unserialize | ||||
* method. Unserialize is not possible, since it would require overwriting | * method. Unserialize is not possible, since it would require overwriting | ||||
* const fields. */ | * const fields. */ | ||||
template <typename Stream> | template <typename Stream> | ||||
CTransaction(deserialize_type, Stream &s) | CTransaction(deserialize_type, Stream &s) | ||||
: CTransaction(CMutableTransaction(deserialize, s)) {} | : CTransaction(CMutableTransaction(deserialize, s)) {} | ||||
bool IsNull() const { return vin.empty() && vout.empty(); } | bool IsNull() const { return vin.empty() && vout.empty(); } | ||||
const uint256 &GetId() const { return hash; } | const txhash_t &GetHash() const { return hash; } | ||||
// Compute a hash that includes both transaction and witness data | /** | ||||
uint256 GetHash() const; | * Returns the identifier of the transaction used by outpoints. | ||||
* For v3 transactions, this is the immutableId | |||||
**/ | |||||
const unspentid_t &Getunspentid() const { return unspentid; } | |||||
// Return sum of txouts. | // Return sum of txouts. | ||||
Amount GetValueOut() const; | Amount GetValueOut() const; | ||||
// GetValueIn() is a method on CCoinsViewCache, because | // GetValueIn() is a method on CCoinsViewCache, because | ||||
// inputs must be known to compute value in. | // inputs must be known to compute value in. | ||||
// Compute priority, given priority of inputs and (optionally) tx size | // Compute priority, given priority of inputs and (optionally) tx size | ||||
double ComputePriority(double dPriorityInputs, | double ComputePriority(double dPriorityInputs, | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | struct CMutableTransaction { | ||||
} | } | ||||
template <typename Stream> | template <typename Stream> | ||||
CMutableTransaction(deserialize_type, Stream &s) { | CMutableTransaction(deserialize_type, Stream &s) { | ||||
Unserialize(s); | Unserialize(s); | ||||
} | } | ||||
/** Compute the hash of this CMutableTransaction. This is computed on the | /** Compute the hash of this CMutableTransaction. This is computed on the | ||||
* fly, as opposed to GetId() in CTransaction, which uses a cached result. | * fly, as opposed to GetHash() in CTransaction, which uses a cached result. | ||||
*/ | |||||
txhash_t GetHash() const; | |||||
/** Compute the unspentid of this CMutableTransaction. This is computed on | |||||
* the | |||||
* fly, as opposed to Getunspentid() in CTransaction, which uses a cached | |||||
* result. | |||||
*/ | */ | ||||
uint256 GetId() const; | unspentid_t Getunspentid() const; | ||||
friend bool operator==(const CMutableTransaction &a, | friend bool operator==(const CMutableTransaction &a, | ||||
const CMutableTransaction &b) { | const CMutableTransaction &b) { | ||||
return a.GetId() == b.GetId(); | return a.GetHash() == b.GetHash(); | ||||
} | } | ||||
}; | }; | ||||
typedef std::shared_ptr<const CTransaction> CTransactionRef; | typedef std::shared_ptr<const CTransaction> CTransactionRef; | ||||
static inline CTransactionRef MakeTransactionRef() { | static inline CTransactionRef MakeTransactionRef() { | ||||
return std::make_shared<const CTransaction>(); | return std::make_shared<const CTransaction>(); | ||||
} | } | ||||
template <typename Tx> | template <typename Tx> | ||||
Show All 22 Lines |