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.
10 #include "serialize.h"
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 */
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
)
49 std::string
ToString() const;
53 /** An inpoint - a combination of a transaction and an index n into its vin */
57 const CTransaction
* ptx
;
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.
75 unsigned int nSequence
;
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());
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
)
109 std::string
ToString() 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.
123 CScript scriptPubKey
;
130 CTxOut(int64_t nValueIn
, CScript scriptPubKeyIn
);
135 READWRITE(scriptPubKey
);
141 scriptPubKey
.clear();
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
)
175 std::string
ToString() const;
180 /** The basic transaction that is broadcasted on the network and contained in
181 * blocks. A transaction can contain multiple inputs and outputs.
186 static int64_t nMinTxFee
;
187 static int64_t nMinRelayTxFee
;
188 static const int CURRENT_VERSION
=1;
190 std::vector
<CTxIn
> vin
;
191 std::vector
<CTxOut
> vout
;
192 unsigned int nLockTime
;
201 READWRITE(this->nVersion
);
202 nVersion
= this->nVersion
;
205 READWRITE(nLockTime
);
210 nVersion
= CTransaction::CURRENT_VERSION
;
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
&&
242 a
.nLockTime
== b
.nLockTime
);
245 friend bool operator!=(const CTransaction
& a
, const CTransaction
& b
)
251 std::string
ToString() const;
255 /** wrapper for CTxOut that provides a more compact serialization */
256 class CTxOutCompressor
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(({
269 uint64_t nVal
= CompressAmount(txout
.nValue
);
270 READWRITE(VARINT(nVal
));
273 READWRITE(VARINT(nVal
));
274 txout
.nValue
= DecompressAmount(nVal
);
276 CScriptCompressor
cscript(REF(txout
.scriptPubKey
));
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)
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
);
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
);
317 fCoinBase
= nCode
& 1;
319 ::Unserialize(s
, VARINT(this->nVersion
), nType
, nVersion
);
320 ::Unserialize(s
, REF(CTxOutCompressor(REF(txout
))), nType
, nVersion
);
324 /** Undo information for a CTransaction */
328 // undo information for all txins
329 std::vector
<CTxInUndo
> 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
348 static const int CURRENT_VERSION
=2;
350 uint256 hashPrevBlock
;
351 uint256 hashMerkleRoot
;
363 READWRITE(this->nVersion
);
364 nVersion
= this->nVersion
;
365 READWRITE(hashPrevBlock
);
366 READWRITE(hashMerkleRoot
);
374 nVersion
= CBlockHeader::CURRENT_VERSION
;
387 uint256
GetHash() const;
389 int64_t GetBlockTime() const
391 return (int64_t)nTime
;
396 class CBlock
: public CBlockHeader
400 std::vector
<CTransaction
> vtx
;
403 mutable std::vector
<uint256
> vMerkleTree
;
410 CBlock(const CBlockHeader
&header
)
413 *((CBlockHeader
*)this) = header
;
418 READWRITE(*(CBlockHeader
*)this);
424 CBlockHeader::SetNull();
429 CBlockHeader
GetBlockHeader() const
432 block
.nVersion
= nVersion
;
433 block
.hashPrevBlock
= hashPrevBlock
;
434 block
.hashMerkleRoot
= hashMerkleRoot
;
437 block
.nNonce
= nNonce
;
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
);
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.
461 std::vector
<uint256
> vHave
;
465 CBlockLocator(const std::vector
<uint256
>& vHaveIn
)
472 if (!(nType
& SER_GETHASH
))
484 return vHave
.empty();