]> Git Repo - VerusCoin.git/blame - src/main.h
Make some global variables less-global (static)
[VerusCoin.git] / src / main.h
CommitLineData
0a61b0df 1// Copyright (c) 2009-2010 Satoshi Nakamoto
b2120e22 2// Copyright (c) 2011 The Bitcoin developers
0a61b0df 3// Distributed under the MIT/X11 software license, see the accompanying
4// file license.txt or http://www.opensource.org/licenses/mit-license.php.
223b6f1b
WL
5#ifndef BITCOIN_MAIN_H
6#define BITCOIN_MAIN_H
7
8#include "bignum.h"
9#include "net.h"
10#include "key.h"
223b6f1b 11#include "script.h"
64c7ee7e 12#include "db.h"
223b6f1b
WL
13
14#include <list>
0a61b0df 15
0a61b0df 16class CBlock;
17class CBlockIndex;
18class CWalletTx;
e89b9f6a 19class CWallet;
0a61b0df 20class CKeyItem;
64c7ee7e
PW
21class CReserveKey;
22class CWalletDB;
0a61b0df 23
40c2614e
JL
24class CMessageHeader;
25class CAddress;
26class CInv;
27class CRequestTracker;
28class CNode;
29class CBlockIndex;
30
0a61b0df 31static const unsigned int MAX_BLOCK_SIZE = 1000000;
3df62878 32static const unsigned int MAX_BLOCK_SIZE_GEN = MAX_BLOCK_SIZE/2;
f1e1fb4b 33static const int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
0a61b0df 34static const int64 COIN = 100000000;
35static const int64 CENT = 1000000;
352b4ea5 36static const int64 MIN_TX_FEE = 50000;
6de1326b 37static const int64 MIN_RELAY_TX_FEE = 10000;
0a61b0df 38static const int64 MAX_MONEY = 21000000 * COIN;
39inline bool MoneyRange(int64 nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
40static const int COINBASE_MATURITY = 100;
aa496b75
WL
41// Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timestamp.
42static const int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC
8bb5edc1
MC
43#ifdef USE_UPNP
44static const int fHaveUPnP = true;
45#else
46static const int fHaveUPnP = false;
47#endif
0a61b0df 48
49
50
51
52
53
54extern CCriticalSection cs_main;
223b6f1b 55extern std::map<uint256, CBlockIndex*> mapBlockIndex;
5cbf7532 56extern uint256 hashGenesisBlock;
0a61b0df 57extern CBlockIndex* pindexGenesisBlock;
58extern int nBestHeight;
59extern CBigNum bnBestChainWork;
60extern CBigNum bnBestInvalidWork;
61extern uint256 hashBestChain;
62extern CBlockIndex* pindexBest;
63extern unsigned int nTransactionsUpdated;
0a61b0df 64extern double dHashesPerSec;
65extern int64 nHPSTimerStart;
e89b9f6a 66extern int64 nTimeBestReceived;
64c7ee7e
PW
67extern CCriticalSection cs_setpwalletRegistered;
68extern std::set<CWallet*> setpwalletRegistered;
0a61b0df 69
70// Settings
71extern int fGenerateBitcoins;
72extern int64 nTransactionFee;
0a61b0df 73extern int fLimitProcessors;
74extern int nLimitProcessors;
75extern int fMinimizeToTray;
76extern int fMinimizeOnClose;
8bb5edc1 77extern int fUseUPnP;
0a61b0df 78
79
80
81
82
1512d5ce
JL
83class CReserveKey;
84class CTxDB;
85class CTxIndex;
0a61b0df 86
64c7ee7e
PW
87void RegisterWallet(CWallet* pwalletIn);
88void UnregisterWallet(CWallet* pwalletIn);
0a61b0df 89bool CheckDiskSpace(uint64 nAdditionalBytes=0);
90FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode="rb");
91FILE* AppendBlockFile(unsigned int& nFileRet);
0a61b0df 92bool LoadBlockIndex(bool fAllowNew=true);
93void PrintBlockTree();
94bool ProcessMessages(CNode* pfrom);
0a61b0df 95bool SendMessages(CNode* pto, bool fSendTrickle);
64c7ee7e 96void GenerateBitcoins(bool fGenerate, CWallet* pwallet);
776d0f34 97CBlock* CreateNewBlock(CReserveKey& reservekey);
98void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime);
99void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1);
64c7ee7e 100bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey);
0a61b0df 101bool CheckProofOfWork(uint256 hash, unsigned int nBits);
eade2131 102int GetTotalBlocksEstimate();
0a61b0df 103bool IsInitialBlockDownload();
223b6f1b 104std::string GetWarnings(std::string strFor);
0a61b0df 105
106
107
108
109
110
111
112
113
114
115
116
64c7ee7e
PW
117bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
118
119template<typename T>
120bool WriteSetting(const std::string& strKey, const T& value)
121{
122 bool fOk = false;
123 BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
124 {
125 std::string strWalletFile;
126 if (!GetWalletFile(pwallet, strWalletFile))
127 continue;
128 fOk |= CWalletDB(strWalletFile).WriteSetting(strKey, value);
129 }
130 return fOk;
131}
132
133
0a61b0df 134class CDiskTxPos
135{
136public:
137 unsigned int nFile;
138 unsigned int nBlockPos;
139 unsigned int nTxPos;
140
141 CDiskTxPos()
142 {
143 SetNull();
144 }
145
146 CDiskTxPos(unsigned int nFileIn, unsigned int nBlockPosIn, unsigned int nTxPosIn)
147 {
148 nFile = nFileIn;
149 nBlockPos = nBlockPosIn;
150 nTxPos = nTxPosIn;
151 }
152
153 IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
154 void SetNull() { nFile = -1; nBlockPos = 0; nTxPos = 0; }
155 bool IsNull() const { return (nFile == -1); }
156
157 friend bool operator==(const CDiskTxPos& a, const CDiskTxPos& b)
158 {
159 return (a.nFile == b.nFile &&
160 a.nBlockPos == b.nBlockPos &&
161 a.nTxPos == b.nTxPos);
162 }
163
164 friend bool operator!=(const CDiskTxPos& a, const CDiskTxPos& b)
165 {
166 return !(a == b);
167 }
168
223b6f1b 169 std::string ToString() const
0a61b0df 170 {
171 if (IsNull())
172 return strprintf("null");
173 else
174 return strprintf("(nFile=%d, nBlockPos=%d, nTxPos=%d)", nFile, nBlockPos, nTxPos);
175 }
176
177 void print() const
178 {
179 printf("%s", ToString().c_str());
180 }
181};
182
183
184
185
186class CInPoint
187{
188public:
189 CTransaction* ptx;
190 unsigned int n;
191
192 CInPoint() { SetNull(); }
193 CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
194 void SetNull() { ptx = NULL; n = -1; }
195 bool IsNull() const { return (ptx == NULL && n == -1); }
196};
197
198
199
200
201class COutPoint
202{
203public:
204 uint256 hash;
205 unsigned int n;
206
207 COutPoint() { SetNull(); }
208 COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
209 IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
210 void SetNull() { hash = 0; n = -1; }
211 bool IsNull() const { return (hash == 0 && n == -1); }
212
213 friend bool operator<(const COutPoint& a, const COutPoint& b)
214 {
215 return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
216 }
217
218 friend bool operator==(const COutPoint& a, const COutPoint& b)
219 {
220 return (a.hash == b.hash && a.n == b.n);
221 }
222
223 friend bool operator!=(const COutPoint& a, const COutPoint& b)
224 {
225 return !(a == b);
226 }
227
223b6f1b 228 std::string ToString() const
0a61b0df 229 {
b22c8842 230 return strprintf("COutPoint(%s, %d)", hash.ToString().substr(0,10).c_str(), n);
0a61b0df 231 }
232
233 void print() const
234 {
235 printf("%s\n", ToString().c_str());
236 }
237};
238
239
240
241
242//
243// An input of a transaction. It contains the location of the previous
244// transaction's output that it claims and a signature that matches the
245// output's public key.
246//
247class CTxIn
248{
249public:
250 COutPoint prevout;
251 CScript scriptSig;
252 unsigned int nSequence;
253
254 CTxIn()
255 {
256 nSequence = UINT_MAX;
257 }
258
259 explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX)
260 {
261 prevout = prevoutIn;
262 scriptSig = scriptSigIn;
263 nSequence = nSequenceIn;
264 }
265
266 CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=UINT_MAX)
267 {
268 prevout = COutPoint(hashPrevTx, nOut);
269 scriptSig = scriptSigIn;
270 nSequence = nSequenceIn;
271 }
272
273 IMPLEMENT_SERIALIZE
274 (
275 READWRITE(prevout);
276 READWRITE(scriptSig);
277 READWRITE(nSequence);
278 )
279
280 bool IsFinal() const
281 {
282 return (nSequence == UINT_MAX);
283 }
284
285 friend bool operator==(const CTxIn& a, const CTxIn& b)
286 {
287 return (a.prevout == b.prevout &&
288 a.scriptSig == b.scriptSig &&
289 a.nSequence == b.nSequence);
290 }
291
292 friend bool operator!=(const CTxIn& a, const CTxIn& b)
293 {
294 return !(a == b);
295 }
296
223b6f1b 297 std::string ToString() const
0a61b0df 298 {
223b6f1b 299 std::string str;
0a61b0df 300 str += strprintf("CTxIn(");
301 str += prevout.ToString();
302 if (prevout.IsNull())
f1e1fb4b 303 str += strprintf(", coinbase %s", HexStr(scriptSig).c_str());
0a61b0df 304 else
305 str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24).c_str());
306 if (nSequence != UINT_MAX)
307 str += strprintf(", nSequence=%u", nSequence);
308 str += ")";
309 return str;
310 }
311
312 void print() const
313 {
314 printf("%s\n", ToString().c_str());
315 }
0a61b0df 316};
317
318
319
320
321//
322// An output of a transaction. It contains the public key that the next input
323// must be able to sign with to claim it.
324//
325class CTxOut
326{
327public:
328 int64 nValue;
329 CScript scriptPubKey;
330
331 CTxOut()
332 {
333 SetNull();
334 }
335
336 CTxOut(int64 nValueIn, CScript scriptPubKeyIn)
337 {
338 nValue = nValueIn;
339 scriptPubKey = scriptPubKeyIn;
340 }
341
342 IMPLEMENT_SERIALIZE
343 (
344 READWRITE(nValue);
345 READWRITE(scriptPubKey);
346 )
347
348 void SetNull()
349 {
350 nValue = -1;
351 scriptPubKey.clear();
352 }
353
354 bool IsNull()
355 {
356 return (nValue == -1);
357 }
358
359 uint256 GetHash() const
360 {
361 return SerializeHash(*this);
362 }
363
0a61b0df 364 friend bool operator==(const CTxOut& a, const CTxOut& b)
365 {
366 return (a.nValue == b.nValue &&
367 a.scriptPubKey == b.scriptPubKey);
368 }
369
370 friend bool operator!=(const CTxOut& a, const CTxOut& b)
371 {
372 return !(a == b);
373 }
374
223b6f1b 375 std::string ToString() const
0a61b0df 376 {
377 if (scriptPubKey.size() < 6)
378 return "CTxOut(error)";
10384941 379 return strprintf("CTxOut(nValue=%"PRI64d".%08"PRI64d", scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30).c_str());
0a61b0df 380 }
381
382 void print() const
383 {
384 printf("%s\n", ToString().c_str());
385 }
386};
387
388
389
390
391//
392// The basic transaction that is broadcasted on the network and contained in
393// blocks. A transaction can contain multiple inputs and outputs.
394//
395class CTransaction
396{
397public:
398 int nVersion;
223b6f1b
WL
399 std::vector<CTxIn> vin;
400 std::vector<CTxOut> vout;
0a61b0df 401 unsigned int nLockTime;
402
403
404 CTransaction()
405 {
406 SetNull();
407 }
408
409 IMPLEMENT_SERIALIZE
410 (
411 READWRITE(this->nVersion);
412 nVersion = this->nVersion;
413 READWRITE(vin);
414 READWRITE(vout);
415 READWRITE(nLockTime);
416 )
417
418 void SetNull()
419 {
420 nVersion = 1;
421 vin.clear();
422 vout.clear();
423 nLockTime = 0;
424 }
425
426 bool IsNull() const
427 {
428 return (vin.empty() && vout.empty());
429 }
430
431 uint256 GetHash() const
432 {
433 return SerializeHash(*this);
434 }
435
436 bool IsFinal(int nBlockHeight=0, int64 nBlockTime=0) const
437 {
438 // Time based nLockTime implemented in 0.1.6
439 if (nLockTime == 0)
440 return true;
441 if (nBlockHeight == 0)
442 nBlockHeight = nBestHeight;
443 if (nBlockTime == 0)
444 nBlockTime = GetAdjustedTime();
aa496b75 445 if ((int64)nLockTime < (nLockTime < LOCKTIME_THRESHOLD ? (int64)nBlockHeight : nBlockTime))
0a61b0df 446 return true;
223b6f1b 447 BOOST_FOREACH(const CTxIn& txin, vin)
0a61b0df 448 if (!txin.IsFinal())
449 return false;
450 return true;
451 }
452
453 bool IsNewerThan(const CTransaction& old) const
454 {
455 if (vin.size() != old.vin.size())
456 return false;
457 for (int i = 0; i < vin.size(); i++)
458 if (vin[i].prevout != old.vin[i].prevout)
459 return false;
460
461 bool fNewer = false;
462 unsigned int nLowest = UINT_MAX;
463 for (int i = 0; i < vin.size(); i++)
464 {
465 if (vin[i].nSequence != old.vin[i].nSequence)
466 {
467 if (vin[i].nSequence <= nLowest)
468 {
469 fNewer = false;
470 nLowest = vin[i].nSequence;
471 }
472 if (old.vin[i].nSequence < nLowest)
473 {
474 fNewer = true;
475 nLowest = old.vin[i].nSequence;
476 }
477 }
478 }
479 return fNewer;
480 }
481
482 bool IsCoinBase() const
483 {
484 return (vin.size() == 1 && vin[0].prevout.IsNull());
485 }
486
f1e1fb4b 487 int GetSigOpCount() const
488 {
489 int n = 0;
223b6f1b 490 BOOST_FOREACH(const CTxIn& txin, vin)
f1e1fb4b 491 n += txin.scriptSig.GetSigOpCount();
223b6f1b 492 BOOST_FOREACH(const CTxOut& txout, vout)
f1e1fb4b 493 n += txout.scriptPubKey.GetSigOpCount();
494 return n;
495 }
496
a206a239 497 bool IsStandard() const
498 {
223b6f1b 499 BOOST_FOREACH(const CTxIn& txin, vin)
a206a239 500 if (!txin.scriptSig.IsPushOnly())
501 return error("nonstandard txin: %s", txin.scriptSig.ToString().c_str());
223b6f1b 502 BOOST_FOREACH(const CTxOut& txout, vout)
a206a239 503 if (!::IsStandard(txout.scriptPubKey))
504 return error("nonstandard txout: %s", txout.scriptPubKey.ToString().c_str());
505 return true;
506 }
507
0a61b0df 508 int64 GetValueOut() const
509 {
510 int64 nValueOut = 0;
223b6f1b 511 BOOST_FOREACH(const CTxOut& txout, vout)
0a61b0df 512 {
513 nValueOut += txout.nValue;
514 if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
223b6f1b 515 throw std::runtime_error("CTransaction::GetValueOut() : value out of range");
0a61b0df 516 }
517 return nValueOut;
518 }
519
395c1f44
GA
520 static bool AllowFree(double dPriority)
521 {
522 // Large (in bytes) low-priority (new, small-coin) transactions
523 // need a fee.
524 return dPriority > COIN * 144 / 250;
525 }
526
2bfda1be 527 int64 GetMinFee(unsigned int nBlockSize=1, bool fAllowFree=true, bool fForRelay=false) const
0a61b0df 528 {
2bfda1be
PW
529 // Base fee is either MIN_TX_FEE or MIN_RELAY_TX_FEE
530 int64 nBaseFee = fForRelay ? MIN_RELAY_TX_FEE : MIN_TX_FEE;
531
0a61b0df 532 unsigned int nBytes = ::GetSerializeSize(*this, SER_NETWORK);
9b8eb4d6 533 unsigned int nNewBlockSize = nBlockSize + nBytes;
2bfda1be 534 int64 nMinFee = (1 + (int64)nBytes / 1000) * nBaseFee;
0a61b0df 535
f35e21e2 536 if (fAllowFree)
537 {
298a7714 538 if (nBlockSize == 1)
539 {
540 // Transactions under 10K are free
541 // (about 4500bc if made of 50bc inputs)
542 if (nBytes < 10000)
543 nMinFee = 0;
544 }
545 else
546 {
547 // Free transaction area
548 if (nNewBlockSize < 27000)
549 nMinFee = 0;
550 }
f35e21e2 551 }
0a61b0df 552
2bfda1be
PW
553 // To limit dust spam, require MIN_TX_FEE/MIN_RELAY_TX_FEE if any output is less than 0.01
554 if (nMinFee < nBaseFee)
223b6f1b 555 BOOST_FOREACH(const CTxOut& txout, vout)
0a61b0df 556 if (txout.nValue < CENT)
2bfda1be 557 nMinFee = nBaseFee;
0a61b0df 558
f1e1fb4b 559 // Raise the price as the block approaches full
9b8eb4d6 560 if (nBlockSize != 1 && nNewBlockSize >= MAX_BLOCK_SIZE_GEN/2)
561 {
562 if (nNewBlockSize >= MAX_BLOCK_SIZE_GEN)
563 return MAX_MONEY;
564 nMinFee *= MAX_BLOCK_SIZE_GEN / (MAX_BLOCK_SIZE_GEN - nNewBlockSize);
565 }
566
f1e1fb4b 567 if (!MoneyRange(nMinFee))
568 nMinFee = MAX_MONEY;
0a61b0df 569 return nMinFee;
570 }
571
572
0a61b0df 573 bool ReadFromDisk(CDiskTxPos pos, FILE** pfileRet=NULL)
574 {
575 CAutoFile filein = OpenBlockFile(pos.nFile, 0, pfileRet ? "rb+" : "rb");
576 if (!filein)
577 return error("CTransaction::ReadFromDisk() : OpenBlockFile failed");
578
579 // Read transaction
580 if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
581 return error("CTransaction::ReadFromDisk() : fseek failed");
582 filein >> *this;
583
584 // Return file pointer
585 if (pfileRet)
586 {
587 if (fseek(filein, pos.nTxPos, SEEK_SET) != 0)
588 return error("CTransaction::ReadFromDisk() : second fseek failed");
589 *pfileRet = filein.release();
590 }
591 return true;
592 }
593
0a61b0df 594 friend bool operator==(const CTransaction& a, const CTransaction& b)
595 {
596 return (a.nVersion == b.nVersion &&
597 a.vin == b.vin &&
598 a.vout == b.vout &&
599 a.nLockTime == b.nLockTime);
600 }
601
602 friend bool operator!=(const CTransaction& a, const CTransaction& b)
603 {
604 return !(a == b);
605 }
606
607
223b6f1b 608 std::string ToString() const
0a61b0df 609 {
223b6f1b 610 std::string str;
0a61b0df 611 str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%d, vout.size=%d, nLockTime=%d)\n",
b22c8842 612 GetHash().ToString().substr(0,10).c_str(),
0a61b0df 613 nVersion,
614 vin.size(),
615 vout.size(),
616 nLockTime);
617 for (int i = 0; i < vin.size(); i++)
618 str += " " + vin[i].ToString() + "\n";
619 for (int i = 0; i < vout.size(); i++)
620 str += " " + vout[i].ToString() + "\n";
621 return str;
622 }
623
624 void print() const
625 {
626 printf("%s", ToString().c_str());
627 }
628
629
461764cb 630 bool ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet);
631 bool ReadFromDisk(CTxDB& txdb, COutPoint prevout);
632 bool ReadFromDisk(COutPoint prevout);
0a61b0df 633 bool DisconnectInputs(CTxDB& txdb);
223b6f1b 634 bool ConnectInputs(CTxDB& txdb, std::map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx,
0a61b0df 635 CBlockIndex* pindexBlock, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee=0);
636 bool ClientConnectInputs();
a790fa46 637 bool CheckTransaction() const;
f1e1fb4b 638 bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL);
e89b9f6a 639 bool AcceptToMemoryPool(bool fCheckInputs=true, bool* pfMissingInputs=NULL);
0a61b0df 640protected:
f1e1fb4b 641 bool AddToMemoryPoolUnchecked();
0a61b0df 642public:
643 bool RemoveFromMemoryPool();
644};
645
646
647
648
649
650//
651// A transaction with a merkle branch linking it to the block chain
652//
653class CMerkleTx : public CTransaction
654{
655public:
656 uint256 hashBlock;
223b6f1b 657 std::vector<uint256> vMerkleBranch;
0a61b0df 658 int nIndex;
659
660 // memory only
a790fa46 661 mutable char fMerkleVerified;
0a61b0df 662
663
664 CMerkleTx()
665 {
666 Init();
667 }
668
669 CMerkleTx(const CTransaction& txIn) : CTransaction(txIn)
670 {
671 Init();
672 }
673
674 void Init()
675 {
676 hashBlock = 0;
677 nIndex = -1;
678 fMerkleVerified = false;
0a61b0df 679 }
680
335e878b 681
0a61b0df 682 IMPLEMENT_SERIALIZE
683 (
684 nSerSize += SerReadWrite(s, *(CTransaction*)this, nType, nVersion, ser_action);
685 nVersion = this->nVersion;
686 READWRITE(hashBlock);
687 READWRITE(vMerkleBranch);
688 READWRITE(nIndex);
689 )
690
0a61b0df 691
692 int SetMerkleBranch(const CBlock* pblock=NULL);
693 int GetDepthInMainChain(int& nHeightRet) const;
694 int GetDepthInMainChain() const { int nHeight; return GetDepthInMainChain(nHeight); }
695 bool IsInMainChain() const { return GetDepthInMainChain() > 0; }
696 int GetBlocksToMaturity() const;
f1e1fb4b 697 bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true);
e89b9f6a 698 bool AcceptToMemoryPool();
0a61b0df 699};
700
701
702
703
704//
705// A txdb record that contains the disk location of a transaction and the
706// locations of transactions that spend its outputs. vSpent is really only
707// used as a flag, but having the location is very helpful for debugging.
708//
709class CTxIndex
710{
711public:
712 CDiskTxPos pos;
223b6f1b 713 std::vector<CDiskTxPos> vSpent;
0a61b0df 714
715 CTxIndex()
716 {
717 SetNull();
718 }
719
720 CTxIndex(const CDiskTxPos& posIn, unsigned int nOutputs)
721 {
722 pos = posIn;
723 vSpent.resize(nOutputs);
724 }
725
726 IMPLEMENT_SERIALIZE
727 (
728 if (!(nType & SER_GETHASH))
729 READWRITE(nVersion);
730 READWRITE(pos);
731 READWRITE(vSpent);
732 )
733
734 void SetNull()
735 {
736 pos.SetNull();
737 vSpent.clear();
738 }
739
740 bool IsNull()
741 {
742 return pos.IsNull();
743 }
744
745 friend bool operator==(const CTxIndex& a, const CTxIndex& b)
746 {
f1e1fb4b 747 return (a.pos == b.pos &&
748 a.vSpent == b.vSpent);
0a61b0df 749 }
750
751 friend bool operator!=(const CTxIndex& a, const CTxIndex& b)
752 {
753 return !(a == b);
754 }
395c1f44 755 int GetDepthInMainChain() const;
0a61b0df 756};
757
758
759
760
761
762//
763// Nodes collect new transactions into a block, hash them into a hash tree,
764// and scan through nonce values to make the block's hash satisfy proof-of-work
765// requirements. When they solve the proof-of-work, they broadcast the block
766// to everyone and the block is added to the block chain. The first transaction
767// in the block is a special one that creates a new coin owned by the creator
768// of the block.
769//
770// Blocks are appended to blk0001.dat files on disk. Their location on disk
771// is indexed by CBlockIndex objects in memory.
772//
773class CBlock
774{
775public:
776 // header
777 int nVersion;
778 uint256 hashPrevBlock;
779 uint256 hashMerkleRoot;
780 unsigned int nTime;
781 unsigned int nBits;
782 unsigned int nNonce;
783
784 // network and disk
223b6f1b 785 std::vector<CTransaction> vtx;
0a61b0df 786
787 // memory only
223b6f1b 788 mutable std::vector<uint256> vMerkleTree;
0a61b0df 789
790
791 CBlock()
792 {
793 SetNull();
794 }
795
796 IMPLEMENT_SERIALIZE
797 (
798 READWRITE(this->nVersion);
799 nVersion = this->nVersion;
800 READWRITE(hashPrevBlock);
801 READWRITE(hashMerkleRoot);
802 READWRITE(nTime);
803 READWRITE(nBits);
804 READWRITE(nNonce);
805
806 // ConnectBlock depends on vtx being last so it can calculate offset
807 if (!(nType & (SER_GETHASH|SER_BLOCKHEADERONLY)))
808 READWRITE(vtx);
809 else if (fRead)
810 const_cast<CBlock*>(this)->vtx.clear();
811 )
812
813 void SetNull()
814 {
815 nVersion = 1;
816 hashPrevBlock = 0;
817 hashMerkleRoot = 0;
818 nTime = 0;
819 nBits = 0;
820 nNonce = 0;
821 vtx.clear();
822 vMerkleTree.clear();
823 }
824
825 bool IsNull() const
826 {
827 return (nBits == 0);
828 }
829
830 uint256 GetHash() const
831 {
832 return Hash(BEGIN(nVersion), END(nNonce));
833 }
834
835 int64 GetBlockTime() const
836 {
837 return (int64)nTime;
838 }
839
f1e1fb4b 840 int GetSigOpCount() const
841 {
842 int n = 0;
223b6f1b 843 BOOST_FOREACH(const CTransaction& tx, vtx)
f1e1fb4b 844 n += tx.GetSigOpCount();
845 return n;
846 }
847
0a61b0df 848
849 uint256 BuildMerkleTree() const
850 {
851 vMerkleTree.clear();
223b6f1b 852 BOOST_FOREACH(const CTransaction& tx, vtx)
0a61b0df 853 vMerkleTree.push_back(tx.GetHash());
854 int j = 0;
855 for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
856 {
857 for (int i = 0; i < nSize; i += 2)
858 {
223b6f1b 859 int i2 = std::min(i+1, nSize-1);
0a61b0df 860 vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
861 BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
862 }
863 j += nSize;
864 }
865 return (vMerkleTree.empty() ? 0 : vMerkleTree.back());
866 }
867
223b6f1b 868 std::vector<uint256> GetMerkleBranch(int nIndex) const
0a61b0df 869 {
870 if (vMerkleTree.empty())
871 BuildMerkleTree();
223b6f1b 872 std::vector<uint256> vMerkleBranch;
0a61b0df 873 int j = 0;
874 for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
875 {
223b6f1b 876 int i = std::min(nIndex^1, nSize-1);
0a61b0df 877 vMerkleBranch.push_back(vMerkleTree[j+i]);
878 nIndex >>= 1;
879 j += nSize;
880 }
881 return vMerkleBranch;
882 }
883
223b6f1b 884 static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
0a61b0df 885 {
886 if (nIndex == -1)
887 return 0;
223b6f1b 888 BOOST_FOREACH(const uint256& otherside, vMerkleBranch)
0a61b0df 889 {
890 if (nIndex & 1)
891 hash = Hash(BEGIN(otherside), END(otherside), BEGIN(hash), END(hash));
892 else
893 hash = Hash(BEGIN(hash), END(hash), BEGIN(otherside), END(otherside));
894 nIndex >>= 1;
895 }
896 return hash;
897 }
898
899
f03304a9 900 bool WriteToDisk(unsigned int& nFileRet, unsigned int& nBlockPosRet)
0a61b0df 901 {
902 // Open history file to append
903 CAutoFile fileout = AppendBlockFile(nFileRet);
904 if (!fileout)
905 return error("CBlock::WriteToDisk() : AppendBlockFile failed");
0a61b0df 906
907 // Write index header
908 unsigned int nSize = fileout.GetSerializeSize(*this);
909 fileout << FLATDATA(pchMessageStart) << nSize;
910
911 // Write block
912 nBlockPosRet = ftell(fileout);
913 if (nBlockPosRet == -1)
914 return error("CBlock::WriteToDisk() : ftell failed");
915 fileout << *this;
916
917 // Flush stdio buffers and commit to disk before returning
918 fflush(fileout);
919 if (!IsInitialBlockDownload() || (nBestHeight+1) % 500 == 0)
920 {
921#ifdef __WXMSW__
922 _commit(_fileno(fileout));
923#else
924 fsync(fileno(fileout));
925#endif
926 }
927
928 return true;
929 }
930
931 bool ReadFromDisk(unsigned int nFile, unsigned int nBlockPos, bool fReadTransactions=true)
932 {
933 SetNull();
934
935 // Open history file to read
936 CAutoFile filein = OpenBlockFile(nFile, nBlockPos, "rb");
937 if (!filein)
938 return error("CBlock::ReadFromDisk() : OpenBlockFile failed");
939 if (!fReadTransactions)
940 filein.nType |= SER_BLOCKHEADERONLY;
941
942 // Read block
943 filein >> *this;
944
945 // Check the header
946 if (!CheckProofOfWork(GetHash(), nBits))
947 return error("CBlock::ReadFromDisk() : errors in block header");
948
949 return true;
950 }
951
952
953
954 void print() const
955 {
956 printf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%d)\n",
957 GetHash().ToString().substr(0,20).c_str(),
958 nVersion,
959 hashPrevBlock.ToString().substr(0,20).c_str(),
b22c8842 960 hashMerkleRoot.ToString().substr(0,10).c_str(),
0a61b0df 961 nTime, nBits, nNonce,
962 vtx.size());
963 for (int i = 0; i < vtx.size(); i++)
964 {
965 printf(" ");
966 vtx[i].print();
967 }
968 printf(" vMerkleTree: ");
969 for (int i = 0; i < vMerkleTree.size(); i++)
b22c8842 970 printf("%s ", vMerkleTree[i].ToString().substr(0,10).c_str());
0a61b0df 971 printf("\n");
972 }
973
974
0a61b0df 975 bool DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex);
976 bool ConnectBlock(CTxDB& txdb, CBlockIndex* pindex);
977 bool ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions=true);
978 bool SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew);
979 bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos);
980 bool CheckBlock() const;
981 bool AcceptBlock();
982};
983
984
985
986
987
988
989//
990// The block chain is a tree shaped structure starting with the
991// genesis block at the root, with each block potentially having multiple
992// candidates to be the next block. pprev and pnext link a path through the
993// main/longest chain. A blockindex may have multiple pprev pointing back
994// to it, but pnext will only point forward to the longest branch, or will
995// be null if the block is not part of the longest chain.
996//
997class CBlockIndex
998{
999public:
1000 const uint256* phashBlock;
1001 CBlockIndex* pprev;
1002 CBlockIndex* pnext;
1003 unsigned int nFile;
1004 unsigned int nBlockPos;
1005 int nHeight;
1006 CBigNum bnChainWork;
1007
1008 // block header
1009 int nVersion;
1010 uint256 hashMerkleRoot;
1011 unsigned int nTime;
1012 unsigned int nBits;
1013 unsigned int nNonce;
1014
1015
1016 CBlockIndex()
1017 {
1018 phashBlock = NULL;
1019 pprev = NULL;
1020 pnext = NULL;
1021 nFile = 0;
1022 nBlockPos = 0;
1023 nHeight = 0;
1024 bnChainWork = 0;
1025
1026 nVersion = 0;
1027 hashMerkleRoot = 0;
1028 nTime = 0;
1029 nBits = 0;
1030 nNonce = 0;
1031 }
1032
1033 CBlockIndex(unsigned int nFileIn, unsigned int nBlockPosIn, CBlock& block)
1034 {
1035 phashBlock = NULL;
1036 pprev = NULL;
1037 pnext = NULL;
1038 nFile = nFileIn;
1039 nBlockPos = nBlockPosIn;
1040 nHeight = 0;
1041 bnChainWork = 0;
1042
1043 nVersion = block.nVersion;
1044 hashMerkleRoot = block.hashMerkleRoot;
1045 nTime = block.nTime;
1046 nBits = block.nBits;
1047 nNonce = block.nNonce;
1048 }
1049
f03304a9 1050 CBlock GetBlockHeader() const
1051 {
1052 CBlock block;
1053 block.nVersion = nVersion;
1054 if (pprev)
1055 block.hashPrevBlock = pprev->GetBlockHash();
1056 block.hashMerkleRoot = hashMerkleRoot;
1057 block.nTime = nTime;
1058 block.nBits = nBits;
1059 block.nNonce = nNonce;
1060 return block;
1061 }
1062
0a61b0df 1063 uint256 GetBlockHash() const
1064 {
1065 return *phashBlock;
1066 }
1067
1068 int64 GetBlockTime() const
1069 {
1070 return (int64)nTime;
1071 }
1072
1073 CBigNum GetBlockWork() const
1074 {
9b8eb4d6 1075 CBigNum bnTarget;
1076 bnTarget.SetCompact(nBits);
1077 if (bnTarget <= 0)
0a61b0df 1078 return 0;
9b8eb4d6 1079 return (CBigNum(1)<<256) / (bnTarget+1);
0a61b0df 1080 }
1081
1082 bool IsInMainChain() const
1083 {
1084 return (pnext || this == pindexBest);
1085 }
1086
1087 bool CheckIndex() const
1088 {
1089 return CheckProofOfWork(GetBlockHash(), nBits);
1090 }
1091
1092 bool EraseBlockFromDisk()
1093 {
1094 // Open history file
1095 CAutoFile fileout = OpenBlockFile(nFile, nBlockPos, "rb+");
1096 if (!fileout)
1097 return false;
1098
1099 // Overwrite with empty null block
1100 CBlock block;
1101 block.SetNull();
1102 fileout << block;
1103
1104 return true;
1105 }
1106
1107 enum { nMedianTimeSpan=11 };
1108
1109 int64 GetMedianTimePast() const
1110 {
1111 int64 pmedian[nMedianTimeSpan];
1112 int64* pbegin = &pmedian[nMedianTimeSpan];
1113 int64* pend = &pmedian[nMedianTimeSpan];
1114
1115 const CBlockIndex* pindex = this;
1116 for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
1117 *(--pbegin) = pindex->GetBlockTime();
1118
223b6f1b 1119 std::sort(pbegin, pend);
0a61b0df 1120 return pbegin[(pend - pbegin)/2];
1121 }
1122
1123 int64 GetMedianTime() const
1124 {
1125 const CBlockIndex* pindex = this;
1126 for (int i = 0; i < nMedianTimeSpan/2; i++)
1127 {
1128 if (!pindex->pnext)
1129 return GetBlockTime();
1130 pindex = pindex->pnext;
1131 }
1132 return pindex->GetMedianTimePast();
1133 }
1134
1135
1136
223b6f1b 1137 std::string ToString() const
0a61b0df 1138 {
1139 return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nHeight=%d, merkle=%s, hashBlock=%s)",
1140 pprev, pnext, nFile, nBlockPos, nHeight,
b22c8842 1141 hashMerkleRoot.ToString().substr(0,10).c_str(),
0a61b0df 1142 GetBlockHash().ToString().substr(0,20).c_str());
1143 }
1144
1145 void print() const
1146 {
1147 printf("%s\n", ToString().c_str());
1148 }
1149};
1150
1151
1152
1153//
1154// Used to marshal pointers into hashes for db storage.
1155//
1156class CDiskBlockIndex : public CBlockIndex
1157{
1158public:
1159 uint256 hashPrev;
1160 uint256 hashNext;
1161
1162 CDiskBlockIndex()
1163 {
1164 hashPrev = 0;
1165 hashNext = 0;
1166 }
1167
1168 explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex)
1169 {
1170 hashPrev = (pprev ? pprev->GetBlockHash() : 0);
1171 hashNext = (pnext ? pnext->GetBlockHash() : 0);
1172 }
1173
1174 IMPLEMENT_SERIALIZE
1175 (
1176 if (!(nType & SER_GETHASH))
1177 READWRITE(nVersion);
1178
1179 READWRITE(hashNext);
1180 READWRITE(nFile);
1181 READWRITE(nBlockPos);
1182 READWRITE(nHeight);
1183
1184 // block header
1185 READWRITE(this->nVersion);
1186 READWRITE(hashPrev);
1187 READWRITE(hashMerkleRoot);
1188 READWRITE(nTime);
1189 READWRITE(nBits);
1190 READWRITE(nNonce);
1191 )
1192
1193 uint256 GetBlockHash() const
1194 {
1195 CBlock block;
1196 block.nVersion = nVersion;
1197 block.hashPrevBlock = hashPrev;
1198 block.hashMerkleRoot = hashMerkleRoot;
1199 block.nTime = nTime;
1200 block.nBits = nBits;
1201 block.nNonce = nNonce;
1202 return block.GetHash();
1203 }
1204
1205
223b6f1b 1206 std::string ToString() const
0a61b0df 1207 {
223b6f1b 1208 std::string str = "CDiskBlockIndex(";
0a61b0df 1209 str += CBlockIndex::ToString();
1210 str += strprintf("\n hashBlock=%s, hashPrev=%s, hashNext=%s)",
1211 GetBlockHash().ToString().c_str(),
1212 hashPrev.ToString().substr(0,20).c_str(),
1213 hashNext.ToString().substr(0,20).c_str());
1214 return str;
1215 }
1216
1217 void print() const
1218 {
1219 printf("%s\n", ToString().c_str());
1220 }
1221};
1222
1223
1224
1225
1226
1227
1228
1229
1230//
1231// Describes a place in the block chain to another node such that if the
1232// other node doesn't have the same branch, it can find a recent common trunk.
1233// The further back it is, the further before the fork it may be.
1234//
1235class CBlockLocator
1236{
1237protected:
223b6f1b 1238 std::vector<uint256> vHave;
0a61b0df 1239public:
1240
1241 CBlockLocator()
1242 {
1243 }
1244
1245 explicit CBlockLocator(const CBlockIndex* pindex)
1246 {
1247 Set(pindex);
1248 }
1249
1250 explicit CBlockLocator(uint256 hashBlock)
1251 {
223b6f1b 1252 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
0a61b0df 1253 if (mi != mapBlockIndex.end())
1254 Set((*mi).second);
1255 }
1256
1257 IMPLEMENT_SERIALIZE
1258 (
1259 if (!(nType & SER_GETHASH))
1260 READWRITE(nVersion);
1261 READWRITE(vHave);
1262 )
1263
f03304a9 1264 void SetNull()
1265 {
1266 vHave.clear();
1267 }
1268
1269 bool IsNull()
1270 {
1271 return vHave.empty();
1272 }
1273
0a61b0df 1274 void Set(const CBlockIndex* pindex)
1275 {
1276 vHave.clear();
1277 int nStep = 1;
1278 while (pindex)
1279 {
1280 vHave.push_back(pindex->GetBlockHash());
1281
1282 // Exponentially larger steps back
1283 for (int i = 0; pindex && i < nStep; i++)
1284 pindex = pindex->pprev;
1285 if (vHave.size() > 10)
1286 nStep *= 2;
1287 }
1288 vHave.push_back(hashGenesisBlock);
1289 }
1290
1291 int GetDistanceBack()
1292 {
1293 // Retrace how far back it was in the sender's branch
1294 int nDistance = 0;
1295 int nStep = 1;
223b6f1b 1296 BOOST_FOREACH(const uint256& hash, vHave)
0a61b0df 1297 {
223b6f1b 1298 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
0a61b0df 1299 if (mi != mapBlockIndex.end())
1300 {
1301 CBlockIndex* pindex = (*mi).second;
1302 if (pindex->IsInMainChain())
1303 return nDistance;
1304 }
1305 nDistance += nStep;
1306 if (nDistance > 10)
1307 nStep *= 2;
1308 }
1309 return nDistance;
1310 }
1311
1312 CBlockIndex* GetBlockIndex()
1313 {
1314 // Find the first block the caller has in the main chain
223b6f1b 1315 BOOST_FOREACH(const uint256& hash, vHave)
0a61b0df 1316 {
223b6f1b 1317 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
0a61b0df 1318 if (mi != mapBlockIndex.end())
1319 {
1320 CBlockIndex* pindex = (*mi).second;
1321 if (pindex->IsInMainChain())
1322 return pindex;
1323 }
1324 }
1325 return pindexGenesisBlock;
1326 }
1327
1328 uint256 GetBlockHash()
1329 {
1330 // Find the first block the caller has in the main chain
223b6f1b 1331 BOOST_FOREACH(const uint256& hash, vHave)
0a61b0df 1332 {
223b6f1b 1333 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
0a61b0df 1334 if (mi != mapBlockIndex.end())
1335 {
1336 CBlockIndex* pindex = (*mi).second;
1337 if (pindex->IsInMainChain())
1338 return hash;
1339 }
1340 }
1341 return hashGenesisBlock;
1342 }
1343
1344 int GetHeight()
1345 {
1346 CBlockIndex* pindex = GetBlockIndex();
1347 if (!pindex)
1348 return 0;
1349 return pindex->nHeight;
1350 }
1351};
1352
1353
1354
1355
1356
1357
e4ff4e68 1358
1359
1360
0a61b0df 1361//
629e37dd 1362// Alerts are for notifying old versions if they become too obsolete and
1363// need to upgrade. The message is displayed in the status bar.
0a61b0df 1364// Alert messages are broadcast as a vector of signed data. Unserializing may
1365// not read the entire buffer if the alert is for a newer version, but older
1366// versions can still relay the original data.
1367//
1368class CUnsignedAlert
1369{
1370public:
1371 int nVersion;
1372 int64 nRelayUntil; // when newer nodes stop relaying to newer nodes
1373 int64 nExpiration;
1374 int nID;
1375 int nCancel;
223b6f1b 1376 std::set<int> setCancel;
0a61b0df 1377 int nMinVer; // lowest version inclusive
1378 int nMaxVer; // highest version inclusive
223b6f1b 1379 std::set<std::string> setSubVer; // empty matches all
0a61b0df 1380 int nPriority;
1381
1382 // Actions
223b6f1b
WL
1383 std::string strComment;
1384 std::string strStatusBar;
1385 std::string strReserved;
0a61b0df 1386
1387 IMPLEMENT_SERIALIZE
1388 (
1389 READWRITE(this->nVersion);
1390 nVersion = this->nVersion;
1391 READWRITE(nRelayUntil);
1392 READWRITE(nExpiration);
1393 READWRITE(nID);
1394 READWRITE(nCancel);
1395 READWRITE(setCancel);
1396 READWRITE(nMinVer);
1397 READWRITE(nMaxVer);
1398 READWRITE(setSubVer);
1399 READWRITE(nPriority);
1400
1401 READWRITE(strComment);
1402 READWRITE(strStatusBar);
986b5e25 1403 READWRITE(strReserved);
0a61b0df 1404 )
1405
1406 void SetNull()
1407 {
1408 nVersion = 1;
1409 nRelayUntil = 0;
1410 nExpiration = 0;
1411 nID = 0;
1412 nCancel = 0;
1413 setCancel.clear();
1414 nMinVer = 0;
1415 nMaxVer = 0;
1416 setSubVer.clear();
1417 nPriority = 0;
1418
1419 strComment.clear();
1420 strStatusBar.clear();
986b5e25 1421 strReserved.clear();
0a61b0df 1422 }
1423
223b6f1b 1424 std::string ToString() const
0a61b0df 1425 {
223b6f1b
WL
1426 std::string strSetCancel;
1427 BOOST_FOREACH(int n, setCancel)
0a61b0df 1428 strSetCancel += strprintf("%d ", n);
223b6f1b
WL
1429 std::string strSetSubVer;
1430 BOOST_FOREACH(std::string str, setSubVer)
0a61b0df 1431 strSetSubVer += "\"" + str + "\" ";
1432 return strprintf(
1433 "CAlert(\n"
1434 " nVersion = %d\n"
1435 " nRelayUntil = %"PRI64d"\n"
1436 " nExpiration = %"PRI64d"\n"
1437 " nID = %d\n"
1438 " nCancel = %d\n"
1439 " setCancel = %s\n"
1440 " nMinVer = %d\n"
1441 " nMaxVer = %d\n"
1442 " setSubVer = %s\n"
1443 " nPriority = %d\n"
1444 " strComment = \"%s\"\n"
1445 " strStatusBar = \"%s\"\n"
0a61b0df 1446 ")\n",
1447 nVersion,
1448 nRelayUntil,
1449 nExpiration,
1450 nID,
1451 nCancel,
1452 strSetCancel.c_str(),
1453 nMinVer,
1454 nMaxVer,
1455 strSetSubVer.c_str(),
1456 nPriority,
1457 strComment.c_str(),
986b5e25 1458 strStatusBar.c_str());
0a61b0df 1459 }
1460
1461 void print() const
1462 {
1463 printf("%s", ToString().c_str());
1464 }
1465};
1466
1467class CAlert : public CUnsignedAlert
1468{
1469public:
223b6f1b
WL
1470 std::vector<unsigned char> vchMsg;
1471 std::vector<unsigned char> vchSig;
0a61b0df 1472
1473 CAlert()
1474 {
1475 SetNull();
1476 }
1477
1478 IMPLEMENT_SERIALIZE
1479 (
1480 READWRITE(vchMsg);
1481 READWRITE(vchSig);
1482 )
1483
1484 void SetNull()
1485 {
1486 CUnsignedAlert::SetNull();
1487 vchMsg.clear();
1488 vchSig.clear();
1489 }
1490
1491 bool IsNull() const
1492 {
1493 return (nExpiration == 0);
1494 }
1495
1496 uint256 GetHash() const
1497 {
1498 return SerializeHash(*this);
1499 }
1500
1501 bool IsInEffect() const
1502 {
1503 return (GetAdjustedTime() < nExpiration);
1504 }
1505
1506 bool Cancels(const CAlert& alert) const
1507 {
1508 if (!IsInEffect())
461764cb 1509 return false; // this was a no-op before 31403
0a61b0df 1510 return (alert.nID <= nCancel || setCancel.count(alert.nID));
1511 }
1512
223b6f1b 1513 bool AppliesTo(int nVersion, std::string strSubVerIn) const
0a61b0df 1514 {
1515 return (IsInEffect() &&
1516 nMinVer <= nVersion && nVersion <= nMaxVer &&
1517 (setSubVer.empty() || setSubVer.count(strSubVerIn)));
1518 }
1519
1520 bool AppliesToMe() const
1521 {
1522 return AppliesTo(VERSION, ::pszSubVer);
1523 }
1524
1525 bool RelayTo(CNode* pnode) const
1526 {
1527 if (!IsInEffect())
1528 return false;
1529 // returns true if wasn't already contained in the set
1530 if (pnode->setKnown.insert(GetHash()).second)
1531 {
1532 if (AppliesTo(pnode->nVersion, pnode->strSubVer) ||
1533 AppliesToMe() ||
1534 GetAdjustedTime() < nRelayUntil)
1535 {
1536 pnode->PushMessage("alert", *this);
1537 return true;
1538 }
1539 }
1540 return false;
1541 }
1542
1543 bool CheckSignature()
1544 {
1545 CKey key;
1546 if (!key.SetPubKey(ParseHex("04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284")))
1547 return error("CAlert::CheckSignature() : SetPubKey failed");
1548 if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
1549 return error("CAlert::CheckSignature() : verify signature failed");
1550
1551 // Now unserialize the data
1552 CDataStream sMsg(vchMsg);
1553 sMsg >> *(CUnsignedAlert*)this;
1554 return true;
1555 }
1556
1557 bool ProcessAlert();
1558};
1559
223b6f1b 1560#endif
This page took 0.267144 seconds and 4 git commands to generate.