2013-01-08 12:02:51 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-02-08 22:50:24 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
2013-01-08 12:02:51 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-01-08 13:17:15 +01:00
|
|
|
#include "core.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2013-06-25 03:03:03 +02:00
|
|
|
#include "util.h"
|
2013-01-08 13:17:15 +01:00
|
|
|
|
2013-06-25 03:03:03 +02:00
|
|
|
std::string COutPoint::ToString() const
|
|
|
|
{
|
2014-01-16 16:15:27 +01:00
|
|
|
return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void COutPoint::print() const
|
|
|
|
{
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("%s\n", ToString());
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, unsigned int nSequenceIn)
|
|
|
|
{
|
|
|
|
prevout = prevoutIn;
|
|
|
|
scriptSig = scriptSigIn;
|
|
|
|
nSequence = nSequenceIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTxIn::CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn, unsigned int 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())
|
2014-01-16 16:15:27 +01:00
|
|
|
str += strprintf(", coinbase %s", HexStr(scriptSig));
|
2013-06-25 03:03:03 +02:00
|
|
|
else
|
2014-01-16 16:15:27 +01:00
|
|
|
str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24));
|
2013-06-25 03:03:03 +02:00
|
|
|
if (nSequence != std::numeric_limits<unsigned int>::max())
|
|
|
|
str += strprintf(", nSequence=%u", nSequence);
|
|
|
|
str += ")";
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxIn::print() const
|
|
|
|
{
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("%s\n", ToString());
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
CTxOut::CTxOut(int64_t nValueIn, CScript scriptPubKeyIn)
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
|
|
|
nValue = nValueIn;
|
|
|
|
scriptPubKey = scriptPubKeyIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 CTxOut::GetHash() const
|
|
|
|
{
|
|
|
|
return SerializeHash(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CTxOut::ToString() const
|
|
|
|
{
|
2014-02-24 09:08:56 +01:00
|
|
|
return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30));
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTxOut::print() const
|
|
|
|
{
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("%s\n", ToString());
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 20:14:18 +02:00
|
|
|
CFeeRate::CFeeRate(int64_t nFeePaid, size_t nSize)
|
|
|
|
{
|
|
|
|
if (nSize > 0)
|
|
|
|
nSatoshisPerK = nFeePaid*1000/nSize;
|
|
|
|
else
|
|
|
|
nSatoshisPerK = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:25:32 +02:00
|
|
|
int64_t CFeeRate::GetFee(size_t nSize) const
|
2014-04-10 20:14:18 +02:00
|
|
|
{
|
2014-07-05 07:30:06 +02:00
|
|
|
int64_t nFee = nSatoshisPerK*nSize / 1000;
|
|
|
|
|
|
|
|
if (nFee == 0 && nSatoshisPerK > 0)
|
|
|
|
nFee = nSatoshisPerK;
|
|
|
|
|
|
|
|
return nFee;
|
2014-04-10 20:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CFeeRate::ToString() const
|
|
|
|
{
|
|
|
|
std::string result = FormatMoney(nSatoshisPerK) + " BTC/kB";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
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
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
|
|
|
return SerializeHash(*this);
|
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
void CTransaction::UpdateHash() const
|
|
|
|
{
|
|
|
|
*const_cast<uint256*>(&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<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;
|
|
|
|
}
|
|
|
|
|
2013-11-11 07:03:51 +01:00
|
|
|
int64_t CTransaction::GetValueOut() const
|
|
|
|
{
|
|
|
|
int64_t nValueOut = 0;
|
|
|
|
BOOST_FOREACH(const CTxOut& txout, vout)
|
|
|
|
{
|
|
|
|
nValueOut += txout.nValue;
|
|
|
|
if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
|
|
|
|
throw std::runtime_error("CTransaction::GetValueOut() : value out of range");
|
|
|
|
}
|
|
|
|
return nValueOut;
|
|
|
|
}
|
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
double CTransaction::ComputePriority(double dPriorityInputs, 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);
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, vin)
|
|
|
|
{
|
|
|
|
unsigned int offset = 41U + std::min(110U, (unsigned int)txin.scriptSig.size());
|
|
|
|
if (nTxSize > offset)
|
|
|
|
nTxSize -= offset;
|
|
|
|
}
|
|
|
|
if (nTxSize == 0) return 0.0;
|
|
|
|
return dPriorityInputs / nTxSize;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:03:03 +02:00
|
|
|
std::string CTransaction::ToString() const
|
|
|
|
{
|
|
|
|
std::string str;
|
2014-05-06 15:25:01 +02:00
|
|
|
str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
|
2014-01-16 16:15:27 +01:00
|
|
|
GetHash().ToString().substr(0,10),
|
2013-06-25 03:03:03 +02:00
|
|
|
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::print() const
|
|
|
|
{
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("%s", ToString());
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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])
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
uint64_t CTxOutCompressor::DecompressAmount(uint64_t x)
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
|
|
|
// 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;
|
2013-04-13 07:13:08 +02:00
|
|
|
uint64_t n = 0;
|
2013-06-25 03:03:03 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 CBlock::BuildMerkleTree() const
|
|
|
|
{
|
|
|
|
vMerkleTree.clear();
|
|
|
|
BOOST_FOREACH(const CTransaction& tx, vtx)
|
|
|
|
vMerkleTree.push_back(tx.GetHash());
|
|
|
|
int j = 0;
|
|
|
|
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);
|
|
|
|
vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
|
|
|
|
BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
|
|
|
|
}
|
|
|
|
j += nSize;
|
|
|
|
}
|
|
|
|
return (vMerkleTree.empty() ? 0 : 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 0;
|
|
|
|
BOOST_FOREACH(const uint256& otherside, vMerkleBranch)
|
|
|
|
{
|
|
|
|
if (nIndex & 1)
|
|
|
|
hash = Hash(BEGIN(otherside), END(otherside), BEGIN(hash), END(hash));
|
|
|
|
else
|
|
|
|
hash = Hash(BEGIN(hash), END(hash), BEGIN(otherside), END(otherside));
|
|
|
|
nIndex >>= 1;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBlock::print() const
|
|
|
|
{
|
2014-05-06 15:25:01 +02:00
|
|
|
LogPrintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
|
2014-01-16 16:15:27 +01:00
|
|
|
GetHash().ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
nVersion,
|
2014-01-16 16:15:27 +01:00
|
|
|
hashPrevBlock.ToString(),
|
|
|
|
hashMerkleRoot.ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
nTime, nBits, nNonce,
|
|
|
|
vtx.size());
|
|
|
|
for (unsigned int i = 0; i < vtx.size(); i++)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf(" ");
|
2013-06-25 03:03:03 +02:00
|
|
|
vtx[i].print();
|
|
|
|
}
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf(" vMerkleTree: ");
|
2013-06-25 03:03:03 +02:00
|
|
|
for (unsigned int i = 0; i < vMerkleTree.size(); i++)
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("%s ", vMerkleTree[i].ToString());
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("\n");
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|