Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
core.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "core.h"
7 
8 #include "util.h"
9 
10 std::string COutPoint::ToString() const
11 {
12  return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
13 }
14 
15 void COutPoint::print() const
16 {
17  LogPrintf("%s\n", ToString());
18 }
19 
20 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, unsigned int nSequenceIn)
21 {
22  prevout = prevoutIn;
23  scriptSig = scriptSigIn;
24  nSequence = nSequenceIn;
25 }
26 
27 CTxIn::CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn, unsigned int nSequenceIn)
28 {
29  prevout = COutPoint(hashPrevTx, nOut);
30  scriptSig = scriptSigIn;
31  nSequence = nSequenceIn;
32 }
33 
34 std::string CTxIn::ToString() const
35 {
36  std::string str;
37  str += "CTxIn(";
38  str += prevout.ToString();
39  if (prevout.IsNull())
40  str += strprintf(", coinbase %s", HexStr(scriptSig));
41  else
42  str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24));
43  if (nSequence != std::numeric_limits<unsigned int>::max())
44  str += strprintf(", nSequence=%u", nSequence);
45  str += ")";
46  return str;
47 }
48 
49 void CTxIn::print() const
50 {
51  LogPrintf("%s\n", ToString());
52 }
53 
54 CTxOut::CTxOut(int64_t nValueIn, CScript scriptPubKeyIn)
55 {
56  nValue = nValueIn;
57  scriptPubKey = scriptPubKeyIn;
58 }
59 
61 {
62  return SerializeHash(*this);
63 }
64 
65 std::string CTxOut::ToString() const
66 {
67  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30));
68 }
69 
70 void CTxOut::print() const
71 {
72  LogPrintf("%s\n", ToString());
73 }
74 
76 {
77  return SerializeHash(*this);
78 }
79 
81 {
82  if (vin.size() != old.vin.size())
83  return false;
84  for (unsigned int i = 0; i < vin.size(); i++)
85  if (vin[i].prevout != old.vin[i].prevout)
86  return false;
87 
88  bool fNewer = false;
89  unsigned int nLowest = std::numeric_limits<unsigned int>::max();
90  for (unsigned int i = 0; i < vin.size(); i++)
91  {
92  if (vin[i].nSequence != old.vin[i].nSequence)
93  {
94  if (vin[i].nSequence <= nLowest)
95  {
96  fNewer = false;
97  nLowest = vin[i].nSequence;
98  }
99  if (old.vin[i].nSequence < nLowest)
100  {
101  fNewer = true;
102  nLowest = old.vin[i].nSequence;
103  }
104  }
105  }
106  return fNewer;
107 }
108 
110 {
111  int64_t nValueOut = 0;
112  BOOST_FOREACH(const CTxOut& txout, vout)
113  {
114  nValueOut += txout.nValue;
115  if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
116  throw std::runtime_error("CTransaction::GetValueOut() : value out of range");
117  }
118  return nValueOut;
119 }
120 
121 double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const
122 {
123  // In order to avoid disincentivizing cleaning up the UTXO set we don't count
124  // the constant overhead for each txin and up to 110 bytes of scriptSig (which
125  // is enough to cover a compressed pubkey p2sh redemption) for priority.
126  // Providing any more cleanup incentive than making additional inputs free would
127  // risk encouraging people to create junk outputs to redeem later.
128  if (nTxSize == 0)
130  BOOST_FOREACH(const CTxIn& txin, vin)
131  {
132  unsigned int offset = 41U + std::min(110U, (unsigned int)txin.scriptSig.size());
133  if (nTxSize > offset)
134  nTxSize -= offset;
135  }
136  if (nTxSize == 0) return 0.0;
137  return dPriorityInputs / nTxSize;
138 }
139 
140 std::string CTransaction::ToString() const
141 {
142  std::string str;
143  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
144  GetHash().ToString().substr(0,10),
145  nVersion,
146  vin.size(),
147  vout.size(),
148  nLockTime);
149  for (unsigned int i = 0; i < vin.size(); i++)
150  str += " " + vin[i].ToString() + "\n";
151  for (unsigned int i = 0; i < vout.size(); i++)
152  str += " " + vout[i].ToString() + "\n";
153  return str;
154 }
155 
157 {
158  LogPrintf("%s", ToString());
159 }
160 
161 // Amount compression:
162 // * If the amount is 0, output 0
163 // * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
164 // * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
165 // * call the result n
166 // * output 1 + 10*(9*n + d - 1) + e
167 // * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
168 // (this is decodable, as d is in [1-9] and e is in [0-9])
169 
171 {
172  if (n == 0)
173  return 0;
174  int e = 0;
175  while (((n % 10) == 0) && e < 9) {
176  n /= 10;
177  e++;
178  }
179  if (e < 9) {
180  int d = (n % 10);
181  assert(d >= 1 && d <= 9);
182  n /= 10;
183  return 1 + (n*9 + d - 1)*10 + e;
184  } else {
185  return 1 + (n - 1)*10 + 9;
186  }
187 }
188 
190 {
191  // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
192  if (x == 0)
193  return 0;
194  x--;
195  // x = 10*(9*n + d - 1) + e
196  int e = x % 10;
197  x /= 10;
198  uint64_t n = 0;
199  if (e < 9) {
200  // x = 9*n + d - 1
201  int d = (x % 9) + 1;
202  x /= 9;
203  // x = n
204  n = x*10 + d;
205  } else {
206  n = x+1;
207  }
208  while (e) {
209  n *= 10;
210  e--;
211  }
212  return n;
213 }
214 
216 {
217  return Hash(BEGIN(nVersion), END(nNonce));
218 }
219 
221 {
222  vMerkleTree.clear();
223  BOOST_FOREACH(const CTransaction& tx, vtx)
224  vMerkleTree.push_back(tx.GetHash());
225  int j = 0;
226  for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
227  {
228  for (int i = 0; i < nSize; i += 2)
229  {
230  int i2 = std::min(i+1, nSize-1);
231  vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
232  BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
233  }
234  j += nSize;
235  }
236  return (vMerkleTree.empty() ? 0 : vMerkleTree.back());
237 }
238 
239 std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
240 {
241  if (vMerkleTree.empty())
242  BuildMerkleTree();
243  std::vector<uint256> vMerkleBranch;
244  int j = 0;
245  for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
246  {
247  int i = std::min(nIndex^1, nSize-1);
248  vMerkleBranch.push_back(vMerkleTree[j+i]);
249  nIndex >>= 1;
250  j += nSize;
251  }
252  return vMerkleBranch;
253 }
254 
255 uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
256 {
257  if (nIndex == -1)
258  return 0;
259  BOOST_FOREACH(const uint256& otherside, vMerkleBranch)
260  {
261  if (nIndex & 1)
262  hash = Hash(BEGIN(otherside), END(otherside), BEGIN(hash), END(hash));
263  else
264  hash = Hash(BEGIN(hash), END(hash), BEGIN(otherside), END(otherside));
265  nIndex >>= 1;
266  }
267  return hash;
268 }
269 
270 void CBlock::print() const
271 {
272  LogPrintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
273  GetHash().ToString(),
274  nVersion,
277  nTime, nBits, nNonce,
278  vtx.size());
279  for (unsigned int i = 0; i < vtx.size(); i++)
280  {
281  LogPrintf(" ");
282  vtx[i].print();
283  }
284  LogPrintf(" vMerkleTree: ");
285  for (unsigned int i = 0; i < vMerkleTree.size(); i++)
286  LogPrintf("%s ", vMerkleTree[i].ToString());
287  LogPrintf("\n");
288 }
int64_t GetValueOut() const
Definition: core.cpp:109
int nVersion
Definition: core.h:189
int nVersion
Definition: core.h:349
CScript scriptPubKey
Definition: core.h:123
void print() const
Definition: core.cpp:49
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
Definition: core.cpp:121
#define END(a)
Definition: util.h:42
void print() const
Definition: core.cpp:70
void print() const
Definition: core.cpp:156
uint256 GetHash() const
Definition: core.cpp:75
#define strprintf
Definition: util.h:116
bool MoneyRange(int64_t nValue)
Definition: core.h:19
unsigned int n
Definition: core.h:26
std::string ToString() const
Definition: core.cpp:65
bool IsNewerThan(const CTransaction &old) const
Definition: core.cpp:80
std::string ToString() const
Definition: script.h:680
uint256 BuildMerkleTree() const
Definition: core.cpp:220
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Definition: hash.h:104
std::vector< uint256 > GetMerkleBranch(int nIndex) const
Definition: core.cpp:239
#define LogPrintf(...)
Definition: util.h:117
unsigned int nLockTime
Definition: core.h:192
uint256 hashMerkleRoot
Definition: core.h:351
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:105
An input of a transaction.
Definition: core.h:70
std::vector< CTxOut > vout
Definition: core.h:191
std::vector< CTxIn > vin
Definition: core.h:190
uint256 hashPrevBlock
Definition: core.h:350
unsigned int nNonce
Definition: core.h:354
std::vector< uint256 > vMerkleTree
Definition: core.h:403
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:19
An output of a transaction.
Definition: core.h:119
static uint64_t CompressAmount(uint64_t nAmount)
Definition: core.cpp:170
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:22
CTxOut()
Definition: core.h:125
void print() const
Definition: core.cpp:270
std::string ToString() const
Definition: core.cpp:140
uint256 GetHash() const
Definition: core.cpp:60
std::string ToString() const
Definition: core.cpp:34
CScript scriptSig
Definition: core.h:74
static uint64_t DecompressAmount(uint64_t nAmount)
Definition: core.cpp:189
unsigned int nSequence
Definition: core.h:75
std::string ToString() const
Definition: core.cpp:10
#define BEGIN(a)
Definition: util.h:41
256-bit unsigned integer
Definition: uint256.h:531
static const int64_t COIN
Definition: util.h:38
unsigned int nTime
Definition: core.h:352
uint256 GetHash() const
Definition: core.cpp:215
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:401
CTxIn()
Definition: core.h:77
std::string ToString() const
Definition: uint256.h:340
static const int PROTOCOL_VERSION
Definition: version.h:29
static uint256 CheckMerkleBranch(uint256 hash, const std::vector< uint256 > &vMerkleBranch, int nIndex)
Definition: core.cpp:255
bool IsNull() const
Definition: core.h:32
std::vector< CTransaction > vtx
Definition: core.h:400
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
unsigned int nBits
Definition: core.h:353
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Definition: util.h:263
COutPoint prevout
Definition: core.h:73
void print() const
Definition: core.cpp:15
int64_t nValue
Definition: core.h:122
uint256 hash
Definition: core.h:25