美文网首页比特币源码学习笔记
比特币源码阅读(交易-CTransaction)

比特币源码阅读(交易-CTransaction)

作者: 坠叶飘香 | 来源:发表于2018-07-27 17:51 被阅读0次

    https://en.bitcoin.it/wiki/Protocol_documentation#tx

    2018-07-27 17-29-16 的屏幕截图.png

    src/primitives/transaction.h

    /** The basic transaction that is broadcasted on the network and contained in
     * blocks.  A transaction can contain multiple inputs and outputs.
     */
    class CTransaction
    {
    public:
        // Default transaction version.
        static const int32_t CURRENT_VERSION=2;
    
        // Changing the default transaction version requires a two step process: first
        // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
        // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
        // MAX_STANDARD_VERSION will be equal.
        static const int32_t MAX_STANDARD_VERSION=2;
    
        // The local variables are made const to prevent unintended modification
        // without updating the cached hash value. However, CTransaction is not
        // actually immutable(不变的); deserialization and assignment are implemented,
        // and bypass the constness. This is safe, as they update the entire
        // structure, including the hash.
        const std::vector<CTxIn> vin; //输入
        const std::vector<CTxOut> vout; //输出
        const int32_t nVersion;
        const uint32_t nLockTime;
    
    private:
        /** Memory only. */
        const uint256 hash;
        const uint256 m_witness_hash;
    
        uint256 ComputeHash() const; //所以区块浏览器上面的交易hash其实是算出来的,不是本来就存在与交易的数据结构中
        uint256 ComputeWitnessHash() const;
    
    public:
        /** Construct a CTransaction that qualifies as IsNull() */
        CTransaction();
    
        /** Convert a CMutableTransaction into a CTransaction. */
        CTransaction(const CMutableTransaction &tx);
        CTransaction(CMutableTransaction &&tx);
    
        template <typename Stream>
        inline void Serialize(Stream& s) const {
            SerializeTransaction(*this, s);
        }
    
        /** This deserializing constructor is provided instead of an Unserialize method.
         *  Unserialize is not possible, since it would require overwriting const fields. */
        template <typename Stream>
        CTransaction(deserialize_type, Stream& s) : CTransaction(CMutableTransaction(deserialize, s)) {}
    
        bool IsNull() const {
            return vin.empty() && vout.empty();
        }
    
        const uint256& GetHash() const { return hash; }
        const uint256& GetWitnessHash() const { return m_witness_hash; };
    
        // Return sum of txouts.
        CAmount GetValueOut() const;
        // GetValueIn() is a method on CCoinsViewCache, because
        // inputs must be known to compute value in.
    
        /**
         * Get the total transaction size in bytes, including witness data.
         * "Total Size" defined in BIP141 and BIP144.
         * @return Total transaction size in bytes
         */
        unsigned int GetTotalSize() const;
    
        bool IsCoinBase() const
        {
            return (vin.size() == 1 && vin[0].prevout.IsNull()); //是否为CoinBase的判断方法
        }
    
        friend bool operator==(const CTransaction& a, const CTransaction& b)
        {
            return a.hash == b.hash; //hash相等
        }
    
        friend bool operator!=(const CTransaction& a, const CTransaction& b)
        {
            return a.hash != b.hash; //hash不等
        }
    
        std::string ToString() const;
    
        bool HasWitness() const
        {
            for (size_t i = 0; i < vin.size(); i++) {
                if (!vin[i].scriptWitness.IsNull()) //会不会存在部分vin存在scriptWitness()
                    return true;
                }
            }
            return false;
        }
    };
    

    相关文章

      网友评论

        本文标题:比特币源码阅读(交易-CTransaction)

        本文链接:https://www.haomeiwen.com/subject/wseimftx.html