Merge pull request #4048 from sipa/nobigb58
[bitcoinplatinum.git] / src / core.h
blob5eb953610d0a0de3f5f94de510d0f953711cf0fd
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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.
6 #ifndef BITCOIN_CORE_H
7 #define BITCOIN_CORE_H
9 #include "script.h"
10 #include "serialize.h"
11 #include "uint256.h"
13 #include <stdint.h>
15 class CTransaction;
17 /** No amount larger than this (in satoshi) is valid */
18 static const int64_t MAX_MONEY = 21000000 * COIN;
19 inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
21 /** An outpoint - a combination of a transaction hash and an index n into its vout */
22 class COutPoint
24 public:
25 uint256 hash;
26 unsigned int n;
28 COutPoint() { SetNull(); }
29 COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
30 IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
31 void SetNull() { hash = 0; n = (unsigned int) -1; }
32 bool IsNull() const { return (hash == 0 && n == (unsigned int) -1); }
34 friend bool operator<(const COutPoint& a, const COutPoint& b)
36 return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
39 friend bool operator==(const COutPoint& a, const COutPoint& b)
41 return (a.hash == b.hash && a.n == b.n);
44 friend bool operator!=(const COutPoint& a, const COutPoint& b)
46 return !(a == b);
49 std::string ToString() const;
50 void print() const;
53 /** An inpoint - a combination of a transaction and an index n into its vin */
54 class CInPoint
56 public:
57 const CTransaction* ptx;
58 unsigned int n;
60 CInPoint() { SetNull(); }
61 CInPoint(const CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
62 void SetNull() { ptx = NULL; n = (unsigned int) -1; }
63 bool IsNull() const { return (ptx == NULL && n == (unsigned int) -1); }
66 /** An input of a transaction. It contains the location of the previous
67 * transaction's output that it claims and a signature that matches the
68 * output's public key.
70 class CTxIn
72 public:
73 COutPoint prevout;
74 CScript scriptSig;
75 unsigned int nSequence;
77 CTxIn()
79 nSequence = std::numeric_limits<unsigned int>::max();
82 explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
83 CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max());
85 IMPLEMENT_SERIALIZE
87 READWRITE(prevout);
88 READWRITE(scriptSig);
89 READWRITE(nSequence);
92 bool IsFinal() const
94 return (nSequence == std::numeric_limits<unsigned int>::max());
97 friend bool operator==(const CTxIn& a, const CTxIn& b)
99 return (a.prevout == b.prevout &&
100 a.scriptSig == b.scriptSig &&
101 a.nSequence == b.nSequence);
104 friend bool operator!=(const CTxIn& a, const CTxIn& b)
106 return !(a == b);
109 std::string ToString() const;
110 void print() const;
116 /** An output of a transaction. It contains the public key that the next input
117 * must be able to sign with to claim it.
119 class CTxOut
121 public:
122 int64_t nValue;
123 CScript scriptPubKey;
125 CTxOut()
127 SetNull();
130 CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
132 IMPLEMENT_SERIALIZE
134 READWRITE(nValue);
135 READWRITE(scriptPubKey);
138 void SetNull()
140 nValue = -1;
141 scriptPubKey.clear();
144 bool IsNull() const
146 return (nValue == -1);
149 uint256 GetHash() const;
151 bool IsDust(int64_t nMinRelayTxFee) const
153 // "Dust" is defined in terms of CTransaction::nMinRelayTxFee,
154 // which has units satoshis-per-kilobyte.
155 // If you'd pay more than 1/3 in fees
156 // to spend something, then we consider it dust.
157 // A typical txout is 34 bytes big, and will
158 // need a CTxIn of at least 148 bytes to spend,
159 // so dust is a txout less than 546 satoshis
160 // with default nMinRelayTxFee.
161 return ((nValue*1000)/(3*((int)GetSerializeSize(SER_DISK,0)+148)) < nMinRelayTxFee);
164 friend bool operator==(const CTxOut& a, const CTxOut& b)
166 return (a.nValue == b.nValue &&
167 a.scriptPubKey == b.scriptPubKey);
170 friend bool operator!=(const CTxOut& a, const CTxOut& b)
172 return !(a == b);
175 std::string ToString() const;
176 void print() const;
180 /** The basic transaction that is broadcasted on the network and contained in
181 * blocks. A transaction can contain multiple inputs and outputs.
183 class CTransaction
185 public:
186 static int64_t nMinTxFee;
187 static int64_t nMinRelayTxFee;
188 static const int CURRENT_VERSION=1;
189 int nVersion;
190 std::vector<CTxIn> vin;
191 std::vector<CTxOut> vout;
192 unsigned int nLockTime;
194 CTransaction()
196 SetNull();
199 IMPLEMENT_SERIALIZE
201 READWRITE(this->nVersion);
202 nVersion = this->nVersion;
203 READWRITE(vin);
204 READWRITE(vout);
205 READWRITE(nLockTime);
208 void SetNull()
210 nVersion = CTransaction::CURRENT_VERSION;
211 vin.clear();
212 vout.clear();
213 nLockTime = 0;
216 bool IsNull() const
218 return (vin.empty() && vout.empty());
221 uint256 GetHash() const;
222 bool IsNewerThan(const CTransaction& old) const;
224 // Return sum of txouts.
225 int64_t GetValueOut() const;
226 // GetValueIn() is a method on CCoinsViewCache, because
227 // inputs must be known to compute value in.
229 // Compute priority, given priority of inputs and (optionally) tx size
230 double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
232 bool IsCoinBase() const
234 return (vin.size() == 1 && vin[0].prevout.IsNull());
237 friend bool operator==(const CTransaction& a, const CTransaction& b)
239 return (a.nVersion == b.nVersion &&
240 a.vin == b.vin &&
241 a.vout == b.vout &&
242 a.nLockTime == b.nLockTime);
245 friend bool operator!=(const CTransaction& a, const CTransaction& b)
247 return !(a == b);
251 std::string ToString() const;
252 void print() const;
255 /** wrapper for CTxOut that provides a more compact serialization */
256 class CTxOutCompressor
258 private:
259 CTxOut &txout;
261 public:
262 static uint64_t CompressAmount(uint64_t nAmount);
263 static uint64_t DecompressAmount(uint64_t nAmount);
265 CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
267 IMPLEMENT_SERIALIZE(({
268 if (!fRead) {
269 uint64_t nVal = CompressAmount(txout.nValue);
270 READWRITE(VARINT(nVal));
271 } else {
272 uint64_t nVal = 0;
273 READWRITE(VARINT(nVal));
274 txout.nValue = DecompressAmount(nVal);
276 CScriptCompressor cscript(REF(txout.scriptPubKey));
277 READWRITE(cscript);
278 });)
281 /** Undo information for a CTxIn
283 * Contains the prevout's CTxOut being spent, and if this was the
284 * last output of the affected transaction, its metadata as well
285 * (coinbase or not, height, transaction version)
287 class CTxInUndo
289 public:
290 CTxOut txout; // the txout data before being spent
291 bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase
292 unsigned int nHeight; // if the outpoint was the last unspent: its height
293 int nVersion; // if the outpoint was the last unspent: its version
295 CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
296 CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
298 unsigned int GetSerializeSize(int nType, int nVersion) const {
299 return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
300 (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
301 ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
304 template<typename Stream>
305 void Serialize(Stream &s, int nType, int nVersion) const {
306 ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
307 if (nHeight > 0)
308 ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
309 ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
312 template<typename Stream>
313 void Unserialize(Stream &s, int nType, int nVersion) {
314 unsigned int nCode = 0;
315 ::Unserialize(s, VARINT(nCode), nType, nVersion);
316 nHeight = nCode / 2;
317 fCoinBase = nCode & 1;
318 if (nHeight > 0)
319 ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
320 ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
324 /** Undo information for a CTransaction */
325 class CTxUndo
327 public:
328 // undo information for all txins
329 std::vector<CTxInUndo> vprevout;
331 IMPLEMENT_SERIALIZE(
332 READWRITE(vprevout);
337 /** Nodes collect new transactions into a block, hash them into a hash tree,
338 * and scan through nonce values to make the block's hash satisfy proof-of-work
339 * requirements. When they solve the proof-of-work, they broadcast the block
340 * to everyone and the block is added to the block chain. The first transaction
341 * in the block is a special one that creates a new coin owned by the creator
342 * of the block.
344 class CBlockHeader
346 public:
347 // header
348 static const int CURRENT_VERSION=2;
349 int nVersion;
350 uint256 hashPrevBlock;
351 uint256 hashMerkleRoot;
352 unsigned int nTime;
353 unsigned int nBits;
354 unsigned int nNonce;
356 CBlockHeader()
358 SetNull();
361 IMPLEMENT_SERIALIZE
363 READWRITE(this->nVersion);
364 nVersion = this->nVersion;
365 READWRITE(hashPrevBlock);
366 READWRITE(hashMerkleRoot);
367 READWRITE(nTime);
368 READWRITE(nBits);
369 READWRITE(nNonce);
372 void SetNull()
374 nVersion = CBlockHeader::CURRENT_VERSION;
375 hashPrevBlock = 0;
376 hashMerkleRoot = 0;
377 nTime = 0;
378 nBits = 0;
379 nNonce = 0;
382 bool IsNull() const
384 return (nBits == 0);
387 uint256 GetHash() const;
389 int64_t GetBlockTime() const
391 return (int64_t)nTime;
396 class CBlock : public CBlockHeader
398 public:
399 // network and disk
400 std::vector<CTransaction> vtx;
402 // memory only
403 mutable std::vector<uint256> vMerkleTree;
405 CBlock()
407 SetNull();
410 CBlock(const CBlockHeader &header)
412 SetNull();
413 *((CBlockHeader*)this) = header;
416 IMPLEMENT_SERIALIZE
418 READWRITE(*(CBlockHeader*)this);
419 READWRITE(vtx);
422 void SetNull()
424 CBlockHeader::SetNull();
425 vtx.clear();
426 vMerkleTree.clear();
429 CBlockHeader GetBlockHeader() const
431 CBlockHeader block;
432 block.nVersion = nVersion;
433 block.hashPrevBlock = hashPrevBlock;
434 block.hashMerkleRoot = hashMerkleRoot;
435 block.nTime = nTime;
436 block.nBits = nBits;
437 block.nNonce = nNonce;
438 return block;
441 uint256 BuildMerkleTree() const;
443 const uint256 &GetTxHash(unsigned int nIndex) const {
444 assert(vMerkleTree.size() > 0); // BuildMerkleTree must have been called first
445 assert(nIndex < vtx.size());
446 return vMerkleTree[nIndex];
449 std::vector<uint256> GetMerkleBranch(int nIndex) const;
450 static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
451 void print() const;
455 /** Describes a place in the block chain to another node such that if the
456 * other node doesn't have the same branch, it can find a recent common trunk.
457 * The further back it is, the further before the fork it may be.
459 struct CBlockLocator
461 std::vector<uint256> vHave;
463 CBlockLocator() {}
465 CBlockLocator(const std::vector<uint256>& vHaveIn)
467 vHave = vHaveIn;
470 IMPLEMENT_SERIALIZE
472 if (!(nType & SER_GETHASH))
473 READWRITE(nVersion);
474 READWRITE(vHave);
477 void SetNull()
479 vHave.clear();
482 bool IsNull()
484 return vHave.empty();
488 #endif