r1
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||
// Copyright (c) 2015-2019 The PIVX developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "primitives/block.h"
|
||||
|
||||
#include "hash.h"
|
||||
#include "script/standard.h"
|
||||
#include "script/sign.h"
|
||||
#include "tinyformat.h"
|
||||
#include "utilstrencodings.h"
|
||||
#include "util.h"
|
||||
|
||||
uint256 CBlockHeader::GetHash() const
|
||||
{
|
||||
if(nVersion < 4)
|
||||
return HashQuark(BEGIN(nVersion), END(nNonce));
|
||||
|
||||
return Hash(BEGIN(nVersion), END(nAccumulatorCheckpoint));
|
||||
}
|
||||
|
||||
uint256 CBlock::BuildMerkleTree(bool* fMutated) const
|
||||
{
|
||||
/* WARNING! If you're reading this because you're learning about crypto
|
||||
and/or designing a new system that will use merkle trees, keep in mind
|
||||
that the following merkle tree algorithm has a serious flaw related to
|
||||
duplicate txids, resulting in a vulnerability (CVE-2012-2459).
|
||||
|
||||
The reason is that if the number of hashes in the list at a given time
|
||||
is odd, the last one is duplicated before computing the next level (which
|
||||
is unusual in Merkle trees). This results in certain sequences of
|
||||
transactions leading to the same merkle root. For example, these two
|
||||
trees:
|
||||
|
||||
A A
|
||||
/ \ / \
|
||||
B C B C
|
||||
/ \ | / \ / \
|
||||
D E F D E F F
|
||||
/ \ / \ / \ / \ / \ / \ / \
|
||||
1 2 3 4 5 6 1 2 3 4 5 6 5 6
|
||||
|
||||
for transaction lists [1,2,3,4,5,6] and [1,2,3,4,5,6,5,6] (where 5 and
|
||||
6 are repeated) result in the same root hash A (because the hash of both
|
||||
of (F) and (F,F) is C).
|
||||
|
||||
The vulnerability results from being able to send a block with such a
|
||||
transaction list, with the same merkle root, and the same block hash as
|
||||
the original without duplication, resulting in failed validation. If the
|
||||
receiving node proceeds to mark that block as permanently invalid
|
||||
however, it will fail to accept further unmodified (and thus potentially
|
||||
valid) versions of the same block. We defend against this by detecting
|
||||
the case where we would hash two identical hashes at the end of the list
|
||||
together, and treating that identically to the block having an invalid
|
||||
merkle root. Assuming no double-SHA256 collisions, this will detect all
|
||||
known ways of changing the transactions without affecting the merkle
|
||||
root.
|
||||
*/
|
||||
vMerkleTree.clear();
|
||||
vMerkleTree.reserve(vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
|
||||
for (std::vector<CTransaction>::const_iterator it(vtx.begin()); it != vtx.end(); ++it)
|
||||
vMerkleTree.push_back(it->GetHash());
|
||||
int j = 0;
|
||||
bool mutated = false;
|
||||
for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
|
||||
{
|
||||
for (int i = 0; i < nSize; i += 2)
|
||||
{
|
||||
int i2 = std::min(i+1, nSize-1);
|
||||
if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
|
||||
// Two identical hashes at the end of the list at a particular level.
|
||||
mutated = true;
|
||||
}
|
||||
vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
|
||||
BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
|
||||
}
|
||||
j += nSize;
|
||||
}
|
||||
if (fMutated) {
|
||||
*fMutated = mutated;
|
||||
}
|
||||
return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
|
||||
}
|
||||
|
||||
std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
|
||||
{
|
||||
if (vMerkleTree.empty())
|
||||
BuildMerkleTree();
|
||||
std::vector<uint256> vMerkleBranch;
|
||||
int j = 0;
|
||||
for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
|
||||
{
|
||||
int i = std::min(nIndex^1, nSize-1);
|
||||
vMerkleBranch.push_back(vMerkleTree[j+i]);
|
||||
nIndex >>= 1;
|
||||
j += nSize;
|
||||
}
|
||||
return vMerkleBranch;
|
||||
}
|
||||
|
||||
uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
|
||||
{
|
||||
if (nIndex == -1)
|
||||
return uint256();
|
||||
for (std::vector<uint256>::const_iterator it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
|
||||
{
|
||||
if (nIndex & 1)
|
||||
hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
|
||||
else
|
||||
hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
|
||||
nIndex >>= 1;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
std::string CBlock::ToString() const
|
||||
{
|
||||
std::stringstream s;
|
||||
s << strprintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
|
||||
GetHash().ToString(),
|
||||
nVersion,
|
||||
hashPrevBlock.ToString(),
|
||||
hashMerkleRoot.ToString(),
|
||||
nTime, nBits, nNonce,
|
||||
vtx.size());
|
||||
for (unsigned int i = 0; i < vtx.size(); i++)
|
||||
{
|
||||
s << " " << vtx[i].ToString() << "\n";
|
||||
}
|
||||
s << " vMerkleTree: ";
|
||||
for (unsigned int i = 0; i < vMerkleTree.size(); i++)
|
||||
s << " " << vMerkleTree[i].ToString();
|
||||
s << "\n";
|
||||
return s.str();
|
||||
}
|
||||
|
||||
void CBlock::print() const
|
||||
{
|
||||
LogPrintf("%s", ToString());
|
||||
}
|
||||
|
||||
bool CBlock::IsZerocoinStake() const
|
||||
{
|
||||
return IsProofOfStake() && vtx[1].HasZerocoinSpendInputs();
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2013 The Bitcoin developers
|
||||
// Copyright (c) 2015-2018 The PIVX developers
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_PRIMITIVES_BLOCK_H
|
||||
#define BITCOIN_PRIMITIVES_BLOCK_H
|
||||
|
||||
#include "primitives/transaction.h"
|
||||
#include "keystore.h"
|
||||
#include "serialize.h"
|
||||
#include "uint256.h"
|
||||
|
||||
/** The maximum allowed size for a serialized block, in bytes (network rule) */
|
||||
static const unsigned int MAX_BLOCK_SIZE_CURRENT = 2000000;
|
||||
static const unsigned int MAX_BLOCK_SIZE_LEGACY = 1000000;
|
||||
|
||||
/** 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
|
||||
* to everyone and the block is added to the block chain. The first transaction
|
||||
* in the block is a special one that creates a new coin owned by the creator
|
||||
* of the block.
|
||||
*/
|
||||
class CBlockHeader
|
||||
{
|
||||
public:
|
||||
// header
|
||||
static const int32_t CURRENT_VERSION=5; // Version 5 supports CLTV activation
|
||||
int32_t nVersion;
|
||||
uint256 hashPrevBlock;
|
||||
uint256 hashMerkleRoot;
|
||||
uint32_t nTime;
|
||||
uint32_t nBits;
|
||||
uint32_t nNonce;
|
||||
uint256 nAccumulatorCheckpoint;
|
||||
|
||||
CBlockHeader()
|
||||
{
|
||||
SetNull();
|
||||
}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||||
READWRITE(this->nVersion);
|
||||
nVersion = this->nVersion;
|
||||
READWRITE(hashPrevBlock);
|
||||
READWRITE(hashMerkleRoot);
|
||||
READWRITE(nTime);
|
||||
READWRITE(nBits);
|
||||
READWRITE(nNonce);
|
||||
|
||||
//zerocoin active, header changes to include accumulator checksum
|
||||
if(nVersion > 3)
|
||||
READWRITE(nAccumulatorCheckpoint);
|
||||
}
|
||||
|
||||
void SetNull()
|
||||
{
|
||||
nVersion = CBlockHeader::CURRENT_VERSION;
|
||||
hashPrevBlock.SetNull();
|
||||
hashMerkleRoot.SetNull();
|
||||
nTime = 0;
|
||||
nBits = 0;
|
||||
nNonce = 0;
|
||||
nAccumulatorCheckpoint = 0;
|
||||
}
|
||||
|
||||
bool IsNull() const
|
||||
{
|
||||
return (nBits == 0);
|
||||
}
|
||||
|
||||
uint256 GetHash() const;
|
||||
|
||||
int64_t GetBlockTime() const
|
||||
{
|
||||
return (int64_t)nTime;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class CBlock : public CBlockHeader
|
||||
{
|
||||
public:
|
||||
// network and disk
|
||||
std::vector<CTransaction> vtx;
|
||||
|
||||
// ppcoin: block signature - signed by one of the coin base txout[N]'s owner
|
||||
std::vector<unsigned char> vchBlockSig;
|
||||
|
||||
// memory only
|
||||
mutable CScript payee;
|
||||
mutable std::vector<uint256> vMerkleTree;
|
||||
|
||||
CBlock()
|
||||
{
|
||||
SetNull();
|
||||
}
|
||||
|
||||
CBlock(const CBlockHeader &header)
|
||||
{
|
||||
SetNull();
|
||||
*((CBlockHeader*)this) = header;
|
||||
}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||||
READWRITE(*(CBlockHeader*)this);
|
||||
READWRITE(vtx);
|
||||
if(vtx.size() > 1 && vtx[1].IsCoinStake())
|
||||
READWRITE(vchBlockSig);
|
||||
}
|
||||
|
||||
void SetNull()
|
||||
{
|
||||
CBlockHeader::SetNull();
|
||||
vtx.clear();
|
||||
vMerkleTree.clear();
|
||||
payee = CScript();
|
||||
vchBlockSig.clear();
|
||||
}
|
||||
|
||||
CBlockHeader GetBlockHeader() const
|
||||
{
|
||||
CBlockHeader block;
|
||||
block.nVersion = nVersion;
|
||||
block.hashPrevBlock = hashPrevBlock;
|
||||
block.hashMerkleRoot = hashMerkleRoot;
|
||||
block.nTime = nTime;
|
||||
block.nBits = nBits;
|
||||
block.nNonce = nNonce;
|
||||
block.nAccumulatorCheckpoint = nAccumulatorCheckpoint;
|
||||
return block;
|
||||
}
|
||||
|
||||
// ppcoin: two types of block: proof-of-work or proof-of-stake
|
||||
bool IsProofOfStake() const
|
||||
{
|
||||
return (vtx.size() > 1 && vtx[1].IsCoinStake());
|
||||
}
|
||||
|
||||
bool IsProofOfWork() const
|
||||
{
|
||||
return !IsProofOfStake();
|
||||
}
|
||||
|
||||
bool IsZerocoinStake() const;
|
||||
|
||||
std::pair<COutPoint, unsigned int> GetProofOfStake() const
|
||||
{
|
||||
return IsProofOfStake()? std::make_pair(vtx[1].vin[0].prevout, nTime) : std::make_pair(COutPoint(), (unsigned int)0);
|
||||
}
|
||||
|
||||
// Build the in-memory merkle tree for this block and return the merkle root.
|
||||
// If non-NULL, *mutated is set to whether mutation was detected in the merkle
|
||||
// tree (a duplication of transactions in the block leading to an identical
|
||||
// merkle root).
|
||||
uint256 BuildMerkleTree(bool* mutated = NULL) const;
|
||||
|
||||
std::vector<uint256> GetMerkleBranch(int nIndex) const;
|
||||
static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
|
||||
std::string ToString() const;
|
||||
void print() const;
|
||||
};
|
||||
|
||||
|
||||
/** Describes a place in the block chain to another node such that if the
|
||||
* other node doesn't have the same branch, it can find a recent common trunk.
|
||||
* The further back it is, the further before the fork it may be.
|
||||
*/
|
||||
struct CBlockLocator
|
||||
{
|
||||
std::vector<uint256> vHave;
|
||||
|
||||
CBlockLocator() {}
|
||||
|
||||
CBlockLocator(const std::vector<uint256>& vHaveIn)
|
||||
{
|
||||
vHave = vHaveIn;
|
||||
}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||||
if (!(nType & SER_GETHASH))
|
||||
READWRITE(nVersion);
|
||||
READWRITE(vHave);
|
||||
}
|
||||
|
||||
void SetNull()
|
||||
{
|
||||
vHave.clear();
|
||||
}
|
||||
|
||||
bool IsNull()
|
||||
{
|
||||
return vHave.empty();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BITCOIN_PRIMITIVES_BLOCK_H
|
||||
@@ -0,0 +1,321 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||
// Copyright (c) 2015-2019 The PIVX developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "primitives/block.h"
|
||||
#include "primitives/transaction.h"
|
||||
|
||||
#include "chain.h"
|
||||
#include "hash.h"
|
||||
#include "main.h"
|
||||
#include "tinyformat.h"
|
||||
#include "utilstrencodings.h"
|
||||
#include "transaction.h"
|
||||
|
||||
|
||||
extern bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow);
|
||||
|
||||
std::string COutPoint::ToString() const
|
||||
{
|
||||
return strprintf("COutPoint(%s, %u)", hash.ToString()/*.substr(0,10)*/, n);
|
||||
}
|
||||
|
||||
std::string COutPoint::ToStringShort() const
|
||||
{
|
||||
return strprintf("%s-%u", hash.ToString().substr(0,64), n);
|
||||
}
|
||||
|
||||
uint256 COutPoint::GetHash()
|
||||
{
|
||||
return Hash(BEGIN(hash), END(hash), BEGIN(n), END(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;
|
||||
}
|
||||
|
||||
CTxIn::CTxIn(const libzerocoin::CoinSpend& spend, libzerocoin::CoinDenomination denom)
|
||||
{
|
||||
//Serialize the coinspend object and append it to a CScript
|
||||
CDataStream serializedCoinSpend(SER_NETWORK, PROTOCOL_VERSION);
|
||||
serializedCoinSpend << spend;
|
||||
std::vector<unsigned char> data(serializedCoinSpend.begin(), serializedCoinSpend.end());
|
||||
|
||||
scriptSig = CScript() << OP_ZEROCOINSPEND << data.size();
|
||||
scriptSig.insert(scriptSig.end(), data.begin(), data.end());
|
||||
prevout.SetNull();
|
||||
nSequence = denom;
|
||||
}
|
||||
|
||||
bool CTxIn::IsZerocoinSpend() const
|
||||
{
|
||||
return prevout.hash == 0 && scriptSig.IsZerocoinSpend();
|
||||
}
|
||||
|
||||
bool CTxIn::IsZerocoinPublicSpend() const
|
||||
{
|
||||
return scriptSig.IsZerocoinPublicSpend();
|
||||
}
|
||||
|
||||
std::string CTxIn::ToString() const
|
||||
{
|
||||
std::string str;
|
||||
str += "CTxIn(";
|
||||
str += prevout.ToString();
|
||||
if (prevout.IsNull())
|
||||
if(IsZerocoinSpend())
|
||||
str += strprintf(", zerocoinspend %s...", HexStr(scriptSig).substr(0, 25));
|
||||
else
|
||||
str += strprintf(", coinbase %s", HexStr(scriptSig));
|
||||
else
|
||||
str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24));
|
||||
if (nSequence != std::numeric_limits<unsigned int>::max())
|
||||
str += strprintf(", nSequence=%u", nSequence);
|
||||
str += ")";
|
||||
return str;
|
||||
}
|
||||
|
||||
CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
|
||||
{
|
||||
nValue = nValueIn;
|
||||
scriptPubKey = scriptPubKeyIn;
|
||||
nRounds = -10;
|
||||
}
|
||||
|
||||
bool COutPoint::IsMasternodeReward(const CTransaction* tx) const
|
||||
{
|
||||
if(!tx->IsCoinStake())
|
||||
return false;
|
||||
|
||||
return (n == tx->vout.size() - 1) && (tx->vout[1].scriptPubKey != tx->vout[n].scriptPubKey);
|
||||
}
|
||||
|
||||
uint256 CTxOut::GetHash() const
|
||||
{
|
||||
return SerializeHash(*this);
|
||||
}
|
||||
|
||||
bool CTxOut::IsZerocoinMint() const
|
||||
{
|
||||
return scriptPubKey.IsZerocoinMint();
|
||||
}
|
||||
|
||||
CAmount CTxOut::GetZerocoinMinted() const
|
||||
{
|
||||
if (!IsZerocoinMint())
|
||||
return CAmount(0);
|
||||
|
||||
return nValue;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
std::string CMutableTransaction::ToString() const
|
||||
{
|
||||
std::string str;
|
||||
str += strprintf("CMutableTransaction(ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
|
||||
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;
|
||||
}
|
||||
|
||||
void CTransaction::UpdateHash() const
|
||||
{
|
||||
*const_cast<uint256*>(&hash) = SerializeHash(*this);
|
||||
}
|
||||
|
||||
CTransaction::CTransaction() : hash(), 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<int*>(&nVersion) = tx.nVersion;
|
||||
*const_cast<std::vector<CTxIn>*>(&vin) = tx.vin;
|
||||
*const_cast<std::vector<CTxOut>*>(&vout) = tx.vout;
|
||||
*const_cast<unsigned int*>(&nLockTime) = tx.nLockTime;
|
||||
*const_cast<uint256*>(&hash) = tx.hash;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool CTransaction::HasZerocoinSpendInputs() const
|
||||
{
|
||||
for (const CTxIn& txin: vin) {
|
||||
if (txin.IsZerocoinSpend() || txin.IsZerocoinPublicSpend())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CTransaction::HasZerocoinMintOutputs() const
|
||||
{
|
||||
for(const CTxOut& txout : vout) {
|
||||
if (txout.IsZerocoinMint())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CTransaction::HasZerocoinPublicSpendInputs() const
|
||||
{
|
||||
// The wallet only allows publicSpend inputs in the same tx and not a combination between agr and zagr
|
||||
for(const CTxIn& txin : vin) {
|
||||
if (txin.IsZerocoinPublicSpend())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CTransaction::IsCoinStake() const
|
||||
{
|
||||
if (vin.empty())
|
||||
return false;
|
||||
|
||||
// ppcoin: the coin stake transaction is marked with the first output empty
|
||||
bool fAllowNull = vin[0].IsZerocoinSpend();
|
||||
if (vin[0].prevout.IsNull() && !fAllowNull)
|
||||
return false;
|
||||
|
||||
return (vout.size() >= 2 && vout[0].IsEmpty());
|
||||
}
|
||||
|
||||
CAmount CTransaction::GetValueOut() const
|
||||
{
|
||||
CAmount nValueOut = 0;
|
||||
for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); ++it)
|
||||
{
|
||||
// Agrarian: previously MoneyRange() was called here. This has been replaced with negative check and boundary wrap check.
|
||||
if (it->nValue < 0)
|
||||
throw std::runtime_error("CTransaction::GetValueOut() : value out of range : less than 0");
|
||||
|
||||
if ((nValueOut + it->nValue) < nValueOut)
|
||||
throw std::runtime_error("CTransaction::GetValueOut() : value out of range : wraps the int64_t boundary");
|
||||
|
||||
nValueOut += it->nValue;
|
||||
}
|
||||
return nValueOut;
|
||||
}
|
||||
|
||||
CAmount CTransaction::GetZerocoinMinted() const
|
||||
{
|
||||
CAmount nValueOut = 0;
|
||||
for (const CTxOut& txOut : vout) {
|
||||
nValueOut += txOut.GetZerocoinMinted();
|
||||
}
|
||||
|
||||
return nValueOut;
|
||||
}
|
||||
|
||||
bool CTransaction::UsesUTXO(const COutPoint out)
|
||||
{
|
||||
for (const CTxIn& in : vin) {
|
||||
if (in.prevout == out)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::list<COutPoint> CTransaction::GetOutPoints() const
|
||||
{
|
||||
std::list<COutPoint> listOutPoints;
|
||||
uint256 txHash = GetHash();
|
||||
for (unsigned int i = 0; i < vout.size(); i++)
|
||||
listOutPoints.emplace_back(COutPoint(txHash, i));
|
||||
return listOutPoints;
|
||||
}
|
||||
|
||||
CAmount CTransaction::GetZerocoinSpent() const
|
||||
{
|
||||
CAmount nValueOut = 0;
|
||||
for (const CTxIn& txin : vin) {
|
||||
if(!txin.IsZerocoinSpend())
|
||||
continue;
|
||||
|
||||
nValueOut += txin.nSequence * COIN;
|
||||
}
|
||||
|
||||
return nValueOut;
|
||||
}
|
||||
|
||||
int CTransaction::GetZerocoinMintCount() const
|
||||
{
|
||||
int nCount = 0;
|
||||
for (const CTxOut& out : vout) {
|
||||
if (out.IsZerocoinMint())
|
||||
nCount++;
|
||||
}
|
||||
return nCount;
|
||||
}
|
||||
|
||||
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<CTxIn>::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;
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||
// Copyright (c) 2015-2019 The PIVX developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
|
||||
#define BITCOIN_PRIMITIVES_TRANSACTION_H
|
||||
|
||||
#include "amount.h"
|
||||
#include "libzerocoin/CoinSpend.h"
|
||||
#include "script/script.h"
|
||||
#include "serialize.h"
|
||||
#include "uint256.h"
|
||||
|
||||
#include <list>
|
||||
|
||||
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 <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||||
READWRITE(FLATDATA(*this));
|
||||
}
|
||||
|
||||
void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
|
||||
bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
|
||||
bool IsMasternodeReward(const CTransaction* tx) const;
|
||||
|
||||
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;
|
||||
std::string ToStringShort() const;
|
||||
|
||||
uint256 GetHash();
|
||||
|
||||
};
|
||||
|
||||
/** 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;
|
||||
CScript prevPubKey;
|
||||
|
||||
CTxIn()
|
||||
{
|
||||
nSequence = std::numeric_limits<unsigned int>::max();
|
||||
}
|
||||
|
||||
explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max());
|
||||
CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<uint32_t>::max());
|
||||
CTxIn(const libzerocoin::CoinSpend& spend, libzerocoin::CoinDenomination denom);
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
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<uint32_t>::max());
|
||||
}
|
||||
|
||||
bool IsZerocoinSpend() const;
|
||||
bool IsZerocoinPublicSpend() const;
|
||||
|
||||
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;
|
||||
int nRounds;
|
||||
|
||||
CTxOut()
|
||||
{
|
||||
SetNull();
|
||||
}
|
||||
|
||||
CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||||
READWRITE(nValue);
|
||||
READWRITE(scriptPubKey);
|
||||
}
|
||||
|
||||
void SetNull()
|
||||
{
|
||||
nValue = -1;
|
||||
scriptPubKey.clear();
|
||||
nRounds = -10; // an initial value, should be no way to get this by calculations
|
||||
}
|
||||
|
||||
bool IsNull() const
|
||||
{
|
||||
return (nValue == -1);
|
||||
}
|
||||
|
||||
void SetEmpty()
|
||||
{
|
||||
nValue = 0;
|
||||
scriptPubKey.clear();
|
||||
}
|
||||
|
||||
bool IsEmpty() const
|
||||
{
|
||||
return (nValue == 0 && scriptPubKey.empty());
|
||||
}
|
||||
|
||||
uint256 GetHash() const;
|
||||
|
||||
bool IsDust(CFeeRate minRelayTxFee) const
|
||||
{
|
||||
// "Dust" is defined in terms of CTransaction::minRelayTxFee, which has units uagr-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
|
||||
// i.e. total is 148 + 32 = 182 bytes. Default -minrelaytxfee is 10000 uagr per kB
|
||||
// and that means that fee per txout is 182 * 10000 / 1000 = 1820 uagr.
|
||||
// So dust is a txout less than 1820 *3 = 5460 uagr
|
||||
// with default -minrelaytxfee = minRelayTxFee = 10000 uagr per kB.
|
||||
size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
|
||||
return (nValue < 3*minRelayTxFee.GetFee(nSize));
|
||||
}
|
||||
|
||||
bool IsZerocoinMint() const;
|
||||
CAmount GetZerocoinMinted() const;
|
||||
|
||||
friend bool operator==(const CTxOut& a, const CTxOut& b)
|
||||
{
|
||||
return (a.nValue == b.nValue &&
|
||||
a.scriptPubKey == b.scriptPubKey &&
|
||||
a.nRounds == b.nRounds);
|
||||
}
|
||||
|
||||
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;
|
||||
std::vector<CTxIn> vin;
|
||||
std::vector<CTxOut> vout;
|
||||
const uint32_t nLockTime;
|
||||
//const unsigned int nTime;
|
||||
|
||||
/** 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 <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||||
READWRITE(*const_cast<int32_t*>(&this->nVersion));
|
||||
nVersion = this->nVersion;
|
||||
READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
|
||||
READWRITE(*const_cast<std::vector<CTxOut>*>(&vout));
|
||||
READWRITE(*const_cast<uint32_t*>(&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 HasZerocoinSpendInputs() const;
|
||||
bool HasZerocoinPublicSpendInputs() const;
|
||||
|
||||
bool HasZerocoinMintOutputs() const;
|
||||
|
||||
bool ContainsZerocoins() const
|
||||
{
|
||||
return HasZerocoinSpendInputs() || HasZerocoinPublicSpendInputs() || HasZerocoinMintOutputs();
|
||||
}
|
||||
|
||||
CAmount GetZerocoinMinted() const;
|
||||
CAmount GetZerocoinSpent() const;
|
||||
int GetZerocoinMintCount() const;
|
||||
|
||||
bool UsesUTXO(const COutPoint out);
|
||||
std::list<COutPoint> GetOutPoints() const;
|
||||
|
||||
bool IsCoinBase() const
|
||||
{
|
||||
return (vin.size() == 1 && vin[0].prevout.IsNull() && !ContainsZerocoins());
|
||||
}
|
||||
|
||||
bool IsCoinStake() const;
|
||||
|
||||
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<CTxIn> vin;
|
||||
std::vector<CTxOut> vout;
|
||||
uint32_t nLockTime;
|
||||
|
||||
CMutableTransaction();
|
||||
CMutableTransaction(const CTransaction& tx);
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
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;
|
||||
|
||||
std::string ToString() const;
|
||||
|
||||
friend bool operator==(const CMutableTransaction& a, const CMutableTransaction& b)
|
||||
{
|
||||
return a.GetHash() == b.GetHash();
|
||||
}
|
||||
|
||||
friend bool operator!=(const CMutableTransaction& a, const CMutableTransaction& b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BITCOIN_PRIMITIVES_TRANSACTION_H
|
||||
Reference in New Issue
Block a user