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