1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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 "timestamps.h"
16 #include "ui_interface.h"
40 static const unsigned int MAX_BLOCK_SIZE = 1000000;
41 static const unsigned int MAX_BLOCK_SIZE_GEN = MAX_BLOCK_SIZE/2;
42 static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
43 static const unsigned int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100;
44 static const unsigned int MAX_INV_SZ = 50000;
46 static const int64_t MIN_TX_FEE = CENT/10;
47 static const int64_t MIN_RELAY_TX_FEE = CENT/50;
49 static const int64_t MAX_MONEY = std::numeric_limits<int64_t>::max();
50 static const int64_t MAX_MINT_PROOF_OF_WORK = 100 * COIN;
51 static const int64_t MAX_MINT_PROOF_OF_STAKE = 1 * COIN;
52 static const int64_t MIN_TXOUT_AMOUNT = CENT/100;
55 inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
56 inline bool MoneyRange(CBigNum nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
57 // Maximum number of script-checking threads allowed
58 static const int MAX_SCRIPTCHECK_THREADS = 16;
60 static const uint256 hashGenesisBlock("0x00000a060336cbb72fe969666d337b87198b1add2abaa59cca226820b32933a4");
61 static const uint256 hashGenesisBlockTestNet("0x000c763e402f2436da9ed36c7286f62c3f6e5dbafce9ff289bd43d7459327eb");
63 inline int64_t PastDrift(int64_t nTime) { return nTime - 2 * nOneHour; } // up to 2 hours from the past
64 inline int64_t FutureDrift(int64_t nTime) { return nTime + 2 * nOneHour; } // up to 2 hours from the future
66 extern CScript COINBASE_FLAGS;
67 extern CCriticalSection cs_main;
68 extern std::map<uint256, CBlockIndex*> mapBlockIndex;
69 extern std::set<std::pair<COutPoint, unsigned int> > setStakeSeen;
70 extern CBlockIndex* pindexGenesisBlock;
71 extern unsigned int nNodeLifespan;
72 extern int nCoinbaseMaturity;
73 extern int nBestHeight;
74 extern uint256 nBestChainTrust;
75 extern uint256 nBestInvalidTrust;
76 extern uint256 hashBestChain;
77 extern CBlockIndex* pindexBest;
78 extern unsigned int nTransactionsUpdated;
79 extern uint64_t nLastBlockTx;
80 extern uint64_t nLastBlockSize;
81 extern uint32_t nLastCoinStakeSearchInterval;
82 extern const std::string strMessageMagic;
83 extern int64_t nTimeBestReceived;
84 extern CCriticalSection cs_setpwalletRegistered;
85 extern std::set<CWallet*> setpwalletRegistered;
86 extern uint32_t nNetworkID;
87 extern std::map<uint256, CBlock*> mapOrphanBlocks;
90 extern int64_t nTransactionFee;
91 extern int64_t nMinimumInputValue;
92 extern bool fUseFastIndex;
93 extern int nScriptCheckThreads;
94 extern const uint256 entropyStore[38];
96 // Minimum disk space required - used in CheckDiskSpace()
97 static const uint64_t nMinDiskSpace = 52428800;
99 void RegisterWallet(CWallet* pwalletIn);
100 void UnregisterWallet(CWallet* pwalletIn);
101 void SyncWithWallets(const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false, bool fConnect = true);
102 bool ProcessBlock(CNode* pfrom, CBlock* pblock);
103 bool CheckDiskSpace(uint64_t nAdditionalBytes=0);
104 FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode="rb");
105 FILE* AppendBlockFile(unsigned int& nFileRet);
107 void UnloadBlockIndex();
108 bool LoadBlockIndex(bool fAllowNew=true);
109 void PrintBlockTree();
110 CBlockIndex* FindBlockByHeight(int nHeight);
111 bool ProcessMessages(CNode* pfrom);
112 bool SendMessages(CNode* pto);
113 bool LoadExternalBlockFile(FILE* fileIn, CClientUIInterface& uiInterface);
115 // Run an instance of the script checking thread
116 void ThreadScriptCheck(void* parg);
117 // Stop the script checking threads
118 void ThreadScriptCheckQuit();
120 bool CheckProofOfWork(uint256 hash, unsigned int nBits);
121 unsigned int GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfStake);
122 int64_t GetProofOfWorkReward(unsigned int nBits, int64_t nFees=0);
123 int64_t GetProofOfStakeReward(int64_t nCoinAge, unsigned int nBits, int64_t nTime, bool bCoinYearOnly=false);
124 unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime);
125 unsigned int ComputeMinStake(unsigned int nBase, int64_t nTime, unsigned int nBlockTime);
126 int GetNumBlocksOfPeers();
127 bool IsInitialBlockDownload();
128 std::string GetWarnings(std::string strFor);
129 bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock);
130 uint256 WantedByOrphan(const CBlock* pblockOrphan);
131 const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake);
132 void ResendWalletTransactions(bool fForceResend=false);
134 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, uint32_t nIn, unsigned int flags, int nHashType);
139 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
141 // Position on disk for a particular transaction.
149 CDiskTxPos() { SetNull(); }
150 CDiskTxPos(unsigned int nFileIn, unsigned int nBlockPosIn, unsigned int nTxPosIn) : nFile(nFileIn), nBlockPos(nBlockPosIn), nTxPos(nTxPosIn) {}
152 IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
153 void SetNull() { nFile = std::numeric_limits<uint32_t>::max(); nBlockPos = 0; nTxPos = 0; }
154 bool IsNull() const { return (nFile == std::numeric_limits<uint32_t>::max()); }
156 friend bool operator==(const CDiskTxPos& a, const CDiskTxPos& b)
158 return (a.nFile == b.nFile &&
159 a.nBlockPos == b.nBlockPos &&
160 a.nTxPos == b.nTxPos);
163 friend bool operator!=(const CDiskTxPos& a, const CDiskTxPos& b)
169 std::string ToString() const
174 return strprintf("(nFile=%" PRIu32 ", nBlockPos=%" PRIu32 ", nTxPos=%" PRIu32 ")", nFile, nBlockPos, nTxPos);
179 printf("%s", ToString().c_str());
185 // An inpoint - a combination of a transaction and an index n into its vin
192 CInPoint() { SetNull(); }
193 CInPoint(CTransaction* ptxIn, uint32_t nIn) : ptx(ptxIn), n(nIn) {}
194 void SetNull() { ptx = NULL; n = std::numeric_limits<uint32_t>::max(); }
195 bool IsNull() const { return (ptx == NULL && n == std::numeric_limits<uint32_t>::max()); }
200 // An outpoint - a combination of a transaction hash and an index n into its vout
207 COutPoint() { SetNull(); }
208 COutPoint(uint256 hashIn, uint32_t nIn) : hash(hashIn), n(nIn) {}
209 IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
210 void SetNull() { hash = 0; n = std::numeric_limits<uint32_t>::max(); }
211 bool IsNull() const { return (hash == 0 && n == std::numeric_limits<uint32_t>::max()); }
213 friend bool operator<(const COutPoint& a, const COutPoint& b)
215 return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
218 friend bool operator==(const COutPoint& a, const COutPoint& b)
220 return (a.hash == b.hash && a.n == b.n);
223 friend bool operator!=(const COutPoint& a, const COutPoint& b)
228 std::string ToString() const
230 return strprintf("COutPoint(%s, %" PRIu32 ")", hash.ToString().substr(0,10).c_str(), n);
235 printf("%s\n", ToString().c_str());
241 // An input of a transaction. It contains the location of the previous
242 // transaction's output that it claims and a signature that matches the
243 // output's public key.
252 CTxIn() : nSequence(std::numeric_limits<unsigned int>::max()) {}
253 explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max())
254 : prevout(prevoutIn), scriptSig(scriptSigIn), nSequence(nSequenceIn) {}
255 CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max())
256 : prevout(COutPoint(hashPrevTx, nOut)), scriptSig(scriptSigIn), nSequence(nSequenceIn) {}
261 READWRITE(scriptSig);
262 READWRITE(nSequence);
267 return (nSequence == std::numeric_limits<unsigned int>::max());
270 friend bool operator==(const CTxIn& a, const CTxIn& b)
272 return (a.prevout == b.prevout &&
273 a.scriptSig == b.scriptSig &&
274 a.nSequence == b.nSequence);
277 friend bool operator!=(const CTxIn& a, const CTxIn& b)
282 std::string ToStringShort() const
284 return strprintf(" %s %d", prevout.hash.ToString().c_str(), prevout.n);
287 std::string ToString() const;
291 printf("%s\n", ToString().c_str());
297 // An output of a transaction. It contains the public key that the next input
298 // must be able to sign with to claim it.
304 CScript scriptPubKey;
306 CTxOut() { SetNull(); }
307 CTxOut(int64_t nValueIn, CScript scriptPubKeyIn) : nValue(nValueIn), scriptPubKey(scriptPubKeyIn) {}
312 READWRITE(scriptPubKey);
318 scriptPubKey.clear();
323 return (nValue == -1);
329 scriptPubKey.clear();
334 return (nValue == 0 && scriptPubKey.empty());
337 uint256 GetHash() const
339 return SerializeHash(*this);
342 friend bool operator==(const CTxOut& a, const CTxOut& b)
344 return (a.nValue == b.nValue &&
345 a.scriptPubKey == b.scriptPubKey);
348 friend bool operator!=(const CTxOut& a, const CTxOut& b)
353 std::string ToStringShort() const
355 return strprintf(" out %s %s", FormatMoney(nValue).c_str(), scriptPubKey.ToString(true).c_str());
358 std::string ToString() const
360 if (IsEmpty()) return "CTxOut(empty)";
361 if (scriptPubKey.size() < 6)
362 return "CTxOut(error)";
363 return strprintf("CTxOut(nValue=%s, scriptPubKey=%s)", FormatMoney(nValue).c_str(), scriptPubKey.ToString().c_str());
368 printf("%s\n", ToString().c_str());
382 typedef std::map<uint256, std::pair<CTxIndex, CTransaction> > MapPrevTx;
384 /** The basic transaction that is broadcasted on the network and contained in
385 * blocks. A transaction can contain multiple inputs and outputs.
390 static const int CURRENT_VERSION=1;
393 std::vector<CTxIn> vin;
394 std::vector<CTxOut> vout;
397 // Denial-of-service detection:
399 bool DoS(int nDoSIn, bool fIn) const { nDoS += nDoSIn; return fIn; }
408 READWRITE(this->nVersion);
409 nVersion = this->nVersion;
413 READWRITE(nLockTime);
418 nVersion = CTransaction::CURRENT_VERSION;
419 nTime = (uint32_t) GetAdjustedTime();
423 nDoS = 0; // Denial-of-service prevention
428 return (vin.empty() && vout.empty());
431 uint256 GetHash() const
433 return SerializeHash(*this);
436 bool IsFinal(int nBlockHeight=0, int64_t nBlockTime=0) const;
438 bool IsCoinBase() const
440 return (vin.size() == 1 && vin[0].prevout.IsNull() && vout.size() >= 1);
443 bool IsCoinStake() const
445 // ppcoin: the coin stake transaction is marked with the first output empty
446 return (vin.size() > 0 && (!vin[0].prevout.IsNull()) && vout.size() >= 2 && vout[0].IsEmpty());
449 /** Check for standard transaction types
450 @return True if all outputs (scriptPubKeys) use only standard transaction forms
452 bool IsStandard(std::string& strReason) const;
453 bool IsStandard() const
455 std::string strReason;
456 return IsStandard(strReason);
459 /** Check for standard transaction types
460 @param[in] mapInputs Map of previous transactions that have outputs we're spending
461 @return True if all inputs (scriptSigs) use only standard transaction forms
462 @see CTransaction::FetchInputs
464 bool AreInputsStandard(const MapPrevTx& mapInputs) const;
466 /** Count ECDSA signature operations the old-fashioned (pre-0.6) way
467 @return number of sigops this transaction's outputs will produce when spent
468 @see CTransaction::FetchInputs
470 unsigned int GetLegacySigOpCount() const;
472 /** Count ECDSA signature operations in pay-to-script-hash inputs.
474 @param[in] mapInputs Map of previous transactions that have outputs we're spending
475 @return maximum number of sigops required to validate this transaction's inputs
476 @see CTransaction::FetchInputs
478 unsigned int GetP2SHSigOpCount(const MapPrevTx& mapInputs) const;
480 /** Amount of bitcoins spent by this transaction.
481 @return sum of all outputs (note: does not include fees)
483 int64_t GetValueOut() const;
485 /** Amount of bitcoins coming in to this transaction
486 Note that lightweight clients may not know anything besides the hash of previous transactions,
487 so may not be able to calculate this.
489 @param[in] mapInputs Map of previous transactions that have outputs we're spending
490 @return Sum of value of all inputs (scriptSigs)
491 @see CTransaction::FetchInputs
493 int64_t GetValueIn(const MapPrevTx& mapInputs) const;
495 static bool AllowFree(double dPriority)
497 // Large (in bytes) low-priority (new, small-coin) transactions
499 return dPriority > COIN * 144 / 250;
502 int64_t GetMinFee(unsigned int nBlockSize=1, bool fAllowFree=false, enum GetMinFee_mode mode=GMF_BLOCK, unsigned int nBytes = 0) const;
504 friend bool operator==(const CTransaction& a, const CTransaction& b)
506 return (a.nVersion == b.nVersion &&
507 a.nTime == b.nTime &&
510 a.nLockTime == b.nLockTime);
513 friend bool operator!=(const CTransaction& a, const CTransaction& b)
518 std::string ToStringShort() const;
519 std::string ToString() const;
523 printf("%s", ToString().c_str());
526 bool ReadFromDisk(CDiskTxPos pos, FILE** pfileRet=NULL);
527 bool ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet);
528 bool ReadFromDisk(CTxDB& txdb, COutPoint prevout);
529 bool ReadFromDisk(COutPoint prevout);
530 bool DisconnectInputs(CTxDB& txdb);
532 /** Fetch from memory and/or disk. inputsRet keys are transaction hashes.
534 @param[in] txdb Transaction database
535 @param[in] mapTestPool List of pending changes to the transaction index database
536 @param[in] fBlock True if being called to add a new best-block to the chain
537 @param[in] fMiner True if being called by CreateNewBlock
538 @param[out] inputsRet Pointers to this transaction's inputs
539 @param[out] fInvalid returns true if transaction is invalid
540 @return Returns true if all inputs are in txdb or mapTestPool
542 bool FetchInputs(CTxDB& txdb, const std::map<uint256, CTxIndex>& mapTestPool,
543 bool fBlock, bool fMiner, MapPrevTx& inputsRet, bool& fInvalid);
545 /** Sanity check previous transactions, then, if all checks succeed,
546 mark them as spent by this transaction.
548 @param[in] inputs Previous transactions (from FetchInputs)
549 @param[out] mapTestPool Keeps track of inputs that need to be updated on disk
550 @param[in] posThisTx Position of this transaction on disk
551 @param[in] pindexBlock
552 @param[in] fBlock true if called from ConnectBlock
553 @param[in] fMiner true if called from CreateNewBlock
554 @param[in] fScriptChecks enable scripts validation?
555 @param[in] flags STRICT_FLAGS script validation flags
556 @param[in] pvChecks NULL If pvChecks is not NULL, script checks are pushed onto it instead of being performed inline.
557 @return Returns true if all checks succeed
559 bool ConnectInputs(CTxDB& txdb, MapPrevTx inputs, std::map<uint256, CTxIndex>& mapTestPool, const CDiskTxPos& posThisTx, const CBlockIndex* pindexBlock,
560 bool fBlock, bool fMiner, bool fScriptChecks=true,
561 unsigned int flags=STRICT_FLAGS, std::vector<CScriptCheck> *pvChecks = NULL);
562 bool ClientConnectInputs();
563 bool CheckTransaction() const;
564 bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true, bool* pfMissingInputs=NULL);
565 bool GetCoinAge(CTxDB& txdb, uint64_t& nCoinAge) const; // ppcoin: get transaction coin age
568 const CTxOut& GetOutputFor(const CTxIn& input, const MapPrevTx& inputs) const;
574 /** A transaction with a merkle branch linking it to the block chain. */
575 class CMerkleTx : public CTransaction
579 std::vector<uint256> vMerkleBranch;
583 mutable bool fMerkleVerified;
591 CMerkleTx(const CTransaction& txIn) : CTransaction(txIn)
600 fMerkleVerified = false;
606 nSerSize += SerReadWrite(s, *(CTransaction*)this, nType, nVersion, ser_action);
607 nVersion = this->nVersion;
608 READWRITE(hashBlock);
609 READWRITE(vMerkleBranch);
614 int SetMerkleBranch(const CBlock* pblock=NULL);
615 int GetDepthInMainChain(CBlockIndex* &pindexRet) const;
616 int GetDepthInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet); }
617 bool IsInMainChain() const { return GetDepthInMainChain() > 0; }
618 int GetBlocksToMaturity() const;
619 bool AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs=true);
620 bool AcceptToMemoryPool();
626 /** A txdb record that contains the disk location of a transaction and the
627 * locations of transactions that spend its outputs. vSpent is really only
628 * used as a flag, but having the location is very helpful for debugging.
634 std::vector<CDiskTxPos> vSpent;
641 CTxIndex(const CDiskTxPos& posIn, unsigned int nOutputs)
644 vSpent.resize(nOutputs);
649 if (!(nType & SER_GETHASH))
666 friend bool operator==(const CTxIndex& a, const CTxIndex& b)
668 return (a.pos == b.pos &&
669 a.vSpent == b.vSpent);
672 friend bool operator!=(const CTxIndex& a, const CTxIndex& b)
676 int GetDepthInMainChain() const;
681 /** Nodes collect new transactions into a block, hash them into a hash tree,
682 * and scan through nonce values to make the block's hash satisfy proof-of-work
683 * requirements. When they solve the proof-of-work, they broadcast the block
684 * to everyone and the block is added to the block chain. The first transaction
685 * in the block is a special one that creates a new coin owned by the creator
688 * Blocks are appended to blk0001.dat files on disk. Their location on disk
689 * is indexed by CBlockIndex objects in memory.
695 static const int CURRENT_VERSION=6;
697 uint256 hashPrevBlock;
698 uint256 hashMerkleRoot;
704 std::vector<CTransaction> vtx;
706 // ppcoin: block signature - signed by one of the coin base txout[N]'s owner
707 std::vector<unsigned char> vchBlockSig;
710 mutable std::vector<uint256> vMerkleTree;
712 // Denial-of-service detection:
714 bool DoS(int nDoSIn, bool fIn) const { nDoS += nDoSIn; return fIn; }
723 READWRITE(this->nVersion);
724 nVersion = this->nVersion;
725 READWRITE(hashPrevBlock);
726 READWRITE(hashMerkleRoot);
731 // ConnectBlock depends on vtx following header to generate CDiskTxPos
732 if (!(nType & (SER_GETHASH|SER_BLOCKHEADERONLY)))
735 READWRITE(vchBlockSig);
739 const_cast<CBlock*>(this)->vtx.clear();
740 const_cast<CBlock*>(this)->vchBlockSig.clear();
746 nVersion = CBlock::CURRENT_VERSION;
763 uint256 GetHash() const
765 return scrypt_blockhash((const uint8_t*)&nVersion);
768 int64_t GetBlockTime() const
770 return (int64_t)nTime;
773 void UpdateTime(const CBlockIndex* pindexPrev);
775 // ppcoin: entropy bit for stake modifier if chosen by modifier
776 unsigned int GetStakeEntropyBit(unsigned int nHeight) const;
778 // ppcoin: two types of block: proof-of-work or proof-of-stake
779 bool IsProofOfStake() const
781 return (vtx.size() > 1 && vtx[1].IsCoinStake());
784 bool IsProofOfWork() const
786 return !IsProofOfStake();
789 std::pair<COutPoint, unsigned int> GetProofOfStake() const
791 if (IsProofOfStake())
792 return { vtx[1].vin[0].prevout, vtx[1].nTime };
793 return { COutPoint(), (unsigned int)0 };
796 // ppcoin: get max transaction timestamp
797 int64_t GetMaxTransactionTime() const;
798 uint256 BuildMerkleTree() const;
799 std::vector<uint256> GetMerkleBranch(int nIndex) const;
800 static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
801 bool WriteToDisk(unsigned int& nFileRet, unsigned int& nBlockPosRet);
802 bool ReadFromDisk(unsigned int nFile, unsigned int nBlockPos, bool fReadTransactions=true);
804 bool DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex);
805 bool ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck=false);
806 bool ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions=true);
807 bool SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew);
808 bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos);
809 bool CheckBlock(bool fCheckPOW=true, bool fCheckMerkleRoot=true, bool fCheckSig=true) const;
811 bool GetCoinAge(uint64_t& nCoinAge) const; // ppcoin: calculate total coin age spent in block
812 bool CheckBlockSignature() const;
815 bool SetBestChainInner(CTxDB& txdb, CBlockIndex *pindexNew);
823 /** The block chain is a tree shaped structure starting with the
824 * genesis block at the root, with each block potentially having multiple
825 * candidates to be the next block. pprev and pnext link a path through the
826 * main/longest chain. A blockindex may have multiple pprev pointing back
827 * to it, but pnext will only point forward to the longest branch, or will
828 * be null if the block is not part of the longest chain.
833 const uint256* phashBlock;
838 uint256 nChainTrust; // ppcoin: trust score of block chain
842 int64_t nMoneySupply;
844 uint32_t nFlags; // ppcoin: block index flags
847 BLOCK_PROOF_OF_STAKE = (1 << 0), // is proof-of-stake block
848 BLOCK_STAKE_ENTROPY = (1 << 1), // entropy bit for stake modifier
849 BLOCK_STAKE_MODIFIER = (1 << 2) // regenerated stake modifier
852 uint64_t nStakeModifier; // hash modifier for proof-of-stake
853 uint32_t nStakeModifierChecksum; // checksum of index; in-memeory only
855 // proof-of-stake specific fields
856 COutPoint prevoutStake;
858 uint256 hashProofOfStake;
862 uint256 hashMerkleRoot;
880 nStakeModifierChecksum = 0;
881 hashProofOfStake = 0;
882 prevoutStake.SetNull();
892 CBlockIndex(unsigned int nFileIn, unsigned int nBlockPosIn, CBlock& block)
898 nBlockPos = nBlockPosIn;
905 nStakeModifierChecksum = 0;
906 hashProofOfStake = 0;
907 if (block.IsProofOfStake())
910 prevoutStake = block.vtx[1].vin[0].prevout;
911 nStakeTime = block.vtx[1].nTime;
915 prevoutStake.SetNull();
919 nVersion = block.nVersion;
920 hashMerkleRoot = block.hashMerkleRoot;
923 nNonce = block.nNonce;
926 CBlock GetBlockHeader() const;
928 uint256 GetBlockHash() const
933 int64_t GetBlockTime() const
935 return (int64_t)nTime;
938 uint256 GetBlockTrust() const;
940 bool IsInMainChain() const
942 return (pnext || this == pindexBest);
945 bool CheckIndex() const
950 int64_t GetMedianTimePast() const;
953 * Returns true if there are nRequired or more blocks of minVersion or above
954 * in the last nToCheck blocks, starting at pstart and going backwards.
956 static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart,
957 unsigned int nRequired, unsigned int nToCheck);
960 bool IsProofOfWork() const
962 return !(nFlags & BLOCK_PROOF_OF_STAKE);
965 bool IsProofOfStake() const
967 return (nFlags & BLOCK_PROOF_OF_STAKE);
970 void SetProofOfStake()
972 nFlags |= BLOCK_PROOF_OF_STAKE;
975 unsigned int GetStakeEntropyBit() const
977 return ((nFlags & BLOCK_STAKE_ENTROPY) >> 1);
980 bool SetStakeEntropyBit(unsigned int nEntropyBit);
982 bool GeneratedStakeModifier() const
984 return (nFlags & BLOCK_STAKE_MODIFIER) != 0;
987 void SetStakeModifier(uint64_t nModifier, bool fGeneratedStakeModifier);
988 std::string ToString() const;
992 printf("%s\n", ToString().c_str());
998 // Used to marshal pointers into hashes for db storage.
999 class CDiskBlockIndex : public CBlockIndex
1008 CDiskBlockIndex() : blockHash(0), hashPrev(0), hashNext(0) {}
1009 explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex)
1011 hashPrev = (pprev ? pprev->GetBlockHash() : 0);
1012 hashNext = (pnext ? pnext->GetBlockHash() : 0);
1017 if (!(nType & SER_GETHASH))
1018 READWRITE(nVersion);
1020 READWRITE(hashNext);
1022 READWRITE(nBlockPos);
1025 READWRITE(nMoneySupply);
1027 READWRITE(nStakeModifier);
1028 if (IsProofOfStake())
1030 READWRITE(prevoutStake);
1031 READWRITE(nStakeTime);
1032 READWRITE(hashProofOfStake);
1036 const_cast<CDiskBlockIndex*>(this)->prevoutStake.SetNull();
1037 const_cast<CDiskBlockIndex*>(this)->nStakeTime = 0;
1038 const_cast<CDiskBlockIndex*>(this)->hashProofOfStake = 0;
1042 READWRITE(this->nVersion);
1043 READWRITE(hashPrev);
1044 READWRITE(hashMerkleRoot);
1048 READWRITE(blockHash);
1051 uint256 GetBlockHash() const;
1052 std::string ToString() const;
1056 printf("%s\n", ToString().c_str());
1062 // Describes a place in the block chain to another node such that if the
1063 // other node doesn't have the same branch, it can find a recent common trunk.
1064 // The further back it is, the further before the fork it may be.
1069 std::vector<uint256> vHave;
1073 CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) { }
1075 explicit CBlockLocator(const CBlockIndex* pindex)
1080 explicit CBlockLocator(uint256 hashBlock)
1082 auto mi = mapBlockIndex.find(hashBlock);
1083 if (mi != mapBlockIndex.end())
1089 if (!(nType & SER_GETHASH))
1090 READWRITE(nVersion);
1101 return vHave.empty();
1104 void Set(const CBlockIndex* pindex);
1105 int GetDistanceBack();
1106 CBlockIndex* GetBlockIndex();
1107 uint256 GetBlockHash();
1116 mutable CCriticalSection cs;
1117 std::map<uint256, CTransaction> mapTx;
1118 std::map<COutPoint, CInPoint> mapNextTx;
1120 bool accept(CTxDB& txdb, CTransaction &tx,
1121 bool fCheckInputs, bool* pfMissingInputs);
1122 bool addUnchecked(const uint256& hash, CTransaction &tx);
1123 bool remove(CTransaction &tx);
1125 void queryHashes(std::vector<uint256>& vtxid);
1130 return mapTx.size();
1133 bool exists(const uint256 &hash)
1135 return (mapTx.count(hash) != 0);
1138 CTransaction& lookup(const uint256 &hash)
1144 extern CTxMemPool mempool;