]> Git Repo - VerusCoin.git/blob - src/core.h
Move fee policy out of core
[VerusCoin.git] / src / core.h
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.
5
6 #ifndef BITCOIN_CORE_H
7 #define BITCOIN_CORE_H
8
9 #include "script.h"
10 #include "serialize.h"
11 #include "uint256.h"
12
13 #include <stdint.h>
14
15 class CTransaction;
16
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); }
20
21 /** An outpoint - a combination of a transaction hash and an index n into its vout */
22 class COutPoint
23 {
24 public:
25     uint256 hash;
26     unsigned int n;
27
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); }
33
34     friend bool operator<(const COutPoint& a, const COutPoint& b)
35     {
36         return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
37     }
38
39     friend bool operator==(const COutPoint& a, const COutPoint& b)
40     {
41         return (a.hash == b.hash && a.n == b.n);
42     }
43
44     friend bool operator!=(const COutPoint& a, const COutPoint& b)
45     {
46         return !(a == b);
47     }
48
49     std::string ToString() const;
50     void print() const;
51 };
52
53 /** An inpoint - a combination of a transaction and an index n into its vin */
54 class CInPoint
55 {
56 public:
57     const CTransaction* ptx;
58     unsigned int n;
59
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); }
64 };
65
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.
69  */
70 class CTxIn
71 {
72 public:
73     COutPoint prevout;
74     CScript scriptSig;
75     unsigned int nSequence;
76
77     CTxIn()
78     {
79         nSequence = std::numeric_limits<unsigned int>::max();
80     }
81
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());
84
85     IMPLEMENT_SERIALIZE
86     (
87         READWRITE(prevout);
88         READWRITE(scriptSig);
89         READWRITE(nSequence);
90     )
91
92     bool IsFinal() const
93     {
94         return (nSequence == std::numeric_limits<unsigned int>::max());
95     }
96
97     friend bool operator==(const CTxIn& a, const CTxIn& b)
98     {
99         return (a.prevout   == b.prevout &&
100                 a.scriptSig == b.scriptSig &&
101                 a.nSequence == b.nSequence);
102     }
103
104     friend bool operator!=(const CTxIn& a, const CTxIn& b)
105     {
106         return !(a == b);
107     }
108
109     std::string ToString() const;
110     void print() const;
111 };
112
113
114
115 /** Type-safe wrapper class to for fee rates
116  * (how much to pay based on transaction size)
117  */
118 class CFeeRate
119 {
120 private:
121     int64_t nSatoshisPerK; // unit is satoshis-per-1,000-bytes
122 public:
123     CFeeRate() : nSatoshisPerK(0) { }
124     explicit CFeeRate(int64_t _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
125     CFeeRate(int64_t nFeePaid, size_t nSize);
126     CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
127
128     int64_t GetFee(size_t size) const; // unit returned is satoshis
129     int64_t GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes
130
131     friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
132     friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
133     friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
134     friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
135     friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
136     std::string ToString() const;
137
138     IMPLEMENT_SERIALIZE( READWRITE(nSatoshisPerK); )
139 };
140
141
142 /** An output of a transaction.  It contains the public key that the next input
143  * must be able to sign with to claim it.
144  */
145 class CTxOut
146 {
147 public:
148     int64_t nValue;
149     CScript scriptPubKey;
150
151     CTxOut()
152     {
153         SetNull();
154     }
155
156     CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
157
158     IMPLEMENT_SERIALIZE
159     (
160         READWRITE(nValue);
161         READWRITE(scriptPubKey);
162     )
163
164     void SetNull()
165     {
166         nValue = -1;
167         scriptPubKey.clear();
168     }
169
170     bool IsNull() const
171     {
172         return (nValue == -1);
173     }
174
175     uint256 GetHash() const;
176
177     bool IsDust(CFeeRate minRelayTxFee) const
178     {
179         // "Dust" is defined in terms of CTransaction::minRelayTxFee,
180         // which has units satoshis-per-kilobyte.
181         // If you'd pay more than 1/3 in fees
182         // to spend something, then we consider it dust.
183         // A typical txout is 34 bytes big, and will
184         // need a CTxIn of at least 148 bytes to spend:
185         // so dust is a txout less than 546 satoshis 
186         // with default minRelayTxFee.
187         size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
188         return (nValue < 3*minRelayTxFee.GetFee(nSize));
189     }
190
191     friend bool operator==(const CTxOut& a, const CTxOut& b)
192     {
193         return (a.nValue       == b.nValue &&
194                 a.scriptPubKey == b.scriptPubKey);
195     }
196
197     friend bool operator!=(const CTxOut& a, const CTxOut& b)
198     {
199         return !(a == b);
200     }
201
202     std::string ToString() const;
203     void print() const;
204 };
205
206
207 struct CMutableTransaction;
208
209 /** The basic transaction that is broadcasted on the network and contained in
210  * blocks.  A transaction can contain multiple inputs and outputs.
211  */
212 class CTransaction
213 {
214 private:
215     /** Memory only. */
216     const uint256 hash;
217     void UpdateHash() const;
218
219 public:
220     static const int CURRENT_VERSION=1;
221
222     // The local variables are made const to prevent unintended modification
223     // without updating the cached hash value. However, CTransaction is not
224     // actually immutable; deserialization and assignment are implemented,
225     // and bypass the constness. This is safe, as they update the entire
226     // structure, including the hash.
227     const int nVersion;
228     const std::vector<CTxIn> vin;
229     const std::vector<CTxOut> vout;
230     const unsigned int nLockTime;
231
232     /** Construct a CTransaction that qualifies as IsNull() */
233     CTransaction();
234
235     /** Convert a CMutableTransaction into a CTransaction. */
236     CTransaction(const CMutableTransaction &tx);
237
238     CTransaction& operator=(const CTransaction& tx);
239
240     IMPLEMENT_SERIALIZE(
241         READWRITE(*const_cast<int*>(&this->nVersion));
242         nVersion = this->nVersion;
243         READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
244         READWRITE(*const_cast<std::vector<CTxOut>*>(&vout));
245         READWRITE(*const_cast<unsigned int*>(&nLockTime));
246         if (fRead)
247             UpdateHash();
248     )
249
250     bool IsNull() const {
251         return vin.empty() && vout.empty();
252     }
253
254     const uint256& GetHash() const {
255         return hash;
256     }
257
258     // True if only scriptSigs are different
259     bool IsEquivalentTo(const CTransaction& tx) const;
260
261     // Return sum of txouts.
262     int64_t GetValueOut() const;
263     // GetValueIn() is a method on CCoinsViewCache, because
264     // inputs must be known to compute value in.
265
266     // Compute priority, given priority of inputs and (optionally) tx size
267     double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
268
269     bool IsCoinBase() const
270     {
271         return (vin.size() == 1 && vin[0].prevout.IsNull());
272     }
273
274     friend bool operator==(const CTransaction& a, const CTransaction& b)
275     {
276         return a.hash == b.hash;
277     }
278
279     friend bool operator!=(const CTransaction& a, const CTransaction& b)
280     {
281         return a.hash != b.hash;
282     }
283
284     std::string ToString() const;
285     void print() const;
286 };
287
288 /** A mutable version of CTransaction. */
289 struct CMutableTransaction
290 {
291     int nVersion;
292     std::vector<CTxIn> vin;
293     std::vector<CTxOut> vout;
294     unsigned int nLockTime;
295
296     CMutableTransaction();
297     CMutableTransaction(const CTransaction& tx);
298
299     IMPLEMENT_SERIALIZE(
300         READWRITE(this->nVersion);
301         nVersion = this->nVersion;
302         READWRITE(vin);
303         READWRITE(vout);
304         READWRITE(nLockTime);
305     )
306
307     /** Compute the hash of this CMutableTransaction. This is computed on the
308      * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
309      */
310     uint256 GetHash() const;
311 };
312
313 /** wrapper for CTxOut that provides a more compact serialization */
314 class CTxOutCompressor
315 {
316 private:
317     CTxOut &txout;
318
319 public:
320     static uint64_t CompressAmount(uint64_t nAmount);
321     static uint64_t DecompressAmount(uint64_t nAmount);
322
323     CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
324
325     IMPLEMENT_SERIALIZE(({
326         if (!fRead) {
327             uint64_t nVal = CompressAmount(txout.nValue);
328             READWRITE(VARINT(nVal));
329         } else {
330             uint64_t nVal = 0;
331             READWRITE(VARINT(nVal));
332             txout.nValue = DecompressAmount(nVal);
333         }
334         CScriptCompressor cscript(REF(txout.scriptPubKey));
335         READWRITE(cscript);
336     });)
337 };
338
339 /** Undo information for a CTxIn
340  *
341  *  Contains the prevout's CTxOut being spent, and if this was the
342  *  last output of the affected transaction, its metadata as well
343  *  (coinbase or not, height, transaction version)
344  */
345 class CTxInUndo
346 {
347 public:
348     CTxOut txout;         // the txout data before being spent
349     bool fCoinBase;       // if the outpoint was the last unspent: whether it belonged to a coinbase
350     unsigned int nHeight; // if the outpoint was the last unspent: its height
351     int nVersion;         // if the outpoint was the last unspent: its version
352
353     CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
354     CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
355
356     unsigned int GetSerializeSize(int nType, int nVersion) const {
357         return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
358                (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
359                ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
360     }
361
362     template<typename Stream>
363     void Serialize(Stream &s, int nType, int nVersion) const {
364         ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
365         if (nHeight > 0)
366             ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
367         ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
368     }
369
370     template<typename Stream>
371     void Unserialize(Stream &s, int nType, int nVersion) {
372         unsigned int nCode = 0;
373         ::Unserialize(s, VARINT(nCode), nType, nVersion);
374         nHeight = nCode / 2;
375         fCoinBase = nCode & 1;
376         if (nHeight > 0)
377             ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
378         ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
379     }
380 };
381
382 /** Undo information for a CTransaction */
383 class CTxUndo
384 {
385 public:
386     // undo information for all txins
387     std::vector<CTxInUndo> vprevout;
388
389     IMPLEMENT_SERIALIZE(
390         READWRITE(vprevout);
391     )
392 };
393
394
395 /** Nodes collect new transactions into a block, hash them into a hash tree,
396  * and scan through nonce values to make the block's hash satisfy proof-of-work
397  * requirements.  When they solve the proof-of-work, they broadcast the block
398  * to everyone and the block is added to the block chain.  The first transaction
399  * in the block is a special one that creates a new coin owned by the creator
400  * of the block.
401  */
402 class CBlockHeader
403 {
404 public:
405     // header
406     static const int CURRENT_VERSION=2;
407     int nVersion;
408     uint256 hashPrevBlock;
409     uint256 hashMerkleRoot;
410     unsigned int nTime;
411     unsigned int nBits;
412     unsigned int nNonce;
413
414     CBlockHeader()
415     {
416         SetNull();
417     }
418
419     IMPLEMENT_SERIALIZE
420     (
421         READWRITE(this->nVersion);
422         nVersion = this->nVersion;
423         READWRITE(hashPrevBlock);
424         READWRITE(hashMerkleRoot);
425         READWRITE(nTime);
426         READWRITE(nBits);
427         READWRITE(nNonce);
428     )
429
430     void SetNull()
431     {
432         nVersion = CBlockHeader::CURRENT_VERSION;
433         hashPrevBlock = 0;
434         hashMerkleRoot = 0;
435         nTime = 0;
436         nBits = 0;
437         nNonce = 0;
438     }
439
440     bool IsNull() const
441     {
442         return (nBits == 0);
443     }
444
445     uint256 GetHash() const;
446
447     int64_t GetBlockTime() const
448     {
449         return (int64_t)nTime;
450     }
451 };
452
453
454 class CBlock : public CBlockHeader
455 {
456 public:
457     // network and disk
458     std::vector<CTransaction> vtx;
459
460     // memory only
461     mutable std::vector<uint256> vMerkleTree;
462
463     CBlock()
464     {
465         SetNull();
466     }
467
468     CBlock(const CBlockHeader &header)
469     {
470         SetNull();
471         *((CBlockHeader*)this) = header;
472     }
473
474     IMPLEMENT_SERIALIZE
475     (
476         READWRITE(*(CBlockHeader*)this);
477         READWRITE(vtx);
478     )
479
480     void SetNull()
481     {
482         CBlockHeader::SetNull();
483         vtx.clear();
484         vMerkleTree.clear();
485     }
486
487     CBlockHeader GetBlockHeader() const
488     {
489         CBlockHeader block;
490         block.nVersion       = nVersion;
491         block.hashPrevBlock  = hashPrevBlock;
492         block.hashMerkleRoot = hashMerkleRoot;
493         block.nTime          = nTime;
494         block.nBits          = nBits;
495         block.nNonce         = nNonce;
496         return block;
497     }
498
499     uint256 BuildMerkleTree() const;
500
501     std::vector<uint256> GetMerkleBranch(int nIndex) const;
502     static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
503     void print() const;
504 };
505
506
507 /** Describes a place in the block chain to another node such that if the
508  * other node doesn't have the same branch, it can find a recent common trunk.
509  * The further back it is, the further before the fork it may be.
510  */
511 struct CBlockLocator
512 {
513     std::vector<uint256> vHave;
514
515     CBlockLocator() {}
516
517     CBlockLocator(const std::vector<uint256>& vHaveIn)
518     {
519         vHave = vHaveIn;
520     }
521
522     IMPLEMENT_SERIALIZE
523     (
524         if (!(nType & SER_GETHASH))
525             READWRITE(nVersion);
526         READWRITE(vHave);
527     )
528
529     void SetNull()
530     {
531         vHave.clear();
532     }
533
534     bool IsNull()
535     {
536         return vHave.empty();
537     }
538 };
539
540 #endif
This page took 0.056081 seconds and 4 git commands to generate.