From eda37330911b005f4be0c2d934346b26247d50f5 Mon Sep 17 00:00:00 2001 From: jtimon Date: Thu, 23 Oct 2014 02:05:11 +0200 Subject: [PATCH 1/5] MOVEONLY: Move CFeeRate and Amount constants to amount.o --- src/Makefile.am | 1 + src/amount.cpp | 31 +++++++++++++++++++++++++++++ src/amount.h | 43 ++++++++++++++++++++++++++++++++++++++++- src/core.cpp | 23 ---------------------- src/core.h | 41 --------------------------------------- src/init.cpp | 1 + src/main.h | 1 + src/miner.cpp | 1 + src/qt/optionsmodel.cpp | 1 + src/rpcmining.cpp | 1 + src/rpcwallet.cpp | 1 + src/txmempool.h | 1 + src/wallet.h | 1 + 13 files changed, 82 insertions(+), 65 deletions(-) create mode 100644 src/amount.cpp diff --git a/src/Makefile.am b/src/Makefile.am index dbefa71fc..d5abbc17d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -208,6 +208,7 @@ univalue_libbitcoin_univalue_a_SOURCES = \ libbitcoin_common_a_CPPFLAGS = $(BITCOIN_INCLUDES) libbitcoin_common_a_SOURCES = \ allocators.cpp \ + amount.cpp \ base58.cpp \ chainparams.cpp \ coins.cpp \ diff --git a/src/amount.cpp b/src/amount.cpp new file mode 100644 index 000000000..e6f5b7d44 --- /dev/null +++ b/src/amount.cpp @@ -0,0 +1,31 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2014 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "amount.h" + +#include "tinyformat.h" + +CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nSize) +{ + if (nSize > 0) + nSatoshisPerK = nFeePaid*1000/nSize; + else + nSatoshisPerK = 0; +} + +CAmount CFeeRate::GetFee(size_t nSize) const +{ + CAmount nFee = nSatoshisPerK*nSize / 1000; + + if (nFee == 0 && nSatoshisPerK > 0) + nFee = nSatoshisPerK; + + return nFee; +} + +std::string CFeeRate::ToString() const +{ + return strprintf("%d.%08d BTC/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN); +} diff --git a/src/amount.h b/src/amount.h index 831fa1f6c..c0d37954c 100644 --- a/src/amount.h +++ b/src/amount.h @@ -6,8 +6,49 @@ #ifndef BITCOIN_AMOUNT_H #define BITCOIN_AMOUNT_H -#include +#include "serialize.h" + +#include +#include typedef int64_t CAmount; +static const CAmount COIN = 100000000; +static const CAmount CENT = 1000000; + +/** No amount larger than this (in satoshi) is valid */ +static const CAmount MAX_MONEY = 21000000 * COIN; +inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } + +/** Type-safe wrapper class to for fee rates + * (how much to pay based on transaction size) + */ +class CFeeRate +{ +private: + CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes +public: + CFeeRate() : nSatoshisPerK(0) { } + explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { } + CFeeRate(const CAmount& nFeePaid, size_t nSize); + CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; } + + CAmount GetFee(size_t size) const; // unit returned is satoshis + CAmount GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes + + friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; } + friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; } + friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; } + friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; } + friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; } + std::string ToString() const; + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(nSatoshisPerK); + } +}; + #endif // BITCOIN_AMOUNT_H diff --git a/src/core.cpp b/src/core.cpp index 73e6de88e..ac180cc50 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -59,29 +59,6 @@ std::string CTxOut::ToString() const return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30)); } -CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nSize) -{ - if (nSize > 0) - nSatoshisPerK = nFeePaid*1000/nSize; - else - nSatoshisPerK = 0; -} - -CAmount CFeeRate::GetFee(size_t nSize) const -{ - CAmount nFee = nSatoshisPerK*nSize / 1000; - - if (nFee == 0 && nSatoshisPerK > 0) - nFee = nSatoshisPerK; - - return nFee; -} - -std::string CFeeRate::ToString() const -{ - return strprintf("%d.%08d BTC/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN); -} - CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {} diff --git a/src/core.h b/src/core.h index a024dad74..f7a46da2c 100644 --- a/src/core.h +++ b/src/core.h @@ -16,13 +16,6 @@ class CTransaction; -static const int64_t COIN = 100000000; -static const int64_t CENT = 1000000; - -/** No amount larger than this (in satoshi) is valid */ -static const CAmount MAX_MONEY = 21000000 * COIN; -inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } - /** An outpoint - a combination of a transaction hash and an index n into its vout */ class COutPoint { @@ -109,40 +102,6 @@ public: std::string ToString() const; }; - - -/** Type-safe wrapper class to for fee rates - * (how much to pay based on transaction size) - */ -class CFeeRate -{ -private: - CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes -public: - CFeeRate() : nSatoshisPerK(0) { } - explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { } - CFeeRate(const CAmount& nFeePaid, size_t nSize); - CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; } - - CAmount GetFee(size_t size) const; // unit returned is satoshis - CAmount GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes - - friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; } - friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; } - friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; } - friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; } - friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; } - std::string ToString() const; - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(nSatoshisPerK); - } -}; - - /** An output of a transaction. It contains the public key that the next input * must be able to sign with to claim it. */ diff --git a/src/init.cpp b/src/init.cpp index 70ac5190d..d928094bb 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -10,6 +10,7 @@ #include "init.h" #include "addrman.h" +#include "amount.h" #include "checkpoints.h" #include "compat/sanity.h" #include "key.h" diff --git a/src/main.h b/src/main.h index 1ef51918c..2686ed948 100644 --- a/src/main.h +++ b/src/main.h @@ -10,6 +10,7 @@ #include "config/bitcoin-config.h" #endif +#include "amount.h" #include "chain.h" #include "chainparams.h" #include "coins.h" diff --git a/src/miner.cpp b/src/miner.cpp index eefccfd64..897f7c56d 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -5,6 +5,7 @@ #include "miner.h" +#include "amount.h" #include "core.h" #include "hash.h" #include "main.h" diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index 6db654dff..c941ebd4c 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -11,6 +11,7 @@ #include "bitcoinunits.h" #include "guiutil.h" +#include "amount.h" #include "init.h" #include "main.h" #include "net.h" diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index c767835a2..8af3c4634 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -3,6 +3,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "amount.h" #include "chainparams.h" #include "core_io.h" #include "init.h" diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 68bb4068b..2728fb4fb 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -3,6 +3,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "amount.h" #include "base58.h" #include "core_io.h" #include "rpcserver.h" diff --git a/src/txmempool.h b/src/txmempool.h index 85cf5310f..df2fbc78c 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -8,6 +8,7 @@ #include +#include "amount.h" #include "coins.h" #include "core.h" #include "sync.h" diff --git a/src/wallet.h b/src/wallet.h index 06706655f..6a4b4b7f8 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_WALLET_H #define BITCOIN_WALLET_H +#include "amount.h" #include "core.h" #include "crypter.h" #include "key.h" From 4a3587d8db9f651866ce70312ac9de11316b42c9 Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 18 Oct 2014 02:34:06 +0200 Subject: [PATCH 2/5] MOVEONLY: Separate CTransaction and dependencies from core --- src/Makefile.am | 2 + src/bitcoin-tx.cpp | 2 +- src/bloom.cpp | 2 +- src/coincontrol.h | 2 +- src/coins.h | 3 +- src/core.cpp | 132 ------------------ src/core.h | 270 +----------------------------------- src/core/transaction.cpp | 142 +++++++++++++++++++ src/core/transaction.h | 276 +++++++++++++++++++++++++++++++++++++ src/core_read.cpp | 2 +- src/core_write.cpp | 2 +- src/main.h | 1 + src/miner.cpp | 1 + src/net.cpp | 2 +- src/qt/bitcoinunits.cpp | 2 +- src/qt/guiutil.cpp | 2 +- src/rpcrawtransaction.cpp | 2 +- src/script/interpreter.cpp | 2 +- src/script/sign.cpp | 2 +- src/test/main_tests.cpp | 2 +- src/test/util_tests.cpp | 2 +- src/txdb.cpp | 1 - src/txmempool.cpp | 1 - src/txmempool.h | 2 +- src/utilmoneystr.cpp | 2 +- src/wallet.h | 1 + 26 files changed, 441 insertions(+), 419 deletions(-) create mode 100644 src/core/transaction.cpp create mode 100644 src/core/transaction.h diff --git a/src/Makefile.am b/src/Makefile.am index d5abbc17d..5485127e6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -81,6 +81,7 @@ BITCOIN_CORE_H = \ coins.h \ compat.h \ core.h \ + core/transaction.h \ core_io.h \ crypter.h \ db.h \ @@ -213,6 +214,7 @@ libbitcoin_common_a_SOURCES = \ chainparams.cpp \ coins.cpp \ core.cpp \ + core/transaction.cpp \ core_read.cpp \ core_write.cpp \ hash.cpp \ diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index da37e60c7..6f3409edf 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "base58.h" -#include "core.h" +#include "core/transaction.h" #include "core_io.h" #include "keystore.h" #include "main.h" // for MAX_BLOCK_SIZE diff --git a/src/bloom.cpp b/src/bloom.cpp index cac71fdbb..c1e7aeb3b 100644 --- a/src/bloom.cpp +++ b/src/bloom.cpp @@ -4,7 +4,7 @@ #include "bloom.h" -#include "core.h" +#include "core/transaction.h" #include "script/script.h" #include "script/standard.h" #include "streams.h" diff --git a/src/coincontrol.h b/src/coincontrol.h index 033092c01..c8f12d92d 100644 --- a/src/coincontrol.h +++ b/src/coincontrol.h @@ -5,7 +5,7 @@ #ifndef COINCONTROL_H #define COINCONTROL_H -#include "core.h" +#include "core/transaction.h" /** Coin Control Features. */ class CCoinControl diff --git a/src/coins.h b/src/coins.h index b8f1e5bcc..eafe3bec6 100644 --- a/src/coins.h +++ b/src/coins.h @@ -6,7 +6,8 @@ #ifndef BITCOIN_COINS_H #define BITCOIN_COINS_H -#include "core.h" +#include "core.h" // Only for CTxOutCompressor +#include "core/transaction.h" #include "serialize.h" #include "uint256.h" diff --git a/src/core.cpp b/src/core.cpp index ac180cc50..34188bca6 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -9,138 +9,6 @@ #include "tinyformat.h" #include "utilstrencodings.h" -std::string COutPoint::ToString() const -{ - return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n); -} - -CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) -{ - prevout = prevoutIn; - scriptSig = scriptSigIn; - nSequence = nSequenceIn; -} - -CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn) -{ - prevout = COutPoint(hashPrevTx, nOut); - scriptSig = scriptSigIn; - nSequence = nSequenceIn; -} - -std::string CTxIn::ToString() const -{ - std::string str; - str += "CTxIn("; - str += prevout.ToString(); - if (prevout.IsNull()) - str += strprintf(", coinbase %s", HexStr(scriptSig)); - else - str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24)); - if (nSequence != std::numeric_limits::max()) - str += strprintf(", nSequence=%u", nSequence); - str += ")"; - return str; -} - -CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn) -{ - nValue = nValueIn; - scriptPubKey = scriptPubKeyIn; -} - -uint256 CTxOut::GetHash() const -{ - return SerializeHash(*this); -} - -std::string CTxOut::ToString() const -{ - return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30)); -} - -CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} -CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {} - -uint256 CMutableTransaction::GetHash() const -{ - return SerializeHash(*this); -} - -void CTransaction::UpdateHash() const -{ - *const_cast(&hash) = SerializeHash(*this); -} - -CTransaction::CTransaction() : hash(0), nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { } - -CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) { - UpdateHash(); -} - -CTransaction& CTransaction::operator=(const CTransaction &tx) { - *const_cast(&nVersion) = tx.nVersion; - *const_cast*>(&vin) = tx.vin; - *const_cast*>(&vout) = tx.vout; - *const_cast(&nLockTime) = tx.nLockTime; - *const_cast(&hash) = tx.hash; - return *this; -} - -CAmount CTransaction::GetValueOut() const -{ - CAmount nValueOut = 0; - for (std::vector::const_iterator it(vout.begin()); it != vout.end(); ++it) - { - nValueOut += it->nValue; - if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut)) - throw std::runtime_error("CTransaction::GetValueOut() : value out of range"); - } - return nValueOut; -} - -double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const -{ - nTxSize = CalculateModifiedSize(nTxSize); - if (nTxSize == 0) return 0.0; - - return dPriorityInputs / nTxSize; -} - -unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const -{ - // In order to avoid disincentivizing cleaning up the UTXO set we don't count - // the constant overhead for each txin and up to 110 bytes of scriptSig (which - // is enough to cover a compressed pubkey p2sh redemption) for priority. - // Providing any more cleanup incentive than making additional inputs free would - // risk encouraging people to create junk outputs to redeem later. - if (nTxSize == 0) - nTxSize = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); - for (std::vector::const_iterator it(vin.begin()); it != vin.end(); ++it) - { - unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size()); - if (nTxSize > offset) - nTxSize -= offset; - } - return nTxSize; -} - -std::string CTransaction::ToString() const -{ - std::string str; - str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n", - GetHash().ToString().substr(0,10), - nVersion, - vin.size(), - vout.size(), - nLockTime); - for (unsigned int i = 0; i < vin.size(); i++) - str += " " + vin[i].ToString() + "\n"; - for (unsigned int i = 0; i < vout.size(); i++) - str += " " + vout[i].ToString() + "\n"; - return str; -} - // Amount compression: // * If the amount is 0, output 0 // * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9) diff --git a/src/core.h b/src/core.h index f7a46da2c..ad4c88325 100644 --- a/src/core.h +++ b/src/core.h @@ -6,279 +6,11 @@ #ifndef BITCOIN_CORE_H #define BITCOIN_CORE_H -#include "amount.h" +#include "core/transaction.h" #include "script/compressor.h" -#include "script/script.h" #include "serialize.h" #include "uint256.h" -#include - -class CTransaction; - -/** An outpoint - a combination of a transaction hash and an index n into its vout */ -class COutPoint -{ -public: - uint256 hash; - uint32_t n; - - COutPoint() { SetNull(); } - COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; } - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(FLATDATA(*this)); - } - - void SetNull() { hash = 0; n = (uint32_t) -1; } - bool IsNull() const { return (hash == 0 && n == (uint32_t) -1); } - - friend bool operator<(const COutPoint& a, const COutPoint& b) - { - return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n)); - } - - friend bool operator==(const COutPoint& a, const COutPoint& b) - { - return (a.hash == b.hash && a.n == b.n); - } - - friend bool operator!=(const COutPoint& a, const COutPoint& b) - { - return !(a == b); - } - - std::string ToString() const; -}; - -/** An input of a transaction. It contains the location of the previous - * transaction's output that it claims and a signature that matches the - * output's public key. - */ -class CTxIn -{ -public: - COutPoint prevout; - CScript scriptSig; - uint32_t nSequence; - - CTxIn() - { - nSequence = std::numeric_limits::max(); - } - - explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits::max()); - CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits::max()); - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(prevout); - READWRITE(scriptSig); - READWRITE(nSequence); - } - - bool IsFinal() const - { - return (nSequence == std::numeric_limits::max()); - } - - friend bool operator==(const CTxIn& a, const CTxIn& b) - { - return (a.prevout == b.prevout && - a.scriptSig == b.scriptSig && - a.nSequence == b.nSequence); - } - - friend bool operator!=(const CTxIn& a, const CTxIn& b) - { - return !(a == b); - } - - std::string ToString() const; -}; - -/** An output of a transaction. It contains the public key that the next input - * must be able to sign with to claim it. - */ -class CTxOut -{ -public: - CAmount nValue; - CScript scriptPubKey; - - CTxOut() - { - SetNull(); - } - - CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn); - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(nValue); - READWRITE(scriptPubKey); - } - - void SetNull() - { - nValue = -1; - scriptPubKey.clear(); - } - - bool IsNull() const - { - return (nValue == -1); - } - - uint256 GetHash() const; - - bool IsDust(CFeeRate minRelayTxFee) const - { - // "Dust" is defined in terms of CTransaction::minRelayTxFee, - // which has units satoshis-per-kilobyte. - // If you'd pay more than 1/3 in fees - // to spend something, then we consider it dust. - // A typical txout is 34 bytes big, and will - // need a CTxIn of at least 148 bytes to spend: - // so dust is a txout less than 546 satoshis - // with default minRelayTxFee. - size_t nSize = GetSerializeSize(SER_DISK,0)+148u; - return (nValue < 3*minRelayTxFee.GetFee(nSize)); - } - - friend bool operator==(const CTxOut& a, const CTxOut& b) - { - return (a.nValue == b.nValue && - a.scriptPubKey == b.scriptPubKey); - } - - friend bool operator!=(const CTxOut& a, const CTxOut& b) - { - return !(a == b); - } - - std::string ToString() const; -}; - - -struct CMutableTransaction; - -/** The basic transaction that is broadcasted on the network and contained in - * blocks. A transaction can contain multiple inputs and outputs. - */ -class CTransaction -{ -private: - /** Memory only. */ - const uint256 hash; - void UpdateHash() const; - -public: - static const int32_t CURRENT_VERSION=1; - - // 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 int32_t nVersion; - const std::vector vin; - const std::vector vout; - const uint32_t nLockTime; - - /** Construct a CTransaction that qualifies as IsNull() */ - CTransaction(); - - /** Convert a CMutableTransaction into a CTransaction. */ - CTransaction(const CMutableTransaction &tx); - - CTransaction& operator=(const CTransaction& tx); - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(*const_cast(&this->nVersion)); - nVersion = this->nVersion; - READWRITE(*const_cast*>(&vin)); - READWRITE(*const_cast*>(&vout)); - READWRITE(*const_cast(&nLockTime)); - if (ser_action.ForRead()) - UpdateHash(); - } - - bool IsNull() const { - return vin.empty() && vout.empty(); - } - - const uint256& GetHash() const { - return hash; - } - - // Return sum of txouts. - CAmount GetValueOut() const; - // GetValueIn() is a method on CCoinsViewCache, because - // inputs must be known to compute value in. - - // Compute priority, given priority of inputs and (optionally) tx size - double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const; - - // Compute modified tx size for priority calculation (optionally given tx size) - unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const; - - bool IsCoinBase() const - { - return (vin.size() == 1 && vin[0].prevout.IsNull()); - } - - friend bool operator==(const CTransaction& a, const CTransaction& b) - { - return a.hash == b.hash; - } - - friend bool operator!=(const CTransaction& a, const CTransaction& b) - { - return a.hash != b.hash; - } - - std::string ToString() const; -}; - -/** A mutable version of CTransaction. */ -struct CMutableTransaction -{ - int32_t nVersion; - std::vector vin; - std::vector vout; - uint32_t nLockTime; - - CMutableTransaction(); - CMutableTransaction(const CTransaction& tx); - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(this->nVersion); - nVersion = this->nVersion; - READWRITE(vin); - READWRITE(vout); - READWRITE(nLockTime); - } - - /** Compute the hash of this CMutableTransaction. This is computed on the - * fly, as opposed to GetHash() in CTransaction, which uses a cached result. - */ - uint256 GetHash() const; -}; - /** wrapper for CTxOut that provides a more compact serialization */ class CTxOutCompressor { diff --git a/src/core/transaction.cpp b/src/core/transaction.cpp new file mode 100644 index 000000000..f835bafb9 --- /dev/null +++ b/src/core/transaction.cpp @@ -0,0 +1,142 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2014 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "core/transaction.h" + +#include "hash.h" +#include "tinyformat.h" +#include "utilstrencodings.h" + +std::string COutPoint::ToString() const +{ + return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n); +} + +CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) +{ + prevout = prevoutIn; + scriptSig = scriptSigIn; + nSequence = nSequenceIn; +} + +CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn) +{ + prevout = COutPoint(hashPrevTx, nOut); + scriptSig = scriptSigIn; + nSequence = nSequenceIn; +} + +std::string CTxIn::ToString() const +{ + std::string str; + str += "CTxIn("; + str += prevout.ToString(); + if (prevout.IsNull()) + str += strprintf(", coinbase %s", HexStr(scriptSig)); + else + str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24)); + if (nSequence != std::numeric_limits::max()) + str += strprintf(", nSequence=%u", nSequence); + str += ")"; + return str; +} + +CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn) +{ + nValue = nValueIn; + scriptPubKey = scriptPubKeyIn; +} + +uint256 CTxOut::GetHash() const +{ + return SerializeHash(*this); +} + +std::string CTxOut::ToString() const +{ + return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30)); +} + +CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} +CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {} + +uint256 CMutableTransaction::GetHash() const +{ + return SerializeHash(*this); +} + +void CTransaction::UpdateHash() const +{ + *const_cast(&hash) = SerializeHash(*this); +} + +CTransaction::CTransaction() : hash(0), nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { } + +CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) { + UpdateHash(); +} + +CTransaction& CTransaction::operator=(const CTransaction &tx) { + *const_cast(&nVersion) = tx.nVersion; + *const_cast*>(&vin) = tx.vin; + *const_cast*>(&vout) = tx.vout; + *const_cast(&nLockTime) = tx.nLockTime; + *const_cast(&hash) = tx.hash; + return *this; +} + +CAmount CTransaction::GetValueOut() const +{ + CAmount nValueOut = 0; + for (std::vector::const_iterator it(vout.begin()); it != vout.end(); ++it) + { + nValueOut += it->nValue; + if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut)) + throw std::runtime_error("CTransaction::GetValueOut() : value out of range"); + } + return nValueOut; +} + +double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const +{ + nTxSize = CalculateModifiedSize(nTxSize); + if (nTxSize == 0) return 0.0; + + return dPriorityInputs / nTxSize; +} + +unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const +{ + // In order to avoid disincentivizing cleaning up the UTXO set we don't count + // the constant overhead for each txin and up to 110 bytes of scriptSig (which + // is enough to cover a compressed pubkey p2sh redemption) for priority. + // Providing any more cleanup incentive than making additional inputs free would + // risk encouraging people to create junk outputs to redeem later. + if (nTxSize == 0) + nTxSize = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); + for (std::vector::const_iterator it(vin.begin()); it != vin.end(); ++it) + { + unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size()); + if (nTxSize > offset) + nTxSize -= offset; + } + return nTxSize; +} + +std::string CTransaction::ToString() const +{ + std::string str; + str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n", + GetHash().ToString().substr(0,10), + nVersion, + vin.size(), + vout.size(), + nLockTime); + for (unsigned int i = 0; i < vin.size(); i++) + str += " " + vin[i].ToString() + "\n"; + for (unsigned int i = 0; i < vout.size(); i++) + str += " " + vout[i].ToString() + "\n"; + return str; +} diff --git a/src/core/transaction.h b/src/core/transaction.h new file mode 100644 index 000000000..c21558cfe --- /dev/null +++ b/src/core/transaction.h @@ -0,0 +1,276 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2014 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef H_BITCOIN_CORE_TRANSACTION +#define H_BITCOIN_CORE_TRANSACTION + +#include "amount.h" +#include "script/script.h" +#include "serialize.h" +#include "uint256.h" + +/** An outpoint - a combination of a transaction hash and an index n into its vout */ +class COutPoint +{ +public: + uint256 hash; + uint32_t n; + + COutPoint() { SetNull(); } + COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; } + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(FLATDATA(*this)); + } + + void SetNull() { hash = 0; n = (uint32_t) -1; } + bool IsNull() const { return (hash == 0 && n == (uint32_t) -1); } + + friend bool operator<(const COutPoint& a, const COutPoint& b) + { + return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n)); + } + + friend bool operator==(const COutPoint& a, const COutPoint& b) + { + return (a.hash == b.hash && a.n == b.n); + } + + friend bool operator!=(const COutPoint& a, const COutPoint& b) + { + return !(a == b); + } + + std::string ToString() const; +}; + +/** An input of a transaction. It contains the location of the previous + * transaction's output that it claims and a signature that matches the + * output's public key. + */ +class CTxIn +{ +public: + COutPoint prevout; + CScript scriptSig; + uint32_t nSequence; + + CTxIn() + { + nSequence = std::numeric_limits::max(); + } + + explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits::max()); + CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits::max()); + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(prevout); + READWRITE(scriptSig); + READWRITE(nSequence); + } + + bool IsFinal() const + { + return (nSequence == std::numeric_limits::max()); + } + + friend bool operator==(const CTxIn& a, const CTxIn& b) + { + return (a.prevout == b.prevout && + a.scriptSig == b.scriptSig && + a.nSequence == b.nSequence); + } + + friend bool operator!=(const CTxIn& a, const CTxIn& b) + { + return !(a == b); + } + + std::string ToString() const; +}; + +/** An output of a transaction. It contains the public key that the next input + * must be able to sign with to claim it. + */ +class CTxOut +{ +public: + CAmount nValue; + CScript scriptPubKey; + + CTxOut() + { + SetNull(); + } + + CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn); + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(nValue); + READWRITE(scriptPubKey); + } + + void SetNull() + { + nValue = -1; + scriptPubKey.clear(); + } + + bool IsNull() const + { + return (nValue == -1); + } + + uint256 GetHash() const; + + bool IsDust(CFeeRate minRelayTxFee) const + { + // "Dust" is defined in terms of CTransaction::minRelayTxFee, + // which has units satoshis-per-kilobyte. + // If you'd pay more than 1/3 in fees + // to spend something, then we consider it dust. + // A typical txout is 34 bytes big, and will + // need a CTxIn of at least 148 bytes to spend: + // so dust is a txout less than 546 satoshis + // with default minRelayTxFee. + size_t nSize = GetSerializeSize(SER_DISK,0)+148u; + return (nValue < 3*minRelayTxFee.GetFee(nSize)); + } + + friend bool operator==(const CTxOut& a, const CTxOut& b) + { + return (a.nValue == b.nValue && + a.scriptPubKey == b.scriptPubKey); + } + + friend bool operator!=(const CTxOut& a, const CTxOut& b) + { + return !(a == b); + } + + std::string ToString() const; +}; + +struct CMutableTransaction; + +/** The basic transaction that is broadcasted on the network and contained in + * blocks. A transaction can contain multiple inputs and outputs. + */ +class CTransaction +{ +private: + /** Memory only. */ + const uint256 hash; + void UpdateHash() const; + +public: + static const int32_t CURRENT_VERSION=1; + + // 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 int32_t nVersion; + const std::vector vin; + const std::vector vout; + const uint32_t nLockTime; + + /** Construct a CTransaction that qualifies as IsNull() */ + CTransaction(); + + /** Convert a CMutableTransaction into a CTransaction. */ + CTransaction(const CMutableTransaction &tx); + + CTransaction& operator=(const CTransaction& tx); + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(*const_cast(&this->nVersion)); + nVersion = this->nVersion; + READWRITE(*const_cast*>(&vin)); + READWRITE(*const_cast*>(&vout)); + READWRITE(*const_cast(&nLockTime)); + if (ser_action.ForRead()) + UpdateHash(); + } + + bool IsNull() const { + return vin.empty() && vout.empty(); + } + + const uint256& GetHash() const { + return hash; + } + + // Return sum of txouts. + CAmount GetValueOut() const; + // GetValueIn() is a method on CCoinsViewCache, because + // inputs must be known to compute value in. + + // Compute priority, given priority of inputs and (optionally) tx size + double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const; + + // Compute modified tx size for priority calculation (optionally given tx size) + unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const; + + bool IsCoinBase() const + { + return (vin.size() == 1 && vin[0].prevout.IsNull()); + } + + friend bool operator==(const CTransaction& a, const CTransaction& b) + { + return a.hash == b.hash; + } + + friend bool operator!=(const CTransaction& a, const CTransaction& b) + { + return a.hash != b.hash; + } + + std::string ToString() const; +}; + +/** A mutable version of CTransaction. */ +struct CMutableTransaction +{ + int32_t nVersion; + std::vector vin; + std::vector vout; + uint32_t nLockTime; + + CMutableTransaction(); + CMutableTransaction(const CTransaction& tx); + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(this->nVersion); + nVersion = this->nVersion; + READWRITE(vin); + READWRITE(vout); + READWRITE(nLockTime); + } + + /** Compute the hash of this CMutableTransaction. This is computed on the + * fly, as opposed to GetHash() in CTransaction, which uses a cached result. + */ + uint256 GetHash() const; +}; + +#endif // H_BITCOIN_CORE_TRANSACTION diff --git a/src/core_read.cpp b/src/core_read.cpp index dcbcf4b4f..d39bc9a78 100644 --- a/src/core_read.cpp +++ b/src/core_read.cpp @@ -4,7 +4,7 @@ #include "core_io.h" -#include "core.h" +#include "core/transaction.h" #include "script/script.h" #include "serialize.h" #include "streams.h" diff --git a/src/core_write.cpp b/src/core_write.cpp index b2b29fb36..a3ae8eec0 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -5,7 +5,7 @@ #include "core_io.h" #include "base58.h" -#include "core.h" +#include "core/transaction.h" #include "script/script.h" #include "script/standard.h" #include "serialize.h" diff --git a/src/main.h b/src/main.h index 2686ed948..3a3687ec1 100644 --- a/src/main.h +++ b/src/main.h @@ -15,6 +15,7 @@ #include "chainparams.h" #include "coins.h" #include "core.h" +#include "core/transaction.h" #include "net.h" #include "pow.h" #include "script/script.h" diff --git a/src/miner.cpp b/src/miner.cpp index 897f7c56d..cf47b65a5 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -7,6 +7,7 @@ #include "amount.h" #include "core.h" +#include "core/transaction.h" #include "hash.h" #include "main.h" #include "net.h" diff --git a/src/net.cpp b/src/net.cpp index 6cf64f51c..6cccdca95 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -11,7 +11,7 @@ #include "addrman.h" #include "chainparams.h" -#include "core.h" +#include "core/transaction.h" #include "ui_interface.h" #ifdef WIN32 diff --git a/src/qt/bitcoinunits.cpp b/src/qt/bitcoinunits.cpp index 423b559bf..c85f569fd 100644 --- a/src/qt/bitcoinunits.cpp +++ b/src/qt/bitcoinunits.cpp @@ -4,7 +4,7 @@ #include "bitcoinunits.h" -#include "core.h" +#include "core/transaction.h" #include diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index 91bb10755..22a1f019e 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -9,7 +9,7 @@ #include "qvalidatedlineedit.h" #include "walletmodel.h" -#include "core.h" +#include "core/transaction.h" #include "init.h" #include "main.h" #include "protocol.h" diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index fdfcb59ee..d3ce3b319 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -4,7 +4,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "base58.h" -#include "core.h" +#include "core/transaction.h" #include "core_io.h" #include "init.h" #include "keystore.h" diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index cd73b8821..44f5df28f 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -5,7 +5,7 @@ #include "interpreter.h" -#include "core.h" +#include "core/transaction.h" #include "crypto/ripemd160.h" #include "crypto/sha1.h" #include "crypto/sha2.h" diff --git a/src/script/sign.cpp b/src/script/sign.cpp index bf98c4039..0eab0626e 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -5,7 +5,7 @@ #include "script/sign.h" -#include "core.h" +#include "core/transaction.h" #include "key.h" #include "keystore.h" #include "script/standard.h" diff --git a/src/test/main_tests.cpp b/src/test/main_tests.cpp index 70a800af5..78c418140 100644 --- a/src/test/main_tests.cpp +++ b/src/test/main_tests.cpp @@ -2,7 +2,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "core.h" +#include "core/transaction.h" #include "main.h" #include diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 61daa0a3f..761210fea 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -4,7 +4,7 @@ #include "util.h" -#include "core.h" +#include "core/transaction.h" #include "random.h" #include "sync.h" #include "utilstrencodings.h" diff --git a/src/txdb.cpp b/src/txdb.cpp index 8a73ce961..0731d843f 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -5,7 +5,6 @@ #include "txdb.h" -#include "core.h" #include "pow.h" #include "uint256.h" diff --git a/src/txmempool.cpp b/src/txmempool.cpp index b0d6b4aef..c042dd846 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -5,7 +5,6 @@ #include "txmempool.h" -#include "core.h" #include "streams.h" #include "util.h" #include "utilmoneystr.h" diff --git a/src/txmempool.h b/src/txmempool.h index df2fbc78c..2ec80cb86 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -10,7 +10,7 @@ #include "amount.h" #include "coins.h" -#include "core.h" +#include "core/transaction.h" #include "sync.h" class CAutoFile; diff --git a/src/utilmoneystr.cpp b/src/utilmoneystr.cpp index 95be06aa1..267a5b845 100644 --- a/src/utilmoneystr.cpp +++ b/src/utilmoneystr.cpp @@ -5,7 +5,7 @@ #include "utilmoneystr.h" -#include "core.h" +#include "core/transaction.h" #include "tinyformat.h" #include "utilstrencodings.h" diff --git a/src/wallet.h b/src/wallet.h index 6a4b4b7f8..4c559cb7b 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -8,6 +8,7 @@ #include "amount.h" #include "core.h" +#include "core/transaction.h" #include "crypter.h" #include "key.h" #include "keystore.h" From 999a2ab41ec96311c145d74822c246a3b92a9b33 Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 18 Oct 2014 19:53:37 +0200 Subject: [PATCH 3/5] MOVEONLY: separate CTxUndo out of core --- src/Makefile.am | 1 + src/coins.h | 1 + src/core.h | 59 ---------------------------------------- src/main.h | 2 +- src/undo.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 60 deletions(-) create mode 100644 src/undo.h diff --git a/src/Makefile.am b/src/Makefile.am index 5485127e6..6d0e1ed7b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -119,6 +119,7 @@ BITCOIN_CORE_H = \ txmempool.h \ ui_interface.h \ uint256.h \ + undo.h \ util.h \ utilstrencodings.h \ utilmoneystr.h \ diff --git a/src/coins.h b/src/coins.h index eafe3bec6..295b3d70d 100644 --- a/src/coins.h +++ b/src/coins.h @@ -10,6 +10,7 @@ #include "core/transaction.h" #include "serialize.h" #include "uint256.h" +#include "undo.h" #include #include diff --git a/src/core.h b/src/core.h index ad4c88325..aeae9a191 100644 --- a/src/core.h +++ b/src/core.h @@ -40,65 +40,6 @@ public: } }; -/** Undo information for a CTxIn - * - * Contains the prevout's CTxOut being spent, and if this was the - * last output of the affected transaction, its metadata as well - * (coinbase or not, height, transaction version) - */ -class CTxInUndo -{ -public: - CTxOut txout; // the txout data before being spent - bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase - unsigned int nHeight; // if the outpoint was the last unspent: its height - int nVersion; // if the outpoint was the last unspent: its version - - CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {} - CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { } - - unsigned int GetSerializeSize(int nType, int nVersion) const { - return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) + - (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) + - ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion); - } - - template - void Serialize(Stream &s, int nType, int nVersion) const { - ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion); - if (nHeight > 0) - ::Serialize(s, VARINT(this->nVersion), nType, nVersion); - ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion); - } - - template - void Unserialize(Stream &s, int nType, int nVersion) { - unsigned int nCode = 0; - ::Unserialize(s, VARINT(nCode), nType, nVersion); - nHeight = nCode / 2; - fCoinBase = nCode & 1; - if (nHeight > 0) - ::Unserialize(s, VARINT(this->nVersion), nType, nVersion); - ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion); - } -}; - -/** Undo information for a CTransaction */ -class CTxUndo -{ -public: - // undo information for all txins - std::vector vprevout; - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(vprevout); - } -}; - - /** Nodes collect new transactions into a block, hash them into a hash tree, * and scan through nonce values to make the block's hash satisfy proof-of-work * requirements. When they solve the proof-of-work, they broadcast the block diff --git a/src/main.h b/src/main.h index 3a3687ec1..09f22471d 100644 --- a/src/main.h +++ b/src/main.h @@ -25,6 +25,7 @@ #include "tinyformat.h" #include "txmempool.h" #include "uint256.h" +#include "undo.h" #include #include @@ -129,7 +130,6 @@ static const uint64_t nMinDiskSpace = 52428800; class CBlockTreeDB; -class CTxUndo; class CScriptCheck; class CValidationState; class CValidationInterface; diff --git a/src/undo.h b/src/undo.h new file mode 100644 index 000000000..78e37f628 --- /dev/null +++ b/src/undo.h @@ -0,0 +1,71 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2013 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef H_BITCOIN_TXUNDO +#define H_BITCOIN_TXUNDO + +#include "core.h" // Only for CTxOutCompressor +#include "core/transaction.h" +#include "serialize.h" + +/** Undo information for a CTxIn + * + * Contains the prevout's CTxOut being spent, and if this was the + * last output of the affected transaction, its metadata as well + * (coinbase or not, height, transaction version) + */ +class CTxInUndo +{ +public: + CTxOut txout; // the txout data before being spent + bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase + unsigned int nHeight; // if the outpoint was the last unspent: its height + int nVersion; // if the outpoint was the last unspent: its version + + CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {} + CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { } + + unsigned int GetSerializeSize(int nType, int nVersion) const { + return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) + + (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) + + ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion); + } + + template + void Serialize(Stream &s, int nType, int nVersion) const { + ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion); + if (nHeight > 0) + ::Serialize(s, VARINT(this->nVersion), nType, nVersion); + ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion); + } + + template + void Unserialize(Stream &s, int nType, int nVersion) { + unsigned int nCode = 0; + ::Unserialize(s, VARINT(nCode), nType, nVersion); + nHeight = nCode / 2; + fCoinBase = nCode & 1; + if (nHeight > 0) + ::Unserialize(s, VARINT(this->nVersion), nType, nVersion); + ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion); + } +}; + +/** Undo information for a CTransaction */ +class CTxUndo +{ +public: + // undo information for all txins + std::vector vprevout; + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(vprevout); + } +}; + +#endif // H_BITCOIN_TXUNDO From 561e9e9de9793c187f29ab2d41b43a36447e9357 Mon Sep 17 00:00:00 2001 From: jtimon Date: Sun, 19 Oct 2014 04:28:43 +0200 Subject: [PATCH 4/5] MOVEONLY: Move script/compressor out of script and put CTxOutCompressor (from core) with it --- src/Makefile.am | 4 +-- src/coins.h | 3 +- src/{script => }/compressor.cpp | 55 +++++++++++++++++++++++++++++++++ src/{script => }/compressor.h | 36 +++++++++++++++++++-- src/core.cpp | 54 -------------------------------- src/core.h | 30 ------------------ src/test/compress_tests.cpp | 2 +- src/undo.h | 2 +- 8 files changed, 93 insertions(+), 93 deletions(-) rename src/{script => }/compressor.cpp (73%) rename src/{script => }/compressor.h (76%) diff --git a/src/Makefile.am b/src/Makefile.am index 6d0e1ed7b..0385ad902 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -80,6 +80,7 @@ BITCOIN_CORE_H = \ coincontrol.h \ coins.h \ compat.h \ + compressor.h \ core.h \ core/transaction.h \ core_io.h \ @@ -103,7 +104,6 @@ BITCOIN_CORE_H = \ rpcclient.h \ rpcprotocol.h \ rpcserver.h \ - script/compressor.h \ script/interpreter.h \ script/script.h \ script/sigcache.h \ @@ -214,6 +214,7 @@ libbitcoin_common_a_SOURCES = \ base58.cpp \ chainparams.cpp \ coins.cpp \ + compressor.cpp \ core.cpp \ core/transaction.cpp \ core_read.cpp \ @@ -223,7 +224,6 @@ libbitcoin_common_a_SOURCES = \ keystore.cpp \ netbase.cpp \ protocol.cpp \ - script/compressor.cpp \ script/interpreter.cpp \ script/script.cpp \ script/sigcache.cpp \ diff --git a/src/coins.h b/src/coins.h index 295b3d70d..ee9051562 100644 --- a/src/coins.h +++ b/src/coins.h @@ -6,8 +6,7 @@ #ifndef BITCOIN_COINS_H #define BITCOIN_COINS_H -#include "core.h" // Only for CTxOutCompressor -#include "core/transaction.h" +#include "compressor.h" #include "serialize.h" #include "uint256.h" #include "undo.h" diff --git a/src/script/compressor.cpp b/src/compressor.cpp similarity index 73% rename from src/script/compressor.cpp rename to src/compressor.cpp index af1acf48d..806175dd3 100644 --- a/src/script/compressor.cpp +++ b/src/compressor.cpp @@ -5,6 +5,7 @@ #include "compressor.h" +#include "hash.h" #include "key.h" #include "script/standard.h" @@ -128,3 +129,57 @@ bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector= 1 && d <= 9); + n /= 10; + return 1 + (n*9 + d - 1)*10 + e; + } else { + return 1 + (n - 1)*10 + 9; + } +} + +uint64_t CTxOutCompressor::DecompressAmount(uint64_t x) +{ + // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9 + if (x == 0) + return 0; + x--; + // x = 10*(9*n + d - 1) + e + int e = x % 10; + x /= 10; + uint64_t n = 0; + if (e < 9) { + // x = 9*n + d - 1 + int d = (x % 9) + 1; + x /= 9; + // x = n + n = x*10 + d; + } else { + n = x+1; + } + while (e) { + n *= 10; + e--; + } + return n; +} diff --git a/src/script/compressor.h b/src/compressor.h similarity index 76% rename from src/script/compressor.h rename to src/compressor.h index 154e0b266..a612c3a88 100644 --- a/src/script/compressor.h +++ b/src/compressor.h @@ -3,9 +3,10 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#ifndef H_BITCOIN_SCRIPT_COMPRESSOR -#define H_BITCOIN_SCRIPT_COMPRESSOR +#ifndef H_BITCOIN_COMPRESSOR +#define H_BITCOIN_COMPRESSOR +#include "core/transaction.h" #include "script/script.h" #include "serialize.h" @@ -86,4 +87,33 @@ public: } }; -#endif // H_BITCOIN_SCRIPT_COMPRESSOR +/** wrapper for CTxOut that provides a more compact serialization */ +class CTxOutCompressor +{ +private: + CTxOut &txout; + +public: + static uint64_t CompressAmount(uint64_t nAmount); + static uint64_t DecompressAmount(uint64_t nAmount); + + CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { } + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + if (!ser_action.ForRead()) { + uint64_t nVal = CompressAmount(txout.nValue); + READWRITE(VARINT(nVal)); + } else { + uint64_t nVal = 0; + READWRITE(VARINT(nVal)); + txout.nValue = DecompressAmount(nVal); + } + CScriptCompressor cscript(REF(txout.scriptPubKey)); + READWRITE(cscript); + } +}; + +#endif // H_BITCOIN_COMPRESSOR diff --git a/src/core.cpp b/src/core.cpp index 34188bca6..08139d542 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -9,60 +9,6 @@ #include "tinyformat.h" #include "utilstrencodings.h" -// Amount compression: -// * If the amount is 0, output 0 -// * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9) -// * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10) -// * call the result n -// * output 1 + 10*(9*n + d - 1) + e -// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9 -// (this is decodable, as d is in [1-9] and e is in [0-9]) - -uint64_t CTxOutCompressor::CompressAmount(uint64_t n) -{ - if (n == 0) - return 0; - int e = 0; - while (((n % 10) == 0) && e < 9) { - n /= 10; - e++; - } - if (e < 9) { - int d = (n % 10); - assert(d >= 1 && d <= 9); - n /= 10; - return 1 + (n*9 + d - 1)*10 + e; - } else { - return 1 + (n - 1)*10 + 9; - } -} - -uint64_t CTxOutCompressor::DecompressAmount(uint64_t x) -{ - // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9 - if (x == 0) - return 0; - x--; - // x = 10*(9*n + d - 1) + e - int e = x % 10; - x /= 10; - uint64_t n = 0; - if (e < 9) { - // x = 9*n + d - 1 - int d = (x % 9) + 1; - x /= 9; - // x = n - n = x*10 + d; - } else { - n = x+1; - } - while (e) { - n *= 10; - e--; - } - return n; -} - uint256 CBlockHeader::GetHash() const { return Hash(BEGIN(nVersion), END(nNonce)); diff --git a/src/core.h b/src/core.h index aeae9a191..b2288e24c 100644 --- a/src/core.h +++ b/src/core.h @@ -7,39 +7,9 @@ #define BITCOIN_CORE_H #include "core/transaction.h" -#include "script/compressor.h" #include "serialize.h" #include "uint256.h" -/** wrapper for CTxOut that provides a more compact serialization */ -class CTxOutCompressor -{ -private: - CTxOut &txout; - -public: - static uint64_t CompressAmount(uint64_t nAmount); - static uint64_t DecompressAmount(uint64_t nAmount); - - CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { } - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - if (!ser_action.ForRead()) { - uint64_t nVal = CompressAmount(txout.nValue); - READWRITE(VARINT(nVal)); - } else { - uint64_t nVal = 0; - READWRITE(VARINT(nVal)); - txout.nValue = DecompressAmount(nVal); - } - CScriptCompressor cscript(REF(txout.scriptPubKey)); - READWRITE(cscript); - } -}; - /** Nodes collect new transactions into a block, hash them into a hash tree, * and scan through nonce values to make the block's hash satisfy proof-of-work * requirements. When they solve the proof-of-work, they broadcast the block diff --git a/src/test/compress_tests.cpp b/src/test/compress_tests.cpp index 719955ba8..bf404cf0c 100644 --- a/src/test/compress_tests.cpp +++ b/src/test/compress_tests.cpp @@ -2,7 +2,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "main.h" +#include "compressor.h" #include "util.h" #include diff --git a/src/undo.h b/src/undo.h index 78e37f628..232c19342 100644 --- a/src/undo.h +++ b/src/undo.h @@ -6,7 +6,7 @@ #ifndef H_BITCOIN_TXUNDO #define H_BITCOIN_TXUNDO -#include "core.h" // Only for CTxOutCompressor +#include "compressor.h" #include "core/transaction.h" #include "serialize.h" From 99f41b9cf7b8e039cea75500a905498a1f6969f3 Mon Sep 17 00:00:00 2001 From: jtimon Date: Tue, 21 Oct 2014 00:13:47 +0200 Subject: [PATCH 5/5] MOVEONLY: core.o -> core/block.o --- src/Makefile.am | 4 ++-- src/chain.h | 2 +- src/chainparams.h | 2 +- src/{core.cpp => core/block.cpp} | 2 +- src/{core.h => core/block.h} | 6 +++--- src/main.h | 2 +- src/miner.cpp | 2 +- src/pow.cpp | 2 +- src/wallet.h | 2 +- 9 files changed, 12 insertions(+), 12 deletions(-) rename src/{core.cpp => core/block.cpp} (99%) rename src/{core.h => core/block.h} (97%) diff --git a/src/Makefile.am b/src/Makefile.am index 0385ad902..4ca75d6a7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -81,7 +81,7 @@ BITCOIN_CORE_H = \ coins.h \ compat.h \ compressor.h \ - core.h \ + core/block.h \ core/transaction.h \ core_io.h \ crypter.h \ @@ -215,7 +215,7 @@ libbitcoin_common_a_SOURCES = \ chainparams.cpp \ coins.cpp \ compressor.cpp \ - core.cpp \ + core/block.cpp \ core/transaction.cpp \ core_read.cpp \ core_write.cpp \ diff --git a/src/chain.h b/src/chain.h index 290150476..2a5577162 100644 --- a/src/chain.h +++ b/src/chain.h @@ -6,7 +6,7 @@ #ifndef H_BITCOIN_CHAIN #define H_BITCOIN_CHAIN -#include "core.h" +#include "core/block.h" #include "pow.h" #include "tinyformat.h" #include "uint256.h" diff --git a/src/chainparams.h b/src/chainparams.h index f157419bb..e27728dbd 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -6,9 +6,9 @@ #ifndef BITCOIN_CHAIN_PARAMS_H #define BITCOIN_CHAIN_PARAMS_H -#include "core.h" #include "chainparamsbase.h" #include "checkpoints.h" +#include "core/block.h" #include "protocol.h" #include "uint256.h" diff --git a/src/core.cpp b/src/core/block.cpp similarity index 99% rename from src/core.cpp rename to src/core/block.cpp index 08139d542..2010d44da 100644 --- a/src/core.cpp +++ b/src/core/block.cpp @@ -3,7 +3,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "core.h" +#include "core/block.h" #include "hash.h" #include "tinyformat.h" diff --git a/src/core.h b/src/core/block.h similarity index 97% rename from src/core.h rename to src/core/block.h index b2288e24c..f1eb7a844 100644 --- a/src/core.h +++ b/src/core/block.h @@ -3,8 +3,8 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_CORE_H -#define BITCOIN_CORE_H +#ifndef H_BITCOIN_CORE_BLOCK +#define H_BITCOIN_CORE_BLOCK #include "core/transaction.h" #include "serialize.h" @@ -165,4 +165,4 @@ struct CBlockLocator } }; -#endif // BITCOIN_CORE_H +#endif // H_BITCOIN_CORE_BLOCK diff --git a/src/main.h b/src/main.h index 09f22471d..1941ca705 100644 --- a/src/main.h +++ b/src/main.h @@ -14,7 +14,7 @@ #include "chain.h" #include "chainparams.h" #include "coins.h" -#include "core.h" +#include "core/block.h" #include "core/transaction.h" #include "net.h" #include "pow.h" diff --git a/src/miner.cpp b/src/miner.cpp index cf47b65a5..0235de3ab 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -6,7 +6,7 @@ #include "miner.h" #include "amount.h" -#include "core.h" +#include "core/block.h" #include "core/transaction.h" #include "hash.h" #include "main.h" diff --git a/src/pow.cpp b/src/pow.cpp index 75fbfc6a6..af7fc488e 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -6,7 +6,7 @@ #include "pow.h" #include "chainparams.h" -#include "core.h" +#include "core/block.h" #include "main.h" #include "timedata.h" #include "uint256.h" diff --git a/src/wallet.h b/src/wallet.h index 4c559cb7b..acb30970c 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -7,7 +7,7 @@ #define BITCOIN_WALLET_H #include "amount.h" -#include "core.h" +#include "core/block.h" #include "core/transaction.h" #include "crypter.h" #include "key.h"