/src/main.cpp
C++ | 3112 lines | 2247 code | 518 blank | 347 comment | 631 complexity | 4e1b29ec025e8891647bb4485e5fb2cf MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- // Copyright (c) 2009-2010 Satoshi Nakamoto
- // Distributed under the MIT/X11 software license, see the accompanying
- // file license.txt or http://www.opensource.org/licenses/mit-license.php.
- #include "headers.h"
- #include "db.h"
- #include "net.h"
- #include "init.h"
- #include "cryptopp/sha.h"
- #include <boost/filesystem.hpp>
- #include <boost/filesystem/fstream.hpp>
- using namespace std;
- using namespace boost;
- //
- // Global state
- //
- CCriticalSection cs_setpwalletRegistered;
- set<CWallet*> setpwalletRegistered;
- CCriticalSection cs_main;
- CCriticalSection cs_mapPubKeys;
- map<uint160, vector<unsigned char> > mapPubKeys;
- map<uint256, CTransaction> mapTransactions;
- CCriticalSection cs_mapTransactions;
- unsigned int nTransactionsUpdated = 0;
- map<COutPoint, CInPoint> mapNextTx;
- map<uint256, CBlockIndex*> mapBlockIndex;
- uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
- CBigNum bnProofOfWorkLimit(~uint256(0) >> 32);
- const int nTotalBlocksEstimate = 134444; // Conservative estimate of total nr of blocks on main chain
- const int nInitialBlockThreshold = 120; // Regard blocks up until N-threshold as "initial download"
- CBlockIndex* pindexGenesisBlock = NULL;
- int nBestHeight = -1;
- CBigNum bnBestChainWork = 0;
- CBigNum bnBestInvalidWork = 0;
- uint256 hashBestChain = 0;
- CBlockIndex* pindexBest = NULL;
- int64 nTimeBestReceived = 0;
- map<uint256, CBlock*> mapOrphanBlocks;
- multimap<uint256, CBlock*> mapOrphanBlocksByPrev;
- map<uint256, CDataStream*> mapOrphanTransactions;
- multimap<uint256, CDataStream*> mapOrphanTransactionsByPrev;
- double dHashesPerSec;
- int64 nHPSTimerStart;
- // Settings
- int fGenerateBitcoins = false;
- int64 nTransactionFee = 0;
- CAddress addrIncoming;
- int fLimitProcessors = false;
- int nLimitProcessors = 1;
- int fMinimizeToTray = true;
- int fMinimizeOnClose = true;
- //////////////////////////////////////////////////////////////////////////////
- //
- // dispatching functions
- //
- void RegisterWallet(CWallet* pwalletIn)
- {
- CRITICAL_BLOCK(cs_setpwalletRegistered)
- {
- setpwalletRegistered.insert(pwalletIn);
- }
- }
- void UnregisterWallet(CWallet* pwalletIn)
- {
- CRITICAL_BLOCK(cs_setpwalletRegistered)
- {
- setpwalletRegistered.erase(pwalletIn);
- }
- }
- bool static IsFromMe(CTransaction& tx)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- if (pwallet->IsFromMe(tx))
- return true;
- return false;
- }
- bool static GetTransaction(const uint256& hashTx, CWalletTx& wtx)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- if (pwallet->GetTransaction(hashTx,wtx))
- return true;
- return false;
- }
- void static EraseFromWallets(uint256 hash)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- pwallet->EraseFromWallet(hash);
- }
- void static SyncWithWallets(const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- pwallet->AddToWalletIfInvolvingMe(tx, pblock, fUpdate);
- }
- void static SetBestChain(const CBlockLocator& loc)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- pwallet->SetBestChain(loc);
- }
- void static UpdatedTransaction(const uint256& hashTx)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- pwallet->UpdatedTransaction(hashTx);
- }
- void static PrintWallets(const CBlock& block)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- pwallet->PrintWallet(block);
- }
- void static Inventory(const uint256& hash)
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- pwallet->Inventory(hash);
- }
- void static ResendWalletTransactions()
- {
- BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
- pwallet->ResendWalletTransactions();
- }
- //////////////////////////////////////////////////////////////////////////////
- //
- // mapOrphanTransactions
- //
- void static AddOrphanTx(const CDataStream& vMsg)
- {
- CTransaction tx;
- CDataStream(vMsg) >> tx;
- uint256 hash = tx.GetHash();
- if (mapOrphanTransactions.count(hash))
- return;
- CDataStream* pvMsg = mapOrphanTransactions[hash] = new CDataStream(vMsg);
- BOOST_FOREACH(const CTxIn& txin, tx.vin)
- mapOrphanTransactionsByPrev.insert(make_pair(txin.prevout.hash, pvMsg));
- }
- void static EraseOrphanTx(uint256 hash)
- {
- if (!mapOrphanTransactions.count(hash))
- return;
- const CDataStream* pvMsg = mapOrphanTransactions[hash];
- CTransaction tx;
- CDataStream(*pvMsg) >> tx;
- BOOST_FOREACH(const CTxIn& txin, tx.vin)
- {
- for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(txin.prevout.hash);
- mi != mapOrphanTransactionsByPrev.upper_bound(txin.prevout.hash);)
- {
- if ((*mi).second == pvMsg)
- mapOrphanTransactionsByPrev.erase(mi++);
- else
- mi++;
- }
- }
- delete pvMsg;
- mapOrphanTransactions.erase(hash);
- }
- //////////////////////////////////////////////////////////////////////////////
- //
- // CTransaction and CTxIndex
- //
- bool CTransaction::ReadFromDisk(CTxDB& txdb, COutPoint prevout, CTxIndex& txindexRet)
- {
- SetNull();
- if (!txdb.ReadTxIndex(prevout.hash, txindexRet))
- return false;
- if (!ReadFromDisk(txindexRet.pos))
- return false;
- if (prevout.n >= vout.size())
- {
- SetNull();
- return false;
- }
- return true;
- }
- bool CTransaction::ReadFromDisk(CTxDB& txdb, COutPoint prevout)
- {
- CTxIndex txindex;
- return ReadFromDisk(txdb, prevout, txindex);
- }
- bool CTransaction::ReadFromDisk(COutPoint prevout)
- {
- CTxDB txdb("r");
- CTxIndex txindex;
- return ReadFromDisk(txdb, prevout, txindex);
- }
- int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
- {
- if (fClient)
- {
- if (hashBlock == 0)
- return 0;
- }
- else
- {
- CBlock blockTmp;
- if (pblock == NULL)
- {
- // Load the block this tx is in
- CTxIndex txindex;
- if (!CTxDB("r").ReadTxIndex(GetHash(), txindex))
- return 0;
- if (!blockTmp.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos))
- return 0;
- pblock = &blockTmp;
- }
- // Update the tx's hashBlock
- hashBlock = pblock->GetHash();
- // Locate the transaction
- for (nIndex = 0; nIndex < pblock->vtx.size(); nIndex++)
- if (pblock->vtx[nIndex] == *(CTransaction*)this)
- break;
- if (nIndex == pblock->vtx.size())
- {
- vMerkleBranch.clear();
- nIndex = -1;
- printf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
- return 0;
- }
- // Fill in merkle branch
- vMerkleBranch = pblock->GetMerkleBranch(nIndex);
- }
- // Is the tx in a block that's in the main chain
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
- if (mi == mapBlockIndex.end())
- return 0;
- CBlockIndex* pindex = (*mi).second;
- if (!pindex || !pindex->IsInMainChain())
- return 0;
- return pindexBest->nHeight - pindex->nHeight + 1;
- }
- bool CTransaction::CheckTransaction() const
- {
- // Basic checks that don't depend on any context
- if (vin.empty() || vout.empty())
- return error("CTransaction::CheckTransaction() : vin or vout empty");
- // Size limits
- if (::GetSerializeSize(*this, SER_NETWORK) > MAX_BLOCK_SIZE)
- return error("CTransaction::CheckTransaction() : size limits failed");
- // Check for negative or overflow output values
- int64 nValueOut = 0;
- BOOST_FOREACH(const CTxOut& txout, vout)
- {
- if (txout.nValue < 0)
- return error("CTransaction::CheckTransaction() : txout.nValue negative");
- if (txout.nValue > MAX_MONEY)
- return error("CTransaction::CheckTransaction() : txout.nValue too high");
- nValueOut += txout.nValue;
- if (!MoneyRange(nValueOut))
- return error("CTransaction::CheckTransaction() : txout total out of range");
- }
- if (IsCoinBase())
- {
- if (vin[0].scriptSig.size() < 2 || vin[0].scriptSig.size() > 100)
- return error("CTransaction::CheckTransaction() : coinbase script size");
- }
- else
- {
- BOOST_FOREACH(const CTxIn& txin, vin)
- if (txin.prevout.IsNull())
- return error("CTransaction::CheckTransaction() : prevout is null");
- }
- return true;
- }
- bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMissingInputs)
- {
- if (pfMissingInputs)
- *pfMissingInputs = false;
- if (!CheckTransaction())
- return error("AcceptToMemoryPool() : CheckTransaction failed");
- // Coinbase is only valid in a block, not as a loose transaction
- if (IsCoinBase())
- return error("AcceptToMemoryPool() : coinbase as individual tx");
- // To help v0.1.5 clients who would see it as a negative number
- if ((int64)nLockTime > INT_MAX)
- return error("AcceptToMemoryPool() : not accepting nLockTime beyond 2038 yet");
- // Safety limits
- unsigned int nSize = ::GetSerializeSize(*this, SER_NETWORK);
- // Checking ECDSA signatures is a CPU bottleneck, so to avoid denial-of-service
- // attacks disallow transactions with more than one SigOp per 34 bytes.
- // 34 bytes because a TxOut is:
- // 20-byte address + 8 byte bitcoin amount + 5 bytes of ops + 1 byte script length
- if (GetSigOpCount() > nSize / 34 || nSize < 100)
- return error("AcceptToMemoryPool() : nonstandard transaction");
- // Rather not work on nonstandard transactions (unless -testnet)
- if (!fTestNet && !IsStandard())
- return error("AcceptToMemoryPool() : nonstandard transaction type");
- // Do we already have it?
- uint256 hash = GetHash();
- CRITICAL_BLOCK(cs_mapTransactions)
- if (mapTransactions.count(hash))
- return false;
- if (fCheckInputs)
- if (txdb.ContainsTx(hash))
- return false;
- // Check for conflicts with in-memory transactions
- CTransaction* ptxOld = NULL;
- for (int i = 0; i < vin.size(); i++)
- {
- COutPoint outpoint = vin[i].prevout;
- if (mapNextTx.count(outpoint))
- {
- // Disable replacement feature for now
- return false;
- // Allow replacing with a newer version of the same transaction
- if (i != 0)
- return false;
- ptxOld = mapNextTx[outpoint].ptx;
- if (ptxOld->IsFinal())
- return false;
- if (!IsNewerThan(*ptxOld))
- return false;
- for (int i = 0; i < vin.size(); i++)
- {
- COutPoint outpoint = vin[i].prevout;
- if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
- return false;
- }
- break;
- }
- }
- if (fCheckInputs)
- {
- // Check against previous transactions
- map<uint256, CTxIndex> mapUnused;
- int64 nFees = 0;
- if (!ConnectInputs(txdb, mapUnused, CDiskTxPos(1,1,1), pindexBest, nFees, false, false))
- {
- if (pfMissingInputs)
- *pfMissingInputs = true;
- return error("AcceptToMemoryPool() : ConnectInputs failed %s", hash.ToString().substr(0,10).c_str());
- }
- // Don't accept it if it can't get into a block
- if (nFees < GetMinFee(1000, true, true))
- return error("AcceptToMemoryPool() : not enough fees");
- // Continuously rate-limit free transactions
- // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
- // be annoying or make other's transactions take longer to confirm.
- if (nFees < MIN_RELAY_TX_FEE)
- {
- static CCriticalSection cs;
- static double dFreeCount;
- static int64 nLastTime;
- int64 nNow = GetTime();
- CRITICAL_BLOCK(cs)
- {
- // Use an exponentially decaying ~10-minute window:
- dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
- nLastTime = nNow;
- // -limitfreerelay unit is thousand-bytes-per-minute
- // At default rate it would take over a month to fill 1GB
- if (dFreeCount > GetArg("-limitfreerelay", 15)*10*1000 && !IsFromMe(*this))
- return error("AcceptToMemoryPool() : free transaction rejected by rate limiter");
- if (fDebug)
- printf("Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
- dFreeCount += nSize;
- }
- }
- }
- // Store transaction in memory
- CRITICAL_BLOCK(cs_mapTransactions)
- {
- if (ptxOld)
- {
- printf("AcceptToMemoryPool() : replacing tx %s with new version\n", ptxOld->GetHash().ToString().c_str());
- ptxOld->RemoveFromMemoryPool();
- }
- AddToMemoryPoolUnchecked();
- }
- ///// are we sure this is ok when loading transactions or restoring block txes
- // If updated, erase old tx from wallet
- if (ptxOld)
- EraseFromWallets(ptxOld->GetHash());
- printf("AcceptToMemoryPool(): accepted %s\n", hash.ToString().substr(0,10).c_str());
- return true;
- }
- bool CTransaction::AcceptToMemoryPool(bool fCheckInputs, bool* pfMissingInputs)
- {
- CTxDB txdb("r");
- return AcceptToMemoryPool(txdb, fCheckInputs, pfMissingInputs);
- }
- bool CTransaction::AddToMemoryPoolUnchecked()
- {
- // Add to memory pool without checking anything. Don't call this directly,
- // call AcceptToMemoryPool to properly check the transaction first.
- CRITICAL_BLOCK(cs_mapTransactions)
- {
- uint256 hash = GetHash();
- mapTransactions[hash] = *this;
- for (int i = 0; i < vin.size(); i++)
- mapNextTx[vin[i].prevout] = CInPoint(&mapTransactions[hash], i);
- nTransactionsUpdated++;
- }
- return true;
- }
- bool CTransaction::RemoveFromMemoryPool()
- {
- // Remove transaction from memory pool
- CRITICAL_BLOCK(cs_mapTransactions)
- {
- BOOST_FOREACH(const CTxIn& txin, vin)
- mapNextTx.erase(txin.prevout);
- mapTransactions.erase(GetHash());
- nTransactionsUpdated++;
- }
- return true;
- }
- int CMerkleTx::GetDepthInMainChain(int& nHeightRet) const
- {
- if (hashBlock == 0 || nIndex == -1)
- return 0;
- // Find the block it claims to be in
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
- if (mi == mapBlockIndex.end())
- return 0;
- CBlockIndex* pindex = (*mi).second;
- if (!pindex || !pindex->IsInMainChain())
- return 0;
- // Make sure the merkle branch connects to this block
- if (!fMerkleVerified)
- {
- if (CBlock::CheckMerkleBranch(GetHash(), vMerkleBranch, nIndex) != pindex->hashMerkleRoot)
- return 0;
- fMerkleVerified = true;
- }
- nHeightRet = pindex->nHeight;
- return pindexBest->nHeight - pindex->nHeight + 1;
- }
- int CMerkleTx::GetBlocksToMaturity() const
- {
- if (!IsCoinBase())
- return 0;
- return max(0, (COINBASE_MATURITY+20) - GetDepthInMainChain());
- }
- bool CMerkleTx::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs)
- {
- if (fClient)
- {
- if (!IsInMainChain() && !ClientConnectInputs())
- return false;
- return CTransaction::AcceptToMemoryPool(txdb, false);
- }
- else
- {
- return CTransaction::AcceptToMemoryPool(txdb, fCheckInputs);
- }
- }
- bool CMerkleTx::AcceptToMemoryPool()
- {
- CTxDB txdb("r");
- return AcceptToMemoryPool(txdb);
- }
- bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
- {
- CRITICAL_BLOCK(cs_mapTransactions)
- {
- // Add previous supporting transactions first
- BOOST_FOREACH(CMerkleTx& tx, vtxPrev)
- {
- if (!tx.IsCoinBase())
- {
- uint256 hash = tx.GetHash();
- if (!mapTransactions.count(hash) && !txdb.ContainsTx(hash))
- tx.AcceptToMemoryPool(txdb, fCheckInputs);
- }
- }
- return AcceptToMemoryPool(txdb, fCheckInputs);
- }
- return false;
- }
- bool CWalletTx::AcceptWalletTransaction()
- {
- CTxDB txdb("r");
- return AcceptWalletTransaction(txdb);
- }
- int CTxIndex::GetDepthInMainChain() const
- {
- // Read block header
- CBlock block;
- if (!block.ReadFromDisk(pos.nFile, pos.nBlockPos, false))
- return 0;
- // Find the block in the index
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(block.GetHash());
- if (mi == mapBlockIndex.end())
- return 0;
- CBlockIndex* pindex = (*mi).second;
- if (!pindex || !pindex->IsInMainChain())
- return 0;
- return 1 + nBestHeight - pindex->nHeight;
- }
- //////////////////////////////////////////////////////////////////////////////
- //
- // CBlock and CBlockIndex
- //
- bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
- {
- if (!fReadTransactions)
- {
- *this = pindex->GetBlockHeader();
- return true;
- }
- if (!ReadFromDisk(pindex->nFile, pindex->nBlockPos, fReadTransactions))
- return false;
- if (GetHash() != pindex->GetBlockHash())
- return error("CBlock::ReadFromDisk() : GetHash() doesn't match index");
- return true;
- }
- uint256 static GetOrphanRoot(const CBlock* pblock)
- {
- // Work back to the first block in the orphan chain
- while (mapOrphanBlocks.count(pblock->hashPrevBlock))
- pblock = mapOrphanBlocks[pblock->hashPrevBlock];
- return pblock->GetHash();
- }
- int64 static GetBlockValue(int nHeight, int64 nFees)
- {
- int64 nSubsidy = 50 * COIN;
- // Subsidy is cut in half every 4 years
- nSubsidy >>= (nHeight / 210000);
- return nSubsidy + nFees;
- }
- unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast)
- {
- const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
- const int64 nTargetSpacing = 10 * 60;
- const int64 nInterval = nTargetTimespan / nTargetSpacing;
- // Genesis block
- if (pindexLast == NULL)
- return bnProofOfWorkLimit.GetCompact();
- // Only change once per interval
- if ((pindexLast->nHeight+1) % nInterval != 0)
- return pindexLast->nBits;
- // Go back by what we want to be 14 days worth of blocks
- const CBlockIndex* pindexFirst = pindexLast;
- for (int i = 0; pindexFirst && i < nInterval-1; i++)
- pindexFirst = pindexFirst->pprev;
- assert(pindexFirst);
- // Limit adjustment step
- int64 nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
- printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
- if (nActualTimespan < nTargetTimespan/4)
- nActualTimespan = nTargetTimespan/4;
- if (nActualTimespan > nTargetTimespan*4)
- nActualTimespan = nTargetTimespan*4;
- // Retarget
- CBigNum bnNew;
- bnNew.SetCompact(pindexLast->nBits);
- bnNew *= nActualTimespan;
- bnNew /= nTargetTimespan;
- if (bnNew > bnProofOfWorkLimit)
- bnNew = bnProofOfWorkLimit;
- /// debug print
- printf("GetNextWorkRequired RETARGET\n");
- printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", nTargetTimespan, nActualTimespan);
- printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
- printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
- return bnNew.GetCompact();
- }
- bool CheckProofOfWork(uint256 hash, unsigned int nBits)
- {
- CBigNum bnTarget;
- bnTarget.SetCompact(nBits);
- // Check range
- if (bnTarget <= 0 || bnTarget > bnProofOfWorkLimit)
- return error("CheckProofOfWork() : nBits below minimum work");
- // Check proof of work matches claimed amount
- if (hash > bnTarget.getuint256())
- return error("CheckProofOfWork() : hash doesn't match nBits");
- return true;
- }
- // Return conservative estimate of total number of blocks, 0 if unknown
- int GetTotalBlocksEstimate()
- {
- if(fTestNet)
- {
- return 0;
- }
- else
- {
- return nTotalBlocksEstimate;
- }
- }
- bool IsInitialBlockDownload()
- {
- if (pindexBest == NULL || nBestHeight < (GetTotalBlocksEstimate()-nInitialBlockThreshold))
- return true;
- static int64 nLastUpdate;
- static CBlockIndex* pindexLastBest;
- if (pindexBest != pindexLastBest)
- {
- pindexLastBest = pindexBest;
- nLastUpdate = GetTime();
- }
- return (GetTime() - nLastUpdate < 10 &&
- pindexBest->GetBlockTime() < GetTime() - 24 * 60 * 60);
- }
- void static InvalidChainFound(CBlockIndex* pindexNew)
- {
- if (pindexNew->bnChainWork > bnBestInvalidWork)
- {
- bnBestInvalidWork = pindexNew->bnChainWork;
- CTxDB().WriteBestInvalidWork(bnBestInvalidWork);
- MainFrameRepaint();
- }
- printf("InvalidChainFound: invalid block=%s height=%d work=%s\n", pindexNew->GetBlockHash().ToString().substr(0,20).c_str(), pindexNew->nHeight, pindexNew->bnChainWork.ToString().c_str());
- printf("InvalidChainFound: current best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
- if (pindexBest && bnBestInvalidWork > bnBestChainWork + pindexBest->GetBlockWork() * 6)
- printf("InvalidChainFound: WARNING: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.\n");
- }
- bool CTransaction::DisconnectInputs(CTxDB& txdb)
- {
- // Relinquish previous transactions' spent pointers
- if (!IsCoinBase())
- {
- BOOST_FOREACH(const CTxIn& txin, vin)
- {
- COutPoint prevout = txin.prevout;
- // Get prev txindex from disk
- CTxIndex txindex;
- if (!txdb.ReadTxIndex(prevout.hash, txindex))
- return error("DisconnectInputs() : ReadTxIndex failed");
- if (prevout.n >= txindex.vSpent.size())
- return error("DisconnectInputs() : prevout.n out of range");
- // Mark outpoint as not spent
- txindex.vSpent[prevout.n].SetNull();
- // Write back
- if (!txdb.UpdateTxIndex(prevout.hash, txindex))
- return error("DisconnectInputs() : UpdateTxIndex failed");
- }
- }
- // Remove transaction from index
- if (!txdb.EraseTxIndex(*this))
- return error("DisconnectInputs() : EraseTxPos failed");
- return true;
- }
- bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx,
- CBlockIndex* pindexBlock, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee)
- {
- // Take over previous transactions' spent pointers
- if (!IsCoinBase())
- {
- int64 nValueIn = 0;
- for (int i = 0; i < vin.size(); i++)
- {
- COutPoint prevout = vin[i].prevout;
- // Read txindex
- CTxIndex txindex;
- bool fFound = true;
- if (fMiner && mapTestPool.count(prevout.hash))
- {
- // Get txindex from current proposed changes
- txindex = mapTestPool[prevout.hash];
- }
- else
- {
- // Read txindex from txdb
- fFound = txdb.ReadTxIndex(prevout.hash, txindex);
- }
- if (!fFound && (fBlock || fMiner))
- return fMiner ? false : error("ConnectInputs() : %s prev tx %s index entry not found", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
- // Read txPrev
- CTransaction txPrev;
- if (!fFound || txindex.pos == CDiskTxPos(1,1,1))
- {
- // Get prev tx from single transactions in memory
- CRITICAL_BLOCK(cs_mapTransactions)
- {
- if (!mapTransactions.count(prevout.hash))
- return error("ConnectInputs() : %s mapTransactions prev not found %s", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
- txPrev = mapTransactions[prevout.hash];
- }
- if (!fFound)
- txindex.vSpent.resize(txPrev.vout.size());
- }
- else
- {
- // Get prev tx from disk
- if (!txPrev.ReadFromDisk(txindex.pos))
- return error("ConnectInputs() : %s ReadFromDisk prev tx %s failed", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
- }
- if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
- return error("ConnectInputs() : %s prevout.n out of range %d %d %d prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str());
- // If prev is coinbase, check that it's matured
- if (txPrev.IsCoinBase())
- for (CBlockIndex* pindex = pindexBlock; pindex && pindexBlock->nHeight - pindex->nHeight < COINBASE_MATURITY; pindex = pindex->pprev)
- if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
- return error("ConnectInputs() : tried to spend coinbase at depth %d", pindexBlock->nHeight - pindex->nHeight);
- // Verify signature
- if (!VerifySignature(txPrev, *this, i))
- return error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,10).c_str());
- // Check for conflicts
- if (!txindex.vSpent[prevout.n].IsNull())
- return fMiner ? false : error("ConnectInputs() : %s prev tx already used at %s", GetHash().ToString().substr(0,10).c_str(), txindex.vSpent[prevout.n].ToString().c_str());
- // Check for negative or overflow input values
- nValueIn += txPrev.vout[prevout.n].nValue;
- if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
- return error("ConnectInputs() : txin values out of range");
- // Mark outpoints as spent
- txindex.vSpent[prevout.n] = posThisTx;
- // Write back
- if (fBlock)
- {
- if (!txdb.UpdateTxIndex(prevout.hash, txindex))
- return error("ConnectInputs() : UpdateTxIndex failed");
- }
- else if (fMiner)
- {
- mapTestPool[prevout.hash] = txindex;
- }
- }
- if (nValueIn < GetValueOut())
- return error("ConnectInputs() : %s value in < value out", GetHash().ToString().substr(0,10).c_str());
- // Tally transaction fees
- int64 nTxFee = nValueIn - GetValueOut();
- if (nTxFee < 0)
- return error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,10).c_str());
- if (nTxFee < nMinFee)
- return false;
- nFees += nTxFee;
- if (!MoneyRange(nFees))
- return error("ConnectInputs() : nFees out of range");
- }
- if (fBlock)
- {
- // Add transaction to disk index
- if (!txdb.AddTxIndex(*this, posThisTx, pindexBlock->nHeight))
- return error("ConnectInputs() : AddTxPos failed");
- }
- else if (fMiner)
- {
- // Add transaction to test pool
- mapTestPool[GetHash()] = CTxIndex(CDiskTxPos(1,1,1), vout.size());
- }
- return true;
- }
- bool CTransaction::ClientConnectInputs()
- {
- if (IsCoinBase())
- return false;
- // Take over previous transactions' spent pointers
- CRITICAL_BLOCK(cs_mapTransactions)
- {
- int64 nValueIn = 0;
- for (int i = 0; i < vin.size(); i++)
- {
- // Get prev tx from single transactions in memory
- COutPoint prevout = vin[i].prevout;
- if (!mapTransactions.count(prevout.hash))
- return false;
- CTransaction& txPrev = mapTransactions[prevout.hash];
- if (prevout.n >= txPrev.vout.size())
- return false;
- // Verify signature
- if (!VerifySignature(txPrev, *this, i))
- return error("ConnectInputs() : VerifySignature failed");
- ///// this is redundant with the mapNextTx stuff, not sure which I want to get rid of
- ///// this has to go away now that posNext is gone
- // // Check for conflicts
- // if (!txPrev.vout[prevout.n].posNext.IsNull())
- // return error("ConnectInputs() : prev tx already used");
- //
- // // Flag outpoints as used
- // txPrev.vout[prevout.n].posNext = posThisTx;
- nValueIn += txPrev.vout[prevout.n].nValue;
- if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
- return error("ClientConnectInputs() : txin values out of range");
- }
- if (GetValueOut() > nValueIn)
- return false;
- }
- return true;
- }
- bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex)
- {
- // Disconnect in reverse order
- for (int i = vtx.size()-1; i >= 0; i--)
- if (!vtx[i].DisconnectInputs(txdb))
- return false;
- // Update block index on disk without changing it in memory.
- // The memory index structure will be changed after the db commits.
- if (pindex->pprev)
- {
- CDiskBlockIndex blockindexPrev(pindex->pprev);
- blockindexPrev.hashNext = 0;
- if (!txdb.WriteBlockIndex(blockindexPrev))
- return error("DisconnectBlock() : WriteBlockIndex failed");
- }
- return true;
- }
- bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
- {
- // Check it again in case a previous version let a bad block in
- if (!CheckBlock())
- return false;
- //// issue here: it doesn't know the version
- unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK) - 1 + GetSizeOfCompactSize(vtx.size());
- map<uint256, CTxIndex> mapUnused;
- int64 nFees = 0;
- BOOST_FOREACH(CTransaction& tx, vtx)
- {
- CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
- nTxPos += ::GetSerializeSize(tx, SER_DISK);
- if (!tx.ConnectInputs(txdb, mapUnused, posThisTx, pindex, nFees, true, false))
- return false;
- }
- if (vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
- return false;
- // Update block index on disk without changing it in memory.
- // The memory index structure will be changed after the db commits.
- if (pindex->pprev)
- {
- CDiskBlockIndex blockindexPrev(pindex->pprev);
- blockindexPrev.hashNext = pindex->GetBlockHash();
- if (!txdb.WriteBlockIndex(blockindexPrev))
- return error("ConnectBlock() : WriteBlockIndex failed");
- }
- // Watch for transactions paying to me
- BOOST_FOREACH(CTransaction& tx, vtx)
- SyncWithWallets(tx, this, true);
- return true;
- }
- bool static Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
- {
- printf("REORGANIZE\n");
- // Find the fork
- CBlockIndex* pfork = pindexBest;
- CBlockIndex* plonger = pindexNew;
- while (pfork != plonger)
- {
- while (plonger->nHeight > pfork->nHeight)
- if (!(plonger = plonger->pprev))
- return error("Reorganize() : plonger->pprev is null");
- if (pfork == plonger)
- break;
- if (!(pfork = pfork->pprev))
- return error("Reorganize() : pfork->pprev is null");
- }
- // List of what to disconnect
- vector<CBlockIndex*> vDisconnect;
- for (CBlockIndex* pindex = pindexBest; pindex != pfork; pindex = pindex->pprev)
- vDisconnect.push_back(pindex);
- // List of what to connect
- vector<CBlockIndex*> vConnect;
- for (CBlockIndex* pindex = pindexNew; pindex != pfork; pindex = pindex->pprev)
- vConnect.push_back(pindex);
- reverse(vConnect.begin(), vConnect.end());
- // Disconnect shorter branch
- vector<CTransaction> vResurrect;
- BOOST_FOREACH(CBlockIndex* pindex, vDisconnect)
- {
- CBlock block;
- if (!block.ReadFromDisk(pindex))
- return error("Reorganize() : ReadFromDisk for disconnect failed");
- if (!block.DisconnectBlock(txdb, pindex))
- return error("Reorganize() : DisconnectBlock failed");
- // Queue memory transactions to resurrect
- BOOST_FOREACH(const CTransaction& tx, block.vtx)
- if (!tx.IsCoinBase())
- vResurrect.push_back(tx);
- }
- // Connect longer branch
- vector<CTransaction> vDelete;
- for (int i = 0; i < vConnect.size(); i++)
- {
- CBlockIndex* pindex = vConnect[i];
- CBlock block;
- if (!block.ReadFromDisk(pindex))
- return error("Reorganize() : ReadFromDisk for connect failed");
- if (!block.ConnectBlock(txdb, pindex))
- {
- // Invalid block
- txdb.TxnAbort();
- return error("Reorganize() : ConnectBlock failed");
- }
- // Queue memory transactions to delete
- BOOST_FOREACH(const CTransaction& tx, block.vtx)
- vDelete.push_back(tx);
- }
- if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
- return error("Reorganize() : WriteHashBestChain failed");
- // Make sure it's successfully written to disk before changing memory structure
- if (!txdb.TxnCommit())
- return error("Reorganize() : TxnCommit failed");
- // Disconnect shorter branch
- BOOST_FOREACH(CBlockIndex* pindex, vDisconnect)
- if (pindex->pprev)
- pindex->pprev->pnext = NULL;
- // Connect longer branch
- BOOST_FOREACH(CBlockIndex* pindex, vConnect)
- if (pindex->pprev)
- pindex->pprev->pnext = pindex;
- // Resurrect memory transactions that were in the disconnected branch
- BOOST_FOREACH(CTransaction& tx, vResurrect)
- tx.AcceptToMemoryPool(txdb, false);
- // Delete redundant memory transactions that are in the connected branch
- BOOST_FOREACH(CTransaction& tx, vDelete)
- tx.RemoveFromMemoryPool();
- return true;
- }
- bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
- {
- uint256 hash = GetHash();
- txdb.TxnBegin();
- if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
- {
- txdb.WriteHashBestChain(hash);
- if (!txdb.TxnCommit())
- return error("SetBestChain() : TxnCommit failed");
- pindexGenesisBlock = pindexNew;
- }
- else if (hashPrevBlock == hashBestChain)
- {
- // Adding to current best branch
- if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
- {
- txdb.TxnAbort();
- InvalidChainFound(pindexNew);
- return error("SetBestChain() : ConnectBlock failed");
- }
- if (!txdb.TxnCommit())
- return error("SetBestChain() : TxnCommit failed");
- // Add to current best branch
- pindexNew->pprev->pnext = pindexNew;
- // Delete redundant memory transactions
- BOOST_FOREACH(CTransaction& tx, vtx)
- tx.RemoveFromMemoryPool();
- }
- else
- {
- // New best branch
- if (!Reorganize(txdb, pindexNew))
- {
- txdb.TxnAbort();
- InvalidChainFound(pindexNew);
- return error("SetBestChain() : Reorganize failed");
- }
- }
- // Update best block in wallet (so we can detect restored wallets)
- if (!IsInitialBlockDownload())
- {
- const CBlockLocator locator(pindexNew);
- ::SetBestChain(locator);
- }
- // New best block
- hashBestChain = hash;
- pindexBest = pindexNew;
- nBestHeight = pindexBest->nHeight;
- bnBestChainWork = pindexNew->bnChainWork;
- nTimeBestReceived = GetTime();
- nTransactionsUpdated++;
- printf("SetBestChain: new best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainWork.ToString().c_str());
- return true;
- }
- bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
- {
- // Check for duplicate
- uint256 hash = GetHash();
- if (mapBlockIndex.count(hash))
- return error("AddToBlockIndex() : %s already exists", hash.ToString().substr(0,20).c_str());
- // Construct new block index object
- CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
- if (!pindexNew)
- return error("AddToBlockIndex() : new CBlockIndex failed");
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
- pindexNew->phashBlock = &((*mi).first);
- map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
- if (miPrev != mapBlockIndex.end())
- {
- pindexNew->pprev = (*miPrev).second;
- pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
- }
- pindexNew->bnChainWork = (pindexNew->pprev ? pindexNew->pprev->bnChainWork : 0) + pindexNew->GetBlockWork();
- CTxDB txdb;
- txdb.TxnBegin();
- txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
- if (!txdb.TxnCommit())
- return false;
- // New best
- if (pindexNew->bnChainWork > bnBestChainWork)
- if (!SetBestChain(txdb, pindexNew))
- return false;
- txdb.Close();
- if (pindexNew == pindexBest)
- {
- // Notify UI to display prev block's coinbase if it was ours
- static uint256 hashPrevBestCoinBase;
- UpdatedTransaction(hashPrevBestCoinBase);
- hashPrevBestCoinBase = vtx[0].GetHash();
- }
- MainFrameRepaint();
- return true;
- }
- bool CBlock::CheckBlock() const
- {
- // These are checks that are independent of context
- // that can be verified before saving an orphan block.
- // Size limits
- if (vtx.empty() || vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(*this, SER_NETWORK) > MAX_BLOCK_SIZE)
- return error("CheckBlock() : size limits failed");
- // Check proof of work matches claimed amount
- if (!CheckProofOfWork(GetHash(), nBits))
- return error("CheckBlock() : proof of work failed");
- // Check timestamp
- if (GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
- return error("CheckBlock() : block timestamp too far in the future");
- // First transaction must be coinbase, the rest must not be
- if (vtx.empty() || !vtx[0].IsCoinBase())
- return error("CheckBlock() : first tx is not coinbase");
- for (int i = 1; i < vtx.size(); i++)
- if (vtx[i].IsCoinBase())
- return error("CheckBlock() : more than one coinbase");
- // Check transactions
- BOOST_FOREACH(const CTransaction& tx, vtx)
- if (!tx.CheckTransaction())
- return error("CheckBlock() : CheckTransaction failed");
- // Check that it's not full of nonstandard transactions
- if (GetSigOpCount() > MAX_BLOCK_SIGOPS)
- return error("CheckBlock() : too many nonstandard transactions");
- // Check merkleroot
- if (hashMerkleRoot != BuildMerkleTree())
- return error("CheckBlock() : hashMerkleRoot mismatch");
- return true;
- }
- bool CBlock::AcceptBlock()
- {
- // Check for duplicate
- uint256 hash = GetHash();
- if (mapBlockIndex.count(hash))
- return error("AcceptBlock() : block already in mapBlockIndex");
- // Get prev block index
- map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrevBlock);
- if (mi == mapBlockIndex.end())
- return error("AcceptBlock() : prev block not found");
- CBlockIndex* pindexPrev = (*mi).second;
- int nHeight = pindexPrev->nHeight+1;
- // Check proof of work
- if (nBits != GetNextWorkRequired(pindexPrev))
- return error("AcceptBlock() : incorrect proof of work");
- // Check timestamp against prev
- if (GetBlockTime() <= pindexPrev->GetMedianTimePast())
- return error("AcceptBlock() : block's timestamp is too early");
- // Check that all transactions are finalized
- BOOST_FOREACH(const CTransaction& tx, vtx)
- if (!tx.IsFinal(nHeight, GetBlockTime()))
- return error("AcceptBlock() : contains a non-final transaction");
- // Check that the block chain matches the known block chain up to a checkpoint
- if (!fTestNet)
- if ((nHeight == 11111 && hash != uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d")) ||
- (nHeight == 33333 && hash != uint256("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6")) ||
- (nHeight == 68555 && hash != uint256("0x00000000001e1b4903550a0b96e9a9405c8a95f387162e4944e8d9fbe501cd6a")) ||
- (nHeight == 70567 && hash != uint256("0x00000000006a49b14bcf27462068f1264c961f11fa2e0eddd2be0791e1d4124a")) ||
- (nHeight == 74000 && hash != uint256("0x0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20")) ||
- (nHeight == 105000 && hash != uint256("0x00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97")) ||
- (nHeight == 118000 && hash != uint256("0x000000000000774a7f8a7a12dc906ddb9e17e75d684f15e00f8767f9e8f36553")) ||
- (nHeight == 134444 && hash != uint256("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe")))
- return error("AcceptBlock() : rejected by checkpoint lockin at %d", nHeight);
- // Write block to history file
- if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK)))
- return error("AcceptBlock() : out of disk space");
- unsigned int nFile = -1;
- unsigned int nBlockPos = 0;
- if (!WriteToDisk(nFile, nBlockPos))
- return error("AcceptBlock() : WriteToDisk failed");
- if (!AddToBlockIndex(nFile, nBlockPos))
- return error("AcceptBlock() : AddToBlockIndex failed");
- // Relay inventory, but don't relay old inventory during initial block download
- if (hashBestChain == hash)
- CRITICAL_BLOCK(cs_vNodes)
- BOOST_FOREACH(CNode* pnode, vNodes)
- if (nBestHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 134444))
- pnode->PushInventory(CInv(MSG_BLOCK, hash));
- return true;
- }
- bool static ProcessBlock(CNode* pfrom, CBlock* pblock)
- {
- // Check for duplicate
- uint256 hash = pblock->GetHash();
- if (mapBlockIndex.count(hash))
- return error("ProcessBlock() : already have block %d %s", mapBlockIndex[hash]->nHeight, hash.ToString().substr(0,20).c_str());
- if (mapOrphanBlocks.count(hash))
- return error("ProcessBlock() : already have block (orphan) %s", hash.ToString().substr(0,20).c_str());
- // Preliminary checks
- if (!pblock->CheckBlock())
- return error("ProcessBlock() : CheckBlock FAILED");
- // If don't already have its previous block, shunt it off to holding area until we get it
- if (!mapBlockIndex.count(pblock->hashPrevBlock))
- {
- printf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().substr(0,20).c_str());
- CBlock* pblock2 = new CBlock(*pblock);
- mapOrphanBlocks.insert(make_pair(hash, pblock2));
- mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrevBlock, pblock2));
- // Ask this guy to fill in what we're missing
- if (pfrom)
- pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock2));
- return true;
- }
- // Store to disk
- if (!pblock->AcceptBlock())
- return error("ProcessBlock() : AcceptBlock FAILED");
- // Recursively process any orphan blocks that depended on this one
- vector<uint256> vWorkQueue;
- vWorkQueue.push_back(hash);
- for (int i = 0; i < vWorkQueue.size(); i++)
- {
- uint256 hashPrev = vWorkQueue[i];
- for (multimap<uint256, CBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
- mi != mapOrphanBlocksByPrev.upper_bound(hashPrev);
- ++mi)
- {
- CBlock* pblockOrphan = (*mi).second;
- if (pblockOrphan->AcceptBlock())
- vWorkQueue.push_back(pblockOrphan->GetHash());
- mapOrphanBlocks.erase(pblockOrphan->GetHash());
- delete pblockOrphan;
- }
- mapOrphanBlocksByPrev.erase(hashPrev);
- }
- printf("ProcessBlock: ACCEPTED\n");
- return true;
- }
- template<typename Stream>
- bool static ScanMessageStart(Stream& s)
- {
- // Scan ahead to the next pchMessageStart, which should normally be immediately
- // at the file pointer. Leaves file pointer at end of pchMessageStart.
- s.clear(0);
- short prevmask = s.exceptions(0);
- const char* p = BEGIN(pchMessageStart);
- try
- {
- loop
- {
- char c;
- s.read(&c, 1);
- if (s.fail())
- {
- s.clear(0);
- s.exceptions(prevmask);
- return false;
- }
- if (*p != c)
- p = BEGIN(pchMessageStart);
- if (*p == c)
- {
- if (++p == END(pchMessageStart))
- {
- s.clear(0);
- s.exceptions(prevmask);
- return true;
- }
- }
- }
- }
- catch (...)
- {
- s.clear(0);
- s.exceptions(prevmask);
- return false;
- }
- }
- bool CheckDiskSpace(uint64 nAdditionalBytes)
- {
- uint64 nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
- // Check for 15MB because database could create another 10MB log file at any time
- if (nFreeBytesAvailable < (uint64)15000000 + nAdditionalBytes)
- {
- fShutdown = true;
- string strMessage = _("Warning: Disk space is low ");
- strMiscWarning = strMessage;
- printf("*** %s\n", strMessage.c_str());
- ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION);
- CreateThread(Shutdown, NULL);
- return false;
- }
- return true;
- }
- FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode)
- {
- if (nFile == -1)
- return NULL;
- FILE* file = fopen(strprintf("%s/blk%04d.dat", GetDataDir().c_str(), nFile).c_str(), pszMode);
- if (!file)
- return NULL;
- if (nBlockPos != 0 && !strchr(pszMode, 'a') && !strchr(pszMode, 'w'))
- {
- if (fseek(file, nBlockPos, SEEK_SET) != 0)
- {
- fclose(file);
- return NULL;
- }
- }
- return file;
- }
- static unsigned int nCurrentBlockFile = 1;
- FILE* AppendBlockFile(unsigned int& nFileRet)
- {
- nFileRet = 0;
- loop
- {
- FILE* file = OpenBlockFile(nCurrentBlockFile, 0, "ab");
- if (!file)
- return NULL;
- if (fseek(file, 0, SEEK_END) != 0)
- return NULL;
- // FAT32 filesize max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
- if (ftell(file) < 0x7F000000 - MAX_SIZE)
- {
- nFileRet = nCurrentBlockFile;
- return file;
- }
- fclose(file);
- nCurrentBlockFile++;
- }
- }
- bool LoadBlockIndex(bool fAllowNew)
- {
- if (fTestNet)
- {
- hashGenesisBlock = uint256("0x00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008");
- bnProofOfWorkLimit = CBigNum(~uint256(0) >> 28);
- pchMessageStart[0] = 0xfa;
- pchMessageStart[1] = 0xbf;
- pchMessageStart[2] = 0xb5;
- pchMessageStart[3] = 0xda;
- }
- //
- // Load block index
- //
- CTxDB txdb("cr");
- if (!txdb.LoadBlockIndex())
- return false;
- txdb.Close();
- //
- // Init with genesis block
- //
- if (mapBlockIndex.empty())
- {
- if (!fAllowNew)
- return false;
- // Genesis Block:
- // CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
- // CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
- // CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
- // CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
- // vMerkleTree: 4a5e1e
- // Genesis block
- const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
- CTransaction txNew;
- txNew.vin.resize(1);
- txNew.vout.resize(1);
- txNew.vin[0].scriptSig = CScript() << 486604799 << CBigNum(4) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
- txNew.vout[0].nValue = 50 * COIN;
- txNew.vout[0].scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
- CBlock block;
- block.vtx.push_back(txNew);
- block.hashPrevBlock = 0;
- block.hashMerkleRoot = block.BuildMerkleTree();
- block.nVersion = 1;
- block.nTime = 1231006505;
- block.nBits = 0x1d00ffff;
- block.nNonce = 2083236893;
- if (fTestNet)
- {
- block.nTime = 1296688602;
- block.nBits = 0x1d07fff8;
- block.nNonce = 384568319;
- }
- //// debug print
- printf("%s\n", block.GetHash().ToString().c_str());
- printf("%s\n", hashGenesisBlock.ToString().c_str());
- printf("%s\n", block.hashMerkleRoot.ToString().c_str());
- assert(block.hashMerkleRoot == uint256("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
- block.print();
- assert(block.GetHash() == hashGenesisBlock);
- // Start new block file
- unsigned int nFile;
- unsigned int nBlockPos;
- if (!block.WriteToDisk(nFile, nBlockPos))
- return error("LoadBlockIndex() : writ…
Large files files are truncated, but you can click here to view the full file