2010-08-29 18:58:15 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
2014-02-08 22:50:24 +01:00
// Copyright (c) 2009-2014 The Bitcoin developers
2010-08-29 18:58:15 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
2012-05-18 16:02:28 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-04-13 07:13:08 +02:00
# include "main.h"
2013-08-27 07:51:57 +02:00
2013-04-13 07:13:08 +02:00
# include "addrman.h"
2012-08-28 23:04:54 +02:00
# include "alert.h"
2013-08-27 07:51:57 +02:00
# include "chainparams.h"
2011-09-08 22:50:58 +02:00
# include "checkpoints.h"
2013-08-27 07:51:57 +02:00
# include "checkqueue.h"
2011-05-15 23:52:31 +02:00
# include "init.h"
2013-08-27 07:51:57 +02:00
# include "net.h"
# include "txdb.h"
# include "txmempool.h"
2012-04-15 22:10:54 +02:00
# include "ui_interface.h"
2013-04-13 07:13:08 +02:00
# include "util.h"
2013-10-28 07:36:11 +01:00
# include <sstream>
2013-04-13 07:13:08 +02:00
# include <boost/algorithm/string/replace.hpp>
# include <boost/filesystem.hpp>
# include <boost/filesystem/fstream.hpp>
2010-08-29 18:58:15 +02:00
2011-05-15 09:11:04 +02:00
using namespace std ;
using namespace boost ;
2010-08-29 18:58:15 +02:00
2013-12-02 20:33:44 +01:00
# if defined(NDEBUG)
# error "Bitcoin cannot be compiled without assertions."
# endif
2010-08-29 18:58:15 +02:00
//
// Global state
//
CCriticalSection cs_main ;
2012-04-13 22:28:07 +02:00
CTxMemPool mempool ;
2010-08-29 18:58:15 +02:00
map < uint256 , CBlockIndex * > mapBlockIndex ;
2013-10-10 23:07:44 +02:00
CChain chainActive ;
2013-11-16 19:28:24 +01:00
CChain chainMostWork ;
2013-04-13 07:13:08 +02:00
int64_t nTimeBestReceived = 0 ;
2012-12-01 23:04:14 +01:00
int nScriptCheckThreads = 0 ;
2012-09-13 14:33:52 +02:00
bool fImporting = false ;
2012-10-21 21:23:13 +02:00
bool fReindex = false ;
2012-12-01 20:10:23 +01:00
bool fBenchmark = false ;
2013-01-11 01:47:57 +01:00
bool fTxIndex = false ;
2012-11-04 17:11:48 +01:00
unsigned int nCoinCacheSize = 5000 ;
2010-08-29 18:58:15 +02:00
2013-04-26 02:11:27 +02:00
/** Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) */
2013-04-13 07:13:08 +02:00
int64_t CTransaction : : nMinTxFee = 10000 ; // Override with -mintxfee
2014-03-10 18:59:12 +01:00
/** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */
2013-11-22 12:31:11 +01:00
int64_t CTransaction : : nMinRelayTxFee = 1000 ;
2013-04-26 02:11:27 +02:00
2013-10-19 15:55:08 +02:00
static CMedianFilter < int > cPeerBlockCounts ( 8 , 0 ) ; // Amount of blocks that other nodes claim to have
2011-09-28 21:35:58 +02:00
2014-01-11 23:33:34 +01:00
struct COrphanBlock {
uint256 hashBlock ;
uint256 hashPrev ;
vector < unsigned char > vchBlock ;
} ;
map < uint256 , COrphanBlock * > mapOrphanBlocks ;
multimap < uint256 , COrphanBlock * > mapOrphanBlocksByPrev ;
2010-08-29 18:58:15 +02:00
2013-08-02 07:14:44 +02:00
map < uint256 , CTransaction > mapOrphanTransactions ;
map < uint256 , set < uint256 > > mapOrphanTransactionsByPrev ;
2010-08-29 18:58:15 +02:00
2012-02-06 21:48:00 +01:00
// Constant stuff for coinbase transactions we create:
CScript COINBASE_FLAGS ;
2010-08-29 18:58:15 +02:00
2011-12-23 16:14:57 +01:00
const string strMessageMagic = " Bitcoin Signed Message: \n " ;
2013-10-14 02:13:44 +02:00
// Internal stuff
namespace {
struct CBlockIndexWorkComparator
{
bool operator ( ) ( CBlockIndex * pa , CBlockIndex * pb ) {
2013-11-16 19:28:24 +01:00
// First sort by most total work, ...
2013-10-14 02:13:44 +02:00
if ( pa - > nChainWork > pb - > nChainWork ) return false ;
if ( pa - > nChainWork < pb - > nChainWork ) return true ;
2011-03-26 13:01:27 +01:00
2013-11-16 19:28:24 +01:00
// ... then by earliest time received, ...
if ( pa - > nSequenceId < pb - > nSequenceId ) return false ;
if ( pa - > nSequenceId > pb - > nSequenceId ) return true ;
2013-10-14 02:13:44 +02:00
2013-11-16 19:28:24 +01:00
// Use pointer address as tie breaker (should only happen with blocks
// loaded from disk, as those all have id 0).
if ( pa < pb ) return false ;
if ( pa > pb ) return true ;
// Identical blocks.
return false ;
2013-10-14 02:13:44 +02:00
}
} ;
CBlockIndex * pindexBestInvalid ;
set < CBlockIndex * , CBlockIndexWorkComparator > setBlockIndexValid ; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
2011-03-26 13:01:27 +01:00
2013-10-14 02:13:44 +02:00
CCriticalSection cs_LastBlockFile ;
CBlockFileInfo infoLastBlockFile ;
int nLastBlockFile = 0 ;
2013-11-16 19:28:24 +01:00
// Every received block is assigned a unique and increasing identifier, so we
// know which one to give priority in case of a fork.
CCriticalSection cs_nBlockSequenceId ;
// Blocks loaded from disk are assigned id 0, so start the counter at 1.
uint32_t nBlockSequenceId = 1 ;
// Sources of received blocks, to be able to send them reject messages or ban
// them, if processing happens afterwards. Protected by cs_main.
map < uint256 , NodeId > mapBlockSource ;
2014-01-10 13:23:26 +01:00
// Blocks that are in flight, and that are in the queue to be downloaded.
// Protected by cs_main.
struct QueuedBlock {
uint256 hash ;
int64_t nTime ; // Time of "getdata" request in microseconds.
int nQueuedBefore ; // Number of blocks in flight at the time of request.
} ;
map < uint256 , pair < NodeId , list < QueuedBlock > : : iterator > > mapBlocksInFlight ;
map < uint256 , pair < NodeId , list < uint256 > : : iterator > > mapBlocksToDownload ;
2013-10-14 02:13:44 +02:00
}
2010-08-29 18:58:15 +02:00
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
//////////////////////////////////////////////////////////////////////////////
//
// dispatching functions
//
2011-11-07 00:05:42 +01:00
// These functions dispatch to one or all registered wallets
2013-10-19 18:34:06 +02:00
namespace {
struct CMainSignals {
// Notifies listeners of updated transaction data (passing hash, transaction, and optionally the block it is found in.
boost : : signals2 : : signal < void ( const uint256 & , const CTransaction & , const CBlock * ) > SyncTransaction ;
// Notifies listeners of an erased transaction (currently disabled, requires transaction replacement).
boost : : signals2 : : signal < void ( const uint256 & ) > EraseTransaction ;
// Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible).
boost : : signals2 : : signal < void ( const uint256 & ) > UpdatedTransaction ;
// Notifies listeners of a new active block chain.
boost : : signals2 : : signal < void ( const CBlockLocator & ) > SetBestChain ;
// Notifies listeners about an inventory item being seen on the network.
boost : : signals2 : : signal < void ( const uint256 & ) > Inventory ;
// Tells listeners to broadcast their data.
boost : : signals2 : : signal < void ( ) > Broadcast ;
} g_signals ;
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
}
2013-10-19 18:34:06 +02:00
void RegisterWallet ( CWalletInterface * pwalletIn ) {
g_signals . SyncTransaction . connect ( boost : : bind ( & CWalletInterface : : SyncTransaction , pwalletIn , _1 , _2 , _3 ) ) ;
g_signals . EraseTransaction . connect ( boost : : bind ( & CWalletInterface : : EraseFromWallet , pwalletIn , _1 ) ) ;
g_signals . UpdatedTransaction . connect ( boost : : bind ( & CWalletInterface : : UpdatedTransaction , pwalletIn , _1 ) ) ;
g_signals . SetBestChain . connect ( boost : : bind ( & CWalletInterface : : SetBestChain , pwalletIn , _1 ) ) ;
g_signals . Inventory . connect ( boost : : bind ( & CWalletInterface : : Inventory , pwalletIn , _1 ) ) ;
g_signals . Broadcast . connect ( boost : : bind ( & CWalletInterface : : ResendWalletTransactions , pwalletIn ) ) ;
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
}
2013-10-19 18:34:06 +02:00
void UnregisterWallet ( CWalletInterface * pwalletIn ) {
g_signals . Broadcast . disconnect ( boost : : bind ( & CWalletInterface : : ResendWalletTransactions , pwalletIn ) ) ;
g_signals . Inventory . disconnect ( boost : : bind ( & CWalletInterface : : Inventory , pwalletIn , _1 ) ) ;
g_signals . SetBestChain . disconnect ( boost : : bind ( & CWalletInterface : : SetBestChain , pwalletIn , _1 ) ) ;
g_signals . UpdatedTransaction . disconnect ( boost : : bind ( & CWalletInterface : : UpdatedTransaction , pwalletIn , _1 ) ) ;
g_signals . EraseTransaction . disconnect ( boost : : bind ( & CWalletInterface : : EraseFromWallet , pwalletIn , _1 ) ) ;
g_signals . SyncTransaction . disconnect ( boost : : bind ( & CWalletInterface : : SyncTransaction , pwalletIn , _1 , _2 , _3 ) ) ;
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
}
2013-10-19 18:34:06 +02:00
void UnregisterAllWallets ( ) {
g_signals . Broadcast . disconnect_all_slots ( ) ;
g_signals . Inventory . disconnect_all_slots ( ) ;
g_signals . SetBestChain . disconnect_all_slots ( ) ;
g_signals . UpdatedTransaction . disconnect_all_slots ( ) ;
g_signals . EraseTransaction . disconnect_all_slots ( ) ;
g_signals . SyncTransaction . disconnect_all_slots ( ) ;
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
}
2013-10-19 18:34:06 +02:00
void SyncWithWallets ( const uint256 & hash , const CTransaction & tx , const CBlock * pblock ) {
g_signals . SyncTransaction ( hash , tx , pblock ) ;
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
}
2013-06-06 05:21:41 +02:00
//////////////////////////////////////////////////////////////////////////////
//
// Registration of network node signals.
//
2013-11-18 01:25:17 +01:00
namespace {
2013-11-16 19:28:24 +01:00
struct CBlockReject {
unsigned char chRejectCode ;
string strRejectReason ;
uint256 hashBlock ;
} ;
2013-11-18 01:25:17 +01:00
// Maintain validation-specific state about nodes, protected by cs_main, instead
// by CNode's own locks. This simplifies asynchronous operation, where
// processing of incoming data is done after the ProcessMessage call returns,
// and we're no longer holding the node's locks.
struct CNodeState {
2013-11-16 19:28:24 +01:00
// Accumulated misbehaviour score for this peer.
2013-11-18 01:25:17 +01:00
int nMisbehavior ;
2013-11-16 19:28:24 +01:00
// Whether this peer should be disconnected and banned.
2013-11-18 01:25:17 +01:00
bool fShouldBan ;
2013-11-16 19:28:24 +01:00
// String name of this peer (debugging/logging purposes).
2013-11-18 01:25:17 +01:00
std : : string name ;
2013-11-16 19:28:24 +01:00
// List of asynchronously-determined block rejections to notify this peer about.
std : : vector < CBlockReject > rejects ;
2014-01-10 13:23:26 +01:00
list < QueuedBlock > vBlocksInFlight ;
int nBlocksInFlight ;
list < uint256 > vBlocksToDownload ;
int nBlocksToDownload ;
int64_t nLastBlockReceive ;
int64_t nLastBlockProcess ;
2013-11-18 01:25:17 +01:00
CNodeState ( ) {
nMisbehavior = 0 ;
fShouldBan = false ;
2014-01-10 13:23:26 +01:00
nBlocksToDownload = 0 ;
nBlocksInFlight = 0 ;
nLastBlockReceive = 0 ;
nLastBlockProcess = 0 ;
2013-11-18 01:25:17 +01:00
}
} ;
2013-11-16 19:28:24 +01:00
// Map maintaining per-node state. Requires cs_main.
2013-11-18 01:25:17 +01:00
map < NodeId , CNodeState > mapNodeState ;
// Requires cs_main.
CNodeState * State ( NodeId pnode ) {
map < NodeId , CNodeState > : : iterator it = mapNodeState . find ( pnode ) ;
if ( it = = mapNodeState . end ( ) )
return NULL ;
return & it - > second ;
}
int GetHeight ( )
2013-10-10 23:07:44 +02:00
{
LOCK ( cs_main ) ;
return chainActive . Height ( ) ;
}
2013-11-18 01:25:17 +01:00
void InitializeNode ( NodeId nodeid , const CNode * pnode ) {
LOCK ( cs_main ) ;
CNodeState & state = mapNodeState . insert ( std : : make_pair ( nodeid , CNodeState ( ) ) ) . first - > second ;
state . name = pnode - > addrName ;
}
void FinalizeNode ( NodeId nodeid ) {
LOCK ( cs_main ) ;
2014-01-10 13:23:26 +01:00
CNodeState * state = State ( nodeid ) ;
BOOST_FOREACH ( const QueuedBlock & entry , state - > vBlocksInFlight )
mapBlocksInFlight . erase ( entry . hash ) ;
BOOST_FOREACH ( const uint256 & hash , state - > vBlocksToDownload )
mapBlocksToDownload . erase ( hash ) ;
2013-11-18 01:25:17 +01:00
mapNodeState . erase ( nodeid ) ;
}
2014-01-10 13:23:26 +01:00
// Requires cs_main.
void MarkBlockAsReceived ( const uint256 & hash , NodeId nodeFrom = - 1 ) {
map < uint256 , pair < NodeId , list < uint256 > : : iterator > > : : iterator itToDownload = mapBlocksToDownload . find ( hash ) ;
if ( itToDownload ! = mapBlocksToDownload . end ( ) ) {
CNodeState * state = State ( itToDownload - > second . first ) ;
state - > vBlocksToDownload . erase ( itToDownload - > second . second ) ;
state - > nBlocksToDownload - - ;
mapBlocksToDownload . erase ( itToDownload ) ;
}
map < uint256 , pair < NodeId , list < QueuedBlock > : : iterator > > : : iterator itInFlight = mapBlocksInFlight . find ( hash ) ;
if ( itInFlight ! = mapBlocksInFlight . end ( ) ) {
CNodeState * state = State ( itInFlight - > second . first ) ;
state - > vBlocksInFlight . erase ( itInFlight - > second . second ) ;
state - > nBlocksInFlight - - ;
if ( itInFlight - > second . first = = nodeFrom )
state - > nLastBlockReceive = GetTimeMicros ( ) ;
mapBlocksInFlight . erase ( itInFlight ) ;
}
}
// Requires cs_main.
bool AddBlockToQueue ( NodeId nodeid , const uint256 & hash ) {
if ( mapBlocksToDownload . count ( hash ) | | mapBlocksInFlight . count ( hash ) )
return false ;
CNodeState * state = State ( nodeid ) ;
if ( state = = NULL )
return false ;
list < uint256 > : : iterator it = state - > vBlocksToDownload . insert ( state - > vBlocksToDownload . end ( ) , hash ) ;
state - > nBlocksToDownload + + ;
if ( state - > nBlocksToDownload > 5000 )
Misbehaving ( nodeid , 10 ) ;
mapBlocksToDownload [ hash ] = std : : make_pair ( nodeid , it ) ;
return true ;
}
// Requires cs_main.
void MarkBlockAsInFlight ( NodeId nodeid , const uint256 & hash ) {
CNodeState * state = State ( nodeid ) ;
assert ( state ! = NULL ) ;
// Make sure it's not listed somewhere already.
MarkBlockAsReceived ( hash ) ;
QueuedBlock newentry = { hash , GetTimeMicros ( ) , state - > nBlocksInFlight } ;
if ( state - > nBlocksInFlight = = 0 )
state - > nLastBlockReceive = newentry . nTime ; // Reset when a first request is sent.
list < QueuedBlock > : : iterator it = state - > vBlocksInFlight . insert ( state - > vBlocksInFlight . end ( ) , newentry ) ;
state - > nBlocksInFlight + + ;
mapBlocksInFlight [ hash ] = std : : make_pair ( nodeid , it ) ;
}
2013-11-18 01:25:17 +01:00
}
bool GetNodeStateStats ( NodeId nodeid , CNodeStateStats & stats ) {
LOCK ( cs_main ) ;
CNodeState * state = State ( nodeid ) ;
if ( state = = NULL )
return false ;
stats . nMisbehavior = state - > nMisbehavior ;
return true ;
}
2013-06-06 05:21:41 +02:00
void RegisterNodeSignals ( CNodeSignals & nodeSignals )
{
2013-10-10 23:07:44 +02:00
nodeSignals . GetHeight . connect ( & GetHeight ) ;
2013-06-06 05:21:41 +02:00
nodeSignals . ProcessMessages . connect ( & ProcessMessages ) ;
nodeSignals . SendMessages . connect ( & SendMessages ) ;
2013-11-18 01:25:17 +01:00
nodeSignals . InitializeNode . connect ( & InitializeNode ) ;
nodeSignals . FinalizeNode . connect ( & FinalizeNode ) ;
2013-06-06 05:21:41 +02:00
}
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
2013-06-06 05:21:41 +02:00
void UnregisterNodeSignals ( CNodeSignals & nodeSignals )
{
2013-10-10 23:07:44 +02:00
nodeSignals . GetHeight . disconnect ( & GetHeight ) ;
2013-06-06 05:21:41 +02:00
nodeSignals . ProcessMessages . disconnect ( & ProcessMessages ) ;
nodeSignals . SendMessages . disconnect ( & SendMessages ) ;
2013-11-18 01:25:17 +01:00
nodeSignals . InitializeNode . disconnect ( & InitializeNode ) ;
nodeSignals . FinalizeNode . disconnect ( & FinalizeNode ) ;
2013-06-06 05:21:41 +02:00
}
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
2013-05-07 13:59:29 +02:00
//////////////////////////////////////////////////////////////////////////////
//
2013-10-12 15:18:08 +02:00
// CChain implementation
2013-05-07 13:59:29 +02:00
//
2013-10-12 15:18:08 +02:00
CBlockIndex * CChain : : SetTip ( CBlockIndex * pindex ) {
if ( pindex = = NULL ) {
vChain . clear ( ) ;
return NULL ;
}
vChain . resize ( pindex - > nHeight + 1 ) ;
while ( pindex & & vChain [ pindex - > nHeight ] ! = pindex ) {
vChain [ pindex - > nHeight ] = pindex ;
pindex = pindex - > pprev ;
}
return pindex ;
2013-05-07 13:59:29 +02:00
}
2013-10-12 15:18:08 +02:00
CBlockLocator CChain : : GetLocator ( const CBlockIndex * pindex ) const {
2013-05-07 13:59:29 +02:00
int nStep = 1 ;
2013-10-12 15:18:08 +02:00
std : : vector < uint256 > vHave ;
vHave . reserve ( 32 ) ;
2013-05-07 13:59:29 +02:00
2013-10-12 15:18:08 +02:00
if ( ! pindex )
pindex = Tip ( ) ;
while ( pindex ) {
vHave . push_back ( pindex - > GetBlockHash ( ) ) ;
// Stop when we have added the genesis block.
if ( pindex - > nHeight = = 0 )
break ;
// Exponentially larger steps back, plus the genesis block.
int nHeight = std : : max ( pindex - > nHeight - nStep , 0 ) ;
// In case pindex is not in this chain, iterate pindex->pprev to find blocks.
while ( pindex - > nHeight > nHeight & & ! Contains ( pindex ) )
2013-05-07 13:59:29 +02:00
pindex = pindex - > pprev ;
2013-10-12 15:18:08 +02:00
// If pindex is in this chain, use direct height-based access.
if ( pindex - > nHeight > nHeight )
pindex = ( * this ) [ nHeight ] ;
2013-05-07 13:59:29 +02:00
if ( vHave . size ( ) > 10 )
nStep * = 2 ;
}
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
2013-10-12 15:18:08 +02:00
return CBlockLocator ( vHave ) ;
2013-05-07 13:59:29 +02:00
}
2013-10-12 15:18:08 +02:00
CBlockIndex * CChain : : FindFork ( const CBlockLocator & locator ) const {
2013-05-07 13:59:29 +02:00
// Find the first block the caller has in the main chain
2013-10-12 15:18:08 +02:00
BOOST_FOREACH ( const uint256 & hash , locator . vHave ) {
2013-05-07 13:59:29 +02:00
std : : map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . find ( hash ) ;
if ( mi ! = mapBlockIndex . end ( ) )
{
CBlockIndex * pindex = ( * mi ) . second ;
2013-10-12 15:18:08 +02:00
if ( Contains ( pindex ) )
2013-05-07 13:59:29 +02:00
return pindex ;
}
}
2013-10-12 15:18:08 +02:00
return Genesis ( ) ;
2013-10-10 23:07:44 +02:00
}
2012-07-06 16:33:34 +02:00
CCoinsViewCache * pcoinsTip = NULL ;
2012-09-03 15:26:57 +02:00
CBlockTreeDB * pblocktree = NULL ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2010-08-29 18:58:15 +02:00
//////////////////////////////////////////////////////////////////////////////
//
// mapOrphanTransactions
//
2013-08-02 07:14:44 +02:00
bool AddOrphanTx ( const CTransaction & tx )
2010-08-29 18:58:15 +02:00
{
uint256 hash = tx . GetHash ( ) ;
if ( mapOrphanTransactions . count ( hash ) )
2012-05-15 21:53:30 +02:00
return false ;
// Ignore big transactions, to avoid a
// send-big-orphans memory exhaustion attack. If a peer has a legitimate
// large transaction with a missing parent then we assume
// it will rebroadcast it later, after the parent transaction(s)
// have been mined or received.
// 10,000 orphans, each of which is at most 5,000 bytes big is
// at most 500 megabytes of orphans:
2013-08-02 07:14:44 +02:00
unsigned int sz = tx . GetSerializeSize ( SER_NETWORK , CTransaction : : CURRENT_VERSION ) ;
if ( sz > 5000 )
2012-05-15 21:53:30 +02:00
{
2014-01-16 16:15:27 +01:00
LogPrint ( " mempool " , " ignoring large orphan tx (size: %u, hash: %s) \n " , sz , hash . ToString ( ) ) ;
2012-05-15 21:53:30 +02:00
return false ;
}
2012-02-29 16:14:18 +01:00
2013-08-02 07:14:44 +02:00
mapOrphanTransactions [ hash ] = tx ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( const CTxIn & txin , tx . vin )
2013-08-02 07:14:44 +02:00
mapOrphanTransactionsByPrev [ txin . prevout . hash ] . insert ( hash ) ;
2012-05-15 21:53:30 +02:00
2014-01-16 16:15:27 +01:00
LogPrint ( " mempool " , " stored orphan tx %s (mapsz % " PRIszu " ) \n " , hash . ToString ( ) ,
2012-05-15 21:53:30 +02:00
mapOrphanTransactions . size ( ) ) ;
return true ;
2010-08-29 18:58:15 +02:00
}
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
void static EraseOrphanTx ( uint256 hash )
2010-08-29 18:58:15 +02:00
{
if ( ! mapOrphanTransactions . count ( hash ) )
return ;
2013-08-02 07:14:44 +02:00
const CTransaction & tx = mapOrphanTransactions [ hash ] ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( const CTxIn & txin , tx . vin )
2010-08-29 18:58:15 +02:00
{
2012-05-15 21:53:30 +02:00
mapOrphanTransactionsByPrev [ txin . prevout . hash ] . erase ( hash ) ;
if ( mapOrphanTransactionsByPrev [ txin . prevout . hash ] . empty ( ) )
mapOrphanTransactionsByPrev . erase ( txin . prevout . hash ) ;
2010-08-29 18:58:15 +02:00
}
mapOrphanTransactions . erase ( hash ) ;
}
2012-04-23 20:14:03 +02:00
unsigned int LimitOrphanTxSize ( unsigned int nMaxOrphans )
2012-02-29 16:14:18 +01:00
{
2012-04-23 20:14:03 +02:00
unsigned int nEvicted = 0 ;
2012-02-29 16:14:18 +01:00
while ( mapOrphanTransactions . size ( ) > nMaxOrphans )
{
// Evict a random orphan:
2012-05-17 18:13:14 +02:00
uint256 randomhash = GetRandHash ( ) ;
2013-08-02 07:14:44 +02:00
map < uint256 , CTransaction > : : iterator it = mapOrphanTransactions . lower_bound ( randomhash ) ;
2012-02-29 16:14:18 +01:00
if ( it = = mapOrphanTransactions . end ( ) )
it = mapOrphanTransactions . begin ( ) ;
EraseOrphanTx ( it - > first ) ;
+ + nEvicted ;
}
return nEvicted ;
}
2010-08-29 18:58:15 +02:00
2013-06-23 08:05:25 +02:00
bool IsStandardTx ( const CTransaction & tx , string & reason )
2013-04-26 02:11:27 +02:00
{
2013-09-09 11:11:11 +02:00
if ( tx . nVersion > CTransaction : : CURRENT_VERSION | | tx . nVersion < 1 ) {
2013-06-23 08:05:25 +02:00
reason = " version " ;
2012-06-27 18:43:19 +02:00
return false ;
2013-06-23 08:05:25 +02:00
}
2012-06-27 18:43:19 +02:00
2014-01-27 03:50:15 +01:00
// Treat non-final transactions as non-standard to prevent a specific type
// of double-spend attack, as well as DoS attacks. (if the transaction
// can't be mined, the attacker isn't expending resources broadcasting it)
// Basically we don't want to propagate transactions that can't included in
// the next block.
//
// However, IsFinalTx() is confusing... Without arguments, it uses
// chainActive.Height() to evaluate nLockTime; when a block is accepted, chainActive.Height()
// is set to the value of nHeight in the block. However, when IsFinalTx()
// is called within CBlock::AcceptBlock(), the height of the block *being*
// evaluated is what is used. Thus if we want to know if a transaction can
// be part of the *next* block, we need to call IsFinalTx() with one more
// than chainActive.Height().
//
// Timestamps on the other hand don't get any special treatment, because we
// can't know what timestamp the next block will have, and there aren't
// timestamp applications where it matters.
if ( ! IsFinalTx ( tx , chainActive . Height ( ) + 1 ) ) {
2013-06-23 08:05:25 +02:00
reason = " non-final " ;
2013-01-26 20:38:40 +01:00
return false ;
2013-06-23 08:05:25 +02:00
}
2013-01-26 20:38:40 +01:00
2013-02-04 22:56:26 +01:00
// Extremely large transactions with lots of inputs can cost the network
// almost as much to process as they cost the sender in fees, because
// computing signature hashes is O(ninputs*txsize). Limiting transactions
// to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
2013-01-08 13:17:15 +01:00
unsigned int sz = tx . GetSerializeSize ( SER_NETWORK , CTransaction : : CURRENT_VERSION ) ;
2013-06-23 08:05:25 +02:00
if ( sz > = MAX_STANDARD_TX_SIZE ) {
reason = " tx-size " ;
2013-02-04 22:56:26 +01:00
return false ;
2013-06-23 08:05:25 +02:00
}
2013-02-04 22:56:26 +01:00
2013-01-08 13:17:15 +01:00
BOOST_FOREACH ( const CTxIn & txin , tx . vin )
2011-10-03 19:05:43 +02:00
{
2011-11-08 19:20:29 +01:00
// Biggest 'standard' txin is a 3-signature 3-of-3 CHECKMULTISIG
2012-01-05 03:40:52 +01:00
// pay-to-script-hash, which is 3 ~80-byte signatures, 3
2011-10-03 19:05:43 +02:00
// ~65-byte public keys, plus a few script ops.
2013-06-23 08:05:25 +02:00
if ( txin . scriptSig . size ( ) > 500 ) {
reason = " scriptsig-size " ;
2012-01-05 03:40:52 +01:00
return false ;
2013-06-23 08:05:25 +02:00
}
if ( ! txin . scriptSig . IsPushOnly ( ) ) {
reason = " scriptsig-not-pushonly " ;
2012-01-05 03:40:52 +01:00
return false ;
2013-09-24 00:48:00 +02:00
}
if ( ! txin . scriptSig . HasCanonicalPushes ( ) ) {
2014-02-13 15:12:18 +01:00
reason = " scriptsig-non-canonical-push " ;
2013-09-24 00:48:00 +02:00
return false ;
2013-06-23 08:05:25 +02:00
}
2011-10-03 19:05:43 +02:00
}
2013-06-24 21:09:50 +02:00
unsigned int nDataOut = 0 ;
txnouttype whichType ;
2013-01-08 13:17:15 +01:00
BOOST_FOREACH ( const CTxOut & txout , tx . vout ) {
2013-06-24 21:09:50 +02:00
if ( ! : : IsStandard ( txout . scriptPubKey , whichType ) ) {
2013-06-23 08:05:25 +02:00
reason = " scriptpubkey " ;
2012-01-05 03:40:52 +01:00
return false ;
2013-06-23 08:05:25 +02:00
}
2013-06-24 21:09:50 +02:00
if ( whichType = = TX_NULL_DATA )
nDataOut + + ;
else if ( txout . IsDust ( CTransaction : : nMinRelayTxFee ) ) {
2013-06-23 08:05:25 +02:00
reason = " dust " ;
2012-08-24 12:14:48 +02:00
return false ;
2013-06-23 08:05:25 +02:00
}
2012-08-24 12:14:48 +02:00
}
2013-06-23 08:05:25 +02:00
2013-06-24 21:09:50 +02:00
// only one OP_RETURN txout is permitted
if ( nDataOut > 1 ) {
2014-01-27 12:13:00 +01:00
reason = " multi-op-return " ;
2013-06-24 21:09:50 +02:00
return false ;
}
2011-10-03 19:05:43 +02:00
return true ;
}
2013-04-13 07:13:08 +02:00
bool IsFinalTx ( const CTransaction & tx , int nBlockHeight , int64_t nBlockTime )
2013-01-08 13:17:15 +01:00
{
// Time based nLockTime implemented in 0.1.6
if ( tx . nLockTime = = 0 )
return true ;
if ( nBlockHeight = = 0 )
2013-10-10 23:07:44 +02:00
nBlockHeight = chainActive . Height ( ) ;
2013-01-08 13:17:15 +01:00
if ( nBlockTime = = 0 )
nBlockTime = GetAdjustedTime ( ) ;
2013-04-13 07:13:08 +02:00
if ( ( int64_t ) tx . nLockTime < ( ( int64_t ) tx . nLockTime < LOCKTIME_THRESHOLD ? ( int64_t ) nBlockHeight : nBlockTime ) )
2013-01-08 13:17:15 +01:00
return true ;
BOOST_FOREACH ( const CTxIn & txin , tx . vin )
if ( ! txin . IsFinal ( ) )
return false ;
return true ;
}
2011-10-03 19:05:43 +02:00
//
// Check transaction inputs, and make sure any
2012-01-05 03:40:52 +01:00
// pay-to-script-hash transactions are evaluating IsStandard scripts
2011-10-03 19:05:43 +02:00
//
// Why bother? To avoid denial-of-service attacks; an attacker
2012-01-05 03:40:52 +01:00
// can submit a standard HASH... OP_EQUAL transaction,
// which will get accepted into blocks. The redemption
// script can be anything; an attacker could use a very
2011-10-03 19:05:43 +02:00
// expensive-to-check-upon-redemption script like:
// DUP CHECKSIG DROP ... repeated 100 times... OP_1
//
2013-01-08 13:17:15 +01:00
bool AreInputsStandard ( const CTransaction & tx , CCoinsViewCache & mapInputs )
2011-10-03 19:05:43 +02:00
{
2013-01-08 13:17:15 +01:00
if ( tx . IsCoinBase ( ) )
2012-01-21 02:59:04 +01:00
return true ; // Coinbases don't use vin normally
2012-01-11 02:18:00 +01:00
2013-01-08 13:17:15 +01:00
for ( unsigned int i = 0 ; i < tx . vin . size ( ) ; i + + )
2011-10-03 19:05:43 +02:00
{
2013-01-08 13:17:15 +01:00
const CTxOut & prev = mapInputs . GetOutputFor ( tx . vin [ i ] ) ;
2011-10-03 19:05:43 +02:00
vector < vector < unsigned char > > vSolutions ;
2011-11-08 19:20:29 +01:00
txnouttype whichType ;
// get the scriptPubKey corresponding to this input:
2012-01-11 02:18:00 +01:00
const CScript & prevScript = prev . scriptPubKey ;
2011-11-08 19:20:29 +01:00
if ( ! Solver ( prevScript , whichType , vSolutions ) )
2012-01-05 03:40:52 +01:00
return false ;
2012-01-19 19:30:54 +01:00
int nArgsExpected = ScriptSigArgsExpected ( whichType , vSolutions ) ;
2012-04-22 19:44:12 +02:00
if ( nArgsExpected < 0 )
return false ;
2012-01-19 19:30:54 +01:00
// Transactions with extra stuff in their scriptSigs are
// non-standard. Note that this EvalScript() call will
// be quick, because if there are any operations
// beside "push data" in the scriptSig the
// IsStandard() call returns false
vector < vector < unsigned char > > stack ;
2013-01-08 13:17:15 +01:00
if ( ! EvalScript ( stack , tx . vin [ i ] . scriptSig , tx , i , false , 0 ) )
2012-01-19 19:30:54 +01:00
return false ;
2011-10-03 19:05:43 +02:00
if ( whichType = = TX_SCRIPTHASH )
{
2012-01-05 03:40:52 +01:00
if ( stack . empty ( ) )
2011-10-03 19:05:43 +02:00
return false ;
2011-11-08 19:20:29 +01:00
CScript subscript ( stack . back ( ) . begin ( ) , stack . back ( ) . end ( ) ) ;
2012-01-19 19:30:54 +01:00
vector < vector < unsigned char > > vSolutions2 ;
txnouttype whichType2 ;
if ( ! Solver ( subscript , whichType2 , vSolutions2 ) )
2012-01-05 03:40:52 +01:00
return false ;
2012-01-19 19:30:54 +01:00
if ( whichType2 = = TX_SCRIPTHASH )
return false ;
2012-04-22 19:44:12 +02:00
int tmpExpected ;
tmpExpected = ScriptSigArgsExpected ( whichType2 , vSolutions2 ) ;
if ( tmpExpected < 0 )
return false ;
nArgsExpected + = tmpExpected ;
2011-10-03 19:05:43 +02:00
}
2012-01-19 19:30:54 +01:00
2012-04-22 19:44:12 +02:00
if ( stack . size ( ) ! = ( unsigned int ) nArgsExpected )
2012-01-19 19:30:54 +01:00
return false ;
2011-10-03 19:05:43 +02:00
}
return true ;
}
2013-01-08 13:17:15 +01:00
unsigned int GetLegacySigOpCount ( const CTransaction & tx )
2012-01-05 03:40:52 +01:00
{
2012-04-23 20:14:03 +02:00
unsigned int nSigOps = 0 ;
2013-01-08 13:17:15 +01:00
BOOST_FOREACH ( const CTxIn & txin , tx . vin )
2012-01-05 03:40:52 +01:00
{
nSigOps + = txin . scriptSig . GetSigOpCount ( false ) ;
}
2013-01-08 13:17:15 +01:00
BOOST_FOREACH ( const CTxOut & txout , tx . vout )
2012-01-05 03:40:52 +01:00
{
nSigOps + = txout . scriptPubKey . GetSigOpCount ( false ) ;
}
return nSigOps ;
}
2010-08-29 18:58:15 +02:00
2013-01-08 13:17:15 +01:00
unsigned int GetP2SHSigOpCount ( const CTransaction & tx , CCoinsViewCache & inputs )
{
if ( tx . IsCoinBase ( ) )
return 0 ;
unsigned int nSigOps = 0 ;
for ( unsigned int i = 0 ; i < tx . vin . size ( ) ; i + + )
{
const CTxOut & prevout = inputs . GetOutputFor ( tx . vin [ i ] ) ;
if ( prevout . scriptPubKey . IsPayToScriptHash ( ) )
nSigOps + = prevout . scriptPubKey . GetSigOpCount ( tx . vin [ i ] . scriptSig ) ;
}
return nSigOps ;
}
2010-08-29 18:58:15 +02:00
int CMerkleTx : : SetMerkleBranch ( const CBlock * pblock )
{
2013-01-09 19:39:52 +01:00
CBlock blockTmp ;
if ( pblock = = NULL ) {
CCoins coins ;
if ( pcoinsTip - > GetCoins ( GetHash ( ) , coins ) ) {
2013-10-10 23:07:44 +02:00
CBlockIndex * pindex = chainActive [ coins . nHeight ] ;
2013-01-09 19:39:52 +01:00
if ( pindex ) {
2013-06-24 03:10:02 +02:00
if ( ! ReadBlockFromDisk ( blockTmp , pindex ) )
2013-01-09 19:39:52 +01:00
return 0 ;
pblock = & blockTmp ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
}
2010-08-29 18:58:15 +02:00
}
2013-01-09 19:39:52 +01:00
}
2010-08-29 18:58:15 +02:00
2013-01-09 19:39:52 +01:00
if ( pblock ) {
2010-08-29 18:58:15 +02:00
// Update the tx's hashBlock
hashBlock = pblock - > GetHash ( ) ;
// Locate the transaction
2012-04-22 19:51:16 +02:00
for ( nIndex = 0 ; nIndex < ( int ) pblock - > vtx . size ( ) ; nIndex + + )
2010-08-29 18:58:15 +02:00
if ( pblock - > vtx [ nIndex ] = = * ( CTransaction * ) this )
break ;
2012-04-22 19:51:16 +02:00
if ( nIndex = = ( int ) pblock - > vtx . size ( ) )
2010-08-29 18:58:15 +02:00
{
vMerkleBranch . clear ( ) ;
nIndex = - 1 ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " ERROR: SetMerkleBranch() : couldn't find tx in block \n " ) ;
2010-08-29 18:58:15 +02:00
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 ;
2013-10-10 23:07:44 +02:00
if ( ! pindex | | ! chainActive . Contains ( pindex ) )
2010-08-29 18:58:15 +02:00
return 0 ;
2013-10-10 23:07:44 +02:00
return chainActive . Height ( ) - pindex - > nHeight + 1 ;
2010-08-29 18:58:15 +02:00
}
2013-01-08 13:17:15 +01:00
bool CheckTransaction ( const CTransaction & tx , CValidationState & state )
2010-09-30 18:23:07 +02:00
{
// Basic checks that don't depend on any context
2013-01-08 13:17:15 +01:00
if ( tx . vin . empty ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 10 , error ( " CheckTransaction() : vin empty " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-vin-empty " ) ;
2013-01-08 13:17:15 +01:00
if ( tx . vout . empty ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 10 , error ( " CheckTransaction() : vout empty " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-vout-empty " ) ;
2010-09-30 18:23:07 +02:00
// Size limits
2013-01-08 13:17:15 +01:00
if ( : : GetSerializeSize ( tx , SER_NETWORK , PROTOCOL_VERSION ) > MAX_BLOCK_SIZE )
2013-11-22 02:14:57 +01:00
return state . DoS ( 100 , error ( " CheckTransaction() : size limits failed " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-oversize " ) ;
2010-09-30 18:23:07 +02:00
// Check for negative or overflow output values
2013-04-13 07:13:08 +02:00
int64_t nValueOut = 0 ;
2013-01-08 13:17:15 +01:00
BOOST_FOREACH ( const CTxOut & txout , tx . vout )
2010-09-30 18:23:07 +02:00
{
if ( txout . nValue < 0 )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckTransaction() : txout.nValue negative " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-vout-negative " ) ;
2010-09-30 18:23:07 +02:00
if ( txout . nValue > MAX_MONEY )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckTransaction() : txout.nValue too high " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-vout-toolarge " ) ;
2010-09-30 18:23:07 +02:00
nValueOut + = txout . nValue ;
if ( ! MoneyRange ( nValueOut ) )
2013-11-22 02:14:57 +01:00
return state . DoS ( 100 , error ( " CheckTransaction() : txout total out of range " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-txouttotal-toolarge " ) ;
2010-09-30 18:23:07 +02:00
}
2011-07-30 23:01:45 +02:00
// Check for duplicate inputs
set < COutPoint > vInOutPoints ;
2013-01-08 13:17:15 +01:00
BOOST_FOREACH ( const CTxIn & txin , tx . vin )
2011-07-30 23:01:45 +02:00
{
if ( vInOutPoints . count ( txin . prevout ) )
2013-11-22 02:14:57 +01:00
return state . DoS ( 100 , error ( " CheckTransaction() : duplicate inputs " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-inputs-duplicate " ) ;
2011-07-30 23:01:45 +02:00
vInOutPoints . insert ( txin . prevout ) ;
}
2013-01-08 13:17:15 +01:00
if ( tx . IsCoinBase ( ) )
2010-09-30 18:23:07 +02:00
{
2013-01-08 13:17:15 +01:00
if ( tx . vin [ 0 ] . scriptSig . size ( ) < 2 | | tx . vin [ 0 ] . scriptSig . size ( ) > 100 )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckTransaction() : coinbase script size " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-cb-length " ) ;
2010-09-30 18:23:07 +02:00
}
else
{
2013-01-08 13:17:15 +01:00
BOOST_FOREACH ( const CTxIn & txin , tx . vin )
2010-09-30 18:23:07 +02:00
if ( txin . prevout . IsNull ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 10 , error ( " CheckTransaction() : prevout is null " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-prevout-null " ) ;
2010-09-30 18:23:07 +02:00
}
return true ;
}
2013-08-12 16:59:09 +02:00
int64_t GetMinFee ( const CTransaction & tx , unsigned int nBytes , bool fAllowFree , enum GetMinFee_mode mode )
2012-09-09 22:39:45 +02:00
{
2013-04-26 02:11:27 +02:00
// Base fee is either nMinTxFee or nMinRelayTxFee
2013-04-13 07:13:08 +02:00
int64_t nBaseFee = ( mode = = GMF_RELAY ) ? tx . nMinRelayTxFee : tx . nMinTxFee ;
2012-09-09 22:39:45 +02:00
2013-04-13 07:13:08 +02:00
int64_t nMinFee = ( 1 + ( int64_t ) nBytes / 1000 ) * nBaseFee ;
2012-09-09 22:39:45 +02:00
if ( fAllowFree )
{
2013-05-17 12:07:02 +02:00
// There is a free transaction area in blocks created by most miners,
// * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
2013-09-18 04:04:29 +02:00
// to be considered to fall into this category. We don't want to encourage sending
// multiple transactions instead of one big transaction to avoid fees.
// * If we are creating a transaction we allow transactions up to 1,000 bytes
// to be considered safe and assume they can likely make it into this section.
if ( nBytes < ( mode = = GMF_SEND ? 1000 : ( DEFAULT_BLOCK_PRIORITY_SIZE - 1000 ) ) )
2013-05-17 12:07:02 +02:00
nMinFee = 0 ;
2012-09-09 22:39:45 +02:00
}
2013-09-18 03:48:41 +02:00
// This code can be removed after enough miners have upgraded to version 0.9.
// Until then, be safe when sending and require a fee if any output
// is less than CENT:
if ( nMinFee < nBaseFee & & mode = = GMF_SEND )
2012-09-09 22:39:45 +02:00
{
2013-01-08 12:42:22 +01:00
BOOST_FOREACH ( const CTxOut & txout , tx . vout )
2012-09-09 22:39:45 +02:00
if ( txout . nValue < CENT )
nMinFee = nBaseFee ;
}
if ( ! MoneyRange ( nMinFee ) )
nMinFee = MAX_MONEY ;
return nMinFee ;
}
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2013-08-27 07:51:57 +02:00
bool AcceptToMemoryPool ( CTxMemPool & pool , CValidationState & state , const CTransaction & tx , bool fLimitFree ,
2013-08-29 00:41:46 +02:00
bool * pfMissingInputs , bool fRejectInsaneFee )
2010-08-29 18:58:15 +02:00
{
if ( pfMissingInputs )
* pfMissingInputs = false ;
2013-01-08 13:17:15 +01:00
if ( ! CheckTransaction ( tx , state ) )
2013-08-27 07:51:57 +02:00
return error ( " AcceptToMemoryPool: : CheckTransaction failed " ) ;
2010-09-30 18:23:07 +02:00
2010-08-29 18:58:15 +02:00
// Coinbase is only valid in a block, not as a loose transaction
2012-04-13 23:34:22 +02:00
if ( tx . IsCoinBase ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " AcceptToMemoryPool: : coinbase as individual tx " ) ,
REJECT_INVALID , " coinbase " ) ;
2010-09-07 03:12:53 +02:00
2013-07-23 17:46:05 +02:00
// Rather not work on nonstandard transactions (unless -testnet/-regtest)
2013-06-23 08:05:25 +02:00
string reason ;
2013-07-23 17:46:05 +02:00
if ( Params ( ) . NetworkID ( ) = = CChainParams : : MAIN & & ! IsStandardTx ( tx , reason ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 0 ,
2014-01-16 16:15:27 +01:00
error ( " AcceptToMemoryPool : nonstandard transaction: %s " , reason ) ,
2013-10-28 07:36:11 +01:00
REJECT_NONSTANDARD , reason ) ;
2010-12-12 19:20:36 +01:00
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// is it already in the memory pool?
2012-04-13 23:34:22 +02:00
uint256 hash = tx . GetHash ( ) ;
2013-08-27 07:51:57 +02:00
if ( pool . exists ( hash ) )
return false ;
2010-08-29 18:58:15 +02:00
// Check for conflicts with in-memory transactions
2013-08-27 07:51:57 +02:00
{
LOCK ( pool . cs ) ; // protect pool.mapNextTx
2012-04-17 20:12:48 +02:00
for ( unsigned int i = 0 ; i < tx . vin . size ( ) ; i + + )
2010-08-29 18:58:15 +02:00
{
2012-04-13 23:34:22 +02:00
COutPoint outpoint = tx . vin [ i ] . prevout ;
2013-08-27 07:51:57 +02:00
if ( pool . mapNextTx . count ( outpoint ) )
2010-08-29 18:58:15 +02:00
{
// Disable replacement feature for now
return false ;
}
}
2013-08-27 07:51:57 +02:00
}
2010-08-29 18:58:15 +02:00
{
2012-10-23 01:16:26 +02:00
CCoinsView dummy ;
CCoinsViewCache view ( dummy ) ;
{
2013-08-27 07:51:57 +02:00
LOCK ( pool . cs ) ;
CCoinsViewMemPool viewMemPool ( * pcoinsTip , pool ) ;
2012-10-23 01:16:26 +02:00
view . SetBackend ( viewMemPool ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// do we already have it?
if ( view . HaveCoins ( hash ) )
2012-05-11 02:31:46 +02:00
return false ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// do all inputs exist?
2012-10-23 00:21:16 +02:00
// Note that this does not check for the presence of actual outputs (see the next check for that),
// only helps filling in pfMissingInputs (to determine missing vs spent).
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
BOOST_FOREACH ( const CTxIn txin , tx . vin ) {
if ( ! view . HaveCoins ( txin . prevout . hash ) ) {
if ( pfMissingInputs )
* pfMissingInputs = true ;
return false ;
}
2011-10-03 19:05:43 +02:00
}
2012-10-23 00:21:16 +02:00
// are the actual inputs available?
2013-01-08 13:17:15 +01:00
if ( ! view . HaveInputs ( tx ) )
2013-10-28 07:36:11 +01:00
return state . Invalid ( error ( " AcceptToMemoryPool : inputs already spent " ) ,
2012-09-10 04:02:35 +02:00
REJECT_DUPLICATE , " bad-txns-inputs-spent " ) ;
2012-11-11 13:11:42 +01:00
2012-10-23 01:16:26 +02:00
// Bring the best block into scope
view . GetBestBlock ( ) ;
// we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
view . SetBackend ( dummy ) ;
}
2012-07-08 19:04:05 +02:00
2012-01-05 03:40:52 +01:00
// Check for non-standard pay-to-script-hash in inputs
2013-07-23 17:46:05 +02:00
if ( Params ( ) . NetworkID ( ) = = CChainParams : : MAIN & & ! AreInputsStandard ( tx , view ) )
2013-08-27 07:51:57 +02:00
return error ( " AcceptToMemoryPool: : nonstandard transaction input " ) ;
2011-10-03 19:05:43 +02:00
2012-01-20 23:07:40 +01:00
// Note: if you modify this code to accept non-standard transactions, then
// you should add code here to check that the transaction does a
// reasonable number of ECDSA signature verifications.
2013-11-11 08:35:14 +01:00
int64_t nValueIn = view . GetValueIn ( tx ) ;
int64_t nValueOut = tx . GetValueOut ( ) ;
int64_t nFees = nValueIn - nValueOut ;
double dPriority = view . GetPriority ( tx , chainActive . Height ( ) ) ;
CTxMemPoolEntry entry ( tx , nFees , GetTime ( ) , dPriority , chainActive . Height ( ) ) ;
unsigned int nSize = entry . GetTxSize ( ) ;
2012-01-11 02:18:00 +01:00
// Don't accept it if it can't get into a block
2013-08-12 16:59:09 +02:00
int64_t txMinFee = GetMinFee ( tx , nSize , true , GMF_RELAY ) ;
2013-01-14 22:52:33 +01:00
if ( fLimitFree & & nFees < txMinFee )
2014-02-24 09:08:56 +01:00
return state . DoS ( 0 , error ( " AcceptToMemoryPool : not enough fees %s, %d < %d " ,
2014-01-16 16:15:27 +01:00
hash . ToString ( ) , nFees , txMinFee ) ,
2013-10-28 07:36:11 +01:00
REJECT_INSUFFICIENTFEE , " insufficient fee " ) ;
2012-01-05 03:40:52 +01:00
2011-03-11 17:50:16 +01:00
// Continuously rate-limit free transactions
2011-03-13 19:38:07 +01:00
// This mitigates 'penny-flooding' -- sending thousands of free transactions just to
2012-07-26 05:25:26 +02:00
// be annoying or make others' transactions take longer to confirm.
2013-04-26 02:11:27 +02:00
if ( fLimitFree & & nFees < CTransaction : : nMinRelayTxFee )
2010-12-12 19:20:36 +01:00
{
2013-08-27 07:51:57 +02:00
static CCriticalSection csFreeLimiter ;
2011-03-11 17:50:16 +01:00
static double dFreeCount ;
2013-04-13 07:13:08 +02:00
static int64_t nLastTime ;
int64_t nNow = GetTime ( ) ;
2011-03-13 19:38:07 +01:00
2013-08-27 07:51:57 +02:00
LOCK ( csFreeLimiter ) ;
2013-01-14 22:52:33 +01:00
// 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
2013-01-24 02:24:10 +01:00
if ( dFreeCount > = GetArg ( " -limitfreerelay " , 15 ) * 10 * 1000 )
2013-10-28 07:36:11 +01:00
return state . DoS ( 0 , error ( " AcceptToMemoryPool : free transaction rejected by rate limiter " ) ,
REJECT_INSUFFICIENTFEE , " insufficient priority " ) ;
2013-08-27 07:51:57 +02:00
LogPrint ( " mempool " , " Rate limit dFreeCount: %g => %g \n " , dFreeCount , dFreeCount + nSize ) ;
2013-01-14 22:52:33 +01:00
dFreeCount + = nSize ;
2010-12-12 19:20:36 +01:00
}
2012-01-11 02:18:00 +01:00
2013-08-29 00:41:46 +02:00
if ( fRejectInsaneFee & & nFees > CTransaction : : nMinRelayTxFee * 10000 )
2014-02-24 09:08:56 +01:00
return error ( " AcceptToMemoryPool: : insane fees %s, %d > %d " ,
2014-01-16 16:15:27 +01:00
hash . ToString ( ) ,
2013-08-29 00:41:46 +02:00
nFees , CTransaction : : nMinRelayTxFee * 10000 ) ;
2012-01-11 02:18:00 +01:00
// Check against previous transactions
// This is done last to help prevent CPU exhaustion denial-of-service attacks.
2013-01-08 13:17:15 +01:00
if ( ! CheckInputs ( tx , state , view , true , SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC ) )
2012-01-11 02:18:00 +01:00
{
2014-01-16 16:15:27 +01:00
return error ( " AcceptToMemoryPool: : ConnectInputs failed %s " , hash . ToString ( ) ) ;
2012-01-11 02:18:00 +01:00
}
2013-11-11 08:35:14 +01:00
// Store transaction in memory
pool . addUnchecked ( hash , entry ) ;
2010-08-29 18:58:15 +02:00
}
2013-10-19 18:34:06 +02:00
g_signals . SyncTransaction ( hash , tx , NULL ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2011-05-27 06:02:51 +02:00
2014-02-12 19:43:07 +01:00
int CMerkleTx : : GetDepthInMainChainINTERNAL ( CBlockIndex * & pindexRet ) const
2010-08-29 18:58:15 +02:00
{
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 ;
2013-10-10 23:07:44 +02:00
if ( ! pindex | | ! chainActive . Contains ( pindex ) )
2010-08-29 18:58:15 +02:00
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 ;
}
2011-07-11 21:49:45 +02:00
pindexRet = pindex ;
2013-10-10 23:07:44 +02:00
return chainActive . Height ( ) - pindex - > nHeight + 1 ;
2010-08-29 18:58:15 +02:00
}
2014-02-12 19:43:07 +01:00
int CMerkleTx : : GetDepthInMainChain ( CBlockIndex * & pindexRet ) const
{
int nResult = GetDepthInMainChainINTERNAL ( pindexRet ) ;
if ( nResult = = 0 & & ! mempool . exists ( GetHash ( ) ) )
return - 1 ; // Not in chain, not in mempool
return nResult ;
}
2010-08-29 18:58:15 +02:00
int CMerkleTx : : GetBlocksToMaturity ( ) const
{
if ( ! IsCoinBase ( ) )
return 0 ;
2013-08-28 19:16:50 +02:00
return max ( 0 , ( COINBASE_MATURITY + 1 ) - GetDepthInMainChain ( ) ) ;
2010-08-29 18:58:15 +02:00
}
2013-06-13 22:28:03 +02:00
bool CMerkleTx : : AcceptToMemoryPool ( bool fLimitFree )
2010-08-29 18:58:15 +02:00
{
2013-01-27 00:14:11 +01:00
CValidationState state ;
2013-08-27 07:51:57 +02:00
return : : AcceptToMemoryPool ( mempool , state , * this , fLimitFree , NULL ) ;
2010-08-29 18:58:15 +02:00
}
2012-02-15 17:49:04 +01:00
// Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
bool GetTransaction ( const uint256 & hash , CTransaction & txOut , uint256 & hashBlock , bool fAllowSlow )
2012-02-15 17:49:04 +01:00
{
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CBlockIndex * pindexSlow = NULL ;
2012-02-15 17:49:04 +01:00
{
LOCK ( cs_main ) ;
{
2013-08-27 07:51:57 +02:00
if ( mempool . lookup ( hash , txOut ) )
2012-02-15 17:49:04 +01:00
{
return true ;
}
}
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2013-01-11 01:47:57 +01:00
if ( fTxIndex ) {
CDiskTxPos postx ;
if ( pblocktree - > ReadTxIndex ( hash , postx ) ) {
CAutoFile file ( OpenBlockFile ( postx , true ) , SER_DISK , CLIENT_VERSION ) ;
CBlockHeader header ;
try {
file > > header ;
fseek ( file , postx . nTxOffset , SEEK_CUR ) ;
file > > txOut ;
} catch ( std : : exception & e ) {
2014-01-06 15:09:26 +01:00
return error ( " %s : Deserialize or I/O error - %s " , __PRETTY_FUNCTION__ , e . what ( ) ) ;
2013-01-11 01:47:57 +01:00
}
hashBlock = header . GetHash ( ) ;
if ( txOut . GetHash ( ) ! = hash )
2014-01-06 15:09:26 +01:00
return error ( " %s : txid mismatch " , __PRETTY_FUNCTION__ ) ;
2013-01-11 01:47:57 +01:00
return true ;
}
}
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( fAllowSlow ) { // use coin database to locate block that contains transaction, and scan it
int nHeight = - 1 ;
{
2012-07-06 16:33:34 +02:00
CCoinsViewCache & view = * pcoinsTip ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CCoins coins ;
if ( view . GetCoins ( hash , coins ) )
nHeight = coins . nHeight ;
}
if ( nHeight > 0 )
2013-10-10 23:07:44 +02:00
pindexSlow = chainActive [ nHeight ] ;
2012-02-15 17:49:04 +01:00
}
}
2010-08-29 18:58:15 +02:00
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( pindexSlow ) {
CBlock block ;
2013-06-24 03:10:02 +02:00
if ( ReadBlockFromDisk ( block , pindexSlow ) ) {
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
BOOST_FOREACH ( const CTransaction & tx , block . vtx ) {
if ( tx . GetHash ( ) = = hash ) {
txOut = tx ;
hashBlock = pindexSlow - > GetBlockHash ( ) ;
return true ;
}
}
}
}
2010-08-29 18:58:15 +02:00
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
return false ;
}
2010-08-29 18:58:15 +02:00
//////////////////////////////////////////////////////////////////////////////
//
// CBlock and CBlockIndex
//
2013-06-24 02:47:47 +02:00
bool WriteBlockToDisk ( CBlock & block , CDiskBlockPos & pos )
{
// Open history file to append
CAutoFile fileout = CAutoFile ( OpenBlockFile ( pos ) , SER_DISK , CLIENT_VERSION ) ;
if ( ! fileout )
2014-01-06 15:09:26 +01:00
return error ( " WriteBlockToDisk : OpenBlockFile failed " ) ;
2013-06-24 02:47:47 +02:00
// Write index header
unsigned int nSize = fileout . GetSerializeSize ( block ) ;
fileout < < FLATDATA ( Params ( ) . MessageStart ( ) ) < < nSize ;
// Write block
long fileOutPos = ftell ( fileout ) ;
if ( fileOutPos < 0 )
2014-01-06 15:09:26 +01:00
return error ( " WriteBlockToDisk : ftell failed " ) ;
2013-06-24 02:47:47 +02:00
pos . nPos = ( unsigned int ) fileOutPos ;
fileout < < block ;
// Flush stdio buffers and commit to disk before returning
fflush ( fileout ) ;
if ( ! IsInitialBlockDownload ( ) )
FileCommit ( fileout ) ;
return true ;
}
2013-06-24 03:21:33 +02:00
bool ReadBlockFromDisk ( CBlock & block , const CDiskBlockPos & pos )
{
block . SetNull ( ) ;
// Open history file to read
CAutoFile filein = CAutoFile ( OpenBlockFile ( pos , true ) , SER_DISK , CLIENT_VERSION ) ;
if ( ! filein )
2014-01-06 15:09:26 +01:00
return error ( " ReadBlockFromDisk : OpenBlockFile failed " ) ;
2013-06-24 03:21:33 +02:00
// Read block
try {
filein > > block ;
}
catch ( std : : exception & e ) {
2014-01-06 15:09:26 +01:00
return error ( " %s : Deserialize or I/O error - %s " , __PRETTY_FUNCTION__ , e . what ( ) ) ;
2013-06-24 03:21:33 +02:00
}
// Check the header
if ( ! CheckProofOfWork ( block . GetHash ( ) , block . nBits ) )
2014-01-06 15:09:26 +01:00
return error ( " ReadBlockFromDisk : Errors in block header " ) ;
2013-06-24 03:21:33 +02:00
return true ;
}
2013-06-24 03:10:02 +02:00
bool ReadBlockFromDisk ( CBlock & block , const CBlockIndex * pindex )
2010-08-29 18:58:15 +02:00
{
2013-06-24 03:10:02 +02:00
if ( ! ReadBlockFromDisk ( block , pindex - > GetBlockPos ( ) ) )
2010-08-29 18:58:15 +02:00
return false ;
2013-06-24 03:10:02 +02:00
if ( block . GetHash ( ) ! = pindex - > GetBlockHash ( ) )
return error ( " ReadBlockFromDisk(CBlock&, CBlockIndex*) : GetHash ( ) doesn ' t match index " ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2014-01-11 23:33:34 +01:00
uint256 static GetOrphanRoot ( const uint256 & hash )
2010-08-29 18:58:15 +02:00
{
2014-01-11 23:33:34 +01:00
map < uint256 , COrphanBlock * > : : iterator it = mapOrphanBlocks . find ( hash ) ;
if ( it = = mapOrphanBlocks . end ( ) )
return hash ;
2010-08-29 18:58:15 +02:00
// Work back to the first block in the orphan chain
2014-01-11 23:33:34 +01:00
do {
map < uint256 , COrphanBlock * > : : iterator it2 = mapOrphanBlocks . find ( it - > second - > hashPrev ) ;
if ( it2 = = mapOrphanBlocks . end ( ) )
return it - > first ;
it = it2 ;
} while ( true ) ;
2010-08-29 18:58:15 +02:00
}
2014-01-11 17:28:34 +01:00
// Remove a random orphan block (which does not have any dependent orphans).
void static PruneOrphanBlocks ( )
{
if ( mapOrphanBlocksByPrev . size ( ) < = MAX_ORPHAN_BLOCKS )
return ;
// Pick a random orphan block.
int pos = insecure_rand ( ) % mapOrphanBlocksByPrev . size ( ) ;
std : : multimap < uint256 , COrphanBlock * > : : iterator it = mapOrphanBlocksByPrev . begin ( ) ;
while ( pos - - ) it + + ;
// As long as this block has other orphans depending on it, move to one of those successors.
do {
std : : multimap < uint256 , COrphanBlock * > : : iterator it2 = mapOrphanBlocksByPrev . find ( it - > second - > hashBlock ) ;
if ( it2 = = mapOrphanBlocksByPrev . end ( ) )
break ;
it = it2 ;
} while ( 1 ) ;
uint256 hash = it - > second - > hashBlock ;
delete it - > second ;
mapOrphanBlocksByPrev . erase ( it ) ;
mapOrphanBlocks . erase ( hash ) ;
}
2013-04-13 07:13:08 +02:00
int64_t GetBlockValue ( int nHeight , int64_t nFees )
2010-08-29 18:58:15 +02:00
{
2013-04-13 07:13:08 +02:00
int64_t nSubsidy = 50 * COIN ;
2010-08-29 18:58:15 +02:00
2013-05-07 15:16:25 +02:00
// Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
nSubsidy > > = ( nHeight / Params ( ) . SubsidyHalvingInterval ( ) ) ;
2010-08-29 18:58:15 +02:00
return nSubsidy + nFees ;
}
2013-04-13 07:13:08 +02:00
static const int64_t nTargetTimespan = 14 * 24 * 60 * 60 ; // two weeks
static const int64_t nTargetSpacing = 10 * 60 ;
static const int64_t nInterval = nTargetTimespan / nTargetSpacing ;
2011-09-08 18:51:43 +02:00
//
// minimum amount of work that could possibly be required nTime after
// minimum work required was nBase
//
2013-04-13 07:13:08 +02:00
unsigned int ComputeMinWork ( unsigned int nBase , int64_t nTime )
2011-09-08 18:51:43 +02:00
{
2013-05-07 15:16:25 +02:00
const CBigNum & bnLimit = Params ( ) . ProofOfWorkLimit ( ) ;
2011-12-05 21:50:22 +01:00
// Testnet has min-difficulty blocks
// after nTargetSpacing*2 time between blocks:
2013-05-07 15:16:25 +02:00
if ( TestNet ( ) & & nTime > nTargetSpacing * 2 )
return bnLimit . GetCompact ( ) ;
2011-12-05 21:50:22 +01:00
2011-09-08 18:51:43 +02:00
CBigNum bnResult ;
bnResult . SetCompact ( nBase ) ;
2013-05-07 15:16:25 +02:00
while ( nTime > 0 & & bnResult < bnLimit )
2011-09-08 18:51:43 +02:00
{
// Maximum 400% adjustment...
bnResult * = 4 ;
// ... in best-case exactly 4-times-normal target time
nTime - = nTargetTimespan * 4 ;
}
2013-05-07 15:16:25 +02:00
if ( bnResult > bnLimit )
bnResult = bnLimit ;
2011-09-08 18:51:43 +02:00
return bnResult . GetCompact ( ) ;
}
2013-07-31 15:43:35 +02:00
unsigned int GetNextWorkRequired ( const CBlockIndex * pindexLast , const CBlockHeader * pblock )
2010-08-29 18:58:15 +02:00
{
2013-05-07 15:16:25 +02:00
unsigned int nProofOfWorkLimit = Params ( ) . ProofOfWorkLimit ( ) . GetCompact ( ) ;
2010-08-29 18:58:15 +02:00
// Genesis block
if ( pindexLast = = NULL )
2011-12-05 21:50:22 +01:00
return nProofOfWorkLimit ;
2010-08-29 18:58:15 +02:00
// Only change once per interval
if ( ( pindexLast - > nHeight + 1 ) % nInterval ! = 0 )
2011-12-05 21:50:22 +01:00
{
2013-05-07 15:16:25 +02:00
if ( TestNet ( ) )
2011-12-05 21:50:22 +01:00
{
2013-05-07 15:16:25 +02:00
// Special difficulty rule for testnet:
2011-12-05 21:50:22 +01:00
// If the new block's timestamp is more than 2* 10 minutes
// then allow mining of a min-difficulty block.
2012-04-13 20:38:05 +02:00
if ( pblock - > nTime > pindexLast - > nTime + nTargetSpacing * 2 )
2011-12-05 21:50:22 +01:00
return nProofOfWorkLimit ;
else
{
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex * pindex = pindexLast ;
while ( pindex - > pprev & & pindex - > nHeight % nInterval ! = 0 & & pindex - > nBits = = nProofOfWorkLimit )
pindex = pindex - > pprev ;
return pindex - > nBits ;
}
}
2010-08-29 18:58:15 +02:00
return pindexLast - > nBits ;
2011-12-05 21:50:22 +01:00
}
2010-08-29 18:58:15 +02:00
// 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
2013-04-13 07:13:08 +02:00
int64_t nActualTimespan = pindexLast - > GetBlockTime ( ) - pindexFirst - > GetBlockTime ( ) ;
2014-02-24 09:08:56 +01:00
LogPrintf ( " nActualTimespan = %d before bounds \n " , nActualTimespan ) ;
2010-08-29 18:58:15 +02:00
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 ;
2013-05-07 15:16:25 +02:00
if ( bnNew > Params ( ) . ProofOfWorkLimit ( ) )
bnNew = Params ( ) . ProofOfWorkLimit ( ) ;
2010-08-29 18:58:15 +02:00
/// debug print
2013-09-18 12:38:08 +02:00
LogPrintf ( " GetNextWorkRequired RETARGET \n " ) ;
2014-02-24 09:08:56 +01:00
LogPrintf ( " nTargetTimespan = %d nActualTimespan = %d \n " , nTargetTimespan , nActualTimespan ) ;
2014-01-16 16:15:27 +01:00
LogPrintf ( " Before: %08x %s \n " , pindexLast - > nBits , CBigNum ( ) . SetCompact ( pindexLast - > nBits ) . getuint256 ( ) . ToString ( ) ) ;
LogPrintf ( " After: %08x %s \n " , bnNew . GetCompact ( ) , bnNew . getuint256 ( ) . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
return bnNew . GetCompact ( ) ;
}
bool CheckProofOfWork ( uint256 hash , unsigned int nBits )
{
CBigNum bnTarget ;
bnTarget . SetCompact ( nBits ) ;
// Check range
2013-05-07 15:16:25 +02:00
if ( bnTarget < = 0 | | bnTarget > Params ( ) . ProofOfWorkLimit ( ) )
2010-08-29 18:58:15 +02:00
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 ;
}
2011-09-02 18:02:22 +02:00
// Return maximum amount of blocks that other nodes claim to have
2011-09-11 10:49:30 +02:00
int GetNumBlocksOfPeers ( )
2011-09-02 18:02:22 +02:00
{
2011-09-08 22:50:58 +02:00
return std : : max ( cPeerBlockCounts . median ( ) , Checkpoints : : GetTotalBlocksEstimate ( ) ) ;
2011-09-02 18:02:22 +02:00
}
2010-08-29 18:58:15 +02:00
bool IsInitialBlockDownload ( )
{
2013-10-10 23:07:44 +02:00
if ( fImporting | | fReindex | | chainActive . Height ( ) < Checkpoints : : GetTotalBlocksEstimate ( ) )
2010-08-29 18:58:15 +02:00
return true ;
2013-04-13 07:13:08 +02:00
static int64_t nLastUpdate ;
2010-08-29 18:58:15 +02:00
static CBlockIndex * pindexLastBest ;
2013-10-10 23:07:44 +02:00
if ( chainActive . Tip ( ) ! = pindexLastBest )
2010-08-29 18:58:15 +02:00
{
2013-10-10 23:07:44 +02:00
pindexLastBest = chainActive . Tip ( ) ;
2010-08-29 18:58:15 +02:00
nLastUpdate = GetTime ( ) ;
}
return ( GetTime ( ) - nLastUpdate < 10 & &
2013-10-10 23:07:44 +02:00
chainActive . Tip ( ) - > GetBlockTime ( ) < GetTime ( ) - 24 * 60 * 60 ) ;
2010-08-29 18:58:15 +02:00
}
2013-05-07 18:33:52 +02:00
bool fLargeWorkForkFound = false ;
2013-05-18 03:09:28 +02:00
bool fLargeWorkInvalidChainFound = false ;
2013-05-07 18:33:52 +02:00
CBlockIndex * pindexBestForkTip = NULL , * pindexBestForkBase = NULL ;
void CheckForkWarningConditions ( )
{
2013-09-04 04:06:02 +02:00
// Before we get past initial download, we cannot reliably alert about forks
// (we assume we don't get stuck on a fork before the last checkpoint)
if ( IsInitialBlockDownload ( ) )
return ;
2013-05-07 18:33:52 +02:00
// If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
// of our head, drop it
2013-10-10 23:07:44 +02:00
if ( pindexBestForkTip & & chainActive . Height ( ) - pindexBestForkTip - > nHeight > = 72 )
2013-05-07 18:33:52 +02:00
pindexBestForkTip = NULL ;
2013-10-13 22:15:48 +02:00
if ( pindexBestForkTip | | ( pindexBestInvalid & & pindexBestInvalid - > nChainWork > chainActive . Tip ( ) - > nChainWork + ( chainActive . Tip ( ) - > GetBlockWork ( ) * 6 ) . getuint256 ( ) ) )
2013-05-07 18:33:52 +02:00
{
2013-05-07 18:37:37 +02:00
if ( ! fLargeWorkForkFound )
{
std : : string strCmd = GetArg ( " -alertnotify " , " " ) ;
if ( ! strCmd . empty ( ) )
{
2013-05-18 03:09:28 +02:00
std : : string warning = std : : string ( " 'Warning: Large-work fork detected, forking after block " ) +
pindexBestForkBase - > phashBlock - > ToString ( ) + std : : string ( " ' " ) ;
2013-05-07 18:37:37 +02:00
boost : : replace_all ( strCmd , " %s " , warning ) ;
boost : : thread t ( runCommand , strCmd ) ; // thread runs free
}
}
2013-05-18 03:09:28 +02:00
if ( pindexBestForkTip )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " CheckForkWarningConditions: Warning: Large valid fork found \n forking the chain at height %d (%s) \n lasting to height %d (%s). \n Chain state database corruption likely. \n " ,
2014-01-16 16:15:27 +01:00
pindexBestForkBase - > nHeight , pindexBestForkBase - > phashBlock - > ToString ( ) ,
pindexBestForkTip - > nHeight , pindexBestForkTip - > phashBlock - > ToString ( ) ) ;
2013-05-18 03:09:28 +02:00
fLargeWorkForkFound = true ;
}
else
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain. \n Chain state database corruption likely. \n " ) ;
2013-05-18 03:09:28 +02:00
fLargeWorkInvalidChainFound = true ;
}
}
else
{
2013-05-07 18:33:52 +02:00
fLargeWorkForkFound = false ;
2013-05-18 03:09:28 +02:00
fLargeWorkInvalidChainFound = false ;
}
2013-05-07 18:33:52 +02:00
}
void CheckForkWarningConditionsOnNewFork ( CBlockIndex * pindexNewForkTip )
{
// If we are on a fork that is sufficiently large, set a warning flag
CBlockIndex * pfork = pindexNewForkTip ;
2013-10-10 23:07:44 +02:00
CBlockIndex * plonger = chainActive . Tip ( ) ;
2013-05-07 18:33:52 +02:00
while ( pfork & & pfork ! = plonger )
{
while ( plonger & & plonger - > nHeight > pfork - > nHeight )
plonger = plonger - > pprev ;
if ( pfork = = plonger )
break ;
pfork = pfork - > pprev ;
}
// We define a condition which we should warn the user about as a fork of at least 7 blocks
// who's tip is within 72 blocks (+/- 12 hours if no one mines it) of ours
// We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
// hash rate operating on the fork.
// or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
// We define it this way because it allows us to only store the highest fork tip (+ base) which meets
// the 7-block condition and from this always have the most-likely-to-cause-warning fork
if ( pfork & & ( ! pindexBestForkTip | | ( pindexBestForkTip & & pindexNewForkTip - > nHeight > pindexBestForkTip - > nHeight ) ) & &
pindexNewForkTip - > nChainWork - pfork - > nChainWork > ( pfork - > GetBlockWork ( ) * 7 ) . getuint256 ( ) & &
2013-10-10 23:07:44 +02:00
chainActive . Height ( ) - pindexNewForkTip - > nHeight < 72 )
2013-05-07 18:33:52 +02:00
{
pindexBestForkTip = pindexNewForkTip ;
pindexBestForkBase = pfork ;
}
CheckForkWarningConditions ( ) ;
}
2014-01-10 13:23:26 +01:00
// Requires cs_main.
2013-11-16 19:28:24 +01:00
void Misbehaving ( NodeId pnode , int howmuch )
{
if ( howmuch = = 0 )
return ;
CNodeState * state = State ( pnode ) ;
if ( state = = NULL )
return ;
state - > nMisbehavior + = howmuch ;
if ( state - > nMisbehavior > = GetArg ( " -banscore " , 100 ) )
{
LogPrintf ( " Misbehaving: %s (%d -> %d) BAN THRESHOLD EXCEEDED \n " , state - > name , state - > nMisbehavior - howmuch , state - > nMisbehavior ) ;
state - > fShouldBan = true ;
} else
LogPrintf ( " Misbehaving: %s (%d -> %d) \n " , state - > name , state - > nMisbehavior - howmuch , state - > nMisbehavior ) ;
}
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
void static InvalidChainFound ( CBlockIndex * pindexNew )
2010-08-29 18:58:15 +02:00
{
2013-10-13 22:15:48 +02:00
if ( ! pindexBestInvalid | | pindexNew - > nChainWork > pindexBestInvalid - > nChainWork )
2010-08-29 18:58:15 +02:00
{
2013-10-13 22:15:48 +02:00
pindexBestInvalid = pindexNew ;
// The current code doesn't actually read the BestInvalidWork entry in
// the block database anymore, as it is derived from the flags in block
// index entry. We only write it for backward compatibility.
pblocktree - > WriteBestInvalidWork ( CBigNum ( pindexBestInvalid - > nChainWork ) ) ;
2012-05-06 19:40:58 +02:00
uiInterface . NotifyBlocksChanged ( ) ;
2010-08-29 18:58:15 +02:00
}
2013-09-18 12:38:08 +02:00
LogPrintf ( " InvalidChainFound: invalid block=%s height=%d log2_work=%.8g date=%s \n " ,
2014-01-16 16:15:27 +01:00
pindexNew - > GetBlockHash ( ) . ToString ( ) , pindexNew - > nHeight ,
2013-03-28 23:51:50 +01:00
log ( pindexNew - > nChainWork . getdouble ( ) ) / log ( 2.0 ) , DateTimeStrFormat ( " %Y-%m-%d %H:%M:%S " ,
2014-01-16 16:15:27 +01:00
pindexNew - > GetBlockTime ( ) ) ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " InvalidChainFound: current best=%s height=%d log2_work=%.8g date=%s \n " ,
2014-01-16 16:15:27 +01:00
chainActive . Tip ( ) - > GetBlockHash ( ) . ToString ( ) , chainActive . Height ( ) , log ( chainActive . Tip ( ) - > nChainWork . getdouble ( ) ) / log ( 2.0 ) ,
DateTimeStrFormat ( " %Y-%m-%d %H:%M:%S " , chainActive . Tip ( ) - > GetBlockTime ( ) ) ) ;
2013-05-07 18:33:52 +02:00
CheckForkWarningConditions ( ) ;
2010-08-29 18:58:15 +02:00
}
2013-11-16 19:28:24 +01:00
void static InvalidBlockFound ( CBlockIndex * pindex , const CValidationState & state ) {
int nDoS = 0 ;
if ( state . IsInvalid ( nDoS ) ) {
std : : map < uint256 , NodeId > : : iterator it = mapBlockSource . find ( pindex - > GetBlockHash ( ) ) ;
if ( it ! = mapBlockSource . end ( ) & & State ( it - > second ) ) {
CBlockReject reject = { state . GetRejectCode ( ) , state . GetRejectReason ( ) , pindex - > GetBlockHash ( ) } ;
State ( it - > second ) - > rejects . push_back ( reject ) ;
if ( nDoS > 0 )
Misbehaving ( it - > second , nDoS ) ;
2012-08-19 00:33:01 +02:00
}
2013-11-16 19:28:24 +01:00
}
if ( ! state . CorruptionPossible ( ) ) {
pindex - > nStatus | = BLOCK_FAILED_VALID ;
pblocktree - > WriteBlockIndex ( CDiskBlockIndex ( pindex ) ) ;
setBlockIndexValid . erase ( pindex ) ;
InvalidChainFound ( pindex ) ;
}
2012-08-19 00:33:01 +02:00
}
2013-01-08 23:58:06 +01:00
void UpdateTime ( CBlockHeader & block , const CBlockIndex * pindexPrev )
2012-02-16 16:22:31 +01:00
{
2013-01-08 23:58:06 +01:00
block . nTime = max ( pindexPrev - > GetMedianTimePast ( ) + 1 , GetAdjustedTime ( ) ) ;
2012-02-16 16:22:31 +01:00
// Updating time can change work required on testnet:
2013-05-07 15:16:25 +02:00
if ( TestNet ( ) )
2013-01-08 23:58:06 +01:00
block . nBits = GetNextWorkRequired ( pindexPrev , & block ) ;
2012-02-16 16:22:31 +01:00
}
2010-08-29 18:58:15 +02:00
2013-01-08 13:17:15 +01:00
void UpdateCoins ( const CTransaction & tx , CValidationState & state , CCoinsViewCache & inputs , CTxUndo & txundo , int nHeight , const uint256 & txhash )
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
{
2013-12-02 20:33:44 +01:00
bool ret ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// mark inputs spent
2013-01-08 13:17:15 +01:00
if ( ! tx . IsCoinBase ( ) ) {
BOOST_FOREACH ( const CTxIn & txin , tx . vin ) {
2012-07-08 19:04:05 +02:00
CCoins & coins = inputs . GetCoins ( txin . prevout . hash ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CTxInUndo undo ;
2013-12-02 20:33:44 +01:00
ret = coins . Spend ( txin . prevout , undo ) ;
assert ( ret ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
txundo . vprevout . push_back ( undo ) ;
}
}
// add outputs
2013-12-02 20:33:44 +01:00
ret = inputs . SetCoins ( txhash , CCoins ( tx , nHeight ) ) ;
assert ( ret ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
}
2012-12-01 22:30:06 +01:00
bool CScriptCheck : : operator ( ) ( ) const {
const CScript & scriptSig = ptxTo - > vin [ nIn ] . scriptSig ;
if ( ! VerifyScript ( scriptSig , scriptPubKey , * ptxTo , nIn , nFlags , nHashType ) )
2014-01-16 16:15:27 +01:00
return error ( " CScriptCheck() : % s VerifySignature failed " , ptxTo->GetHash().ToString()) ;
2012-12-01 22:30:06 +01:00
return true ;
}
2012-09-08 17:33:10 +02:00
bool VerifySignature ( const CCoins & txFrom , const CTransaction & txTo , unsigned int nIn , unsigned int flags , int nHashType )
{
2012-12-01 22:30:06 +01:00
return CScriptCheck ( txFrom , txTo , nIn , flags , nHashType ) ( ) ;
2012-09-08 17:33:10 +02:00
}
2013-01-08 13:17:15 +01:00
bool CheckInputs ( const CTransaction & tx , CValidationState & state , CCoinsViewCache & inputs , bool fScriptChecks , unsigned int flags , std : : vector < CScriptCheck > * pvChecks )
2010-08-29 18:58:15 +02:00
{
2013-01-08 13:17:15 +01:00
if ( ! tx . IsCoinBase ( ) )
2010-08-29 18:58:15 +02:00
{
2012-12-01 23:04:14 +01:00
if ( pvChecks )
2013-01-08 13:17:15 +01:00
pvChecks - > reserve ( tx . vin . size ( ) ) ;
2012-12-01 23:04:14 +01:00
2012-07-08 19:04:05 +02:00
// This doesn't trigger the DoS code on purpose; if it did, it would make it easier
// for an attacker to attempt to split the network.
2013-01-08 13:17:15 +01:00
if ( ! inputs . HaveInputs ( tx ) )
2014-01-16 16:15:27 +01:00
return state . Invalid ( error ( " CheckInputs() : %s inputs unavailable " , tx . GetHash ( ) . ToString ( ) ) ) ;
2012-07-08 19:04:05 +02:00
2012-10-22 09:22:15 +02:00
// While checking, GetBestBlock() refers to the parent block.
// This is also true for mempool checks.
2013-11-05 02:27:39 +01:00
CBlockIndex * pindexPrev = mapBlockIndex . find ( inputs . GetBestBlock ( ) ) - > second ;
int nSpendHeight = pindexPrev - > nHeight + 1 ;
2013-04-13 07:13:08 +02:00
int64_t nValueIn = 0 ;
int64_t nFees = 0 ;
2013-01-08 13:17:15 +01:00
for ( unsigned int i = 0 ; i < tx . vin . size ( ) ; i + + )
2010-08-29 18:58:15 +02:00
{
2013-01-08 13:17:15 +01:00
const COutPoint & prevout = tx . vin [ i ] . prevout ;
2012-07-08 19:04:05 +02:00
const CCoins & coins = inputs . GetCoins ( prevout . hash ) ;
2010-08-29 18:58:15 +02:00
// If prev is coinbase, check that it's matured
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( coins . IsCoinBase ( ) ) {
2012-10-22 09:22:15 +02:00
if ( nSpendHeight - coins . nHeight < COINBASE_MATURITY )
2013-10-28 07:36:11 +01:00
return state . Invalid (
error ( " CheckInputs() : tried to spend coinbase at depth %d " , nSpendHeight - coins . nHeight ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-premature-spend-of-coinbase " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
}
2010-08-29 18:58:15 +02:00
2012-05-16 17:26:56 +02:00
// Check for negative or overflow input values
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
nValueIn + = coins . vout [ prevout . n ] . nValue ;
if ( ! MoneyRange ( coins . vout [ prevout . n ] . nValue ) | | ! MoneyRange ( nValueIn ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckInputs() : txin values out of range " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-inputvalues-outofrange " ) ;
2012-05-16 17:26:56 +02:00
}
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2013-11-11 07:03:51 +01:00
if ( nValueIn < tx . GetValueOut ( ) )
2014-01-16 16:15:27 +01:00
return state . DoS ( 100 , error ( " CheckInputs() : %s value in < value out " , tx . GetHash ( ) . ToString ( ) ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-in-belowout " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// Tally transaction fees
2013-11-11 07:03:51 +01:00
int64_t nTxFee = nValueIn - tx . GetValueOut ( ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( nTxFee < 0 )
2014-01-16 16:15:27 +01:00
return state . DoS ( 100 , error ( " CheckInputs() : %s nTxFee < 0 " , tx . GetHash ( ) . ToString ( ) ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-fee-negative " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
nFees + = nTxFee ;
if ( ! MoneyRange ( nFees ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckInputs() : nFees out of range " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-fee-outofrange " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2012-05-16 17:26:56 +02:00
// The first loop above does all the inexpensive checks.
// Only if ALL inputs pass do we perform expensive ECDSA signature checks.
// Helps prevent CPU exhaustion attacks.
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// Skip ECDSA signature verification when connecting blocks
2012-10-05 19:22:21 +02:00
// before the last block chain checkpoint. This is safe because block merkle hashes are
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// still computed and checked, and any change will be caught at the next checkpoint.
2012-12-01 22:51:10 +01:00
if ( fScriptChecks ) {
2013-01-08 13:17:15 +01:00
for ( unsigned int i = 0 ; i < tx . vin . size ( ) ; i + + ) {
const COutPoint & prevout = tx . vin [ i ] . prevout ;
2012-07-08 19:04:05 +02:00
const CCoins & coins = inputs . GetCoins ( prevout . hash ) ;
2012-01-11 02:18:00 +01:00
2011-09-02 22:59:47 +02:00
// Verify signature
2013-01-08 13:17:15 +01:00
CScriptCheck check ( coins , tx , i , flags , 0 ) ;
2012-12-01 23:04:14 +01:00
if ( pvChecks ) {
pvChecks - > push_back ( CScriptCheck ( ) ) ;
check . swap ( pvChecks - > back ( ) ) ;
2013-04-18 22:17:05 +02:00
} else if ( ! check ( ) ) {
if ( flags & SCRIPT_VERIFY_STRICTENC ) {
// For now, check whether the failure was caused by non-canonical
// encodings or not; if so, don't trigger DoS protection.
2013-01-08 13:17:15 +01:00
CScriptCheck check ( coins , tx , i , flags & ( ~ SCRIPT_VERIFY_STRICTENC ) , 0 ) ;
2013-04-18 22:17:05 +02:00
if ( check ( ) )
2013-10-28 07:36:11 +01:00
return state . Invalid ( false , REJECT_NONSTANDARD , " non-canonical " ) ;
2013-04-18 22:17:05 +02:00
}
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , false , REJECT_NONSTANDARD , " non-canonical " ) ;
2013-04-18 22:17:05 +02:00
}
2011-11-08 19:20:29 +01:00
}
2010-08-29 18:58:15 +02:00
}
}
return true ;
}
2013-06-24 03:32:58 +02:00
bool DisconnectBlock ( CBlock & block , CValidationState & state , CBlockIndex * pindex , CCoinsViewCache & view , bool * pfClean )
2010-08-29 18:58:15 +02:00
{
2013-11-05 02:27:39 +01:00
assert ( pindex - > GetBlockHash ( ) = = view . GetBestBlock ( ) ) ;
2010-08-29 18:58:15 +02:00
2012-12-30 15:29:39 +01:00
if ( pfClean )
* pfClean = false ;
bool fClean = true ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CBlockUndo blockUndo ;
2012-12-30 23:41:41 +01:00
CDiskBlockPos pos = pindex - > GetUndoPos ( ) ;
if ( pos . IsNull ( ) )
return error ( " DisconnectBlock() : no undo data available " ) ;
if ( ! blockUndo . ReadFromDisk ( pos , pindex - > pprev - > GetBlockHash ( ) ) )
return error ( " DisconnectBlock() : failure reading undo data " ) ;
2010-08-29 18:58:15 +02:00
2013-06-24 03:32:58 +02:00
if ( blockUndo . vtxundo . size ( ) + 1 ! = block . vtx . size ( ) )
2012-12-30 15:29:39 +01:00
return error ( " DisconnectBlock() : block and undo data inconsistent " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// undo transactions in reverse order
2013-06-24 03:32:58 +02:00
for ( int i = block . vtx . size ( ) - 1 ; i > = 0 ; i - - ) {
const CTransaction & tx = block . vtx [ i ] ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
uint256 hash = tx . GetHash ( ) ;
2013-10-26 21:26:29 +02:00
// Check that all outputs are available and match the outputs in the block itself
// exactly. Note that transactions with only provably unspendable outputs won't
// have outputs available even in the block itself, so we handle that case
// specially with outsEmpty.
CCoins outsEmpty ;
CCoins & outs = view . HaveCoins ( hash ) ? view . GetCoins ( hash ) : outsEmpty ;
2013-09-24 14:11:17 +02:00
outs . ClearUnspendable ( ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CCoins outsBlock = CCoins ( tx , pindex - > nHeight ) ;
2013-09-09 11:11:11 +02:00
// The CCoins serialization does not serialize negative numbers.
// No network rules currently depend on the version here, so an inconsistency is harmless
// but it must be corrected before txout nversion ever influences a network rule.
if ( outsBlock . nVersion < 0 )
outs . nVersion = outsBlock . nVersion ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( outs ! = outsBlock )
2012-12-30 15:29:39 +01:00
fClean = fClean & & error ( " DisconnectBlock() : added transaction mismatch? database corrupted " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// remove outputs
2012-07-08 19:04:05 +02:00
outs = CCoins ( ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// restore inputs
if ( i > 0 ) { // not coinbases
const CTxUndo & txundo = blockUndo . vtxundo [ i - 1 ] ;
2012-12-30 15:29:39 +01:00
if ( txundo . vprevout . size ( ) ! = tx . vin . size ( ) )
return error ( " DisconnectBlock() : transaction and undo data inconsistent " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
for ( unsigned int j = tx . vin . size ( ) ; j - - > 0 ; ) {
const COutPoint & out = tx . vin [ j ] . prevout ;
const CTxInUndo & undo = txundo . vprevout [ j ] ;
CCoins coins ;
view . GetCoins ( out . hash , coins ) ; // this can fail if the prevout was already entirely spent
2012-12-30 15:29:39 +01:00
if ( undo . nHeight ! = 0 ) {
// undo data contains height: this is the last output of the prevout tx being spent
if ( ! coins . IsPruned ( ) )
fClean = fClean & & error ( " DisconnectBlock() : undo data overwriting existing transaction " ) ;
coins = CCoins ( ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
coins . fCoinBase = undo . fCoinBase ;
coins . nHeight = undo . nHeight ;
coins . nVersion = undo . nVersion ;
} else {
2012-12-30 15:29:39 +01:00
if ( coins . IsPruned ( ) )
fClean = fClean & & error ( " DisconnectBlock() : undo data adding output to missing transaction " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
}
if ( coins . IsAvailable ( out . n ) )
2012-12-30 15:29:39 +01:00
fClean = fClean & & error ( " DisconnectBlock() : undo data overwriting existing output " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( coins . vout . size ( ) < out . n + 1 )
coins . vout . resize ( out . n + 1 ) ;
coins . vout [ out . n ] = undo . txout ;
if ( ! view . SetCoins ( out . hash , coins ) )
return error ( " DisconnectBlock() : cannot restore coin inputs " ) ;
}
}
}
// move best block pointer to prevout block
2013-11-05 02:27:39 +01:00
view . SetBestBlock ( pindex - > pprev - > GetBlockHash ( ) ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2012-12-30 15:29:39 +01:00
if ( pfClean ) {
* pfClean = fClean ;
return true ;
} else {
return fClean ;
}
2010-08-29 18:58:15 +02:00
}
2013-01-30 04:17:33 +01:00
void static FlushBlockFile ( bool fFinalize = false )
2012-09-06 03:21:18 +02:00
{
LOCK ( cs_LastBlockFile ) ;
2012-12-03 10:14:54 +01:00
CDiskBlockPos posOld ( nLastBlockFile , 0 ) ;
2012-09-06 03:21:18 +02:00
FILE * fileOld = OpenBlockFile ( posOld ) ;
2012-12-01 11:36:53 +01:00
if ( fileOld ) {
2013-01-30 04:17:33 +01:00
if ( fFinalize )
TruncateFile ( fileOld , infoLastBlockFile . nSize ) ;
2012-12-01 11:36:53 +01:00
FileCommit ( fileOld ) ;
fclose ( fileOld ) ;
}
2012-09-06 03:21:18 +02:00
fileOld = OpenUndoFile ( posOld ) ;
2012-12-01 11:36:53 +01:00
if ( fileOld ) {
2013-01-30 04:17:33 +01:00
if ( fFinalize )
TruncateFile ( fileOld , infoLastBlockFile . nUndoSize ) ;
2012-12-01 11:36:53 +01:00
FileCommit ( fileOld ) ;
fclose ( fileOld ) ;
}
2012-09-06 03:21:18 +02:00
}
2013-01-27 00:14:11 +01:00
bool FindUndoPos ( CValidationState & state , int nFile , CDiskBlockPos & pos , unsigned int nAddSize ) ;
2012-08-13 19:11:05 +02:00
2012-12-01 23:04:14 +01:00
static CCheckQueue < CScriptCheck > scriptcheckqueue ( 128 ) ;
2013-03-07 04:31:26 +01:00
void ThreadScriptCheck ( ) {
2012-12-01 23:04:14 +01:00
RenameThread ( " bitcoin-scriptch " ) ;
scriptcheckqueue . Thread ( ) ;
}
2013-06-24 03:50:06 +02:00
bool ConnectBlock ( CBlock & block , CValidationState & state , CBlockIndex * pindex , CCoinsViewCache & view , bool fJustCheck )
2010-08-29 18:58:15 +02:00
{
// Check it again in case a previous version let a bad block in
2013-06-24 04:14:11 +02:00
if ( ! CheckBlock ( block , state , ! fJustCheck , ! fJustCheck ) )
2010-08-29 18:58:15 +02:00
return false ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
// verify that the view's current state corresponds to the previous block
2013-11-05 02:27:39 +01:00
uint256 hashPrevBlock = pindex - > pprev = = NULL ? uint256 ( 0 ) : pindex - > pprev - > GetBlockHash ( ) ;
assert ( hashPrevBlock = = view . GetBestBlock ( ) ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2013-01-19 00:35:17 +01:00
// Special case for the genesis block, skipping connection of its transactions
// (its coinbase is unspendable)
2013-06-24 03:50:06 +02:00
if ( block . GetHash ( ) = = Params ( ) . HashGenesisBlock ( ) ) {
2013-11-05 02:27:39 +01:00
view . SetBestBlock ( pindex - > GetBlockHash ( ) ) ;
2013-01-19 00:35:17 +01:00
return true ;
}
2012-12-01 22:51:10 +01:00
bool fScriptChecks = pindex - > nHeight > = Checkpoints : : GetTotalBlocksEstimate ( ) ;
2012-02-17 17:58:02 +01:00
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
// unless those are already completely spent.
// If such overwrites are allowed, coinbases and transactions depending upon those
// can be duplicated to remove the ability to spend the first instance -- even after
// being sent to another address.
// See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
// This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
2012-07-26 05:25:26 +02:00
// already refuses previously-known transaction ids entirely.
2012-09-10 02:11:04 +02:00
// This rule was originally applied all blocks whose timestamp was after March 15, 2012, 0:00 UTC.
// Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
// two in the chain that violate it. This prevents exploiting the issue against nodes in their
// initial block download.
2012-10-24 07:41:52 +02:00
bool fEnforceBIP30 = ( ! pindex - > phashBlock ) | | // Enforce on CreateNewBlock invocations which don't have a hash.
! ( ( pindex - > nHeight = = 91842 & & pindex - > GetBlockHash ( ) = = uint256 ( " 0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec " ) ) | |
2012-09-10 02:11:04 +02:00
( pindex - > nHeight = = 91880 & & pindex - > GetBlockHash ( ) = = uint256 ( " 0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721 " ) ) ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( fEnforceBIP30 ) {
2013-06-24 03:50:06 +02:00
for ( unsigned int i = 0 ; i < block . vtx . size ( ) ; i + + ) {
uint256 hash = block . GetTxHash ( i ) ;
2012-07-08 19:04:05 +02:00
if ( view . HaveCoins ( hash ) & & ! view . GetCoins ( hash ) . IsPruned ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " ConnectBlock() : tried to overwrite transaction " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-BIP30 " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
}
}
2012-02-17 17:58:02 +01:00
2012-06-22 17:50:52 +02:00
// BIP16 didn't become active until Apr 1 2012
2013-04-13 07:13:08 +02:00
int64_t nBIP16SwitchTime = 1333238400 ;
2012-03-21 20:45:58 +01:00
bool fStrictPayToScriptHash = ( pindex - > nTime > = nBIP16SwitchTime ) ;
2012-01-20 23:07:40 +01:00
2012-12-08 22:49:04 +01:00
unsigned int flags = SCRIPT_VERIFY_NOCACHE |
( fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE ) ;
2012-06-23 14:17:13 +02:00
CBlockUndo blockundo ;
2012-12-01 23:04:14 +01:00
CCheckQueueControl < CScriptCheck > control ( fScriptChecks & & nScriptCheckThreads ? & scriptcheckqueue : NULL ) ;
2013-04-13 07:13:08 +02:00
int64_t nStart = GetTimeMicros ( ) ;
int64_t nFees = 0 ;
2012-12-01 20:10:23 +01:00
int nInputs = 0 ;
2012-04-23 20:14:03 +02:00
unsigned int nSigOps = 0 ;
2013-06-24 03:50:06 +02:00
CDiskTxPos pos ( pindex - > GetBlockPos ( ) , GetSizeOfCompactSize ( block . vtx . size ( ) ) ) ;
2013-01-11 01:47:57 +01:00
std : : vector < std : : pair < uint256 , CDiskTxPos > > vPos ;
2013-06-24 03:50:06 +02:00
vPos . reserve ( block . vtx . size ( ) ) ;
for ( unsigned int i = 0 ; i < block . vtx . size ( ) ; i + + )
2010-08-29 18:58:15 +02:00
{
2013-06-24 03:50:06 +02:00
const CTransaction & tx = block . vtx [ i ] ;
2012-07-08 00:06:34 +02:00
2012-12-01 20:10:23 +01:00
nInputs + = tx . vin . size ( ) ;
2013-01-08 13:17:15 +01:00
nSigOps + = GetLegacySigOpCount ( tx ) ;
2012-01-20 23:07:40 +01:00
if ( nSigOps > MAX_BLOCK_SIGOPS )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " ConnectBlock() : too many sigops " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-blk-sigops " ) ;
2012-01-20 23:07:40 +01:00
2012-01-11 02:18:00 +01:00
if ( ! tx . IsCoinBase ( ) )
{
2013-01-08 13:17:15 +01:00
if ( ! view . HaveInputs ( tx ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " ConnectBlock() : inputs missing/spent " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-inputs-missingorspent " ) ;
2012-01-05 03:40:52 +01:00
2012-01-20 23:07:40 +01:00
if ( fStrictPayToScriptHash )
{
// Add in sigops done by pay-to-script-hash inputs;
// this is to prevent a "rogue miner" from creating
// an incredibly-expensive-to-validate block.
2013-01-08 13:17:15 +01:00
nSigOps + = GetP2SHSigOpCount ( tx , view ) ;
2012-01-20 23:07:40 +01:00
if ( nSigOps > MAX_BLOCK_SIGOPS )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " ConnectBlock() : too many sigops " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-blk-sigops " ) ;
2012-01-20 23:07:40 +01:00
}
2012-01-05 03:40:52 +01:00
2013-11-11 07:03:51 +01:00
nFees + = view . GetValueIn ( tx ) - tx . GetValueOut ( ) ;
2012-06-23 14:17:13 +02:00
2012-12-01 23:04:14 +01:00
std : : vector < CScriptCheck > vChecks ;
2013-01-08 13:17:15 +01:00
if ( ! CheckInputs ( tx , state , view , fScriptChecks , flags , nScriptCheckThreads ? & vChecks : NULL ) )
2012-01-11 02:45:55 +01:00
return false ;
2012-12-01 23:04:14 +01:00
control . Add ( vChecks ) ;
2012-01-11 02:18:00 +01:00
}
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CTxUndo txundo ;
2013-06-24 03:50:06 +02:00
UpdateCoins ( tx , state , view , txundo , pindex - > nHeight , block . GetTxHash ( i ) ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( ! tx . IsCoinBase ( ) )
blockundo . vtxundo . push_back ( txundo ) ;
2012-12-01 20:10:23 +01:00
2013-06-24 03:50:06 +02:00
vPos . push_back ( std : : make_pair ( block . GetTxHash ( i ) , pos ) ) ;
2013-01-11 01:47:57 +01:00
pos . nTxOffset + = : : GetSerializeSize ( tx , SER_DISK , CLIENT_VERSION ) ;
2010-08-29 18:58:15 +02:00
}
2013-04-13 07:13:08 +02:00
int64_t nTime = GetTimeMicros ( ) - nStart ;
2012-12-01 20:10:23 +01:00
if ( fBenchmark )
2013-09-18 12:38:08 +02:00
LogPrintf ( " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) \n " , ( unsigned ) block . vtx . size ( ) , 0.001 * nTime , 0.001 * nTime / block . vtx . size ( ) , nInputs < = 1 ? 0 : 0.001 * nTime / ( nInputs - 1 ) ) ;
2011-10-03 19:05:43 +02:00
2013-11-11 07:03:51 +01:00
if ( block . vtx [ 0 ] . GetValueOut ( ) > GetBlockValue ( pindex - > nHeight , nFees ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 ,
2014-02-24 09:08:56 +01:00
error ( " ConnectBlock() : coinbase pays too much (actual=%d vs limit=%d) " ,
2013-11-11 07:03:51 +01:00
block . vtx [ 0 ] . GetValueOut ( ) , GetBlockValue ( pindex - > nHeight , nFees ) ) ,
2014-02-10 16:31:06 +01:00
REJECT_INVALID , " bad-cb-amount " ) ;
2012-10-22 21:46:00 +02:00
2012-12-01 23:04:14 +01:00
if ( ! control . Wait ( ) )
2013-01-27 00:14:11 +01:00
return state . DoS ( 100 , false ) ;
2013-04-13 07:13:08 +02:00
int64_t nTime2 = GetTimeMicros ( ) - nStart ;
2012-12-01 23:04:14 +01:00
if ( fBenchmark )
2013-09-18 12:38:08 +02:00
LogPrintf ( " - Verify %u txins: %.2fms (%.3fms/txin) \n " , nInputs - 1 , 0.001 * nTime2 , nInputs < = 1 ? 0 : 0.001 * nTime2 / ( nInputs - 1 ) ) ;
2012-12-01 23:04:14 +01:00
2012-05-09 19:24:44 +02:00
if ( fJustCheck )
return true ;
2012-08-13 19:11:05 +02:00
// Write undo information to disk
2012-08-19 00:33:01 +02:00
if ( pindex - > GetUndoPos ( ) . IsNull ( ) | | ( pindex - > nStatus & BLOCK_VALID_MASK ) < BLOCK_VALID_SCRIPTS )
2012-08-13 19:11:05 +02:00
{
2012-08-19 00:33:01 +02:00
if ( pindex - > GetUndoPos ( ) . IsNull ( ) ) {
CDiskBlockPos pos ;
2013-01-27 00:14:11 +01:00
if ( ! FindUndoPos ( state , pindex - > nFile , pos , : : GetSerializeSize ( blockundo , SER_DISK , CLIENT_VERSION ) + 40 ) )
2012-08-19 00:33:01 +02:00
return error ( " ConnectBlock() : FindUndoPos failed " ) ;
2012-12-30 23:41:41 +01:00
if ( ! blockundo . WriteToDisk ( pos , pindex - > pprev - > GetBlockHash ( ) ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write undo data " ) ) ;
2012-08-19 00:33:01 +02:00
// update nUndoPos in block index
pindex - > nUndoPos = pos . nPos ;
pindex - > nStatus | = BLOCK_HAVE_UNDO ;
}
pindex - > nStatus = ( pindex - > nStatus & ~ BLOCK_VALID_MASK ) | BLOCK_VALID_SCRIPTS ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
CDiskBlockIndex blockindex ( pindex ) ;
2012-09-03 15:26:57 +02:00
if ( ! pblocktree - > WriteBlockIndex ( blockindex ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write block index " ) ) ;
2010-08-29 18:58:15 +02:00
}
2013-01-11 01:47:57 +01:00
if ( fTxIndex )
2013-01-27 00:14:11 +01:00
if ( ! pblocktree - > WriteTxIndex ( vPos ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write transaction index " ) ) ;
2013-01-11 01:47:57 +01:00
2012-10-05 19:22:21 +02:00
// add this block to the view's block chain
2013-12-02 20:33:44 +01:00
bool ret ;
ret = view . SetBestBlock ( pindex - > GetBlockHash ( ) ) ;
assert ( ret ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2010-08-29 18:58:15 +02:00
// Watch for transactions paying to me
2013-06-24 03:50:06 +02:00
for ( unsigned int i = 0 ; i < block . vtx . size ( ) ; i + + )
2013-10-19 18:34:06 +02:00
g_signals . SyncTransaction ( block . GetTxHash ( i ) , block . vtx [ i ] , & block ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2013-11-16 19:28:24 +01:00
// Update the on-disk chain state.
2013-11-16 18:40:55 +01:00
bool static WriteChainState ( CValidationState & state ) {
2013-11-16 19:28:24 +01:00
static int64_t nLastWrite = 0 ;
if ( ! IsInitialBlockDownload ( ) | | pcoinsTip - > GetCacheSize ( ) > nCoinCacheSize | | GetTimeMicros ( ) > nLastWrite + 600 * 1000000 ) {
2013-01-27 00:22:15 +01:00
// Typical CCoins structures on disk are around 100 bytes in size.
// Pushing a new one to the database can cause it to be written
// twice (once in the log, and once in the tables). This is already
// an overestimation, as most will delete an existing entry or
// overwrite one. Still, use a conservative safety factor of 2.
if ( ! CheckDiskSpace ( 100 * 2 * 2 * pcoinsTip - > GetCacheSize ( ) ) )
2012-09-10 04:02:35 +02:00
return state . Error ( " out of disk space " ) ;
2012-09-06 03:21:18 +02:00
FlushBlockFile ( ) ;
2012-09-03 21:14:03 +02:00
pblocktree - > Sync ( ) ;
2012-12-01 16:46:23 +01:00
if ( ! pcoinsTip - > Flush ( ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write to coin database " ) ) ;
2013-11-16 19:28:24 +01:00
nLastWrite = GetTimeMicros ( ) ;
2012-09-06 03:21:18 +02:00
}
2013-11-16 18:40:55 +01:00
return true ;
}
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
2013-11-16 19:28:24 +01:00
// Update chainActive and related internal data structures.
2013-11-16 18:40:55 +01:00
void static UpdateTip ( CBlockIndex * pindexNew ) {
2013-10-10 23:07:44 +02:00
chainActive . SetTip ( pindexNew ) ;
2010-08-29 18:58:15 +02:00
2011-04-13 16:16:30 +02:00
// Update best block in wallet (so we can detect restored wallets)
2013-11-16 18:40:55 +01:00
bool fIsInitialDownload = IsInitialBlockDownload ( ) ;
if ( ( chainActive . Height ( ) % 20160 ) = = 0 | | ( ! fIsInitialDownload & & ( chainActive . Height ( ) % 144 ) = = 0 ) )
g_signals . SetBestChain ( chainActive . GetLocator ( ) ) ;
2011-04-13 16:16:30 +02:00
2010-08-29 18:58:15 +02:00
// New best block
nTimeBestReceived = GetTime ( ) ;
2013-08-27 07:51:57 +02:00
mempool . AddTransactionsUpdated ( 1 ) ;
2013-11-16 18:40:55 +01:00
LogPrintf ( " UpdateTip: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f \n " ,
chainActive . Tip ( ) - > GetBlockHash ( ) . ToString ( ) , chainActive . Height ( ) , log ( chainActive . Tip ( ) - > nChainWork . getdouble ( ) ) / log ( 2.0 ) , ( unsigned long ) chainActive . Tip ( ) - > nChainTx ,
2014-01-16 16:15:27 +01:00
DateTimeStrFormat ( " %Y-%m-%d %H:%M:%S " , chainActive . Tip ( ) - > GetBlockTime ( ) ) ,
2013-10-10 23:07:44 +02:00
Checkpoints : : GuessVerificationProgress ( chainActive . Tip ( ) ) ) ;
2010-08-29 18:58:15 +02:00
2012-06-27 19:51:51 +02:00
// Check the version of the last 100 blocks to see if we need to upgrade:
if ( ! fIsInitialDownload )
{
int nUpgraded = 0 ;
2013-10-10 23:07:44 +02:00
const CBlockIndex * pindex = chainActive . Tip ( ) ;
2012-06-27 19:51:51 +02:00
for ( int i = 0 ; i < 100 & & pindex ! = NULL ; i + + )
{
if ( pindex - > nVersion > CBlock : : CURRENT_VERSION )
+ + nUpgraded ;
pindex = pindex - > pprev ;
}
if ( nUpgraded > 0 )
2014-01-16 15:52:37 +01:00
LogPrintf ( " SetBestChain: %d of last 100 blocks above version %d \n " , nUpgraded , ( int ) CBlock : : CURRENT_VERSION ) ;
2012-06-27 19:51:51 +02:00
if ( nUpgraded > 100 / 2 )
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2012-07-27 08:36:43 +02:00
strMiscWarning = _ ( " Warning: This version is obsolete, upgrade required! " ) ;
2012-06-27 19:51:51 +02:00
}
2013-11-16 19:28:24 +01:00
}
2012-06-27 19:51:51 +02:00
2013-11-16 19:28:24 +01:00
// Disconnect chainActive's tip.
bool static DisconnectTip ( CValidationState & state ) {
CBlockIndex * pindexDelete = chainActive . Tip ( ) ;
assert ( pindexDelete ) ;
mempool . check ( pcoinsTip ) ;
// Read block from disk.
CBlock block ;
if ( ! ReadBlockFromDisk ( block , pindexDelete ) )
return state . Abort ( _ ( " Failed to read block " ) ) ;
// Apply the block atomically to the chain state.
int64_t nStart = GetTimeMicros ( ) ;
2012-01-03 21:24:28 +01:00
{
2013-11-16 19:28:24 +01:00
CCoinsViewCache view ( * pcoinsTip , true ) ;
if ( ! DisconnectBlock ( block , state , pindexDelete , view ) )
return error ( " DisconnectTip() : DisconnectBlock % s failed " , pindexDelete->GetBlockHash().ToString()) ;
assert ( view . Flush ( ) ) ;
2012-01-03 21:24:28 +01:00
}
2013-11-16 19:28:24 +01:00
if ( fBenchmark )
LogPrintf ( " - Disconnect: %.2fms \n " , ( GetTimeMicros ( ) - nStart ) * 0.001 ) ;
// Write the chain state to disk, if necessary.
if ( ! WriteChainState ( state ) )
return false ;
2014-02-15 22:38:28 +01:00
// Resurrect mempool transactions from the disconnected block.
2013-11-16 19:28:24 +01:00
BOOST_FOREACH ( const CTransaction & tx , block . vtx ) {
// ignore validation errors in resurrected transactions
2014-02-15 22:38:28 +01:00
list < CTransaction > removed ;
2013-11-16 19:28:24 +01:00
CValidationState stateDummy ;
if ( ! tx . IsCoinBase ( ) )
if ( ! AcceptToMemoryPool ( mempool , stateDummy , tx , false , NULL ) )
2014-02-15 22:38:28 +01:00
mempool . remove ( tx , removed , true ) ;
2013-11-16 19:28:24 +01:00
}
mempool . check ( pcoinsTip ) ;
// Update chainActive and related variables.
UpdateTip ( pindexDelete - > pprev ) ;
2014-02-15 22:38:28 +01:00
// Let wallets know transactions went from 1-confirmed to
// 0-confirmed or conflicted:
BOOST_FOREACH ( const CTransaction & tx , block . vtx ) {
SyncWithWallets ( tx . GetHash ( ) , tx , NULL ) ;
}
2013-11-16 19:28:24 +01:00
return true ;
2013-11-16 18:40:55 +01:00
}
2012-01-03 21:24:28 +01:00
2013-11-16 19:28:24 +01:00
// Connect a new block to chainActive.
bool static ConnectTip ( CValidationState & state , CBlockIndex * pindexNew ) {
assert ( pindexNew - > pprev = = chainActive . Tip ( ) ) ;
2013-11-05 02:47:07 +01:00
mempool . check ( pcoinsTip ) ;
2013-11-16 19:28:24 +01:00
// Read block from disk.
CBlock block ;
if ( ! ReadBlockFromDisk ( block , pindexNew ) )
return state . Abort ( _ ( " Failed to read block " ) ) ;
// Apply the block atomically to the chain state.
int64_t nStart = GetTimeMicros ( ) ;
2010-08-29 18:58:15 +02:00
{
2013-11-16 19:28:24 +01:00
CCoinsViewCache view ( * pcoinsTip , true ) ;
CInv inv ( MSG_BLOCK , pindexNew - > GetBlockHash ( ) ) ;
if ( ! ConnectBlock ( block , state , pindexNew , view ) ) {
if ( state . IsInvalid ( ) )
InvalidBlockFound ( pindexNew , state ) ;
return error ( " ConnectTip() : ConnectBlock % s failed " , pindexNew->GetBlockHash().ToString()) ;
2013-01-27 01:24:06 +01:00
}
2013-11-16 19:28:24 +01:00
mapBlockSource . erase ( inv . hash ) ;
assert ( view . Flush ( ) ) ;
2010-08-29 18:58:15 +02:00
}
2013-11-16 19:28:24 +01:00
if ( fBenchmark )
LogPrintf ( " - Connect: %.2fms \n " , ( GetTimeMicros ( ) - nStart ) * 0.001 ) ;
// Write the chain state to disk, if necessary.
if ( ! WriteChainState ( state ) )
return false ;
// Remove conflicting transactions from the mempool.
2014-02-15 22:38:28 +01:00
list < CTransaction > txConflicted ;
2013-11-16 19:28:24 +01:00
BOOST_FOREACH ( const CTransaction & tx , block . vtx ) {
2014-02-15 22:38:28 +01:00
list < CTransaction > unused ;
mempool . remove ( tx , unused ) ;
mempool . removeConflicts ( tx , txConflicted ) ;
2013-11-16 19:28:24 +01:00
}
mempool . check ( pcoinsTip ) ;
// Update chainActive & related variables.
UpdateTip ( pindexNew ) ;
2014-02-15 22:38:28 +01:00
// Tell wallet about transactions that went from mempool
// to conflicted:
BOOST_FOREACH ( const CTransaction & tx , txConflicted ) {
SyncWithWallets ( tx . GetHash ( ) , tx , NULL ) ;
}
// ... and about transactions that got confirmed:
BOOST_FOREACH ( const CTransaction & tx , block . vtx ) {
SyncWithWallets ( tx . GetHash ( ) , tx , & block ) ;
}
2010-08-29 18:58:15 +02:00
return true ;
}
2013-11-16 19:28:24 +01:00
// Make chainMostWork correspond to the chain with the most work in it, that isn't
// known to be invalid (it's however far from certain to be valid).
void static FindMostWorkChain ( ) {
CBlockIndex * pindexNew = NULL ;
2010-08-29 18:58:15 +02:00
2013-11-16 19:28:24 +01:00
// In case the current best is invalid, do not consider it.
while ( chainMostWork . Tip ( ) & & ( chainMostWork . Tip ( ) - > nStatus & BLOCK_FAILED_MASK ) ) {
setBlockIndexValid . erase ( chainMostWork . Tip ( ) ) ;
chainMostWork . SetTip ( chainMostWork . Tip ( ) - > pprev ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
}
2012-03-21 13:15:27 +01:00
2013-11-16 19:28:24 +01:00
do {
// Find the best candidate header.
{
std : : set < CBlockIndex * , CBlockIndexWorkComparator > : : reverse_iterator it = setBlockIndexValid . rbegin ( ) ;
if ( it = = setBlockIndexValid . rend ( ) )
return ;
pindexNew = * it ;
}
// Check whether all blocks on the path between the currently active chain and the candidate are valid.
// Just going until the active chain is an optimization, as we know all blocks in it are valid already.
CBlockIndex * pindexTest = pindexNew ;
bool fInvalidAncestor = false ;
while ( pindexTest & & ! chainActive . Contains ( pindexTest ) ) {
if ( pindexTest - > nStatus & BLOCK_FAILED_MASK ) {
// Candidate has an invalid ancestor, remove entire chain from the set.
if ( pindexBestInvalid = = NULL | | pindexNew - > nChainWork > pindexBestInvalid - > nChainWork )
pindexBestInvalid = pindexNew ; CBlockIndex * pindexFailed = pindexNew ;
while ( pindexTest ! = pindexFailed ) {
pindexFailed - > nStatus | = BLOCK_FAILED_CHILD ;
setBlockIndexValid . erase ( pindexFailed ) ;
pindexFailed = pindexFailed - > pprev ;
}
fInvalidAncestor = true ;
break ;
2013-01-27 00:14:11 +01:00
}
2013-11-16 19:28:24 +01:00
pindexTest = pindexTest - > pprev ;
2010-08-29 18:58:15 +02:00
}
2013-11-16 19:28:24 +01:00
if ( fInvalidAncestor )
continue ;
2010-08-29 18:58:15 +02:00
2013-11-16 19:28:24 +01:00
break ;
} while ( true ) ;
2010-08-29 18:58:15 +02:00
2013-11-16 19:28:24 +01:00
// Check whether it's actually an improvement.
if ( chainMostWork . Tip ( ) & & ! CBlockIndexWorkComparator ( ) ( chainMostWork . Tip ( ) , pindexNew ) )
return ;
2012-12-01 16:46:23 +01:00
2013-11-16 19:28:24 +01:00
// We have a new best.
chainMostWork . SetTip ( pindexNew ) ;
}
2010-08-29 18:58:15 +02:00
2013-11-16 19:28:24 +01:00
// Try to activate to the most-work chain (thereby connecting it).
bool ActivateBestChain ( CValidationState & state ) {
CBlockIndex * pindexOldTip = chainActive . Tip ( ) ;
bool fComplete = false ;
while ( ! fComplete ) {
FindMostWorkChain ( ) ;
fComplete = true ;
2010-08-29 18:58:15 +02:00
2013-11-16 19:28:24 +01:00
// Check whether we have something to do.
if ( chainMostWork . Tip ( ) = = NULL ) break ;
// Disconnect active blocks which are no longer in the best chain.
while ( chainActive . Tip ( ) & & ! chainMostWork . Contains ( chainActive . Tip ( ) ) ) {
if ( ! DisconnectTip ( state ) )
return false ;
}
// Connect new blocks.
while ( ! chainActive . Contains ( chainMostWork . Tip ( ) ) ) {
CBlockIndex * pindexConnect = chainMostWork [ chainActive . Height ( ) + 1 ] ;
if ( ! ConnectTip ( state , pindexConnect ) ) {
if ( state . IsInvalid ( ) ) {
// The block violates a consensus rule.
if ( ! state . CorruptionPossible ( ) )
InvalidChainFound ( chainMostWork . Tip ( ) ) ;
fComplete = false ;
state = CValidationState ( ) ;
break ;
} else {
// A system error occurred (disk space, database error, ...).
return false ;
}
}
}
2012-11-24 14:26:51 +01:00
}
2010-08-29 18:58:15 +02:00
2013-11-16 19:28:24 +01:00
if ( chainActive . Tip ( ) ! = pindexOldTip ) {
std : : string strCmd = GetArg ( " -blocknotify " , " " ) ;
if ( ! IsInitialBlockDownload ( ) & & ! strCmd . empty ( ) )
{
boost : : replace_all ( strCmd , " %s " , chainActive . Tip ( ) - > GetBlockHash ( ) . GetHex ( ) ) ;
boost : : thread t ( runCommand , strCmd ) ; // thread runs free
}
}
2013-08-03 13:08:34 +02:00
2010-08-29 18:58:15 +02:00
return true ;
}
2013-06-24 04:00:18 +02:00
bool AddToBlockIndex ( CBlock & block , CValidationState & state , const CDiskBlockPos & pos )
2010-08-29 18:58:15 +02:00
{
// Check for duplicate
2013-06-24 04:00:18 +02:00
uint256 hash = block . GetHash ( ) ;
2010-08-29 18:58:15 +02:00
if ( mapBlockIndex . count ( hash ) )
2012-09-10 04:02:35 +02:00
return state . Invalid ( error ( " AddToBlockIndex() : %s already exists " , hash . ToString ( ) ) , 0 , " duplicate " ) ;
2010-08-29 18:58:15 +02:00
// Construct new block index object
2013-06-24 04:00:18 +02:00
CBlockIndex * pindexNew = new CBlockIndex ( block ) ;
2013-11-16 19:28:24 +01:00
{
LOCK ( cs_nBlockSequenceId ) ;
pindexNew - > nSequenceId = nBlockSequenceId + + ;
}
2013-01-27 01:24:06 +01:00
assert ( pindexNew ) ;
2010-08-29 18:58:15 +02:00
map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . insert ( make_pair ( hash , pindexNew ) ) . first ;
pindexNew - > phashBlock = & ( ( * mi ) . first ) ;
2013-06-24 04:00:18 +02:00
map < uint256 , CBlockIndex * > : : iterator miPrev = mapBlockIndex . find ( block . hashPrevBlock ) ;
2010-08-29 18:58:15 +02:00
if ( miPrev ! = mapBlockIndex . end ( ) )
{
pindexNew - > pprev = ( * miPrev ) . second ;
pindexNew - > nHeight = pindexNew - > pprev - > nHeight + 1 ;
}
2013-06-24 04:00:18 +02:00
pindexNew - > nTx = block . vtx . size ( ) ;
2013-03-28 23:51:50 +01:00
pindexNew - > nChainWork = ( pindexNew - > pprev ? pindexNew - > pprev - > nChainWork : 0 ) + pindexNew - > GetBlockWork ( ) . getuint256 ( ) ;
2012-08-19 00:33:01 +02:00
pindexNew - > nChainTx = ( pindexNew - > pprev ? pindexNew - > pprev - > nChainTx : 0 ) + pindexNew - > nTx ;
pindexNew - > nFile = pos . nFile ;
pindexNew - > nDataPos = pos . nPos ;
2012-08-13 19:11:05 +02:00
pindexNew - > nUndoPos = 0 ;
2012-08-19 00:33:01 +02:00
pindexNew - > nStatus = BLOCK_VALID_TRANSACTIONS | BLOCK_HAVE_DATA ;
setBlockIndexValid . insert ( pindexNew ) ;
2010-08-29 18:58:15 +02:00
2013-01-27 00:14:11 +01:00
if ( ! pblocktree - > WriteBlockIndex ( CDiskBlockIndex ( pindexNew ) ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write block index " ) ) ;
2010-08-29 18:58:15 +02:00
2012-08-19 00:33:01 +02:00
// New best?
2013-11-16 19:28:24 +01:00
if ( ! ActivateBestChain ( state ) )
2012-07-06 16:33:34 +02:00
return false ;
2010-08-29 18:58:15 +02:00
2013-10-10 23:07:44 +02:00
if ( pindexNew = = chainActive . Tip ( ) )
2010-08-29 18:58:15 +02:00
{
2013-05-07 18:33:52 +02:00
// Clear fork warning if its no longer applicable
CheckForkWarningConditions ( ) ;
2010-08-29 18:58:15 +02:00
// Notify UI to display prev block's coinbase if it was ours
static uint256 hashPrevBestCoinBase ;
2013-10-19 18:34:06 +02:00
g_signals . UpdatedTransaction ( hashPrevBestCoinBase ) ;
2013-06-24 04:00:18 +02:00
hashPrevBestCoinBase = block . GetTxHash ( 0 ) ;
2013-05-07 18:33:52 +02:00
} else
CheckForkWarningConditionsOnNewFork ( pindexNew ) ;
2010-08-29 18:58:15 +02:00
2013-01-27 00:14:11 +01:00
if ( ! pblocktree - > Flush ( ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to sync block index " ) ) ;
2012-09-03 15:26:57 +02:00
2012-05-06 19:40:58 +02:00
uiInterface . NotifyBlocksChanged ( ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2013-04-13 07:13:08 +02:00
bool FindBlockPos ( CValidationState & state , CDiskBlockPos & pos , unsigned int nAddSize , unsigned int nHeight , uint64_t nTime , bool fKnown = false )
2012-08-13 19:11:05 +02:00
{
bool fUpdatedLast = false ;
LOCK ( cs_LastBlockFile ) ;
2012-10-21 21:23:13 +02:00
if ( fKnown ) {
if ( nLastBlockFile ! = pos . nFile ) {
nLastBlockFile = pos . nFile ;
infoLastBlockFile . SetNull ( ) ;
pblocktree - > ReadBlockFileInfo ( nLastBlockFile , infoLastBlockFile ) ;
2012-12-04 23:46:30 +01:00
fUpdatedLast = true ;
2012-10-21 21:23:13 +02:00
}
} else {
while ( infoLastBlockFile . nSize + nAddSize > = MAX_BLOCKFILE_SIZE ) {
2014-01-16 16:15:27 +01:00
LogPrintf ( " Leaving block file %i: %s \n " , nLastBlockFile , infoLastBlockFile . ToString ( ) ) ;
2013-01-30 04:17:33 +01:00
FlushBlockFile ( true ) ;
2012-10-21 21:23:13 +02:00
nLastBlockFile + + ;
infoLastBlockFile . SetNull ( ) ;
pblocktree - > ReadBlockFileInfo ( nLastBlockFile , infoLastBlockFile ) ; // check whether data for the new file somehow already exist; can fail just fine
fUpdatedLast = true ;
}
pos . nFile = nLastBlockFile ;
pos . nPos = infoLastBlockFile . nSize ;
2012-08-13 19:11:05 +02:00
}
infoLastBlockFile . nSize + = nAddSize ;
infoLastBlockFile . AddBlock ( nHeight , nTime ) ;
2012-10-21 21:23:13 +02:00
if ( ! fKnown ) {
unsigned int nOldChunks = ( pos . nPos + BLOCKFILE_CHUNK_SIZE - 1 ) / BLOCKFILE_CHUNK_SIZE ;
unsigned int nNewChunks = ( infoLastBlockFile . nSize + BLOCKFILE_CHUNK_SIZE - 1 ) / BLOCKFILE_CHUNK_SIZE ;
if ( nNewChunks > nOldChunks ) {
2012-12-04 07:48:57 +01:00
if ( CheckDiskSpace ( nNewChunks * BLOCKFILE_CHUNK_SIZE - pos . nPos ) ) {
FILE * file = OpenBlockFile ( pos ) ;
if ( file ) {
2013-09-18 12:38:08 +02:00
LogPrintf ( " Pre-allocating up to position 0x%x in blk%05u.dat \n " , nNewChunks * BLOCKFILE_CHUNK_SIZE , pos . nFile ) ;
2012-12-04 07:48:57 +01:00
AllocateFileRange ( file , pos . nPos , nNewChunks * BLOCKFILE_CHUNK_SIZE - pos . nPos ) ;
fclose ( file ) ;
}
2012-10-21 21:23:13 +02:00
}
2012-12-04 07:48:57 +01:00
else
2012-09-10 04:02:35 +02:00
return state . Error ( " out of disk space " ) ;
2012-08-16 02:21:28 +02:00
}
}
2012-09-03 15:26:57 +02:00
if ( ! pblocktree - > WriteBlockFileInfo ( nLastBlockFile , infoLastBlockFile ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write file info " ) ) ;
2012-08-13 19:11:05 +02:00
if ( fUpdatedLast )
2012-09-03 15:26:57 +02:00
pblocktree - > WriteLastBlockFile ( nLastBlockFile ) ;
2012-08-13 19:11:05 +02:00
return true ;
}
2013-01-27 00:14:11 +01:00
bool FindUndoPos ( CValidationState & state , int nFile , CDiskBlockPos & pos , unsigned int nAddSize )
2012-08-13 19:11:05 +02:00
{
pos . nFile = nFile ;
LOCK ( cs_LastBlockFile ) ;
2012-08-16 02:21:28 +02:00
unsigned int nNewSize ;
2012-08-13 19:11:05 +02:00
if ( nFile = = nLastBlockFile ) {
pos . nPos = infoLastBlockFile . nUndoSize ;
2012-08-16 02:21:28 +02:00
nNewSize = ( infoLastBlockFile . nUndoSize + = nAddSize ) ;
2012-09-03 15:26:57 +02:00
if ( ! pblocktree - > WriteBlockFileInfo ( nLastBlockFile , infoLastBlockFile ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write block info " ) ) ;
2012-08-16 02:21:28 +02:00
} else {
CBlockFileInfo info ;
2012-09-03 15:26:57 +02:00
if ( ! pblocktree - > ReadBlockFileInfo ( nFile , info ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to read block info " ) ) ;
2012-08-16 02:21:28 +02:00
pos . nPos = info . nUndoSize ;
nNewSize = ( info . nUndoSize + = nAddSize ) ;
2012-09-03 15:26:57 +02:00
if ( ! pblocktree - > WriteBlockFileInfo ( nFile , info ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write block info " ) ) ;
2012-08-16 02:21:28 +02:00
}
unsigned int nOldChunks = ( pos . nPos + UNDOFILE_CHUNK_SIZE - 1 ) / UNDOFILE_CHUNK_SIZE ;
unsigned int nNewChunks = ( nNewSize + UNDOFILE_CHUNK_SIZE - 1 ) / UNDOFILE_CHUNK_SIZE ;
if ( nNewChunks > nOldChunks ) {
2012-12-04 07:48:57 +01:00
if ( CheckDiskSpace ( nNewChunks * UNDOFILE_CHUNK_SIZE - pos . nPos ) ) {
FILE * file = OpenUndoFile ( pos ) ;
if ( file ) {
2013-09-18 12:38:08 +02:00
LogPrintf ( " Pre-allocating up to position 0x%x in rev%05u.dat \n " , nNewChunks * UNDOFILE_CHUNK_SIZE , pos . nFile ) ;
2012-12-04 07:48:57 +01:00
AllocateFileRange ( file , pos . nPos , nNewChunks * UNDOFILE_CHUNK_SIZE - pos . nPos ) ;
fclose ( file ) ;
}
2012-08-16 02:21:28 +02:00
}
2012-12-04 07:48:57 +01:00
else
2012-09-10 04:02:35 +02:00
return state . Error ( " out of disk space " ) ;
2012-08-13 19:11:05 +02:00
}
return true ;
}
2010-08-29 18:58:15 +02:00
2013-06-24 04:14:11 +02:00
bool CheckBlock ( const CBlock & block , CValidationState & state , bool fCheckPOW , bool fCheckMerkleRoot )
2010-08-29 18:58:15 +02:00
{
// These are checks that are independent of context
// that can be verified before saving an orphan block.
// Size limits
2013-06-24 04:14:11 +02:00
if ( block . vtx . empty ( ) | | block . vtx . size ( ) > MAX_BLOCK_SIZE | | : : GetSerializeSize ( block , SER_NETWORK , PROTOCOL_VERSION ) > MAX_BLOCK_SIZE )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckBlock() : size limits failed " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-blk-length " ) ;
2010-08-29 18:58:15 +02:00
2010-09-19 23:20:34 +02:00
// Check proof of work matches claimed amount
2013-06-24 04:14:11 +02:00
if ( fCheckPOW & & ! CheckProofOfWork ( block . GetHash ( ) , block . nBits ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 50 , error ( " CheckBlock() : proof of work failed " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " high-hash " ) ;
2010-09-19 23:20:34 +02:00
2010-08-29 18:58:15 +02:00
// Check timestamp
2013-06-24 04:14:11 +02:00
if ( block . GetBlockTime ( ) > GetAdjustedTime ( ) + 2 * 60 * 60 )
2013-10-28 07:36:11 +01:00
return state . Invalid ( error ( " CheckBlock() : block timestamp too far in the future " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " time-too-new " ) ;
2010-08-29 18:58:15 +02:00
// First transaction must be coinbase, the rest must not be
2013-06-24 04:14:11 +02:00
if ( block . vtx . empty ( ) | | ! block . vtx [ 0 ] . IsCoinBase ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckBlock() : first tx is not coinbase " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-cb-missing " ) ;
2013-06-24 04:14:11 +02:00
for ( unsigned int i = 1 ; i < block . vtx . size ( ) ; i + + )
if ( block . vtx [ i ] . IsCoinBase ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckBlock() : more than one coinbase " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-cb-multiple " ) ;
2010-08-29 18:58:15 +02:00
// Check transactions
2013-06-24 04:14:11 +02:00
BOOST_FOREACH ( const CTransaction & tx , block . vtx )
2013-01-08 13:17:15 +01:00
if ( ! CheckTransaction ( tx , state ) )
2013-01-27 00:14:11 +01:00
return error ( " CheckBlock() : CheckTransaction failed " ) ;
2010-08-29 18:58:15 +02:00
2012-10-23 00:21:16 +02:00
// Build the merkle tree already. We need it anyway later, and it makes the
// block cache the transaction hashes, which means they don't need to be
// recalculated many times during this block's validation.
2013-06-24 04:14:11 +02:00
block . BuildMerkleTree ( ) ;
2012-10-23 00:21:16 +02:00
2012-04-30 02:56:55 +02:00
// Check for duplicate txids. This is caught by ConnectInputs(),
// but catching it earlier avoids a potential DoS attack:
set < uint256 > uniqueTx ;
2013-06-24 04:14:11 +02:00
for ( unsigned int i = 0 ; i < block . vtx . size ( ) ; i + + ) {
uniqueTx . insert ( block . GetTxHash ( i ) ) ;
2012-04-30 02:56:55 +02:00
}
2013-06-24 04:14:11 +02:00
if ( uniqueTx . size ( ) ! = block . vtx . size ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckBlock() : duplicate transaction " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-duplicate " , true ) ;
2012-04-30 02:56:55 +02:00
2012-04-23 20:14:03 +02:00
unsigned int nSigOps = 0 ;
2013-06-24 04:14:11 +02:00
BOOST_FOREACH ( const CTransaction & tx , block . vtx )
2011-10-03 19:05:43 +02:00
{
2013-01-08 13:17:15 +01:00
nSigOps + = GetLegacySigOpCount ( tx ) ;
2011-10-03 19:05:43 +02:00
}
if ( nSigOps > MAX_BLOCK_SIGOPS )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckBlock() : out-of-bounds SigOpCount " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-blk-sigops " , true ) ;
2010-08-29 18:58:15 +02:00
2012-07-26 02:48:39 +02:00
// Check merkle root
2013-08-10 15:26:12 +02:00
if ( fCheckMerkleRoot & & block . hashMerkleRoot ! = block . vMerkleTree . back ( ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " CheckBlock() : hashMerkleRoot mismatch " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txnmrklroot " , true ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2013-06-24 04:27:02 +02:00
bool AcceptBlock ( CBlock & block , CValidationState & state , CDiskBlockPos * dbp )
2010-08-29 18:58:15 +02:00
{
// Check for duplicate
2013-06-24 04:27:02 +02:00
uint256 hash = block . GetHash ( ) ;
2010-08-29 18:58:15 +02:00
if ( mapBlockIndex . count ( hash ) )
2012-09-10 04:02:35 +02:00
return state . Invalid ( error ( " AcceptBlock() : block already in mapBlockIndex " ) , 0 , " duplicate " ) ;
2010-08-29 18:58:15 +02:00
// Get prev block index
2012-10-21 21:23:13 +02:00
CBlockIndex * pindexPrev = NULL ;
int nHeight = 0 ;
2013-05-07 15:16:25 +02:00
if ( hash ! = Params ( ) . HashGenesisBlock ( ) ) {
2013-06-24 04:27:02 +02:00
map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . find ( block . hashPrevBlock ) ;
2012-11-10 14:26:34 +01:00
if ( mi = = mapBlockIndex . end ( ) )
2012-09-10 04:02:35 +02:00
return state . DoS ( 10 , error ( " AcceptBlock() : prev block not found " ) , 0 , " bad-prevblk " ) ;
2012-11-10 14:26:34 +01:00
pindexPrev = ( * mi ) . second ;
nHeight = pindexPrev - > nHeight + 1 ;
// Check proof of work
2013-06-24 04:27:02 +02:00
if ( block . nBits ! = GetNextWorkRequired ( pindexPrev , & block ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " AcceptBlock() : incorrect proof of work " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-diffbits " ) ;
2012-11-10 14:26:34 +01:00
// Check timestamp against prev
2013-06-24 04:27:02 +02:00
if ( block . GetBlockTime ( ) < = pindexPrev - > GetMedianTimePast ( ) )
2013-10-28 07:36:11 +01:00
return state . Invalid ( error ( " AcceptBlock() : block's timestamp is too early " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " time-too-old " ) ;
2012-11-10 14:26:34 +01:00
// Check that all transactions are finalized
2013-06-24 04:27:02 +02:00
BOOST_FOREACH ( const CTransaction & tx , block . vtx )
if ( ! IsFinalTx ( tx , nHeight , block . GetBlockTime ( ) ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 10 , error ( " AcceptBlock() : contains a non-final transaction " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-txns-nonfinal " ) ;
2012-11-10 14:26:34 +01:00
// Check that the block chain matches the known block chain up to a checkpoint
if ( ! Checkpoints : : CheckBlock ( nHeight , hash ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " AcceptBlock() : rejected by checkpoint lock-in at %d " , nHeight ) ,
REJECT_CHECKPOINT , " checkpoint mismatch " ) ;
2012-11-10 14:26:34 +01:00
2014-01-20 00:59:57 +01:00
// Don't accept any forks from the main chain prior to last checkpoint
CBlockIndex * pcheckpoint = Checkpoints : : GetLastCheckpoint ( mapBlockIndex ) ;
if ( pcheckpoint & & nHeight < pcheckpoint - > nHeight )
return state . DoS ( 100 , error ( " AcceptBlock() : forked chain older than last checkpoint (height %d) " , nHeight ) ) ;
2012-11-10 14:26:34 +01:00
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2013-06-24 04:27:02 +02:00
if ( block . nVersion < 2 )
2012-07-06 03:22:16 +02:00
{
2013-05-07 15:16:25 +02:00
if ( ( ! TestNet ( ) & & CBlockIndex : : IsSuperMajority ( 2 , pindexPrev , 950 , 1000 ) ) | |
( TestNet ( ) & & CBlockIndex : : IsSuperMajority ( 2 , pindexPrev , 75 , 100 ) ) )
2012-11-10 14:26:34 +01:00
{
2013-10-28 07:36:11 +01:00
return state . Invalid ( error ( " AcceptBlock() : rejected nVersion=1 block " ) ,
2012-09-10 04:02:35 +02:00
REJECT_OBSOLETE , " bad-version " ) ;
2012-11-10 14:26:34 +01:00
}
2012-07-06 03:22:16 +02:00
}
2012-11-10 14:26:34 +01:00
// Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
2013-06-24 04:27:02 +02:00
if ( block . nVersion > = 2 )
2012-06-28 01:30:39 +02:00
{
2012-11-10 14:26:34 +01:00
// if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
2013-05-07 15:16:25 +02:00
if ( ( ! TestNet ( ) & & CBlockIndex : : IsSuperMajority ( 2 , pindexPrev , 750 , 1000 ) ) | |
( TestNet ( ) & & CBlockIndex : : IsSuperMajority ( 2 , pindexPrev , 51 , 100 ) ) )
2012-11-10 14:26:34 +01:00
{
CScript expect = CScript ( ) < < nHeight ;
2013-08-24 23:22:13 +02:00
if ( block . vtx [ 0 ] . vin [ 0 ] . scriptSig . size ( ) < expect . size ( ) | |
! std : : equal ( expect . begin ( ) , expect . end ( ) , block . vtx [ 0 ] . vin [ 0 ] . scriptSig . begin ( ) ) )
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " AcceptBlock() : block height mismatch in coinbase " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-cb-height " ) ;
2012-11-10 14:26:34 +01:00
}
2012-06-28 01:30:39 +02:00
}
}
2010-08-29 18:58:15 +02:00
// Write block to history file
2013-01-29 01:44:19 +01:00
try {
2013-06-24 04:27:02 +02:00
unsigned int nBlockSize = : : GetSerializeSize ( block , SER_DISK , CLIENT_VERSION ) ;
2013-01-29 01:44:19 +01:00
CDiskBlockPos blockPos ;
if ( dbp ! = NULL )
blockPos = * dbp ;
2013-06-24 04:27:02 +02:00
if ( ! FindBlockPos ( state , blockPos , nBlockSize + 8 , nHeight , block . nTime , dbp ! = NULL ) )
2013-01-29 01:44:19 +01:00
return error ( " AcceptBlock() : FindBlockPos failed " ) ;
if ( dbp = = NULL )
2013-06-24 04:27:02 +02:00
if ( ! WriteBlockToDisk ( block , blockPos ) )
2013-01-29 01:44:19 +01:00
return state . Abort ( _ ( " Failed to write block " ) ) ;
2013-06-24 04:27:02 +02:00
if ( ! AddToBlockIndex ( block , state , blockPos ) )
2013-01-29 01:44:19 +01:00
return error ( " AcceptBlock() : AddToBlockIndex failed " ) ;
} catch ( std : : runtime_error & e ) {
return state . Abort ( _ ( " System error: " ) + e . what ( ) ) ;
}
2010-08-29 18:58:15 +02:00
// Relay inventory, but don't relay old inventory during initial block download
2012-03-20 18:45:45 +01:00
int nBlockEstimate = Checkpoints : : GetTotalBlocksEstimate ( ) ;
2013-10-10 23:07:44 +02:00
if ( chainActive . Tip ( ) - > GetBlockHash ( ) = = hash )
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
2013-10-10 23:07:44 +02:00
if ( chainActive . Height ( ) > ( pnode - > nStartingHeight ! = - 1 ? pnode - > nStartingHeight - 2000 : nBlockEstimate ) )
2012-04-06 18:39:12 +02:00
pnode - > PushInventory ( CInv ( MSG_BLOCK , hash ) ) ;
}
2010-08-29 18:58:15 +02:00
return true ;
}
2012-06-28 01:30:39 +02:00
bool CBlockIndex : : IsSuperMajority ( int minVersion , const CBlockIndex * pstart , unsigned int nRequired , unsigned int nToCheck )
{
unsigned int nFound = 0 ;
for ( unsigned int i = 0 ; i < nToCheck & & nFound < nRequired & & pstart ! = NULL ; i + + )
{
if ( pstart - > nVersion > = minVersion )
+ + nFound ;
pstart = pstart - > pprev ;
}
return ( nFound > = nRequired ) ;
}
2013-04-13 07:13:08 +02:00
int64_t CBlockIndex : : GetMedianTime ( ) const
2013-10-10 23:07:44 +02:00
{
const CBlockIndex * pindex = this ;
for ( int i = 0 ; i < nMedianTimeSpan / 2 ; i + + )
{
if ( ! chainActive . Next ( pindex ) )
return GetBlockTime ( ) ;
pindex = chainActive . Next ( pindex ) ;
}
return pindex - > GetMedianTimePast ( ) ;
}
2013-01-07 15:39:53 +01:00
void PushGetBlocks ( CNode * pnode , CBlockIndex * pindexBegin , uint256 hashEnd )
{
// Filter out duplicate requests
if ( pindexBegin = = pnode - > pindexLastGetBlocksBegin & & hashEnd = = pnode - > hashLastGetBlocksEnd )
return ;
pnode - > pindexLastGetBlocksBegin = pindexBegin ;
pnode - > hashLastGetBlocksEnd = hashEnd ;
2013-10-12 15:18:08 +02:00
pnode - > PushMessage ( " getblocks " , chainActive . GetLocator ( pindexBegin ) , hashEnd ) ;
2013-01-07 15:39:53 +01:00
}
2013-01-27 00:14:11 +01:00
bool ProcessBlock ( CValidationState & state , CNode * pfrom , CBlock * pblock , CDiskBlockPos * dbp )
2010-08-29 18:58:15 +02:00
{
2013-12-19 09:09:51 +01:00
AssertLockHeld ( cs_main ) ;
2013-11-29 08:25:30 +01:00
2010-08-29 18:58:15 +02:00
// Check for duplicate
uint256 hash = pblock - > GetHash ( ) ;
if ( mapBlockIndex . count ( hash ) )
2012-09-10 04:02:35 +02:00
return state . Invalid ( error ( " ProcessBlock() : already have block %d %s " , mapBlockIndex [ hash ] - > nHeight , hash . ToString ( ) ) , 0 , " duplicate " ) ;
2010-08-29 18:58:15 +02:00
if ( mapOrphanBlocks . count ( hash ) )
2012-09-10 04:02:35 +02:00
return state . Invalid ( error ( " ProcessBlock() : already have block (orphan) %s " , hash . ToString ( ) ) , 0 , " duplicate " ) ;
2010-08-29 18:58:15 +02:00
// Preliminary checks
2014-01-10 13:23:26 +01:00
if ( ! CheckBlock ( * pblock , state ) )
2010-08-29 18:58:15 +02:00
return error ( " ProcessBlock() : CheckBlock FAILED " ) ;
2011-09-08 18:51:43 +02:00
CBlockIndex * pcheckpoint = Checkpoints : : GetLastCheckpoint ( mapBlockIndex ) ;
2013-10-10 23:07:44 +02:00
if ( pcheckpoint & & pblock - > hashPrevBlock ! = ( chainActive . Tip ( ) ? chainActive . Tip ( ) - > GetBlockHash ( ) : uint256 ( 0 ) ) )
2011-09-08 18:51:43 +02:00
{
// Extra checks to prevent "fill up memory by spamming with bogus blocks"
2013-04-13 07:13:08 +02:00
int64_t deltaTime = pblock - > GetBlockTime ( ) - pcheckpoint - > nTime ;
2011-09-08 18:51:43 +02:00
if ( deltaTime < 0 )
{
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " ProcessBlock() : block with timestamp before last checkpoint " ) ,
2012-09-10 04:02:35 +02:00
REJECT_CHECKPOINT , " time-too-old " ) ;
2011-09-08 18:51:43 +02:00
}
CBigNum bnNewBlock ;
bnNewBlock . SetCompact ( pblock - > nBits ) ;
CBigNum bnRequired ;
bnRequired . SetCompact ( ComputeMinWork ( pcheckpoint - > nBits , deltaTime ) ) ;
if ( bnNewBlock > bnRequired )
{
2013-10-28 07:36:11 +01:00
return state . DoS ( 100 , error ( " ProcessBlock() : block with too little proof-of-work " ) ,
2012-09-10 04:02:35 +02:00
REJECT_INVALID , " bad-diffbits " ) ;
2011-09-08 18:51:43 +02:00
}
}
2012-08-21 19:18:53 +02:00
// If we don't already have its previous block, shunt it off to holding area until we get it
2012-10-21 21:23:13 +02:00
if ( pblock - > hashPrevBlock ! = 0 & & ! mapBlockIndex . count ( pblock - > hashPrevBlock ) )
2010-08-29 18:58:15 +02:00
{
2014-01-11 17:28:34 +01:00
LogPrintf ( " ProcessBlock: ORPHAN BLOCK %lu, prev=%s \n " , ( unsigned long ) mapOrphanBlocks . size ( ) , pblock - > hashPrevBlock . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
2012-08-21 19:18:53 +02:00
// Accept orphans as long as there is a node to request its parents from
if ( pfrom ) {
2014-01-11 17:28:34 +01:00
PruneOrphanBlocks ( ) ;
2014-01-11 23:33:34 +01:00
COrphanBlock * pblock2 = new COrphanBlock ( ) ;
{
CDataStream ss ( SER_DISK , CLIENT_VERSION ) ;
ss < < * pblock ;
pblock2 - > vchBlock = std : : vector < unsigned char > ( ss . begin ( ) , ss . end ( ) ) ;
}
pblock2 - > hashBlock = hash ;
pblock2 - > hashPrev = pblock - > hashPrevBlock ;
2012-08-21 19:18:53 +02:00
mapOrphanBlocks . insert ( make_pair ( hash , pblock2 ) ) ;
2014-01-11 23:33:34 +01:00
mapOrphanBlocksByPrev . insert ( make_pair ( pblock2 - > hashPrev , pblock2 ) ) ;
2012-08-21 19:18:53 +02:00
// Ask this guy to fill in what we're missing
2014-01-11 23:33:34 +01:00
PushGetBlocks ( pfrom , chainActive . Tip ( ) , GetOrphanRoot ( hash ) ) ;
2012-08-21 19:18:53 +02:00
}
2010-08-29 18:58:15 +02:00
return true ;
}
// Store to disk
2013-06-24 04:27:02 +02:00
if ( ! AcceptBlock ( * pblock , state , dbp ) )
2010-08-29 18:58:15 +02:00
return error ( " ProcessBlock() : AcceptBlock FAILED " ) ;
// Recursively process any orphan blocks that depended on this one
vector < uint256 > vWorkQueue ;
vWorkQueue . push_back ( hash ) ;
2012-04-15 22:52:09 +02:00
for ( unsigned int i = 0 ; i < vWorkQueue . size ( ) ; i + + )
2010-08-29 18:58:15 +02:00
{
uint256 hashPrev = vWorkQueue [ i ] ;
2014-01-11 23:33:34 +01:00
for ( multimap < uint256 , COrphanBlock * > : : iterator mi = mapOrphanBlocksByPrev . lower_bound ( hashPrev ) ;
2010-08-29 18:58:15 +02:00
mi ! = mapOrphanBlocksByPrev . upper_bound ( hashPrev ) ;
+ + mi )
{
2014-01-11 23:33:34 +01:00
CBlock block ;
{
CDataStream ss ( mi - > second - > vchBlock , SER_DISK , CLIENT_VERSION ) ;
ss > > block ;
}
block . BuildMerkleTree ( ) ;
2013-01-31 04:53:21 +01:00
// Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan resolution (that is, feeding people an invalid block based on LegitBlockX in order to get anyone relaying LegitBlockX banned)
CValidationState stateDummy ;
2014-01-11 23:33:34 +01:00
if ( AcceptBlock ( block , stateDummy ) )
vWorkQueue . push_back ( mi - > second - > hashBlock ) ;
mapOrphanBlocks . erase ( mi - > second - > hashBlock ) ;
delete mi - > second ;
2010-08-29 18:58:15 +02:00
}
mapOrphanBlocksByPrev . erase ( hashPrev ) ;
}
2013-09-18 12:38:08 +02:00
LogPrintf ( " ProcessBlock: ACCEPTED \n " ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2012-08-19 05:40:00 +02:00
CMerkleBlock : : CMerkleBlock ( const CBlock & block , CBloomFilter & filter )
{
header = block . GetBlockHeader ( ) ;
2012-11-08 22:26:25 +01:00
vector < bool > vMatch ;
vector < uint256 > vHashes ;
vMatch . reserve ( block . vtx . size ( ) ) ;
vHashes . reserve ( block . vtx . size ( ) ) ;
for ( unsigned int i = 0 ; i < block . vtx . size ( ) ; i + + )
2012-08-19 05:40:00 +02:00
{
uint256 hash = block . vtx [ i ] . GetHash ( ) ;
if ( filter . IsRelevantAndUpdate ( block . vtx [ i ] , hash ) )
{
2012-11-08 22:26:25 +01:00
vMatch . push_back ( true ) ;
vMatchedTxn . push_back ( make_pair ( i , hash ) ) ;
2012-08-19 05:40:00 +02:00
}
2012-11-08 22:26:25 +01:00
else
vMatch . push_back ( false ) ;
vHashes . push_back ( hash ) ;
2012-08-19 05:40:00 +02:00
}
2012-11-08 22:26:25 +01:00
txn = CPartialMerkleTree ( vHashes , vMatch ) ;
2012-08-19 05:40:00 +02:00
}
2012-10-27 21:08:45 +02:00
uint256 CPartialMerkleTree : : CalcHash ( int height , unsigned int pos , const std : : vector < uint256 > & vTxid ) {
if ( height = = 0 ) {
// hash at height 0 is the txids themself
return vTxid [ pos ] ;
} else {
// calculate left hash
uint256 left = CalcHash ( height - 1 , pos * 2 , vTxid ) , right ;
// calculate right hash if not beyong the end of the array - copy left hash otherwise1
if ( pos * 2 + 1 < CalcTreeWidth ( height - 1 ) )
right = CalcHash ( height - 1 , pos * 2 + 1 , vTxid ) ;
else
right = left ;
// combine subhashes
return Hash ( BEGIN ( left ) , END ( left ) , BEGIN ( right ) , END ( right ) ) ;
}
}
void CPartialMerkleTree : : TraverseAndBuild ( int height , unsigned int pos , const std : : vector < uint256 > & vTxid , const std : : vector < bool > & vMatch ) {
// determine whether this node is the parent of at least one matched txid
bool fParentOfMatch = false ;
for ( unsigned int p = pos < < height ; p < ( pos + 1 ) < < height & & p < nTransactions ; p + + )
fParentOfMatch | = vMatch [ p ] ;
// store as flag bit
vBits . push_back ( fParentOfMatch ) ;
if ( height = = 0 | | ! fParentOfMatch ) {
// if at height 0, or nothing interesting below, store hash and stop
vHash . push_back ( CalcHash ( height , pos , vTxid ) ) ;
} else {
// otherwise, don't store any hash, but descend into the subtrees
TraverseAndBuild ( height - 1 , pos * 2 , vTxid , vMatch ) ;
if ( pos * 2 + 1 < CalcTreeWidth ( height - 1 ) )
TraverseAndBuild ( height - 1 , pos * 2 + 1 , vTxid , vMatch ) ;
}
}
uint256 CPartialMerkleTree : : TraverseAndExtract ( int height , unsigned int pos , unsigned int & nBitsUsed , unsigned int & nHashUsed , std : : vector < uint256 > & vMatch ) {
if ( nBitsUsed > = vBits . size ( ) ) {
// overflowed the bits array - failure
fBad = true ;
return 0 ;
}
bool fParentOfMatch = vBits [ nBitsUsed + + ] ;
if ( height = = 0 | | ! fParentOfMatch ) {
// if at height 0, or nothing interesting below, use stored hash and do not descend
if ( nHashUsed > = vHash . size ( ) ) {
// overflowed the hash array - failure
fBad = true ;
return 0 ;
}
const uint256 & hash = vHash [ nHashUsed + + ] ;
if ( height = = 0 & & fParentOfMatch ) // in case of height 0, we have a matched txid
vMatch . push_back ( hash ) ;
return hash ;
} else {
// otherwise, descend into the subtrees to extract matched txids and hashes
uint256 left = TraverseAndExtract ( height - 1 , pos * 2 , nBitsUsed , nHashUsed , vMatch ) , right ;
if ( pos * 2 + 1 < CalcTreeWidth ( height - 1 ) )
right = TraverseAndExtract ( height - 1 , pos * 2 + 1 , nBitsUsed , nHashUsed , vMatch ) ;
else
right = left ;
// and combine them before returning
return Hash ( BEGIN ( left ) , END ( left ) , BEGIN ( right ) , END ( right ) ) ;
}
}
CPartialMerkleTree : : CPartialMerkleTree ( const std : : vector < uint256 > & vTxid , const std : : vector < bool > & vMatch ) : nTransactions ( vTxid . size ( ) ) , fBad ( false ) {
// reset state
vBits . clear ( ) ;
vHash . clear ( ) ;
// calculate height of tree
int nHeight = 0 ;
while ( CalcTreeWidth ( nHeight ) > 1 )
nHeight + + ;
// traverse the partial tree
TraverseAndBuild ( nHeight , 0 , vTxid , vMatch ) ;
}
CPartialMerkleTree : : CPartialMerkleTree ( ) : nTransactions ( 0 ) , fBad ( true ) { }
uint256 CPartialMerkleTree : : ExtractMatches ( std : : vector < uint256 > & vMatch ) {
vMatch . clear ( ) ;
// An empty set will not work
if ( nTransactions = = 0 )
return 0 ;
// check for excessively high numbers of transactions
if ( nTransactions > MAX_BLOCK_SIZE / 60 ) // 60 is the lower bound for the size of a serialized CTransaction
return 0 ;
// there can never be more hashes provided than one for every txid
if ( vHash . size ( ) > nTransactions )
return 0 ;
// there must be at least one bit per node in the partial tree, and at least one node per hash
if ( vBits . size ( ) < vHash . size ( ) )
return 0 ;
// calculate height of tree
int nHeight = 0 ;
while ( CalcTreeWidth ( nHeight ) > 1 )
nHeight + + ;
// traverse the partial tree
unsigned int nBitsUsed = 0 , nHashUsed = 0 ;
uint256 hashMerkleRoot = TraverseAndExtract ( nHeight , 0 , nBitsUsed , nHashUsed , vMatch ) ;
// verify that no problems occured during the tree traversal
if ( fBad )
return 0 ;
// verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
if ( ( nBitsUsed + 7 ) / 8 ! = ( vBits . size ( ) + 7 ) / 8 )
return 0 ;
// verify that all hashes were consumed
if ( nHashUsed ! = vHash . size ( ) )
return 0 ;
return hashMerkleRoot ;
}
2013-01-27 01:24:06 +01:00
bool AbortNode ( const std : : string & strMessage ) {
strMiscWarning = strMessage ;
2014-01-16 16:15:27 +01:00
LogPrintf ( " *** %s \n " , strMessage ) ;
2013-01-18 15:07:05 +01:00
uiInterface . ThreadSafeMessageBox ( strMessage , " " , CClientUIInterface : : MSG_ERROR ) ;
2013-01-27 01:24:06 +01:00
StartShutdown ( ) ;
return false ;
}
2012-10-27 21:08:45 +02:00
2013-04-13 07:13:08 +02:00
bool CheckDiskSpace ( uint64_t nAdditionalBytes )
2010-08-29 18:58:15 +02:00
{
2013-04-13 07:13:08 +02:00
uint64_t nFreeBytesAvailable = filesystem : : space ( GetDataDir ( ) ) . available ;
2010-08-29 18:58:15 +02:00
2012-05-14 07:49:17 +02:00
// Check for nMinDiskSpace bytes (currently 50MB)
if ( nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes )
2013-01-27 01:24:06 +01:00
return AbortNode ( _ ( " Error: Disk space is low! " ) ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2012-08-13 19:11:05 +02:00
FILE * OpenDiskFile ( const CDiskBlockPos & pos , const char * prefix , bool fReadOnly )
2012-09-05 03:40:26 +02:00
{
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( pos . IsNull ( ) )
2010-08-29 18:58:15 +02:00
return NULL ;
2012-08-13 19:11:05 +02:00
boost : : filesystem : : path path = GetDataDir ( ) / " blocks " / strprintf ( " %s%05u.dat " , prefix , pos . nFile ) ;
boost : : filesystem : : create_directories ( path . parent_path ( ) ) ;
FILE * file = fopen ( path . string ( ) . c_str ( ) , " rb+ " ) ;
if ( ! file & & ! fReadOnly )
file = fopen ( path . string ( ) . c_str ( ) , " wb+ " ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( ! file ) {
2014-01-16 16:15:27 +01:00
LogPrintf ( " Unable to open file %s \n " , path . string ( ) ) ;
2010-08-29 18:58:15 +02:00
return NULL ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
}
2012-08-13 19:11:05 +02:00
if ( pos . nPos ) {
if ( fseek ( file , pos . nPos , SEEK_SET ) ) {
2014-01-16 16:15:27 +01:00
LogPrintf ( " Unable to seek to position %u of %s \n " , pos . nPos , path . string ( ) ) ;
2012-08-13 19:11:05 +02:00
fclose ( file ) ;
return NULL ;
}
}
2010-08-29 18:58:15 +02:00
return file ;
}
2012-08-13 19:11:05 +02:00
FILE * OpenBlockFile ( const CDiskBlockPos & pos , bool fReadOnly ) {
return OpenDiskFile ( pos , " blk " , fReadOnly ) ;
}
2013-01-18 15:07:05 +01:00
FILE * OpenUndoFile ( const CDiskBlockPos & pos , bool fReadOnly ) {
2012-08-13 19:11:05 +02:00
return OpenDiskFile ( pos , " rev " , fReadOnly ) ;
}
2012-09-03 21:14:03 +02:00
CBlockIndex * InsertBlockIndex ( uint256 hash )
{
if ( hash = = 0 )
return NULL ;
// Return existing
map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . find ( hash ) ;
if ( mi ! = mapBlockIndex . end ( ) )
return ( * mi ) . second ;
// Create new
CBlockIndex * pindexNew = new CBlockIndex ( ) ;
if ( ! pindexNew )
throw runtime_error ( " LoadBlockIndex() : new CBlockIndex failed " ) ;
mi = mapBlockIndex . insert ( make_pair ( hash , pindexNew ) ) . first ;
pindexNew - > phashBlock = & ( ( * mi ) . first ) ;
return pindexNew ;
}
bool static LoadBlockIndexDB ( )
{
if ( ! pblocktree - > LoadBlockIndexGuts ( ) )
return false ;
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2012-09-03 21:14:03 +02:00
2013-03-28 23:51:50 +01:00
// Calculate nChainWork
2012-09-03 21:14:03 +02:00
vector < pair < int , CBlockIndex * > > vSortedByHeight ;
vSortedByHeight . reserve ( mapBlockIndex . size ( ) ) ;
BOOST_FOREACH ( const PAIRTYPE ( uint256 , CBlockIndex * ) & item , mapBlockIndex )
{
CBlockIndex * pindex = item . second ;
vSortedByHeight . push_back ( make_pair ( pindex - > nHeight , pindex ) ) ;
}
sort ( vSortedByHeight . begin ( ) , vSortedByHeight . end ( ) ) ;
BOOST_FOREACH ( const PAIRTYPE ( int , CBlockIndex * ) & item , vSortedByHeight )
{
CBlockIndex * pindex = item . second ;
2013-03-28 23:51:50 +01:00
pindex - > nChainWork = ( pindex - > pprev ? pindex - > pprev - > nChainWork : 0 ) + pindex - > GetBlockWork ( ) . getuint256 ( ) ;
2012-09-03 21:14:03 +02:00
pindex - > nChainTx = ( pindex - > pprev ? pindex - > pprev - > nChainTx : 0 ) + pindex - > nTx ;
if ( ( pindex - > nStatus & BLOCK_VALID_MASK ) > = BLOCK_VALID_TRANSACTIONS & & ! ( pindex - > nStatus & BLOCK_FAILED_MASK ) )
setBlockIndexValid . insert ( pindex ) ;
2013-10-13 22:15:48 +02:00
if ( pindex - > nStatus & BLOCK_FAILED_MASK & & ( ! pindexBestInvalid | | pindex - > nChainWork > pindexBestInvalid - > nChainWork ) )
pindexBestInvalid = pindex ;
2012-09-03 21:14:03 +02:00
}
// Load block file info
pblocktree - > ReadLastBlockFile ( nLastBlockFile ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " LoadBlockIndexDB(): last block file = %i \n " , nLastBlockFile ) ;
2012-09-03 21:14:03 +02:00
if ( pblocktree - > ReadBlockFileInfo ( nLastBlockFile , infoLastBlockFile ) )
2014-01-16 16:15:27 +01:00
LogPrintf ( " LoadBlockIndexDB(): last block file info: %s \n " , infoLastBlockFile . ToString ( ) ) ;
2012-10-05 19:22:21 +02:00
2012-12-02 21:59:22 +01:00
// Check whether we need to continue reindexing
bool fReindexing = false ;
pblocktree - > ReadReindexing ( fReindexing ) ;
fReindex | = fReindexing ;
2013-01-11 01:47:57 +01:00
// Check whether we have a transaction index
pblocktree - > ReadFlag ( " txindex " , fTxIndex ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " LoadBlockIndexDB(): transaction index %s \n " , fTxIndex ? " enabled " : " disabled " ) ;
2013-01-11 01:47:57 +01:00
2013-10-13 22:15:48 +02:00
// Load pointer to end of best chain
2013-11-05 02:27:39 +01:00
std : : map < uint256 , CBlockIndex * > : : iterator it = mapBlockIndex . find ( pcoinsTip - > GetBestBlock ( ) ) ;
if ( it = = mapBlockIndex . end ( ) )
2012-12-02 21:59:22 +01:00
return true ;
2013-11-05 02:27:39 +01:00
chainActive . SetTip ( it - > second ) ;
2014-02-18 20:04:06 +01:00
LogPrintf ( " LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f \n " ,
2014-01-16 16:15:27 +01:00
chainActive . Tip ( ) - > GetBlockHash ( ) . ToString ( ) , chainActive . Height ( ) ,
2014-02-18 20:04:06 +01:00
DateTimeStrFormat ( " %Y-%m-%d %H:%M:%S " , chainActive . Tip ( ) - > GetBlockTime ( ) ) ,
Checkpoints : : GuessVerificationProgress ( chainActive . Tip ( ) ) ) ;
2012-09-03 21:14:03 +02:00
2013-01-03 15:29:07 +01:00
return true ;
}
2013-06-19 17:32:49 +02:00
bool VerifyDB ( int nCheckLevel , int nCheckDepth )
{
2013-10-10 23:07:44 +02:00
if ( chainActive . Tip ( ) = = NULL | | chainActive . Tip ( ) - > pprev = = NULL )
2013-01-03 15:29:07 +01:00
return true ;
2012-09-03 21:14:03 +02:00
// Verify blocks in the best chain
2013-06-19 17:53:02 +02:00
if ( nCheckDepth < = 0 )
2012-09-03 21:14:03 +02:00
nCheckDepth = 1000000000 ; // suffices until the year 19000
2013-10-10 23:07:44 +02:00
if ( nCheckDepth > chainActive . Height ( ) )
nCheckDepth = chainActive . Height ( ) ;
2013-01-03 15:29:07 +01:00
nCheckLevel = std : : max ( 0 , std : : min ( 4 , nCheckLevel ) ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " Verifying last %i blocks at level %i \n " , nCheckDepth , nCheckLevel ) ;
2013-01-03 15:29:07 +01:00
CCoinsViewCache coins ( * pcoinsTip , true ) ;
2013-10-10 23:07:44 +02:00
CBlockIndex * pindexState = chainActive . Tip ( ) ;
2013-01-03 15:29:07 +01:00
CBlockIndex * pindexFailure = NULL ;
int nGoodTransactions = 0 ;
2013-01-27 00:14:11 +01:00
CValidationState state ;
2013-10-10 23:07:44 +02:00
for ( CBlockIndex * pindex = chainActive . Tip ( ) ; pindex & & pindex - > pprev ; pindex = pindex - > pprev )
2012-09-03 21:14:03 +02:00
{
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2013-10-10 23:07:44 +02:00
if ( pindex - > nHeight < chainActive . Height ( ) - nCheckDepth )
2012-09-03 21:14:03 +02:00
break ;
CBlock block ;
2013-01-03 15:29:07 +01:00
// check level 0: read from disk
2013-06-24 03:10:02 +02:00
if ( ! ReadBlockFromDisk ( block , pindex ) )
2014-01-16 16:15:27 +01:00
return error ( " VerifyDB() : * * * ReadBlockFromDisk failed at % d , hash = % s " , pindex->nHeight, pindex->GetBlockHash().ToString()) ;
2012-09-03 21:14:03 +02:00
// check level 1: verify block validity
2013-06-24 04:14:11 +02:00
if ( nCheckLevel > = 1 & & ! CheckBlock ( block , state ) )
2014-01-16 16:15:27 +01:00
return error ( " VerifyDB() : * * * found bad block at % d , hash = % s \ n " , pindex->nHeight, pindex->GetBlockHash().ToString()) ;
2013-01-03 15:29:07 +01:00
// check level 2: verify undo validity
if ( nCheckLevel > = 2 & & pindex ) {
CBlockUndo undo ;
CDiskBlockPos pos = pindex - > GetUndoPos ( ) ;
if ( ! pos . IsNull ( ) ) {
if ( ! undo . ReadFromDisk ( pos , pindex - > pprev - > GetBlockHash ( ) ) )
2014-01-16 16:15:27 +01:00
return error ( " VerifyDB() : * * * found bad undo data at % d , hash = % s \ n " , pindex->nHeight, pindex->GetBlockHash().ToString()) ;
2013-01-03 15:29:07 +01:00
}
}
// check level 3: check for inconsistencies during memory-only disconnect of tip blocks
if ( nCheckLevel > = 3 & & pindex = = pindexState & & ( coins . GetCacheSize ( ) + pcoinsTip - > GetCacheSize ( ) ) < = 2 * nCoinCacheSize + 32000 ) {
bool fClean = true ;
2013-06-24 03:32:58 +02:00
if ( ! DisconnectBlock ( block , state , pindex , coins , & fClean ) )
2014-01-16 16:15:27 +01:00
return error ( " VerifyDB() : * * * irrecoverable inconsistency in block data at % d , hash = % s " , pindex->nHeight, pindex->GetBlockHash().ToString()) ;
2013-01-03 15:29:07 +01:00
pindexState = pindex - > pprev ;
if ( ! fClean ) {
nGoodTransactions = 0 ;
pindexFailure = pindex ;
} else
nGoodTransactions + = block . vtx . size ( ) ;
2012-09-03 21:14:03 +02:00
}
}
2013-01-03 15:29:07 +01:00
if ( pindexFailure )
2013-10-10 23:07:44 +02:00
return error ( " VerifyDB() : * * * coin database inconsistencies found ( last % i blocks , % i good transactions before that ) \ n " , chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions) ;
2013-01-03 15:29:07 +01:00
// check level 4: try reconnecting blocks
if ( nCheckLevel > = 4 ) {
CBlockIndex * pindex = pindexState ;
2013-10-10 23:07:44 +02:00
while ( pindex ! = chainActive . Tip ( ) ) {
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2013-10-10 23:07:44 +02:00
pindex = chainActive . Next ( pindex ) ;
2013-04-04 11:30:55 +02:00
CBlock block ;
2013-06-24 03:10:02 +02:00
if ( ! ReadBlockFromDisk ( block , pindex ) )
2014-01-16 16:15:27 +01:00
return error ( " VerifyDB() : * * * ReadBlockFromDisk failed at % d , hash = % s " , pindex->nHeight, pindex->GetBlockHash().ToString()) ;
2013-06-24 03:50:06 +02:00
if ( ! ConnectBlock ( block , state , pindex , coins ) )
2014-01-16 16:15:27 +01:00
return error ( " VerifyDB() : * * * found unconnectable block at % d , hash = % s " , pindex->nHeight, pindex->GetBlockHash().ToString()) ;
2013-01-03 15:29:07 +01:00
}
2012-09-03 21:14:03 +02:00
}
2013-10-10 23:07:44 +02:00
LogPrintf ( " No coin database inconsistencies in last %i blocks (%i transactions) \n " , chainActive . Height ( ) - pindexState - > nHeight , nGoodTransactions ) ;
2013-01-03 15:29:07 +01:00
2012-09-03 21:14:03 +02:00
return true ;
}
2013-02-16 17:58:45 +01:00
void UnloadBlockIndex ( )
{
mapBlockIndex . clear ( ) ;
setBlockIndexValid . clear ( ) ;
2013-10-10 23:07:44 +02:00
chainActive . SetTip ( NULL ) ;
2013-10-13 22:15:48 +02:00
pindexBestInvalid = NULL ;
2013-02-16 17:58:45 +01:00
}
2012-10-21 21:23:13 +02:00
bool LoadBlockIndex ( )
2010-08-29 18:58:15 +02:00
{
2012-09-03 15:26:57 +02:00
// Load block index from databases
2013-01-11 01:47:57 +01:00
if ( ! fReindex & & ! LoadBlockIndexDB ( ) )
2010-08-29 18:58:15 +02:00
return false ;
2013-01-30 21:43:36 +01:00
return true ;
}
2013-01-11 01:47:57 +01:00
2013-01-30 21:43:36 +01:00
bool InitBlockIndex ( ) {
// Check whether we're already initialized
2013-10-10 23:07:44 +02:00
if ( chainActive . Genesis ( ) ! = NULL )
2013-01-30 21:43:36 +01:00
return true ;
// Use the provided setting for -txindex in the new database
fTxIndex = GetBoolArg ( " -txindex " , false ) ;
pblocktree - > WriteFlag ( " txindex " , fTxIndex ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " Initializing databases... \n " ) ;
2013-01-30 21:43:36 +01:00
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
if ( ! fReindex ) {
try {
2013-05-07 15:16:25 +02:00
CBlock & block = const_cast < CBlock & > ( Params ( ) . GenesisBlock ( ) ) ;
// Start new block file
2013-01-30 21:43:36 +01:00
unsigned int nBlockSize = : : GetSerializeSize ( block , SER_DISK , CLIENT_VERSION ) ;
CDiskBlockPos blockPos ;
CValidationState state ;
if ( ! FindBlockPos ( state , blockPos , nBlockSize + 8 , 0 , block . nTime ) )
2013-01-18 15:07:05 +01:00
return error ( " LoadBlockIndex() : FindBlockPos failed " ) ;
2013-06-24 02:35:01 +02:00
if ( ! WriteBlockToDisk ( block , blockPos ) )
2013-01-30 21:43:36 +01:00
return error ( " LoadBlockIndex() : writing genesis block to disk failed " ) ;
2013-06-24 04:00:18 +02:00
if ( ! AddToBlockIndex ( block , state , blockPos ) )
2013-01-30 21:43:36 +01:00
return error ( " LoadBlockIndex() : genesis block not accepted " ) ;
} catch ( std : : runtime_error & e ) {
return error ( " LoadBlockIndex() : failed to initialize block database : % s " , e.what()) ;
}
2010-08-29 18:58:15 +02:00
}
return true ;
}
void PrintBlockTree ( )
{
2012-07-26 02:48:39 +02:00
// pre-compute tree structure
2010-08-29 18:58:15 +02:00
map < CBlockIndex * , vector < CBlockIndex * > > mapNext ;
for ( map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . begin ( ) ; mi ! = mapBlockIndex . end ( ) ; + + mi )
{
CBlockIndex * pindex = ( * mi ) . second ;
mapNext [ pindex - > pprev ] . push_back ( pindex ) ;
// test
//while (rand() % 3 == 0)
// mapNext[pindex->pprev].push_back(pindex);
}
vector < pair < int , CBlockIndex * > > vStack ;
2013-10-10 23:07:44 +02:00
vStack . push_back ( make_pair ( 0 , chainActive . Genesis ( ) ) ) ;
2010-08-29 18:58:15 +02:00
int nPrevCol = 0 ;
while ( ! vStack . empty ( ) )
{
int nCol = vStack . back ( ) . first ;
CBlockIndex * pindex = vStack . back ( ) . second ;
vStack . pop_back ( ) ;
// print split or gap
if ( nCol > nPrevCol )
{
for ( int i = 0 ; i < nCol - 1 ; i + + )
2013-09-18 12:38:08 +02:00
LogPrintf ( " | " ) ;
LogPrintf ( " | \\ \n " ) ;
2010-08-29 18:58:15 +02:00
}
else if ( nCol < nPrevCol )
{
for ( int i = 0 ; i < nCol ; i + + )
2013-09-18 12:38:08 +02:00
LogPrintf ( " | " ) ;
LogPrintf ( " | \n " ) ;
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
}
2010-08-29 18:58:15 +02:00
nPrevCol = nCol ;
// print columns
for ( int i = 0 ; i < nCol ; i + + )
2013-09-18 12:38:08 +02:00
LogPrintf ( " | " ) ;
2010-08-29 18:58:15 +02:00
// print item
CBlock block ;
2013-06-24 03:10:02 +02:00
ReadBlockFromDisk ( block , pindex ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " %d (blk%05u.dat:0x%x) %s tx % " PRIszu " " ,
2010-08-29 18:58:15 +02:00
pindex - > nHeight ,
2012-08-13 19:11:05 +02:00
pindex - > GetBlockPos ( ) . nFile , pindex - > GetBlockPos ( ) . nPos ,
2014-01-16 16:15:27 +01:00
DateTimeStrFormat ( " %Y-%m-%d %H:%M:%S " , block . GetBlockTime ( ) ) ,
2010-08-29 18:58:15 +02:00
block . vtx . size ( ) ) ;
2012-07-26 02:48:39 +02:00
// put the main time-chain first
2010-08-29 18:58:15 +02:00
vector < CBlockIndex * > & vNext = mapNext [ pindex ] ;
2012-04-15 22:52:09 +02:00
for ( unsigned int i = 0 ; i < vNext . size ( ) ; i + + )
2010-08-29 18:58:15 +02:00
{
2013-10-10 23:07:44 +02:00
if ( chainActive . Next ( vNext [ i ] ) )
2010-08-29 18:58:15 +02:00
{
swap ( vNext [ 0 ] , vNext [ i ] ) ;
break ;
}
}
// iterate children
2012-04-15 22:52:09 +02:00
for ( unsigned int i = 0 ; i < vNext . size ( ) ; i + + )
2010-08-29 18:58:15 +02:00
vStack . push_back ( make_pair ( nCol + i , vNext [ i ] ) ) ;
}
}
2012-10-21 21:23:13 +02:00
bool LoadExternalBlockFile ( FILE * fileIn , CDiskBlockPos * dbp )
2012-02-20 20:50:26 +01:00
{
2013-04-13 07:13:08 +02:00
int64_t nStart = GetTimeMillis ( ) ;
2012-08-17 00:14:40 +02:00
2012-02-20 20:50:26 +01:00
int nLoaded = 0 ;
2013-01-29 01:44:19 +01:00
try {
2012-10-27 22:01:38 +02:00
CBufferedFile blkdat ( fileIn , 2 * MAX_BLOCK_SIZE , MAX_BLOCK_SIZE + 8 , SER_DISK , CLIENT_VERSION ) ;
2013-04-13 07:13:08 +02:00
uint64_t nStartByte = 0 ;
2012-10-21 21:23:13 +02:00
if ( dbp ) {
// (try to) skip already indexed part
CBlockFileInfo info ;
if ( pblocktree - > ReadBlockFileInfo ( dbp - > nFile , info ) ) {
nStartByte = info . nSize ;
blkdat . Seek ( info . nSize ) ;
}
}
2013-04-13 07:13:08 +02:00
uint64_t nRewind = blkdat . GetPos ( ) ;
2013-03-07 04:31:26 +01:00
while ( blkdat . good ( ) & & ! blkdat . eof ( ) ) {
boost : : this_thread : : interruption_point ( ) ;
2012-10-27 22:01:38 +02:00
blkdat . SetPos ( nRewind ) ;
nRewind + + ; // start one byte further next time, in case of failure
blkdat . SetLimit ( ) ; // remove former limit
2012-10-21 21:23:13 +02:00
unsigned int nSize = 0 ;
2012-10-27 22:01:38 +02:00
try {
// locate a header
unsigned char buf [ 4 ] ;
2013-05-07 15:16:25 +02:00
blkdat . FindByte ( Params ( ) . MessageStart ( ) [ 0 ] ) ;
2012-10-27 22:01:38 +02:00
nRewind = blkdat . GetPos ( ) + 1 ;
blkdat > > FLATDATA ( buf ) ;
2013-05-07 15:16:25 +02:00
if ( memcmp ( buf , Params ( ) . MessageStart ( ) , 4 ) )
2012-10-27 22:01:38 +02:00
continue ;
// read size
2012-02-20 20:50:26 +01:00
blkdat > > nSize ;
2012-10-27 22:01:38 +02:00
if ( nSize < 80 | | nSize > MAX_BLOCK_SIZE )
continue ;
2012-10-21 21:23:13 +02:00
} catch ( std : : exception & e ) {
// no valid block header found; don't complain
break ;
}
try {
2012-10-27 22:01:38 +02:00
// read block
2013-04-13 07:13:08 +02:00
uint64_t nBlockPos = blkdat . GetPos ( ) ;
2012-10-21 21:23:13 +02:00
blkdat . SetLimit ( nBlockPos + nSize ) ;
2012-10-27 22:01:38 +02:00
CBlock block ;
blkdat > > block ;
nRewind = blkdat . GetPos ( ) ;
2012-10-21 21:23:13 +02:00
// process block
if ( nBlockPos > = nStartByte ) {
2012-09-13 14:33:52 +02:00
LOCK ( cs_main ) ;
2012-10-21 21:23:13 +02:00
if ( dbp )
dbp - > nPos = nBlockPos ;
2013-01-27 00:14:11 +01:00
CValidationState state ;
if ( ProcessBlock ( state , NULL , & block , dbp ) )
2012-02-20 20:50:26 +01:00
nLoaded + + ;
2013-01-27 00:14:11 +01:00
if ( state . IsError ( ) )
break ;
2012-02-20 20:50:26 +01:00
}
2012-10-27 22:01:38 +02:00
} catch ( std : : exception & e ) {
2014-01-06 15:09:26 +01:00
LogPrintf ( " %s : Deserialize or I/O error - %s " , __PRETTY_FUNCTION__ , e . what ( ) ) ;
2012-02-20 20:50:26 +01:00
}
}
2012-10-27 22:01:38 +02:00
fclose ( fileIn ) ;
2013-01-29 01:44:19 +01:00
} catch ( std : : runtime_error & e ) {
AbortNode ( _ ( " Error: system error: " ) + e . what ( ) ) ;
2012-02-20 20:50:26 +01:00
}
2012-10-21 21:23:13 +02:00
if ( nLoaded > 0 )
2014-02-24 09:08:56 +01:00
LogPrintf ( " Loaded %i blocks from external file in %dms \n " , nLoaded , GetTimeMillis ( ) - nStart ) ;
2012-02-20 20:50:26 +01:00
return nLoaded > 0 ;
}
2010-08-29 18:58:15 +02:00
2012-09-13 14:33:52 +02:00
2010-08-29 18:58:15 +02:00
//////////////////////////////////////////////////////////////////////////////
//
// CAlert
//
string GetWarnings ( string strFor )
{
int nPriority = 0 ;
string strStatusBar ;
string strRPC ;
2012-10-24 21:47:07 +02:00
2013-04-28 17:37:50 +02:00
if ( GetBoolArg ( " -testsafemode " , false ) )
2010-08-29 18:58:15 +02:00
strRPC = " test " ;
2012-10-24 21:47:07 +02:00
if ( ! CLIENT_VERSION_IS_RELEASE )
strStatusBar = _ ( " This is a pre-release test build - use at your own risk - do not use for mining or merchant applications " ) ;
2010-08-29 18:58:15 +02:00
// Misc warnings like out of disk space and clock is wrong
if ( strMiscWarning ! = " " )
{
nPriority = 1000 ;
strStatusBar = strMiscWarning ;
}
2013-05-07 18:33:52 +02:00
if ( fLargeWorkForkFound )
2010-08-29 18:58:15 +02:00
{
nPriority = 2000 ;
2013-05-18 03:09:28 +02:00
strStatusBar = strRPC = _ ( " Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues. " ) ;
}
else if ( fLargeWorkInvalidChainFound )
2010-08-29 18:58:15 +02:00
{
nPriority = 2000 ;
2013-05-18 03:09:28 +02:00
strStatusBar = strRPC = _ ( " Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade. " ) ;
2010-08-29 18:58:15 +02:00
}
// Alerts
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_mapAlerts ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( PAIRTYPE ( const uint256 , CAlert ) & item , mapAlerts )
2010-08-29 18:58:15 +02:00
{
const CAlert & alert = item . second ;
if ( alert . AppliesToMe ( ) & & alert . nPriority > nPriority )
{
nPriority = alert . nPriority ;
strStatusBar = alert . strStatusBar ;
}
}
}
if ( strFor = = " statusbar " )
return strStatusBar ;
else if ( strFor = = " rpc " )
return strRPC ;
2011-06-24 19:56:23 +02:00
assert ( ! " GetWarnings() : invalid parameter " ) ;
2010-08-29 18:58:15 +02:00
return " error " ;
}
//////////////////////////////////////////////////////////////////////////////
//
// Messages
//
2012-07-06 16:33:34 +02:00
bool static AlreadyHave ( const CInv & inv )
2010-08-29 18:58:15 +02:00
{
switch ( inv . type )
{
2012-04-14 00:24:55 +02:00
case MSG_TX :
{
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
bool txInMap = false ;
2013-08-27 07:51:57 +02:00
txInMap = mempool . exists ( inv . hash ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
return txInMap | | mapOrphanTransactions . count ( inv . hash ) | |
2012-07-06 16:33:34 +02:00
pcoinsTip - > HaveCoins ( inv . hash ) ;
2012-04-14 00:24:55 +02:00
}
case MSG_BLOCK :
return mapBlockIndex . count ( inv . hash ) | |
mapOrphanBlocks . count ( inv . hash ) ;
2010-08-29 18:58:15 +02:00
}
// Don't know what it is, just say we already got one
return true ;
}
2013-03-29 23:49:38 +01:00
void static ProcessGetData ( CNode * pfrom )
{
std : : deque < CInv > : : iterator it = pfrom - > vRecvGetData . begin ( ) ;
vector < CInv > vNotFound ;
2013-07-25 02:34:42 +02:00
LOCK ( cs_main ) ;
2013-03-29 23:49:38 +01:00
while ( it ! = pfrom - > vRecvGetData . end ( ) ) {
// Don't bother if send buffer is too full to respond anyway
if ( pfrom - > nSendSize > = SendBufferSize ( ) )
break ;
const CInv & inv = * it ;
{
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2013-03-29 23:49:38 +01:00
it + + ;
if ( inv . type = = MSG_BLOCK | | inv . type = = MSG_FILTERED_BLOCK )
{
2014-01-20 00:59:57 +01:00
bool send = false ;
2013-03-29 23:49:38 +01:00
map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . find ( inv . hash ) ;
if ( mi ! = mapBlockIndex . end ( ) )
{
2014-01-20 00:59:57 +01:00
// If the requested block is at a height below our last
// checkpoint, only serve it if it's in the checkpointed chain
int nHeight = mi - > second - > nHeight ;
CBlockIndex * pcheckpoint = Checkpoints : : GetLastCheckpoint ( mapBlockIndex ) ;
if ( pcheckpoint & & nHeight < pcheckpoint - > nHeight ) {
2014-02-10 16:31:06 +01:00
if ( ! chainActive . Contains ( mi - > second ) )
{
LogPrintf ( " ProcessGetData(): ignoring request for old block that isn't in the main chain \n " ) ;
} else {
send = true ;
}
2014-01-20 00:59:57 +01:00
} else {
2014-02-10 16:31:06 +01:00
send = true ;
2014-01-20 00:59:57 +01:00
}
}
if ( send )
{
// Send block from disk
2013-03-29 23:49:38 +01:00
CBlock block ;
2013-06-24 03:10:02 +02:00
ReadBlockFromDisk ( block , ( * mi ) . second ) ;
2013-03-29 23:49:38 +01:00
if ( inv . type = = MSG_BLOCK )
pfrom - > PushMessage ( " block " , block ) ;
else // MSG_FILTERED_BLOCK)
{
LOCK ( pfrom - > cs_filter ) ;
if ( pfrom - > pfilter )
{
CMerkleBlock merkleBlock ( block , * pfrom - > pfilter ) ;
pfrom - > PushMessage ( " merkleblock " , merkleBlock ) ;
// CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
// This avoids hurting performance by pointlessly requiring a round-trip
// Note that there is currently no way for a node to request any single transactions we didnt send here -
// they must either disconnect and retry or request the full block.
// Thus, the protocol spec specified allows for us to provide duplicate txn here,
// however we MUST always provide at least what the remote peer needs
typedef std : : pair < unsigned int , uint256 > PairType ;
BOOST_FOREACH ( PairType & pair , merkleBlock . vMatchedTxn )
if ( ! pfrom - > setInventoryKnown . count ( CInv ( MSG_TX , pair . second ) ) )
pfrom - > PushMessage ( " tx " , block . vtx [ pair . first ] ) ;
}
// else
// no response
}
// Trigger them to send a getblocks request for the next batch of inventory
if ( inv . hash = = pfrom - > hashContinue )
{
// Bypass PushInventory, this must send even if redundant,
// and we want it right after the last block so they don't
// wait for other stuff first.
vector < CInv > vInv ;
2013-10-10 23:07:44 +02:00
vInv . push_back ( CInv ( MSG_BLOCK , chainActive . Tip ( ) - > GetBlockHash ( ) ) ) ;
2013-03-29 23:49:38 +01:00
pfrom - > PushMessage ( " inv " , vInv ) ;
pfrom - > hashContinue = 0 ;
}
}
}
else if ( inv . IsKnownType ( ) )
{
// Send stream from relay memory
bool pushed = false ;
{
LOCK ( cs_mapRelay ) ;
map < CInv , CDataStream > : : iterator mi = mapRelay . find ( inv ) ;
if ( mi ! = mapRelay . end ( ) ) {
pfrom - > PushMessage ( inv . GetCommand ( ) , ( * mi ) . second ) ;
pushed = true ;
}
}
if ( ! pushed & & inv . type = = MSG_TX ) {
2013-08-27 07:51:57 +02:00
CTransaction tx ;
if ( mempool . lookup ( inv . hash , tx ) ) {
2013-03-29 23:49:38 +01:00
CDataStream ss ( SER_NETWORK , PROTOCOL_VERSION ) ;
ss . reserve ( 1000 ) ;
ss < < tx ;
pfrom - > PushMessage ( " tx " , ss ) ;
pushed = true ;
}
}
if ( ! pushed ) {
vNotFound . push_back ( inv ) ;
}
}
// Track requests for our stuff.
2013-10-19 18:34:06 +02:00
g_signals . Inventory ( inv . hash ) ;
2013-11-11 16:20:39 +01:00
2013-10-28 21:20:21 +01:00
if ( inv . type = = MSG_BLOCK | | inv . type = = MSG_FILTERED_BLOCK )
break ;
2013-03-29 23:49:38 +01:00
}
}
pfrom - > vRecvGetData . erase ( pfrom - > vRecvGetData . begin ( ) , it ) ;
if ( ! vNotFound . empty ( ) ) {
// Let the peer know that we didn't find what it asked for, so it doesn't
// have to wait around forever. Currently only SPV clients actually care
// about this message: it's needed when they are recursively walking the
// dependencies of relevant unconfirmed transactions. SPV clients want to
// do that because they want to know about (and store and rebroadcast and
// risk analyze) the dependencies of transactions relevant to them, without
// having to download the entire memory pool.
pfrom - > PushMessage ( " notfound " , vNotFound ) ;
}
}
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
bool static ProcessMessage ( CNode * pfrom , string strCommand , CDataStream & vRecv )
2010-08-29 18:58:15 +02:00
{
RandAddSeedPerfmon ( ) ;
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " received: %s (% " PRIszu " bytes) \n " , strCommand , vRecv . size ( ) ) ;
2010-08-29 18:58:15 +02:00
if ( mapArgs . count ( " -dropmessagestest " ) & & GetRand ( atoi ( mapArgs [ " -dropmessagestest " ] ) ) = = 0 )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " dropmessagestest DROPPING RECV MESSAGE \n " ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2014-01-10 13:23:26 +01:00
State ( pfrom - > GetId ( ) ) - > nLastBlockProcess = GetTimeMicros ( ) ;
2010-08-29 18:58:15 +02:00
if ( strCommand = = " version " )
{
// Each connection can only send one version message
if ( pfrom - > nVersion ! = 0 )
2011-09-06 23:41:51 +02:00
{
2013-10-28 07:36:11 +01:00
pfrom - > PushMessage ( " reject " , strCommand , REJECT_DUPLICATE , string ( " Duplicate version message " ) ) ;
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 1 ) ;
2010-08-29 18:58:15 +02:00
return false ;
2011-09-06 23:41:51 +02:00
}
2010-08-29 18:58:15 +02:00
2013-04-13 07:13:08 +02:00
int64_t nTime ;
2010-08-29 18:58:15 +02:00
CAddress addrMe ;
CAddress addrFrom ;
2013-04-13 07:13:08 +02:00
uint64_t nNonce = 1 ;
2010-08-29 18:58:15 +02:00
vRecv > > pfrom - > nVersion > > pfrom - > nServices > > nTime > > addrMe ;
2013-10-26 11:21:21 +02:00
if ( pfrom - > nVersion < MIN_PEER_PROTO_VERSION )
2012-02-20 01:33:31 +01:00
{
2013-10-26 11:21:21 +02:00
// disconnect from peers older than this proto version
2014-01-16 16:15:27 +01:00
LogPrintf ( " partner %s using obsolete version %i; disconnecting \n " , pfrom - > addr . ToString ( ) , pfrom - > nVersion ) ;
2013-10-28 07:36:11 +01:00
pfrom - > PushMessage ( " reject " , strCommand , REJECT_OBSOLETE ,
strprintf ( " Version must be %d or greater " , MIN_PEER_PROTO_VERSION ) ) ;
2012-02-20 01:33:31 +01:00
pfrom - > fDisconnect = true ;
return false ;
}
2010-08-29 18:58:15 +02:00
if ( pfrom - > nVersion = = 10300 )
pfrom - > nVersion = 300 ;
2012-02-20 01:33:31 +01:00
if ( ! vRecv . empty ( ) )
2010-08-29 18:58:15 +02:00
vRecv > > addrFrom > > nNonce ;
2013-11-26 12:52:21 +01:00
if ( ! vRecv . empty ( ) ) {
2010-08-29 18:58:15 +02:00
vRecv > > pfrom - > strSubVer ;
2013-11-26 12:52:21 +01:00
pfrom - > cleanSubVer = SanitizeString ( pfrom - > strSubVer ) ;
}
2012-02-20 01:33:31 +01:00
if ( ! vRecv . empty ( ) )
2010-08-29 18:58:15 +02:00
vRecv > > pfrom - > nStartingHeight ;
2012-08-21 03:10:25 +02:00
if ( ! vRecv . empty ( ) )
vRecv > > pfrom - > fRelayTxes ; // set to true after we get the first filter* message
else
pfrom - > fRelayTxes = true ;
2010-08-29 18:58:15 +02:00
2012-02-12 13:45:24 +01:00
if ( pfrom - > fInbound & & addrMe . IsRoutable ( ) )
{
pfrom - > addrLocal = addrMe ;
SeenLocal ( addrMe ) ;
}
2010-08-29 18:58:15 +02:00
// Disconnect if we connected to ourself
if ( nNonce = = nLocalHostNonce & & nNonce > 1 )
{
2014-01-16 16:15:27 +01:00
LogPrintf ( " connected to self at %s, disconnecting \n " , pfrom - > addr . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
pfrom - > fDisconnect = true ;
return true ;
}
2011-01-24 16:42:17 +01:00
// Be shy and don't send version until we hear
if ( pfrom - > fInbound )
pfrom - > PushVersion ( ) ;
2010-08-29 18:58:15 +02:00
pfrom - > fClient = ! ( pfrom - > nServices & NODE_NETWORK ) ;
// Change version
2012-02-20 01:33:31 +01:00
pfrom - > PushMessage ( " verack " ) ;
2013-03-24 16:52:24 +01:00
pfrom - > ssSend . SetVersion ( min ( pfrom - > nVersion , PROTOCOL_VERSION ) ) ;
2010-08-29 18:58:15 +02:00
2010-10-23 19:43:53 +02:00
if ( ! pfrom - > fInbound )
{
// Advertise our address
2012-05-24 19:02:21 +02:00
if ( ! fNoListen & & ! IsInitialBlockDownload ( ) )
2010-10-23 19:43:53 +02:00
{
2012-02-12 13:45:24 +01:00
CAddress addr = GetLocalAddress ( & pfrom - > addr ) ;
if ( addr . IsRoutable ( ) )
pfrom - > PushAddress ( addr ) ;
2010-10-23 19:43:53 +02:00
}
// Get recent addresses
2012-04-24 02:15:00 +02:00
if ( pfrom - > fOneShot | | pfrom - > nVersion > = CADDR_TIME_VERSION | | addrman . size ( ) < 1000 )
2010-10-23 19:43:53 +02:00
{
pfrom - > PushMessage ( " getaddr " ) ;
pfrom - > fGetAddr = true ;
}
2012-01-04 23:39:45 +01:00
addrman . Good ( pfrom - > addr ) ;
} else {
if ( ( ( CNetAddr ) pfrom - > addr ) = = ( CNetAddr ) addrFrom )
{
addrman . Add ( addrFrom , addrFrom ) ;
addrman . Good ( addrFrom ) ;
}
2010-10-23 19:43:53 +02:00
}
2010-08-29 18:58:15 +02:00
// Relay alerts
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_mapAlerts ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( PAIRTYPE ( const uint256 , CAlert ) & item , mapAlerts )
2010-08-29 18:58:15 +02:00
item . second . RelayTo ( pfrom ) ;
2012-04-06 18:39:12 +02:00
}
2010-08-29 18:58:15 +02:00
pfrom - > fSuccessfullyConnected = true ;
2014-01-16 16:15:27 +01:00
LogPrintf ( " receive version message: %s: version %d, blocks=%d, us=%s, them=%s, peer=%s \n " , pfrom - > cleanSubVer , pfrom - > nVersion , pfrom - > nStartingHeight , addrMe . ToString ( ) , addrFrom . ToString ( ) , pfrom - > addr . ToString ( ) ) ;
2011-09-28 21:35:58 +02:00
2013-07-25 02:34:42 +02:00
AddTimeData ( pfrom - > addr , nTime ) ;
2013-10-19 15:55:08 +02:00
LOCK ( cs_main ) ;
2011-09-28 21:35:58 +02:00
cPeerBlockCounts . input ( pfrom - > nStartingHeight ) ;
2010-08-29 18:58:15 +02:00
}
else if ( pfrom - > nVersion = = 0 )
{
// Must have a version message before anything else
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 1 ) ;
2010-08-29 18:58:15 +02:00
return false ;
}
else if ( strCommand = = " verack " )
{
2012-11-16 01:41:12 +01:00
pfrom - > SetRecvVersion ( min ( pfrom - > nVersion , PROTOCOL_VERSION ) ) ;
2010-08-29 18:58:15 +02:00
}
else if ( strCommand = = " addr " )
{
vector < CAddress > vAddr ;
vRecv > > vAddr ;
2010-10-23 19:43:53 +02:00
// Don't want addr from older versions unless seeding
2012-04-13 02:07:49 +02:00
if ( pfrom - > nVersion < CADDR_TIME_VERSION & & addrman . size ( ) > 1000 )
2010-08-29 18:58:15 +02:00
return true ;
if ( vAddr . size ( ) > 1000 )
2011-09-06 23:41:51 +02:00
{
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 20 ) ;
2012-09-29 11:57:44 +02:00
return error ( " message addr size() = % " PRIszu " " , vAddr.size()) ;
2011-09-06 23:41:51 +02:00
}
2010-08-29 18:58:15 +02:00
// Store the new addresses
2012-04-10 20:22:04 +02:00
vector < CAddress > vAddrOk ;
2013-04-13 07:13:08 +02:00
int64_t nNow = GetAdjustedTime ( ) ;
int64_t nSince = nNow - 10 * 60 ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CAddress & addr , vAddr )
2010-08-29 18:58:15 +02:00
{
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-10-23 19:43:53 +02:00
if ( addr . nTime < = 100000000 | | addr . nTime > nNow + 10 * 60 )
addr . nTime = nNow - 5 * 24 * 60 * 60 ;
2010-08-29 18:58:15 +02:00
pfrom - > AddAddressKnown ( addr ) ;
2012-04-10 20:22:04 +02:00
bool fReachable = IsReachable ( addr ) ;
2010-10-23 19:43:53 +02:00
if ( addr . nTime > nSince & & ! pfrom - > fGetAddr & & vAddr . size ( ) < = 10 & & addr . IsRoutable ( ) )
2010-08-29 18:58:15 +02:00
{
// Relay to a limited number of other nodes
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2010-10-19 19:16:51 +02:00
// Use deterministic randomness to send to the same nodes for 24 hours
// at a time so the setAddrKnowns of the chosen nodes prevent repeats
2010-08-29 18:58:15 +02:00
static uint256 hashSalt ;
if ( hashSalt = = 0 )
2012-05-17 18:13:14 +02:00
hashSalt = GetRandHash ( ) ;
2013-04-13 07:13:08 +02:00
uint64_t hashAddr = addr . GetHash ( ) ;
2012-01-03 23:33:31 +01:00
uint256 hashRand = hashSalt ^ ( hashAddr < < 32 ) ^ ( ( GetTime ( ) + hashAddr ) / ( 24 * 60 * 60 ) ) ;
2010-10-19 19:16:51 +02:00
hashRand = Hash ( BEGIN ( hashRand ) , END ( hashRand ) ) ;
2010-08-29 18:58:15 +02:00
multimap < uint256 , CNode * > mapMix ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodes )
2010-10-19 19:16:51 +02:00
{
2012-04-13 02:07:49 +02:00
if ( pnode - > nVersion < CADDR_TIME_VERSION )
2010-10-23 19:43:53 +02:00
continue ;
2010-10-19 19:16:51 +02:00
unsigned int nPointer ;
memcpy ( & nPointer , & pnode , sizeof ( nPointer ) ) ;
uint256 hashKey = hashRand ^ nPointer ;
hashKey = Hash ( BEGIN ( hashKey ) , END ( hashKey ) ) ;
mapMix . insert ( make_pair ( hashKey , pnode ) ) ;
}
2012-04-10 20:22:04 +02:00
int nRelayNodes = fReachable ? 2 : 1 ; // limited relaying of addresses outside our network(s)
2010-08-29 18:58:15 +02:00
for ( multimap < uint256 , CNode * > : : iterator mi = mapMix . begin ( ) ; mi ! = mapMix . end ( ) & & nRelayNodes - - > 0 ; + + mi )
( ( * mi ) . second ) - > PushAddress ( addr ) ;
}
}
2012-04-10 20:22:04 +02:00
// Do not store addresses outside our network
if ( fReachable )
vAddrOk . push_back ( addr ) ;
2010-08-29 18:58:15 +02:00
}
2012-04-10 20:22:04 +02:00
addrman . Add ( vAddrOk , pfrom - > addr , 2 * 60 * 60 ) ;
2010-08-29 18:58:15 +02:00
if ( vAddr . size ( ) < 1000 )
pfrom - > fGetAddr = false ;
2012-04-24 02:15:00 +02:00
if ( pfrom - > fOneShot )
pfrom - > fDisconnect = true ;
2010-08-29 18:58:15 +02:00
}
else if ( strCommand = = " inv " )
{
vector < CInv > vInv ;
vRecv > > vInv ;
2012-07-31 23:42:35 +02:00
if ( vInv . size ( ) > MAX_INV_SZ )
2011-09-06 23:41:51 +02:00
{
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 20 ) ;
2012-09-29 11:57:44 +02:00
return error ( " message inv size() = % " PRIszu " " , vInv.size()) ;
2011-09-06 23:41:51 +02:00
}
2010-08-29 18:58:15 +02:00
2013-07-25 02:34:42 +02:00
LOCK ( cs_main ) ;
2012-04-15 22:52:09 +02:00
for ( unsigned int nInv = 0 ; nInv < vInv . size ( ) ; nInv + + )
2010-08-29 18:58:15 +02:00
{
2012-03-18 23:47:26 +01:00
const CInv & inv = vInv [ nInv ] ;
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-08-29 18:58:15 +02:00
pfrom - > AddInventoryKnown ( inv ) ;
2012-07-06 16:33:34 +02:00
bool fAlreadyHave = AlreadyHave ( inv ) ;
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " got inventory: %s %s \n " , inv . ToString ( ) , fAlreadyHave ? " have " : " new " ) ;
2010-08-29 18:58:15 +02:00
2012-09-13 14:33:52 +02:00
if ( ! fAlreadyHave ) {
2014-01-10 13:23:26 +01:00
if ( ! fImporting & & ! fReindex ) {
if ( inv . type = = MSG_BLOCK )
AddBlockToQueue ( pfrom - > GetId ( ) , inv . hash ) ;
else
pfrom - > AskFor ( inv ) ;
}
2012-09-13 14:33:52 +02:00
} else if ( inv . type = = MSG_BLOCK & & mapOrphanBlocks . count ( inv . hash ) ) {
2014-01-11 23:33:34 +01:00
PushGetBlocks ( pfrom , chainActive . Tip ( ) , GetOrphanRoot ( inv . hash ) ) ;
2012-05-07 21:36:30 +02:00
}
2010-08-29 18:58:15 +02:00
// Track requests for our stuff
2013-10-19 18:34:06 +02:00
g_signals . Inventory ( inv . hash ) ;
2010-08-29 18:58:15 +02:00
}
}
else if ( strCommand = = " getdata " )
{
vector < CInv > vInv ;
vRecv > > vInv ;
2012-07-31 23:42:35 +02:00
if ( vInv . size ( ) > MAX_INV_SZ )
2011-09-06 23:41:51 +02:00
{
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 20 ) ;
2012-09-29 11:57:44 +02:00
return error ( " message getdata size() = % " PRIszu " " , vInv.size()) ;
2011-09-06 23:41:51 +02:00
}
2010-08-29 18:58:15 +02:00
2013-10-08 12:09:40 +02:00
if ( fDebug | | ( vInv . size ( ) ! = 1 ) )
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " received getdata (% " PRIszu " invsz) \n " , vInv . size ( ) ) ;
2012-06-24 03:38:33 +02:00
2013-10-08 12:09:40 +02:00
if ( ( fDebug & & vInv . size ( ) > 0 ) | | ( vInv . size ( ) = = 1 ) )
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " received getdata for: %s \n " , vInv [ 0 ] . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
2013-03-29 23:49:38 +01:00
pfrom - > vRecvGetData . insert ( pfrom - > vRecvGetData . end ( ) , vInv . begin ( ) , vInv . end ( ) ) ;
ProcessGetData ( pfrom ) ;
2010-08-29 18:58:15 +02:00
}
else if ( strCommand = = " getblocks " )
{
CBlockLocator locator ;
uint256 hashStop ;
vRecv > > locator > > hashStop ;
2013-07-25 02:34:42 +02:00
LOCK ( cs_main ) ;
2010-12-05 10:29:30 +01:00
// Find the last block the caller has in the main chain
2013-10-12 15:18:08 +02:00
CBlockIndex * pindex = chainActive . FindFork ( locator ) ;
2010-08-29 18:58:15 +02:00
// Send the rest of the chain
if ( pindex )
2013-10-10 23:07:44 +02:00
pindex = chainActive . Next ( pindex ) ;
2012-03-22 03:10:50 +01:00
int nLimit = 500 ;
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " getblocks %d to %s limit %d \n " , ( pindex ? pindex - > nHeight : - 1 ) , hashStop . ToString ( ) , nLimit ) ;
2013-10-10 23:07:44 +02:00
for ( ; pindex ; pindex = chainActive . Next ( pindex ) )
2010-08-29 18:58:15 +02:00
{
if ( pindex - > GetBlockHash ( ) = = hashStop )
{
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " getblocks stopping at %d %s \n " , pindex - > nHeight , pindex - > GetBlockHash ( ) . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
break ;
}
pfrom - > PushInventory ( CInv ( MSG_BLOCK , pindex - > GetBlockHash ( ) ) ) ;
2012-03-22 03:10:50 +01:00
if ( - - nLimit < = 0 )
2010-08-29 18:58:15 +02:00
{
// When this block is requested, we'll send an inv that'll make them
// getblocks the next batch of inventory.
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " getblocks stopping at limit %d %s \n " , pindex - > nHeight , pindex - > GetBlockHash ( ) . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
pfrom - > hashContinue = pindex - > GetBlockHash ( ) ;
break ;
}
}
}
2010-12-05 10:29:30 +01:00
else if ( strCommand = = " getheaders " )
{
CBlockLocator locator ;
uint256 hashStop ;
vRecv > > locator > > hashStop ;
2013-07-25 02:34:42 +02:00
LOCK ( cs_main ) ;
2010-12-05 10:29:30 +01:00
CBlockIndex * pindex = NULL ;
if ( locator . IsNull ( ) )
{
// If locator is null, return the hashStop block
map < uint256 , CBlockIndex * > : : iterator mi = mapBlockIndex . find ( hashStop ) ;
if ( mi = = mapBlockIndex . end ( ) )
return true ;
pindex = ( * mi ) . second ;
}
else
{
// Find the last block the caller has in the main chain
2013-10-12 15:18:08 +02:00
pindex = chainActive . FindFork ( locator ) ;
2010-12-05 10:29:30 +01:00
if ( pindex )
2013-10-10 23:07:44 +02:00
pindex = chainActive . Next ( pindex ) ;
2010-12-05 10:29:30 +01:00
}
2012-11-14 22:18:10 +01:00
// we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
2010-12-05 10:29:30 +01:00
vector < CBlock > vHeaders ;
2012-03-19 05:13:15 +01:00
int nLimit = 2000 ;
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " getheaders %d to %s \n " , ( pindex ? pindex - > nHeight : - 1 ) , hashStop . ToString ( ) ) ;
2013-10-10 23:07:44 +02:00
for ( ; pindex ; pindex = chainActive . Next ( pindex ) )
2010-12-05 10:29:30 +01:00
{
vHeaders . push_back ( pindex - > GetBlockHeader ( ) ) ;
if ( - - nLimit < = 0 | | pindex - > GetBlockHash ( ) = = hashStop )
break ;
}
pfrom - > PushMessage ( " headers " , vHeaders ) ;
}
2010-08-29 18:58:15 +02:00
else if ( strCommand = = " tx " )
{
vector < uint256 > vWorkQueue ;
2012-05-17 16:12:04 +02:00
vector < uint256 > vEraseQueue ;
2010-08-29 18:58:15 +02:00
CTransaction tx ;
vRecv > > tx ;
CInv inv ( MSG_TX , tx . GetHash ( ) ) ;
pfrom - > AddInventoryKnown ( inv ) ;
2013-07-25 02:34:42 +02:00
LOCK ( cs_main ) ;
2010-08-29 18:58:15 +02:00
bool fMissingInputs = false ;
2013-01-27 00:14:11 +01:00
CValidationState state ;
2013-08-27 07:51:57 +02:00
if ( AcceptToMemoryPool ( mempool , state , tx , true , & fMissingInputs ) )
2010-08-29 18:58:15 +02:00
{
2013-11-05 02:47:07 +01:00
mempool . check ( pcoinsTip ) ;
2013-08-02 07:14:44 +02:00
RelayTransaction ( tx , inv . hash ) ;
2010-08-29 18:58:15 +02:00
mapAlreadyAskedFor . erase ( inv ) ;
vWorkQueue . push_back ( inv . hash ) ;
2012-05-17 16:12:04 +02:00
vEraseQueue . push_back ( inv . hash ) ;
2010-08-29 18:58:15 +02:00
2013-11-21 14:38:29 +01:00
LogPrint ( " mempool " , " AcceptToMemoryPool: %s %s : accepted %s (poolsz % " PRIszu " ) \n " ,
2014-01-16 16:15:27 +01:00
pfrom - > addr . ToString ( ) , pfrom - > cleanSubVer ,
tx . GetHash ( ) . ToString ( ) ,
2013-11-21 14:38:29 +01:00
mempool . mapTx . size ( ) ) ;
2010-08-29 18:58:15 +02:00
// Recursively process any orphan transactions that depended on this one
2012-04-15 22:52:09 +02:00
for ( unsigned int i = 0 ; i < vWorkQueue . size ( ) ; i + + )
2010-08-29 18:58:15 +02:00
{
uint256 hashPrev = vWorkQueue [ i ] ;
2013-08-02 07:14:44 +02:00
for ( set < uint256 > : : iterator mi = mapOrphanTransactionsByPrev [ hashPrev ] . begin ( ) ;
2012-05-15 21:53:30 +02:00
mi ! = mapOrphanTransactionsByPrev [ hashPrev ] . end ( ) ;
2010-08-29 18:58:15 +02:00
+ + mi )
{
2013-08-02 07:14:44 +02:00
const uint256 & orphanHash = * mi ;
const CTransaction & orphanTx = mapOrphanTransactions [ orphanHash ] ;
2012-05-17 16:12:04 +02:00
bool fMissingInputs2 = false ;
2013-08-02 07:14:44 +02:00
// Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
// resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
// anyone relaying LegitTxX banned)
2013-01-31 04:53:21 +01:00
CValidationState stateDummy ;
2010-08-29 18:58:15 +02:00
2013-08-27 07:51:57 +02:00
if ( AcceptToMemoryPool ( mempool , stateDummy , orphanTx , true , & fMissingInputs2 ) )
2010-08-29 18:58:15 +02:00
{
2014-01-16 16:15:27 +01:00
LogPrint ( " mempool " , " accepted orphan tx %s \n " , orphanHash . ToString ( ) ) ;
2013-08-02 07:14:44 +02:00
RelayTransaction ( orphanTx , orphanHash ) ;
mapAlreadyAskedFor . erase ( CInv ( MSG_TX , orphanHash ) ) ;
vWorkQueue . push_back ( orphanHash ) ;
vEraseQueue . push_back ( orphanHash ) ;
2012-05-17 16:12:04 +02:00
}
else if ( ! fMissingInputs2 )
{
2013-01-14 22:52:33 +01:00
// invalid or too-little-fee orphan
2013-08-02 07:14:44 +02:00
vEraseQueue . push_back ( orphanHash ) ;
2014-01-16 16:15:27 +01:00
LogPrint ( " mempool " , " removed orphan tx %s \n " , orphanHash . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
}
2013-11-05 02:47:07 +01:00
mempool . check ( pcoinsTip ) ;
2010-08-29 18:58:15 +02:00
}
}
2012-05-17 16:12:04 +02:00
BOOST_FOREACH ( uint256 hash , vEraseQueue )
2010-08-29 18:58:15 +02:00
EraseOrphanTx ( hash ) ;
}
else if ( fMissingInputs )
{
2013-08-02 07:14:44 +02:00
AddOrphanTx ( tx ) ;
2012-02-29 16:14:18 +01:00
// DoS prevention: do not allow mapOrphanTransactions to grow unbounded
2012-04-23 20:14:03 +02:00
unsigned int nEvicted = LimitOrphanTxSize ( MAX_ORPHAN_TRANSACTIONS ) ;
2012-02-29 16:14:18 +01:00
if ( nEvicted > 0 )
2013-09-18 12:38:08 +02:00
LogPrint ( " mempool " , " mapOrphan overflow, removed %u tx \n " , nEvicted ) ;
2010-08-29 18:58:15 +02:00
}
2013-10-01 16:26:42 +02:00
int nDoS = 0 ;
2013-10-07 08:55:04 +02:00
if ( state . IsInvalid ( nDoS ) )
2014-02-10 16:31:06 +01:00
{
2014-01-16 16:15:27 +01:00
LogPrint ( " mempool " , " %s from %s %s was not accepted into the memory pool: %s \n " , tx . GetHash ( ) . ToString ( ) ,
pfrom - > addr . ToString ( ) , pfrom - > cleanSubVer ,
state . GetRejectReason ( ) ) ;
2013-10-28 07:36:11 +01:00
pfrom - > PushMessage ( " reject " , strCommand , state . GetRejectCode ( ) ,
state . GetRejectReason ( ) , inv . hash ) ;
2013-10-07 08:55:04 +02:00
if ( nDoS > 0 )
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , nDoS ) ;
2013-10-28 07:36:11 +01:00
}
2010-08-29 18:58:15 +02:00
}
2012-10-21 21:23:13 +02:00
else if ( strCommand = = " block " & & ! fImporting & & ! fReindex ) // Ignore blocks received while importing
2010-08-29 18:58:15 +02:00
{
2010-12-05 10:29:30 +01:00
CBlock block ;
vRecv > > block ;
2010-08-29 18:58:15 +02:00
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " received block %s \n " , block . GetHash ( ) . ToString ( ) ) ;
2010-12-05 10:29:30 +01:00
// block.print();
2010-08-29 18:58:15 +02:00
2010-12-05 10:29:30 +01:00
CInv inv ( MSG_BLOCK , block . GetHash ( ) ) ;
2010-08-29 18:58:15 +02:00
pfrom - > AddInventoryKnown ( inv ) ;
2013-07-25 02:34:42 +02:00
LOCK ( cs_main ) ;
2013-11-16 19:28:24 +01:00
// Remember who we got this block from.
mapBlockSource [ inv . hash ] = pfrom - > GetId ( ) ;
2014-01-10 13:23:26 +01:00
MarkBlockAsReceived ( inv . hash , pfrom - > GetId ( ) ) ;
2013-07-25 02:34:42 +02:00
2013-01-27 00:14:11 +01:00
CValidationState state ;
2013-11-16 19:28:24 +01:00
ProcessBlock ( state , pfrom , & block ) ;
2010-08-29 18:58:15 +02:00
}
else if ( strCommand = = " getaddr " )
{
pfrom - > vAddrToSend . clear ( ) ;
2012-01-04 23:39:45 +01:00
vector < CAddress > vAddr = addrman . GetAddr ( ) ;
BOOST_FOREACH ( const CAddress & addr , vAddr )
pfrom - > PushAddress ( addr ) ;
2010-08-29 18:58:15 +02:00
}
2012-07-31 23:42:35 +02:00
else if ( strCommand = = " mempool " )
{
2013-08-27 07:51:57 +02:00
LOCK2 ( cs_main , pfrom - > cs_filter ) ;
2013-07-25 02:34:42 +02:00
2012-07-31 23:42:35 +02:00
std : : vector < uint256 > vtxid ;
mempool . queryHashes ( vtxid ) ;
vector < CInv > vInv ;
2013-01-10 20:06:30 +01:00
BOOST_FOREACH ( uint256 & hash , vtxid ) {
CInv inv ( MSG_TX , hash ) ;
2013-08-27 07:51:57 +02:00
CTransaction tx ;
bool fInMemPool = mempool . lookup ( hash , tx ) ;
if ( ! fInMemPool ) continue ; // another thread removed since queryHashes, maybe...
if ( ( pfrom - > pfilter & & pfrom - > pfilter - > IsRelevantAndUpdate ( tx , hash ) ) | |
2013-01-10 20:06:30 +01:00
( ! pfrom - > pfilter ) )
vInv . push_back ( inv ) ;
2013-08-27 08:12:43 +02:00
if ( vInv . size ( ) = = MAX_INV_SZ ) {
pfrom - > PushMessage ( " inv " , vInv ) ;
vInv . clear ( ) ;
}
2012-07-31 23:42:35 +02:00
}
if ( vInv . size ( ) > 0 )
pfrom - > PushMessage ( " inv " , vInv ) ;
}
2010-08-29 18:58:15 +02:00
else if ( strCommand = = " ping " )
{
2012-04-11 18:38:03 +02:00
if ( pfrom - > nVersion > BIP0031_VERSION )
{
2013-04-13 07:13:08 +02:00
uint64_t nonce = 0 ;
2012-04-11 18:38:03 +02:00
vRecv > > nonce ;
// Echo the message back with the nonce. This allows for two useful features:
//
// 1) A remote node can quickly check if the connection is operational
// 2) Remote nodes can measure the latency of the network thread. If this node
// is overloaded it won't respond to pings quickly and the remote node can
// avoid sending us more work, like chain download requests.
//
// The nonce stops the remote getting confused between different pings: without
// it, if the remote node sends a ping once per second and this node takes 5
// seconds to respond to each, the 5th ping the remote sends would appear to
// return very quickly.
pfrom - > PushMessage ( " pong " , nonce ) ;
}
2010-08-29 18:58:15 +02:00
}
2013-08-22 13:34:33 +02:00
else if ( strCommand = = " pong " )
{
2013-04-13 07:13:08 +02:00
int64_t pingUsecEnd = GetTimeMicros ( ) ;
uint64_t nonce = 0 ;
2013-08-22 13:34:33 +02:00
size_t nAvail = vRecv . in_avail ( ) ;
bool bPingFinished = false ;
std : : string sProblem ;
2013-11-11 16:20:39 +01:00
2013-08-22 13:34:33 +02:00
if ( nAvail > = sizeof ( nonce ) ) {
vRecv > > nonce ;
2013-11-11 16:20:39 +01:00
2013-08-22 13:34:33 +02:00
// Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
if ( pfrom - > nPingNonceSent ! = 0 ) {
if ( nonce = = pfrom - > nPingNonceSent ) {
// Matching pong received, this ping is no longer outstanding
bPingFinished = true ;
2013-04-13 07:13:08 +02:00
int64_t pingUsecTime = pingUsecEnd - pfrom - > nPingUsecStart ;
2013-08-22 13:34:33 +02:00
if ( pingUsecTime > 0 ) {
// Successful ping time measurement, replace previous
pfrom - > nPingUsecTime = pingUsecTime ;
} else {
// This should never happen
sProblem = " Timing mishap " ;
}
} else {
// Nonce mismatches are normal when pings are overlapping
sProblem = " Nonce mismatch " ;
if ( nonce = = 0 ) {
// This is most likely a bug in another implementation somewhere, cancel this ping
bPingFinished = true ;
sProblem = " Nonce zero " ;
}
}
} else {
sProblem = " Unsolicited pong without ping " ;
}
} else {
// This is most likely a bug in another implementation somewhere, cancel this ping
bPingFinished = true ;
sProblem = " Short payload " ;
}
2013-11-11 16:20:39 +01:00
2013-08-22 13:34:33 +02:00
if ( ! ( sProblem . empty ( ) ) ) {
2014-02-24 09:08:56 +01:00
LogPrint ( " net " , " pong %s %s: %s, %x expected, %x received, % " PRIszu " bytes \n " ,
2014-01-16 16:15:27 +01:00
pfrom - > addr . ToString ( ) ,
pfrom - > cleanSubVer ,
sProblem ,
2013-10-15 14:50:58 +02:00
pfrom - > nPingNonceSent ,
nonce ,
nAvail ) ;
2013-08-22 13:34:33 +02:00
}
if ( bPingFinished ) {
pfrom - > nPingNonceSent = 0 ;
}
}
2013-11-11 16:20:39 +01:00
2010-08-29 18:58:15 +02:00
else if ( strCommand = = " alert " )
{
CAlert alert ;
vRecv > > alert ;
2012-08-26 23:08:18 +02:00
uint256 alertHash = alert . GetHash ( ) ;
if ( pfrom - > setKnown . count ( alertHash ) = = 0 )
2010-08-29 18:58:15 +02:00
{
2012-08-26 23:08:18 +02:00
if ( alert . ProcessAlert ( ) )
2012-04-06 18:39:12 +02:00
{
2012-08-26 23:08:18 +02:00
// Relay
pfrom - > setKnown . insert ( alertHash ) ;
{
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
alert . RelayTo ( pnode ) ;
}
}
else {
// Small DoS penalty so peers that send us lots of
// duplicate/expired/invalid-signature/whatever alerts
// eventually get banned.
// This isn't a Misbehaving(100) (immediate ban) because the
// peer might be an older or different implementation with
// a different signature key, etc.
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 10 ) ;
2012-04-06 18:39:12 +02:00
}
2010-08-29 18:58:15 +02:00
}
}
2012-08-13 05:26:29 +02:00
else if ( strCommand = = " filterload " )
{
CBloomFilter filter ;
vRecv > > filter ;
if ( ! filter . IsWithinSizeConstraints ( ) )
// There is no excuse for sending a too-large filter
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 100 ) ;
2012-08-13 05:26:29 +02:00
else
{
LOCK ( pfrom - > cs_filter ) ;
delete pfrom - > pfilter ;
pfrom - > pfilter = new CBloomFilter ( filter ) ;
2013-08-21 02:41:42 +02:00
pfrom - > pfilter - > UpdateEmptyFull ( ) ;
2012-08-13 05:26:29 +02:00
}
2012-08-21 03:10:25 +02:00
pfrom - > fRelayTxes = true ;
2012-08-13 05:26:29 +02:00
}
else if ( strCommand = = " filteradd " )
{
vector < unsigned char > vData ;
vRecv > > vData ;
// Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
// and thus, the maximum size any matched object can have) in a filteradd message
2013-01-18 19:55:18 +01:00
if ( vData . size ( ) > MAX_SCRIPT_ELEMENT_SIZE )
2012-08-13 05:26:29 +02:00
{
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 100 ) ;
2012-08-13 05:26:29 +02:00
} else {
LOCK ( pfrom - > cs_filter ) ;
if ( pfrom - > pfilter )
pfrom - > pfilter - > insert ( vData ) ;
else
2013-11-18 01:25:17 +01:00
Misbehaving ( pfrom - > GetId ( ) , 100 ) ;
2012-08-13 05:26:29 +02:00
}
}
else if ( strCommand = = " filterclear " )
{
LOCK ( pfrom - > cs_filter ) ;
delete pfrom - > pfilter ;
2013-08-19 05:21:06 +02:00
pfrom - > pfilter = new CBloomFilter ( ) ;
2012-08-21 03:10:25 +02:00
pfrom - > fRelayTxes = true ;
2012-08-13 05:26:29 +02:00
}
2013-10-28 07:36:11 +01:00
else if ( strCommand = = " reject " )
{
if ( fDebug )
{
string strMsg ; unsigned char ccode ; string strReason ;
vRecv > > strMsg > > ccode > > strReason ;
ostringstream ss ;
ss < < strMsg < < " code " < < itostr ( ccode ) < < " : " < < strReason ;
if ( strMsg = = " block " | | strMsg = = " tx " )
{
uint256 hash ;
vRecv > > hash ;
ss < < " : hash " < < hash . ToString ( ) ;
}
// Truncate to reasonable length and sanitize before printing:
string s = ss . str ( ) ;
if ( s . size ( ) > 111 ) s . erase ( 111 , string : : npos ) ;
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " Reject %s \n " , SanitizeString ( s ) ) ;
2013-10-28 07:36:11 +01:00
}
}
2010-08-29 18:58:15 +02:00
else
{
// Ignore unknown commands for extensibility
}
// Update the last seen time for this node's address
if ( pfrom - > fNetworkNode )
if ( strCommand = = " version " | | strCommand = = " addr " | | strCommand = = " inv " | | strCommand = = " getdata " | | strCommand = = " ping " )
AddressCurrentlyConnected ( pfrom - > addr ) ;
return true ;
}
2012-11-16 01:41:12 +01:00
// requires LOCK(cs_vRecvMsg)
2011-06-01 18:27:05 +02:00
bool ProcessMessages ( CNode * pfrom )
{
//if (fDebug)
2013-10-15 14:50:58 +02:00
// LogPrintf("ProcessMessages(%"PRIszu" messages)\n", pfrom->vRecvMsg.size());
2010-08-29 18:58:15 +02:00
2011-06-01 18:27:05 +02:00
//
// Message format
// (4) message start
// (12) command
// (4) size
// (4) checksum
// (x) data
//
2013-03-01 01:41:28 +01:00
bool fOk = true ;
2010-08-29 18:58:15 +02:00
2013-03-29 23:49:38 +01:00
if ( ! pfrom - > vRecvGetData . empty ( ) )
ProcessGetData ( pfrom ) ;
2013-11-11 16:20:39 +01:00
2013-10-28 21:20:21 +01:00
// this maintains the order of responses
if ( ! pfrom - > vRecvGetData . empty ( ) ) return fOk ;
2013-11-11 16:20:39 +01:00
2013-03-01 01:41:28 +01:00
std : : deque < CNetMessage > : : iterator it = pfrom - > vRecvMsg . begin ( ) ;
2013-03-24 16:52:24 +01:00
while ( ! pfrom - > fDisconnect & & it ! = pfrom - > vRecvMsg . end ( ) ) {
2012-03-22 03:10:50 +01:00
// Don't bother if send buffer is too full to respond anyway
2013-03-24 16:52:24 +01:00
if ( pfrom - > nSendSize > = SendBufferSize ( ) )
2012-03-22 03:10:50 +01:00
break ;
2013-03-01 01:41:28 +01:00
// get next message
CNetMessage & msg = * it ;
2012-11-16 01:41:12 +01:00
//if (fDebug)
2013-10-15 14:50:58 +02:00
// LogPrintf("ProcessMessages(message %u msgsz, %"PRIszu" bytes, complete:%s)\n",
2012-11-16 01:41:12 +01:00
// msg.hdr.nMessageSize, msg.vRecv.size(),
// msg.complete() ? "Y" : "N");
2013-03-01 01:41:28 +01:00
// end, if an incomplete message is found
2012-11-16 01:41:12 +01:00
if ( ! msg . complete ( ) )
2011-06-01 18:27:05 +02:00
break ;
2012-11-16 01:41:12 +01:00
2013-03-01 01:41:28 +01:00
// at this point, any failure means we can delete the current message
it + + ;
2012-11-16 01:41:12 +01:00
// Scan for message start
2013-05-07 15:16:25 +02:00
if ( memcmp ( msg . hdr . pchMessageStart , Params ( ) . MessageStart ( ) , MESSAGE_START_SIZE ) ! = 0 ) {
2013-09-18 12:38:08 +02:00
LogPrintf ( " \n \n PROCESSMESSAGE: INVALID MESSAGESTART \n \n " ) ;
2013-03-01 01:41:28 +01:00
fOk = false ;
break ;
2011-06-01 18:27:05 +02:00
}
2010-08-29 18:58:15 +02:00
2011-06-01 18:27:05 +02:00
// Read header
2012-11-16 01:41:12 +01:00
CMessageHeader & hdr = msg . hdr ;
2011-06-01 18:27:05 +02:00
if ( ! hdr . IsValid ( ) )
{
2014-01-16 16:15:27 +01:00
LogPrintf ( " \n \n PROCESSMESSAGE: ERRORS IN HEADER %s \n \n \n " , hdr . GetCommand ( ) ) ;
2011-06-01 18:27:05 +02:00
continue ;
}
string strCommand = hdr . GetCommand ( ) ;
// Message size
unsigned int nMessageSize = hdr . nMessageSize ;
// Checksum
2012-11-16 01:41:12 +01:00
CDataStream & vRecv = msg . vRecv ;
2012-02-20 01:33:31 +01:00
uint256 hash = Hash ( vRecv . begin ( ) , vRecv . begin ( ) + nMessageSize ) ;
unsigned int nChecksum = 0 ;
memcpy ( & nChecksum , & hash , sizeof ( nChecksum ) ) ;
if ( nChecksum ! = hdr . nChecksum )
2011-06-01 18:27:05 +02:00
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " ProcessMessages(%s, %u bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x \n " ,
2014-01-16 16:15:27 +01:00
strCommand , nMessageSize , nChecksum , hdr . nChecksum ) ;
2012-02-20 01:33:31 +01:00
continue ;
2011-06-01 18:27:05 +02:00
}
// Process message
bool fRet = false ;
try
{
2013-07-25 02:34:42 +02:00
fRet = ProcessMessage ( pfrom , strCommand , vRecv ) ;
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2011-06-01 18:27:05 +02:00
}
catch ( std : : ios_base : : failure & e )
{
2013-10-28 07:36:11 +01:00
pfrom - > PushMessage ( " reject " , strCommand , REJECT_MALFORMED , string ( " error parsing message " ) ) ;
2011-06-01 18:27:05 +02:00
if ( strstr ( e . what ( ) , " end of data " ) )
{
2012-07-26 02:48:39 +02:00
// Allow exceptions from under-length message on vRecv
2014-01-16 16:15:27 +01:00
LogPrintf ( " ProcessMessages(%s, %u bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length \n " , strCommand , nMessageSize , e . what ( ) ) ;
2011-06-01 18:27:05 +02:00
}
else if ( strstr ( e . what ( ) , " size too large " ) )
{
2012-07-26 02:48:39 +02:00
// Allow exceptions from over-long size
2014-01-16 16:15:27 +01:00
LogPrintf ( " ProcessMessages(%s, %u bytes) : Exception '%s' caught \n " , strCommand , nMessageSize , e . what ( ) ) ;
2011-06-01 18:27:05 +02:00
}
else
{
2012-05-22 13:06:08 +02:00
PrintExceptionContinue ( & e , " ProcessMessages() " ) ;
2011-06-01 18:27:05 +02:00
}
}
2013-03-09 18:02:57 +01:00
catch ( boost : : thread_interrupted ) {
throw ;
}
2011-06-01 18:27:05 +02:00
catch ( std : : exception & e ) {
2012-05-22 13:06:08 +02:00
PrintExceptionContinue ( & e , " ProcessMessages() " ) ;
2011-06-01 18:27:05 +02:00
} catch ( . . . ) {
2012-05-22 13:06:08 +02:00
PrintExceptionContinue ( NULL , " ProcessMessages() " ) ;
2011-06-01 18:27:05 +02:00
}
if ( ! fRet )
2014-01-16 16:15:27 +01:00
LogPrintf ( " ProcessMessage(%s, %u bytes) FAILED \n " , strCommand , nMessageSize ) ;
2013-11-11 16:20:39 +01:00
2013-10-28 21:20:21 +01:00
break ;
2011-06-01 18:27:05 +02:00
}
2013-03-24 16:52:24 +01:00
// In case the connection got shut down, its receive buffer was wiped
if ( ! pfrom - > fDisconnect )
pfrom - > vRecvMsg . erase ( pfrom - > vRecvMsg . begin ( ) , it ) ;
2013-03-01 01:41:28 +01:00
return fOk ;
2011-06-01 18:27:05 +02:00
}
2010-08-29 18:58:15 +02:00
bool SendMessages ( CNode * pto , bool fSendTrickle )
{
2013-07-20 14:34:16 +02:00
{
2010-08-29 18:58:15 +02:00
// Don't send anything until we get their version message
if ( pto - > nVersion = = 0 )
return true ;
2013-08-22 13:34:33 +02:00
//
// Message: ping
//
bool pingSend = false ;
if ( pto - > fPingQueued ) {
// RPC ping request by user
pingSend = true ;
}
2013-03-24 16:52:24 +01:00
if ( pto - > nLastSend & & GetTime ( ) - pto - > nLastSend > 30 * 60 & & pto - > vSendMsg . empty ( ) ) {
2013-08-22 13:34:33 +02:00
// Ping automatically sent as a keepalive
pingSend = true ;
}
if ( pingSend ) {
2013-04-13 07:13:08 +02:00
uint64_t nonce = 0 ;
2013-08-22 13:34:33 +02:00
while ( nonce = = 0 ) {
RAND_bytes ( ( unsigned char * ) & nonce , sizeof ( nonce ) ) ;
}
pto - > nPingNonceSent = nonce ;
pto - > fPingQueued = false ;
if ( pto - > nVersion > BIP0031_VERSION ) {
// Take timestamp as close as possible before transmitting ping
pto - > nPingUsecStart = GetTimeMicros ( ) ;
2012-06-14 18:31:08 +02:00
pto - > PushMessage ( " ping " , nonce ) ;
2013-08-22 13:34:33 +02:00
} else {
// Peer is too old to support ping command with nonce, pong will never arrive, disable timing
pto - > nPingUsecStart = 0 ;
2012-04-11 18:38:03 +02:00
pto - > PushMessage ( " ping " ) ;
2013-08-22 13:34:33 +02:00
}
2012-04-11 18:38:03 +02:00
}
2010-08-29 18:58:15 +02:00
// Address refresh broadcast
2013-04-13 07:13:08 +02:00
static int64_t nLastRebroadcast ;
2012-02-02 00:08:03 +01:00
if ( ! IsInitialBlockDownload ( ) & & ( GetTime ( ) - nLastRebroadcast > 24 * 60 * 60 ) )
2010-08-29 18:58:15 +02:00
{
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodes )
2010-08-29 18:58:15 +02:00
{
// Periodically clear setAddrKnown to allow refresh broadcasts
2012-02-02 00:08:03 +01:00
if ( nLastRebroadcast )
pnode - > setAddrKnown . clear ( ) ;
2010-08-29 18:58:15 +02:00
// Rebroadcast our address
2012-05-24 19:02:21 +02:00
if ( ! fNoListen )
2010-10-23 19:43:53 +02:00
{
2012-02-12 13:45:24 +01:00
CAddress addr = GetLocalAddress ( & pnode - > addr ) ;
if ( addr . IsRoutable ( ) )
pnode - > PushAddress ( addr ) ;
2010-10-23 19:43:53 +02:00
}
2010-08-29 18:58:15 +02:00
}
}
2012-02-02 00:08:03 +01:00
nLastRebroadcast = GetTime ( ) ;
2010-08-29 18:58:15 +02:00
}
//
// Message: addr
//
if ( fSendTrickle )
{
vector < CAddress > vAddr ;
vAddr . reserve ( pto - > vAddrToSend . size ( ) ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( const CAddress & addr , pto - > vAddrToSend )
2010-08-29 18:58:15 +02:00
{
// returns true if wasn't already contained in the set
if ( pto - > setAddrKnown . insert ( addr ) . second )
{
vAddr . push_back ( addr ) ;
// receiver rejects addr messages larger than 1000
if ( vAddr . size ( ) > = 1000 )
{
pto - > PushMessage ( " addr " , vAddr ) ;
vAddr . clear ( ) ;
}
}
}
pto - > vAddrToSend . clear ( ) ;
if ( ! vAddr . empty ( ) )
pto - > PushMessage ( " addr " , vAddr ) ;
}
2013-07-20 14:34:16 +02:00
TRY_LOCK ( cs_main , lockMain ) ;
if ( ! lockMain )
return true ;
2013-11-16 19:28:24 +01:00
CNodeState & state = * State ( pto - > GetId ( ) ) ;
if ( state . fShouldBan ) {
2013-11-18 01:25:17 +01:00
if ( pto - > addr . IsLocal ( ) )
2014-01-16 16:15:27 +01:00
LogPrintf ( " Warning: not banning local node %s! \n " , pto - > addr . ToString ( ) ) ;
2013-11-18 01:25:17 +01:00
else {
pto - > fDisconnect = true ;
CNode : : Ban ( pto - > addr ) ;
}
2013-11-16 19:28:24 +01:00
state . fShouldBan = false ;
2013-11-18 01:25:17 +01:00
}
2013-11-16 19:28:24 +01:00
BOOST_FOREACH ( const CBlockReject & reject , state . rejects )
pto - > PushMessage ( " reject " , ( string ) " block " , reject . chRejectCode , reject . strRejectReason , reject . hashBlock ) ;
state . rejects . clear ( ) ;
2013-07-20 14:34:16 +02:00
// Start block sync
if ( pto - > fStartSync & & ! fImporting & & ! fReindex ) {
pto - > fStartSync = false ;
PushGetBlocks ( pto , chainActive . Tip ( ) , uint256 ( 0 ) ) ;
}
// Resend wallet transactions that haven't gotten in a block yet
// Except during reindex, importing and IBD, when old wallet
// transactions become unconfirmed and spams other nodes.
if ( ! fReindex & & ! fImporting & & ! IsInitialBlockDownload ( ) )
{
2013-10-19 18:34:06 +02:00
g_signals . Broadcast ( ) ;
2013-07-20 14:34:16 +02:00
}
2010-08-29 18:58:15 +02:00
//
// Message: inventory
//
vector < CInv > vInv ;
vector < CInv > vInvWait ;
{
2012-04-06 18:39:12 +02:00
LOCK ( pto - > cs_inventory ) ;
2010-08-29 18:58:15 +02:00
vInv . reserve ( pto - > vInventoryToSend . size ( ) ) ;
vInvWait . reserve ( pto - > vInventoryToSend . size ( ) ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( const CInv & inv , pto - > vInventoryToSend )
2010-08-29 18:58:15 +02:00
{
if ( pto - > setInventoryKnown . count ( inv ) )
continue ;
// trickle out tx inv to protect privacy
if ( inv . type = = MSG_TX & & ! fSendTrickle )
{
// 1/4 of tx invs blast to all immediately
static uint256 hashSalt ;
if ( hashSalt = = 0 )
2012-05-17 18:13:14 +02:00
hashSalt = GetRandHash ( ) ;
2010-08-29 18:58:15 +02:00
uint256 hashRand = inv . hash ^ hashSalt ;
hashRand = Hash ( BEGIN ( hashRand ) , END ( hashRand ) ) ;
bool fTrickleWait = ( ( hashRand & 3 ) ! = 0 ) ;
if ( fTrickleWait )
{
vInvWait . push_back ( inv ) ;
continue ;
}
}
// returns true if wasn't already contained in the set
if ( pto - > setInventoryKnown . insert ( inv ) . second )
{
vInv . push_back ( inv ) ;
if ( vInv . size ( ) > = 1000 )
{
pto - > PushMessage ( " inv " , vInv ) ;
vInv . clear ( ) ;
}
}
}
pto - > vInventoryToSend = vInvWait ;
}
if ( ! vInv . empty ( ) )
pto - > PushMessage ( " inv " , vInv ) ;
2014-01-10 13:23:26 +01:00
// Detect stalled peers. Require that blocks are in flight, we haven't
// received a (requested) block in one minute, and that all blocks are
// in flight for over two minutes, since we first had a chance to
// process an incoming block.
int64_t nNow = GetTimeMicros ( ) ;
if ( ! pto - > fDisconnect & & state . nBlocksInFlight & &
state . nLastBlockReceive < state . nLastBlockProcess - BLOCK_DOWNLOAD_TIMEOUT * 1000000 & &
state . vBlocksInFlight . front ( ) . nTime < state . nLastBlockProcess - 2 * BLOCK_DOWNLOAD_TIMEOUT * 1000000 ) {
LogPrintf ( " Peer %s is stalling block download, disconnecting \n " , state . name . c_str ( ) ) ;
pto - > fDisconnect = true ;
}
2010-08-29 18:58:15 +02:00
//
2014-01-10 13:23:26 +01:00
// Message: getdata (blocks)
2010-08-29 18:58:15 +02:00
//
vector < CInv > vGetData ;
2014-01-10 13:23:26 +01:00
while ( ! pto - > fDisconnect & & state . nBlocksToDownload & & state . nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER ) {
uint256 hash = state . vBlocksToDownload . front ( ) ;
vGetData . push_back ( CInv ( MSG_BLOCK , hash ) ) ;
MarkBlockAsInFlight ( pto - > GetId ( ) , hash ) ;
LogPrint ( " net " , " Requesting block %s from %s \n " , hash . ToString ( ) . c_str ( ) , state . name . c_str ( ) ) ;
if ( vGetData . size ( ) > = 1000 )
{
pto - > PushMessage ( " getdata " , vGetData ) ;
vGetData . clear ( ) ;
}
}
//
// Message: getdata (non-blocks)
//
while ( ! pto - > fDisconnect & & ! pto - > mapAskFor . empty ( ) & & ( * pto - > mapAskFor . begin ( ) ) . first < = nNow )
2010-08-29 18:58:15 +02:00
{
const CInv & inv = ( * pto - > mapAskFor . begin ( ) ) . second ;
2012-07-06 16:33:34 +02:00
if ( ! AlreadyHave ( inv ) )
2010-08-29 18:58:15 +02:00
{
2013-10-08 12:09:40 +02:00
if ( fDebug )
2014-01-16 16:15:27 +01:00
LogPrint ( " net " , " sending getdata: %s \n " , inv . ToString ( ) ) ;
2010-08-29 18:58:15 +02:00
vGetData . push_back ( inv ) ;
if ( vGetData . size ( ) > = 1000 )
{
pto - > PushMessage ( " getdata " , vGetData ) ;
vGetData . clear ( ) ;
}
}
pto - > mapAskFor . erase ( pto - > mapAskFor . begin ( ) ) ;
}
if ( ! vGetData . empty ( ) )
pto - > PushMessage ( " getdata " , vGetData ) ;
}
return true ;
}
2013-03-29 02:17:10 +01:00
class CMainCleanup
{
public :
CMainCleanup ( ) { }
~ CMainCleanup ( ) {
// block headers
std : : map < uint256 , CBlockIndex * > : : iterator it1 = mapBlockIndex . begin ( ) ;
for ( ; it1 ! = mapBlockIndex . end ( ) ; it1 + + )
delete ( * it1 ) . second ;
mapBlockIndex . clear ( ) ;
// orphan blocks
2014-01-11 23:33:34 +01:00
std : : map < uint256 , COrphanBlock * > : : iterator it2 = mapOrphanBlocks . begin ( ) ;
2013-03-29 02:17:10 +01:00
for ( ; it2 ! = mapOrphanBlocks . end ( ) ; it2 + + )
delete ( * it2 ) . second ;
mapOrphanBlocks . clear ( ) ;
// orphan transactions
mapOrphanTransactions . clear ( ) ;
}
} instance_of_cmaincleanup ;