2011-05-14 20:10:21 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
2016-12-31 19:01:21 +01:00
// Copyright (c) 2009-2016 The Bitcoin Core developers
2014-12-01 02:39:44 +01:00
// Distributed under the MIT software license, see the accompanying
2012-05-18 16:02:28 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2012-11-05 08:04:21 +01:00
2013-05-28 01:55:01 +02:00
# if defined(HAVE_CONFIG_H)
2014-06-23 20:04:24 +02:00
# include "config/bitcoin-config.h"
2013-05-28 01:55:01 +02:00
# endif
2013-01-07 17:07:51 +01:00
# include "init.h"
2013-04-13 07:13:08 +02:00
# include "addrman.h"
2014-10-23 02:05:11 +02:00
# include "amount.h"
2015-07-05 14:17:46 +02:00
# include "chain.h"
# include "chainparams.h"
2013-04-13 07:13:08 +02:00
# include "checkpoints.h"
2014-09-14 12:43:56 +02:00
# include "compat/sanity.h"
2015-01-24 15:57:12 +01:00
# include "consensus/validation.h"
2017-03-01 16:54:22 +01:00
# include "fs.h"
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
# include "httpserver.h"
# include "httprpc.h"
2014-06-03 01:21:03 +02:00
# include "key.h"
2016-12-02 01:06:41 +01:00
# include "validation.h"
2013-07-31 15:43:35 +02:00
# include "miner.h"
2016-05-31 23:42:38 +02:00
# include "netbase.h"
2013-04-13 07:13:08 +02:00
# include "net.h"
2016-12-02 00:45:50 +01:00
# include "net_processing.h"
2016-03-31 23:30:17 +02:00
# include "policy/feerate.h"
2017-02-15 21:48:48 +01:00
# include "policy/fees.h"
2015-06-24 07:25:30 +02:00
# include "policy/policy.h"
2016-01-15 01:55:17 +01:00
# include "rpc/server.h"
2016-03-29 19:43:02 +02:00
# include "rpc/register.h"
2017-03-27 16:04:31 +02:00
# include "rpc/blockchain.h"
2014-10-11 01:55:14 +02:00
# include "script/standard.h"
2015-10-30 23:14:38 +01:00
# include "script/sigcache.h"
2015-04-03 17:50:06 +02:00
# include "scheduler.h"
2016-03-29 16:40:00 +02:00
# include "timedata.h"
2013-04-13 07:13:08 +02:00
# include "txdb.h"
2015-07-05 14:17:46 +02:00
# include "txmempool.h"
2015-08-25 20:12:08 +02:00
# include "torcontrol.h"
2012-04-15 22:10:54 +02:00
# include "ui_interface.h"
2013-04-13 07:13:08 +02:00
# include "util.h"
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
# include "utilmoneystr.h"
2015-03-24 22:14:44 +01:00
# include "validationinterface.h"
2013-11-29 16:04:29 +01:00
# ifdef ENABLE_WALLET
2015-02-03 21:09:47 +01:00
# include "wallet/wallet.h"
2013-11-29 16:04:29 +01:00
# endif
2016-11-30 07:07:42 +01:00
# include "warnings.h"
2013-04-13 07:13:08 +02:00
# include <stdint.h>
2014-05-11 15:29:16 +02:00
# include <stdio.h>
2016-04-16 20:47:18 +02:00
# include <memory>
2011-05-14 20:10:21 +02:00
2012-04-15 22:10:54 +02:00
# ifndef WIN32
# include <signal.h>
2012-03-18 23:14:03 +01:00
# endif
2011-10-07 16:46:56 +02:00
2016-01-28 06:27:25 +01:00
# include <boost/algorithm/string/classification.hpp>
2014-08-29 22:52:41 +02:00
# include <boost/algorithm/string/replace.hpp>
2016-01-28 06:27:25 +01:00
# include <boost/algorithm/string/split.hpp>
2015-03-26 16:20:59 +01:00
# include <boost/bind.hpp>
2013-04-13 07:13:08 +02:00
# include <boost/interprocess/sync/file_lock.hpp>
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
# include <boost/thread.hpp>
2013-04-13 07:13:08 +02:00
# include <openssl/crypto.h>
2014-11-18 18:06:32 +01:00
# if ENABLE_ZMQ
# include "zmq/zmqnotificationinterface.h"
# endif
2014-09-18 14:08:43 +02:00
bool fFeeEstimatesInitialized = false ;
2015-06-27 21:21:41 +02:00
static const bool DEFAULT_PROXYRANDOMIZE = true ;
static const bool DEFAULT_REST_ENABLE = false ;
2017-08-08 22:12:45 +02:00
static const bool DEFAULT_DISABLE_SAFEMODE = true ;
2015-06-27 21:21:41 +02:00
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false ;
2011-06-26 19:23:24 +02:00
2016-04-16 20:47:18 +02:00
std : : unique_ptr < CConnman > g_connman ;
2016-10-04 19:49:44 +02:00
std : : unique_ptr < PeerLogicValidation > peerLogic ;
2016-02-11 07:35:25 +01:00
2014-11-18 18:06:32 +01:00
# if ENABLE_ZMQ
2017-08-07 07:36:37 +02:00
static CZMQNotificationInterface * pzmqNotificationInterface = nullptr ;
2014-11-18 18:06:32 +01:00
# endif
2013-04-26 00:46:47 +02:00
# ifdef WIN32
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
2015-04-28 16:48:28 +02:00
// accessing block files don't count towards the fd_set size limit
2013-04-26 00:46:47 +02:00
// anyway.
# define MIN_CORE_FILEDESCRIPTORS 0
# else
# define MIN_CORE_FILEDESCRIPTORS 150
# endif
2014-03-17 13:19:54 +01:00
static const char * FEE_ESTIMATES_FILENAME = " fee_estimates.dat " ;
2013-01-07 16:35:31 +01:00
2011-05-14 20:10:21 +02:00
//////////////////////////////////////////////////////////////////////////////
//
// Shutdown
//
2013-03-09 18:02:57 +01:00
//
// Thread management and startup/shutdown:
//
// The network-processing threads are all part of a thread group
// created by AppInit() or the Qt main() function.
//
// A clean exit happens when StartShutdown() or the SIGTERM
// signal handler sets fRequestShutdown, which triggers
// the DetectShutdownThread(), which interrupts the main thread group.
// DetectShutdownThread() then exits, which causes AppInit() to
// continue (it .joins the shutdown thread).
// Shutdown() is then
// called to clean up database connections, and stop other
// threads that should only be stopped after the main network-processing
// threads have exited.
//
// Shutdown for Qt is very similar, only it uses a QTimer to detect
2013-03-23 23:14:12 +01:00
// fRequestShutdown getting set, and then does the normal Qt
// shutdown thing.
2013-03-09 18:02:57 +01:00
//
2016-05-30 15:39:37 +02:00
std : : atomic < bool > fRequestShutdown ( false ) ;
2016-12-22 20:11:26 +01:00
std : : atomic < bool > fDumpMempoolLater ( false ) ;
2011-05-14 20:10:21 +02:00
2012-06-11 07:40:14 +02:00
void StartShutdown ( )
{
2013-03-09 18:02:57 +01:00
fRequestShutdown = true ;
2012-06-11 07:40:14 +02:00
}
2013-03-23 23:14:12 +01:00
bool ShutdownRequested ( )
{
return fRequestShutdown ;
}
2012-06-11 07:40:14 +02:00
2016-05-06 06:10:49 +02:00
/**
* This is a minimally invasive approach to shutdown on LevelDB read errors from the
* chainstate , while keeping user interface out of the common library , which is shared
* between bitcoind , and bitcoin - qt and non - server tools .
*/
2015-01-08 14:38:06 +01:00
class CCoinsViewErrorCatcher : public CCoinsViewBacked
{
public :
2017-08-01 12:22:41 +02:00
explicit CCoinsViewErrorCatcher ( CCoinsView * view ) : CCoinsViewBacked ( view ) { }
2017-05-31 02:58:54 +02:00
bool GetCoin ( const COutPoint & outpoint , Coin & coin ) const override {
2015-01-08 14:38:06 +01:00
try {
2017-05-31 02:58:54 +02:00
return CCoinsViewBacked : : GetCoin ( outpoint , coin ) ;
2015-01-08 14:38:06 +01:00
} catch ( const std : : runtime_error & e ) {
uiInterface . ThreadSafeMessageBox ( _ ( " Error reading from database, shutting down. " ) , " " , CClientUIInterface : : MSG_ERROR ) ;
LogPrintf ( " Error reading from database: %s \n " , e . what ( ) ) ;
// Starting the shutdown sequence and returning false to the caller would be
// interpreted as 'entry not found' (as opposed to unable to read data), and
2015-02-11 05:24:38 +01:00
// could lead to invalid interpretation. Just exit immediately, as we can't
2015-01-08 14:38:06 +01:00
// continue anyway, and all writes should be atomic.
abort ( ) ;
}
}
// Writes do not need similar protection, as failure to write is handled by the caller.
} ;
2017-08-07 07:36:37 +02:00
static CCoinsViewErrorCatcher * pcoinscatcher = nullptr ;
2016-08-30 22:41:56 +02:00
static std : : unique_ptr < ECCVerifyHandle > globalVerifyHandle ;
2012-07-06 16:33:34 +02:00
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
void Interrupt ( boost : : thread_group & threadGroup )
{
InterruptHTTPServer ( ) ;
InterruptHTTPRPC ( ) ;
InterruptRPC ( ) ;
InterruptREST ( ) ;
2015-09-08 17:48:45 +02:00
InterruptTorControl ( ) ;
2016-12-27 23:12:44 +01:00
if ( g_connman )
g_connman - > Interrupt ( ) ;
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
threadGroup . interrupt_all ( ) ;
}
2013-03-09 18:02:57 +01:00
void Shutdown ( )
2011-05-14 20:10:21 +02:00
{
2014-06-27 14:41:11 +02:00
LogPrintf ( " %s: In progress... \n " , __func__ ) ;
2011-05-14 20:10:21 +02:00
static CCriticalSection cs_Shutdown ;
2013-03-09 18:02:57 +01:00
TRY_LOCK ( cs_Shutdown , lockShutdown ) ;
2014-06-27 14:41:11 +02:00
if ( ! lockShutdown )
return ;
2012-06-24 17:03:57 +02:00
2017-02-23 08:38:41 +01:00
/// Note: Shutdown() must be able to handle cases in which initialization failed part of the way,
2014-09-18 14:08:43 +02:00
/// for example if the data directory was found to be locked.
/// Be sure that anything that writes files or flushes caches only does this if the respective
/// module was initialized.
2012-06-24 17:03:57 +02:00
RenameThread ( " bitcoin-shutoff " ) ;
2013-08-27 07:51:57 +02:00
mempool . AddTransactionsUpdated ( 1 ) ;
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
StopHTTPRPC ( ) ;
StopREST ( ) ;
StopRPC ( ) ;
StopHTTPServer ( ) ;
2013-12-08 15:26:08 +01:00
# ifdef ENABLE_WALLET
2016-09-09 10:42:30 +02:00
for ( CWalletRef pwallet : vpwallets ) {
pwallet - > Flush ( false ) ;
}
2013-11-29 16:04:29 +01:00
# endif
2016-05-27 05:29:39 +02:00
MapPort ( false ) ;
2016-10-04 19:49:44 +02:00
UnregisterValidationInterface ( peerLogic . get ( ) ) ;
peerLogic . reset ( ) ;
2016-04-16 20:47:18 +02:00
g_connman . reset ( ) ;
2015-08-25 20:12:08 +02:00
StopTorControl ( ) ;
2013-11-18 01:25:17 +01:00
UnregisterNodeSignals ( GetNodeSignals ( ) ) ;
2017-08-01 21:17:40 +02:00
if ( fDumpMempoolLater & & gArgs . GetArg ( " -persistmempool " , DEFAULT_PERSIST_MEMPOOL ) ) {
2016-12-22 20:11:26 +01:00
DumpMempool ( ) ;
2017-03-09 23:12:25 +01:00
}
2014-03-17 13:19:54 +01:00
2014-09-18 14:08:43 +02:00
if ( fFeeEstimatesInitialized )
2014-07-16 21:46:01 +02:00
{
2017-03-09 21:26:05 +01:00
: : feeEstimator . FlushUnconfirmed ( : : mempool ) ;
2017-03-01 17:05:50 +01:00
fs : : path est_path = GetDataDir ( ) / FEE_ESTIMATES_FILENAME ;
2017-03-01 17:28:39 +01:00
CAutoFile est_fileout ( fsbridge : : fopen ( est_path , " wb " ) , SER_DISK , CLIENT_VERSION ) ;
2014-10-20 12:45:50 +02:00
if ( ! est_fileout . IsNull ( ) )
2017-02-15 21:48:48 +01:00
: : feeEstimator . Write ( est_fileout ) ;
2014-07-16 21:46:01 +02:00
else
LogPrintf ( " %s: Failed to write fee estimates to %s \n " , __func__ , est_path . string ( ) ) ;
2014-09-18 14:08:43 +02:00
fFeeEstimatesInitialized = false ;
2014-07-16 21:46:01 +02:00
}
2014-03-17 13:19:54 +01:00
2017-06-28 01:07:52 +02:00
// FlushStateToDisk generates a SetBestChain callback, which we should avoid missing
2017-07-16 02:03:11 +02:00
if ( pcoinsTip ! = nullptr ) {
FlushStateToDisk ( ) ;
}
2017-06-28 01:07:52 +02:00
// After there are no more peers/RPC left to give us new data which may generate
// CValidationInterface callbacks, flush them...
GetMainSignals ( ) . FlushBackgroundCallbacks ( ) ;
// Any future callbacks will be dropped. This should absolutely be safe - if
// missing a callback results in an unrecoverable situation, unclean shutdown
// would too. The only reason to do the above flushes is to let the wallet catch
// up with our current chain to avoid any strange pruning edge cases and make
// next startup faster by avoiding rescan.
2011-05-14 20:10:21 +02:00
{
2013-03-09 18:02:57 +01:00
LOCK ( cs_main ) ;
2017-08-07 07:36:37 +02:00
if ( pcoinsTip ! = nullptr ) {
Improve chainstate/blockindex disk writing policy
There are 3 pieces of data that are maintained on disk. The actual block
and undo data, the block index (which can refer to positions on disk),
and the chainstate (which refers to the best block hash).
Earlier, there was no guarantee that blocks were written to disk before
block index entries referring to them were written. This commit introduces
dirty flags for block index data, and delays writing entries until the actual
block data is flushed.
With this stricter ordering in writes, it is now safe to not always flush
after every block, so there is no need for the IsInitialBlockDownload()
check there - instead we just write whenever enough time has passed or
the cache size grows too large. Also updating the wallet's best known block
is delayed until this is done, otherwise the wallet may end up referring to an
unknown block.
In addition, only do a write inside the block processing loop if necessary
(because of cache size exceeded). Otherwise, move the writing to a point
after processing is done, after relaying.
2014-11-07 11:38:35 +01:00
FlushStateToDisk ( ) ;
}
2014-06-27 14:41:11 +02:00
delete pcoinsTip ;
2017-08-07 07:36:37 +02:00
pcoinsTip = nullptr ;
2015-01-08 14:38:06 +01:00
delete pcoinscatcher ;
2017-08-07 07:36:37 +02:00
pcoinscatcher = nullptr ;
2014-06-27 14:41:11 +02:00
delete pcoinsdbview ;
2017-08-07 07:36:37 +02:00
pcoinsdbview = nullptr ;
2014-06-27 14:41:11 +02:00
delete pblocktree ;
2017-08-07 07:36:37 +02:00
pblocktree = nullptr ;
2011-05-14 20:10:21 +02:00
}
2013-11-29 16:04:29 +01:00
# ifdef ENABLE_WALLET
2016-09-09 10:42:30 +02:00
for ( CWalletRef pwallet : vpwallets ) {
pwallet - > Flush ( true ) ;
}
2013-11-29 16:04:29 +01:00
# endif
2014-11-18 18:06:32 +01:00
# if ENABLE_ZMQ
if ( pzmqNotificationInterface ) {
UnregisterValidationInterface ( pzmqNotificationInterface ) ;
delete pzmqNotificationInterface ;
2017-08-07 07:36:37 +02:00
pzmqNotificationInterface = nullptr ;
2014-11-18 18:06:32 +01:00
}
# endif
2014-09-20 10:56:25 +02:00
# ifndef WIN32
2015-05-19 01:37:43 +02:00
try {
2017-03-01 17:05:50 +01:00
fs : : remove ( GetPidFile ( ) ) ;
} catch ( const fs : : filesystem_error & e ) {
2015-05-19 01:37:43 +02:00
LogPrintf ( " %s: Unable to remove pidfile: %s \n " , __func__ , e . what ( ) ) ;
}
2014-09-20 10:56:25 +02:00
# endif
2014-10-20 03:17:01 +02:00
UnregisterAllValidationInterfaces ( ) ;
2017-01-19 22:49:22 +01:00
GetMainSignals ( ) . UnregisterBackgroundSignalScheduler ( ) ;
2013-11-29 16:04:29 +01:00
# ifdef ENABLE_WALLET
2016-09-09 10:42:30 +02:00
for ( CWalletRef pwallet : vpwallets ) {
delete pwallet ;
}
vpwallets . clear ( ) ;
2013-11-29 16:04:29 +01:00
# endif
2015-07-28 20:11:20 +02:00
globalVerifyHandle . reset ( ) ;
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
ECC_Stop ( ) ;
2014-06-27 14:41:11 +02:00
LogPrintf ( " %s: done \n " , __func__ ) ;
2011-05-14 20:10:21 +02:00
}
2014-12-01 02:39:44 +01:00
/**
2017-03-22 21:29:41 +01:00
* Signal handlers are very limited in what they are allowed to do .
* The execution context the handler is invoked in is not guaranteed ,
* so we restrict handler operations to just touching variables :
2014-12-01 02:39:44 +01:00
*/
2017-03-22 21:29:41 +01:00
static void HandleSIGTERM ( int )
2011-05-14 20:10:21 +02:00
{
fRequestShutdown = true ;
}
2017-03-22 21:29:41 +01:00
static void HandleSIGHUP ( int )
2012-03-02 20:31:16 +01:00
{
fReopenDebugLog = true ;
}
2011-05-14 20:10:21 +02:00
2017-03-22 21:29:41 +01:00
# ifndef WIN32
static void registerSignalHandler ( int signal , void ( * handler ) ( int ) )
{
struct sigaction sa ;
sa . sa_handler = handler ;
sigemptyset ( & sa . sa_mask ) ;
sa . sa_flags = 0 ;
2017-08-07 07:36:37 +02:00
sigaction ( signal , & sa , nullptr ) ;
2017-03-22 21:29:41 +01:00
}
# endif
2016-09-02 03:55:21 +02:00
void OnRPCStarted ( )
{
uiInterface . NotifyBlockTip . connect ( & RPCNotifyBlockChange ) ;
}
2012-05-11 15:28:59 +02:00
2014-10-19 10:46:17 +02:00
void OnRPCStopped ( )
{
2016-09-02 03:55:21 +02:00
uiInterface . NotifyBlockTip . disconnect ( & RPCNotifyBlockChange ) ;
RPCNotifyBlockChange ( false , nullptr ) ;
2014-10-19 10:46:17 +02:00
cvBlockChange . notify_all ( ) ;
2016-12-25 21:19:40 +01:00
LogPrint ( BCLog : : RPC , " RPC stopped. \n " ) ;
2014-10-19 10:46:17 +02:00
}
void OnRPCPreCommand ( const CRPCCommand & cmd )
{
// Observe safe mode
2017-01-27 09:43:41 +01:00
std : : string strWarning = GetWarnings ( " rpc " ) ;
2017-08-01 21:17:40 +02:00
if ( strWarning ! = " " & & ! gArgs . GetBoolArg ( " -disablesafemode " , DEFAULT_DISABLE_SAFEMODE ) & &
2014-10-19 10:46:17 +02:00
! cmd . okSafeMode )
2017-01-27 09:43:41 +01:00
throw JSONRPCError ( RPC_FORBIDDEN_BY_SAFE_MODE , std : : string ( " Safe mode: " ) + strWarning ) ;
2014-10-19 10:46:17 +02:00
}
2014-06-17 09:13:52 +02:00
std : : string HelpMessage ( HelpMessageMode mode )
2012-05-13 11:36:10 +02:00
{
2015-05-22 03:50:01 +02:00
const auto defaultBaseParams = CreateBaseChainParams ( CBaseChainParams : : MAIN ) ;
const auto testnetBaseParams = CreateBaseChainParams ( CBaseChainParams : : TESTNET ) ;
2015-11-28 15:04:35 +01:00
const auto defaultChainParams = CreateChainParams ( CBaseChainParams : : MAIN ) ;
const auto testnetChainParams = CreateChainParams ( CBaseChainParams : : TESTNET ) ;
2017-08-01 21:17:40 +02:00
const bool showDebug = gArgs . GetBoolArg ( " -help-debug " , false ) ;
2015-02-04 09:11:49 +01:00
2014-06-17 09:13:52 +02:00
// When adding new options to the categories, please keep and ensure alphabetical ordering.
2015-06-10 11:59:23 +02:00
// Do not translate _(...) -help-debug options, Many technical terms, and only a very small audience, so is unnecessary stress to translators.
2017-01-27 09:43:41 +01:00
std : : string strUsage = HelpMessageGroup ( _ ( " Options: " ) ) ;
2016-01-04 18:50:17 +01:00
strUsage + = HelpMessageOpt ( " -? " , _ ( " Print this help message and exit " ) ) ;
strUsage + = HelpMessageOpt ( " -version " , _ ( " Print version and exit " ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -alertnotify=<cmd> " , _ ( " Execute command when a relevant alert is received or we see a really long fork (%s in cmd is replaced by message) " ) ) ;
strUsage + = HelpMessageOpt ( " -blocknotify=<cmd> " , _ ( " Execute command when the best block changes (%s in cmd is replaced by block hash) " ) ) ;
2015-11-14 13:47:53 +01:00
if ( showDebug )
strUsage + = HelpMessageOpt ( " -blocksonly " , strprintf ( _ ( " Whether to operate in a blocks only mode (default: %u) " ) , DEFAULT_BLOCKSONLY ) ) ;
2015-11-28 15:04:35 +01:00
strUsage + = HelpMessageOpt ( " -assumevalid=<hex> " , strprintf ( _ ( " If this block is in the chain assume that it and its ancestors are valid and potentially skip their script verification (0 to verify all, default: %s, testnet: %s) " ) , defaultChainParams - > GetConsensus ( ) . defaultAssumeValid . GetHex ( ) , testnetChainParams - > GetConsensus ( ) . defaultAssumeValid . GetHex ( ) ) ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -conf=<file> " , strprintf ( _ ( " Specify configuration file (default: %s) " ) , BITCOIN_CONF_FILENAME ) ) ;
2014-06-17 09:13:52 +02:00
if ( mode = = HMM_BITCOIND )
2014-02-03 07:23:20 +01:00
{
2016-06-28 08:35:41 +02:00
# if HAVE_DECL_DAEMON
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -daemon " , _ ( " Run in the background as a daemon and accept commands " ) ) ;
2014-02-03 07:23:20 +01:00
# endif
}
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -datadir=<dir> " , _ ( " Specify data directory " ) ) ;
2017-04-19 18:34:30 +02:00
if ( showDebug ) {
strUsage + = HelpMessageOpt ( " -dbbatchsize " , strprintf ( " Maximum database write batch size in bytes (default: %u) " , nDefaultDbBatchSize ) ) ;
}
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -dbcache=<n> " , strprintf ( _ ( " Set database cache size in megabytes (%d to %d, default: %d) " ) , nMinDbCache , nMaxDbCache , nDefaultDbCache ) ) ;
2016-06-06 17:20:58 +02:00
if ( showDebug )
strUsage + = HelpMessageOpt ( " -feefilter " , strprintf ( " Tell other nodes to filter invs to us by our mempool min fee (default: %u) " , DEFAULT_FEEFILTER ) ) ;
2015-11-06 13:22:00 +01:00
strUsage + = HelpMessageOpt ( " -loadblock=<file> " , _ ( " Imports blocks from external blk000??.dat file on startup " ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -maxorphantx=<n> " , strprintf ( _ ( " Keep at most <n> unconnectable transactions in memory (default: %u) " ) , DEFAULT_MAX_ORPHAN_TRANSACTIONS ) ) ;
2015-10-02 23:19:55 +02:00
strUsage + = HelpMessageOpt ( " -maxmempool=<n> " , strprintf ( _ ( " Keep the transaction memory pool below <n> megabytes (default: %u) " ) , DEFAULT_MAX_MEMPOOL_SIZE ) ) ;
2015-10-02 23:43:30 +02:00
strUsage + = HelpMessageOpt ( " -mempoolexpiry=<n> " , strprintf ( _ ( " Do not keep transactions in the mempool longer than <n> hours (default: %u) " ) , DEFAULT_MEMPOOL_EXPIRY ) ) ;
2017-03-09 23:12:25 +01:00
strUsage + = HelpMessageOpt ( " -persistmempool " , strprintf ( _ ( " Whether to save the mempool on shutdown and load on restart (default: %u) " ) , DEFAULT_PERSIST_MEMPOOL ) ) ;
2017-01-09 20:38:06 +01:00
strUsage + = HelpMessageOpt ( " -blockreconstructionextratxn=<n> " , strprintf ( _ ( " Extra transactions to keep in memory for compact block reconstructions (default: %u) " ) , DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -par=<n> " , strprintf ( _ ( " Set the number of script verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d) " ) ,
2015-07-01 17:38:15 +02:00
- GetNumCores ( ) , MAX_SCRIPTCHECK_THREADS , DEFAULT_SCRIPTCHECK_THREADS ) ) ;
2014-09-20 10:56:25 +02:00
# ifndef WIN32
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -pid=<file> " , strprintf ( _ ( " Specify pid file (default: %s) " ) , BITCOIN_PID_FILENAME ) ) ;
2014-09-20 10:56:25 +02:00
# endif
2016-11-29 18:39:19 +01:00
strUsage + = HelpMessageOpt ( " -prune=<n> " , strprintf ( _ ( " Reduce storage requirements by enabling pruning (deleting) of old blocks. This allows the pruneblockchain RPC to be called to delete specific blocks, and enables automatic pruning of old blocks if a target size in MiB is provided. This mode is incompatible with -txindex and -rescan. "
2015-05-04 18:46:33 +02:00
" Warning: Reverting this setting requires re-downloading the entire blockchain. "
2016-11-29 18:39:19 +01:00
" (default: 0 = disable pruning blocks, 1 = allow manual pruning via RPC, >%u = automatically prune block files to stay under the specified target size in MiB) " ) , MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024 ) ) ;
2016-04-21 14:14:37 +02:00
strUsage + = HelpMessageOpt ( " -reindex-chainstate " , _ ( " Rebuild chain state from the currently indexed blocks " ) ) ;
strUsage + = HelpMessageOpt ( " -reindex " , _ ( " Rebuild chain state and block index from the blk*.dat files on disk " ) ) ;
2015-09-11 23:31:30 +02:00
# ifndef WIN32
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -sysperms " , _ ( " Create new files with system default permissions, instead of umask 077 (only effective with disabled wallet functionality) " ) ) ;
2014-06-04 13:16:07 +02:00
# endif
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -txindex " , strprintf ( _ ( " Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u) " ) , DEFAULT_TXINDEX ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageGroup ( _ ( " Connection options: " ) ) ;
strUsage + = HelpMessageOpt ( " -addnode=<ip> " , _ ( " Add a node to connect to and attempt to keep the connection open " ) ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -banscore=<n> " , strprintf ( _ ( " Threshold for disconnecting misbehaving peers (default: %u) " ) , DEFAULT_BANSCORE_THRESHOLD ) ) ;
strUsage + = HelpMessageOpt ( " -bantime=<n> " , strprintf ( _ ( " Number of seconds to keep misbehaving peers from reconnecting (default: %u) " ) , DEFAULT_MISBEHAVING_BANTIME ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -bind=<addr> " , _ ( " Bind to given address and always listen on it. Use [host]:port notation for IPv6 " ) ) ;
2017-03-26 15:13:20 +02:00
strUsage + = HelpMessageOpt ( " -connect=<ip> " , _ ( " Connect only to the specified node(s); -connect=0 disables automatic connections " ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -discover " , _ ( " Discover own IP addresses (default: 1 when listening and no -externalip or -proxy) " ) ) ;
2015-11-09 19:16:38 +01:00
strUsage + = HelpMessageOpt ( " -dns " , _ ( " Allow DNS lookups for -addnode, -seednode and -connect " ) + " " + strprintf ( _ ( " (default: %u) " ) , DEFAULT_NAME_LOOKUP ) ) ;
2017-03-26 15:13:20 +02:00
strUsage + = HelpMessageOpt ( " -dnsseed " , _ ( " Query for peer addresses via DNS lookup, if low on addresses (default: 1 unless -connect used) " ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -externalip=<ip> " , _ ( " Specify your own public address " ) ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -forcednsseed " , strprintf ( _ ( " Always query for peer addresses via DNS lookup (default: %u) " ) , DEFAULT_FORCEDNSSEED ) ) ;
2017-03-26 15:13:20 +02:00
strUsage + = HelpMessageOpt ( " -listen " , _ ( " Accept connections from outside (default: 1 if no -proxy or -connect) " ) ) ;
2015-08-25 20:12:08 +02:00
strUsage + = HelpMessageOpt ( " -listenonion " , strprintf ( _ ( " Automatically create Tor hidden service (default: %d) " ) , DEFAULT_LISTEN_ONION ) ) ;
2015-08-01 19:41:21 +02:00
strUsage + = HelpMessageOpt ( " -maxconnections=<n> " , strprintf ( _ ( " Maintain at most <n> connections to peers (default: %u) " ) , DEFAULT_MAX_PEER_CONNECTIONS ) ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -maxreceivebuffer=<n> " , strprintf ( _ ( " Maximum per-connection receive buffer, <n>*1000 bytes (default: %u) " ) , DEFAULT_MAXRECEIVEBUFFER ) ) ;
strUsage + = HelpMessageOpt ( " -maxsendbuffer=<n> " , strprintf ( _ ( " Maximum per-connection send buffer, <n>*1000 bytes (default: %u) " ) , DEFAULT_MAXSENDBUFFER ) ) ;
2016-03-29 16:40:00 +02:00
strUsage + = HelpMessageOpt ( " -maxtimeadjustment " , strprintf ( _ ( " Maximum allowed median peer time offset adjustment. Local perspective of time may be influenced by peers forward or backward by this amount. (default: %u seconds) " ) , DEFAULT_MAX_TIME_ADJUSTMENT ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -onion=<ip:port> " , strprintf ( _ ( " Use separate SOCKS5 proxy to reach peers via Tor hidden services (default: %s) " ) , " -proxy " ) ) ;
strUsage + = HelpMessageOpt ( " -onlynet=<net> " , _ ( " Only connect to nodes in network <net> (ipv4, ipv6 or onion) " ) ) ;
2015-11-09 19:16:38 +01:00
strUsage + = HelpMessageOpt ( " -permitbaremultisig " , strprintf ( _ ( " Relay non-P2SH multisig (default: %u) " ) , DEFAULT_PERMIT_BAREMULTISIG ) ) ;
2016-02-11 07:35:25 +01:00
strUsage + = HelpMessageOpt ( " -peerbloomfilters " , strprintf ( _ ( " Support filtering of blocks and transaction with bloom filters (default: %u) " ) , DEFAULT_PEERBLOOMFILTERS ) ) ;
2015-11-28 15:04:35 +01:00
strUsage + = HelpMessageOpt ( " -port=<port> " , strprintf ( _ ( " Listen for connections on <port> (default: %u or testnet: %u) " ) , defaultChainParams - > GetDefaultPort ( ) , testnetChainParams - > GetDefaultPort ( ) ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -proxy=<ip:port> " , _ ( " Connect through SOCKS5 proxy " ) ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -proxyrandomize " , strprintf ( _ ( " Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u) " ) , DEFAULT_PROXYRANDOMIZE ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -seednode=<ip> " , _ ( " Connect to a node to retrieve peer addresses, and disconnect " ) ) ;
strUsage + = HelpMessageOpt ( " -timeout=<n> " , strprintf ( _ ( " Specify connection timeout in milliseconds (minimum: 1, default: %d) " ) , DEFAULT_CONNECT_TIMEOUT ) ) ;
2015-08-25 20:12:08 +02:00
strUsage + = HelpMessageOpt ( " -torcontrol=<ip>:<port> " , strprintf ( _ ( " Tor control port to use if onion listening enabled (default: %s) " ) , DEFAULT_TOR_CONTROL ) ) ;
2015-09-08 17:48:45 +02:00
strUsage + = HelpMessageOpt ( " -torpassword=<pass> " , _ ( " Tor control port password (default: empty) " ) ) ;
2012-05-13 11:36:10 +02:00
# ifdef USE_UPNP
# if USE_UPNP
2015-06-01 14:37:21 +02:00
strUsage + = HelpMessageOpt ( " -upnp " , _ ( " Use UPnP to map the listening port (default: 1 when listening and no -proxy) " ) ) ;
2012-05-13 11:36:10 +02:00
# else
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -upnp " , strprintf ( _ ( " Use UPnP to map the listening port (default: %u) " ) , 0 ) ) ;
2013-10-11 23:09:59 +02:00
# endif
2012-05-13 11:36:10 +02:00
# endif
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -whitebind=<addr> " , _ ( " Bind to given address and whitelist peers connecting to it. Use [host]:port notation for IPv6 " ) ) ;
2016-11-30 17:19:08 +01:00
strUsage + = HelpMessageOpt ( " -whitelist=<IP address or network> " , _ ( " Whitelist peers connecting from the given IP address (e.g. 1.2.3.4) or CIDR notated network (e.g. 1.2.3.0/24). Can be specified multiple times. " ) +
2015-02-04 09:11:49 +01:00
" " + _ ( " Whitelisted peers cannot be DoS banned and their transactions are always relayed, even if they are already in the mempool, useful e.g. for a gateway " ) ) ;
2015-11-06 00:05:06 +01:00
strUsage + = HelpMessageOpt ( " -maxuploadtarget=<n> " , strprintf ( _ ( " Tries to keep outbound traffic under the given target (in MiB per 24h), 0 = no limit (default: %d) " ) , DEFAULT_MAX_UPLOAD_TARGET ) ) ;
2014-02-03 07:23:20 +01:00
# ifdef ENABLE_WALLET
2016-02-22 10:48:44 +01:00
strUsage + = CWallet : : GetWalletHelpString ( showDebug ) ;
2014-02-03 07:23:20 +01:00
# endif
2014-11-18 18:06:32 +01:00
# if ENABLE_ZMQ
strUsage + = HelpMessageGroup ( _ ( " ZeroMQ notification options: " ) ) ;
strUsage + = HelpMessageOpt ( " -zmqpubhashblock=<address> " , _ ( " Enable publish hash block in <address> " ) ) ;
2015-11-19 02:34:19 +01:00
strUsage + = HelpMessageOpt ( " -zmqpubhashtx=<address> " , _ ( " Enable publish hash transaction in <address> " ) ) ;
2014-11-18 18:06:32 +01:00
strUsage + = HelpMessageOpt ( " -zmqpubrawblock=<address> " , _ ( " Enable publish raw block in <address> " ) ) ;
2015-11-19 02:34:19 +01:00
strUsage + = HelpMessageOpt ( " -zmqpubrawtx=<address> " , _ ( " Enable publish raw transaction in <address> " ) ) ;
2014-11-18 18:06:32 +01:00
# endif
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageGroup ( _ ( " Debugging/Testing options: " ) ) ;
2016-01-04 18:50:17 +01:00
strUsage + = HelpMessageOpt ( " -uacomment=<cmt> " , _ ( " Append comment to the user agent string " ) ) ;
2015-06-10 11:59:23 +02:00
if ( showDebug )
2014-02-03 07:23:20 +01:00
{
2017-01-06 12:49:59 +01:00
strUsage + = HelpMessageOpt ( " -checkblocks=<n> " , strprintf ( _ ( " How many blocks to check at startup (default: %u, 0 = all) " ) , DEFAULT_CHECKBLOCKS ) ) ;
strUsage + = HelpMessageOpt ( " -checklevel=<n> " , strprintf ( _ ( " How thorough the block verification of -checkblocks is (0-4, default: %u) " ) , DEFAULT_CHECKLEVEL ) ) ;
2015-11-28 15:04:35 +01:00
strUsage + = HelpMessageOpt ( " -checkblockindex " , strprintf ( " Do a full consistency check for mapBlockIndex, setBlockIndexCandidates, chainActive and mapBlocksUnlinked occasionally. Also sets -checkmempool (default: %u) " , defaultChainParams - > DefaultConsistencyChecks ( ) ) ) ;
strUsage + = HelpMessageOpt ( " -checkmempool=<n> " , strprintf ( " Run checks every <n> transactions (default: %u) " , defaultChainParams - > DefaultConsistencyChecks ( ) ) ) ;
2015-11-09 19:16:38 +01:00
strUsage + = HelpMessageOpt ( " -checkpoints " , strprintf ( " Disable expensive verification for known chain history (default: %u) " , DEFAULT_CHECKPOINTS_ENABLED ) ) ;
strUsage + = HelpMessageOpt ( " -disablesafemode " , strprintf ( " Disable safemode, override a real safe mode event (default: %u) " , DEFAULT_DISABLE_SAFEMODE ) ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -testsafemode " , strprintf ( " Force safe mode (default: %u) " , DEFAULT_TESTSAFEMODE ) ) ;
2015-06-10 11:59:23 +02:00
strUsage + = HelpMessageOpt ( " -dropmessagestest=<n> " , " Randomly drop 1 of every <n> network messages " ) ;
strUsage + = HelpMessageOpt ( " -fuzzmessagestest=<n> " , " Randomly fuzz 1 of every <n> network messages " ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -stopafterblockimport " , strprintf ( " Stop running after importing blocks from disk (default: %u) " , DEFAULT_STOPAFTERBLOCKIMPORT ) ) ;
2017-04-27 21:25:13 +02:00
strUsage + = HelpMessageOpt ( " -stopatheight " , strprintf ( " Stop running after reaching the given height in the main chain (default: %u) " , DEFAULT_STOPATHEIGHT ) ) ;
2015-07-15 20:47:45 +02:00
strUsage + = HelpMessageOpt ( " -limitancestorcount=<n> " , strprintf ( " Do not accept transactions if number of in-mempool ancestors is <n> or more (default: %u) " , DEFAULT_ANCESTOR_LIMIT ) ) ;
strUsage + = HelpMessageOpt ( " -limitancestorsize=<n> " , strprintf ( " Do not accept transactions whose size with all in-mempool ancestors exceeds <n> kilobytes (default: %u) " , DEFAULT_ANCESTOR_SIZE_LIMIT ) ) ;
strUsage + = HelpMessageOpt ( " -limitdescendantcount=<n> " , strprintf ( " Do not accept transactions if any ancestor would have <n> or more in-mempool descendants (default: %u) " , DEFAULT_DESCENDANT_LIMIT ) ) ;
strUsage + = HelpMessageOpt ( " -limitdescendantsize=<n> " , strprintf ( " Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u). " , DEFAULT_DESCENDANT_SIZE_LIMIT ) ) ;
2017-05-05 05:21:34 +02:00
strUsage + = HelpMessageOpt ( " -vbparams=deployment:start:end " , " Use given start/end times for specified version bits deployment (regtest-only) " ) ;
2014-02-03 07:23:20 +01:00
}
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -debug=<category> " , strprintf ( _ ( " Output debugging information (default: %u, supplying <category> is optional) " ) , 0 ) + " . " +
2016-12-25 21:19:40 +01:00
_ ( " If <category> is not supplied or if <category> = 1, output all debugging information. " ) + " " + _ ( " <category> can be: " ) + " " + ListLogCategories ( ) + " . " ) ;
2017-04-03 01:56:03 +02:00
strUsage + = HelpMessageOpt ( " -debugexclude=<category> " , strprintf ( _ ( " Exclude debugging information for a category. Can be used in conjunction with -debug=1 to output debug logs for all categories except one or more specified categories. " ) ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -help-debug " , _ ( " Show all debugging options (usage: --help -help-debug) " ) ) ;
2015-11-09 19:16:38 +01:00
strUsage + = HelpMessageOpt ( " -logips " , strprintf ( _ ( " Include IP addresses in debug output (default: %u) " ) , DEFAULT_LOGIPS ) ) ;
strUsage + = HelpMessageOpt ( " -logtimestamps " , strprintf ( _ ( " Prepend debug output with timestamp (default: %u) " ) , DEFAULT_LOGTIMESTAMPS ) ) ;
2015-06-10 11:59:23 +02:00
if ( showDebug )
2013-10-08 12:09:40 +02:00
{
2015-10-23 19:07:36 +02:00
strUsage + = HelpMessageOpt ( " -logtimemicros " , strprintf ( " Add microsecond precision to debug timestamps (default: %u) " , DEFAULT_LOGTIMEMICROS ) ) ;
2016-01-09 13:59:25 +01:00
strUsage + = HelpMessageOpt ( " -mocktime=<n> " , " Replace actual time with <n> seconds since epoch (default: 0) " ) ;
2017-04-21 22:38:12 +02:00
strUsage + = HelpMessageOpt ( " -maxsigcachesize=<n> " , strprintf ( " Limit sum of signature cache and script execution cache sizes to <n> MiB (default: %u) " , DEFAULT_MAX_SIG_CACHE_SIZE ) ) ;
2015-12-14 13:23:45 +01:00
strUsage + = HelpMessageOpt ( " -maxtipage=<n> " , strprintf ( " Maximum tip age in seconds to consider node in initial block download (default: %u) " , DEFAULT_MAX_TIP_AGE ) ) ;
2013-10-08 12:09:40 +02:00
}
2015-11-23 20:32:36 +01:00
strUsage + = HelpMessageOpt ( " -maxtxfee=<amt> " , strprintf ( _ ( " Maximum total fees (in %s) to use in a single wallet transaction or raw transaction; setting this too low may abort large transactions (default: %s) " ) ,
CURRENCY_UNIT , FormatMoney ( DEFAULT_TRANSACTION_MAXFEE ) ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -printtoconsole " , _ ( " Send trace/debug info to console instead of debug.log file " ) ) ;
2015-06-10 11:59:23 +02:00
if ( showDebug )
2013-10-08 12:09:40 +02:00
{
2017-01-19 20:59:28 +01:00
strUsage + = HelpMessageOpt ( " -printpriority " , strprintf ( " Log transaction fee per kB when mining blocks (default: %u) " , DEFAULT_PRINTPRIORITY ) ) ;
2013-10-08 12:09:40 +02:00
}
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -shrinkdebugfile " , _ ( " Shrink debug.log file on client startup (default: 1 when no -debug) " ) ) ;
2015-05-25 09:00:17 +02:00
AppendParamsHelpMessages ( strUsage , showDebug ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageGroup ( _ ( " Node relay options: " ) ) ;
2016-12-13 19:43:35 +01:00
if ( showDebug ) {
2015-11-28 15:04:35 +01:00
strUsage + = HelpMessageOpt ( " -acceptnonstdtxn " , strprintf ( " Relay and mine \" non-standard \" transactions (%sdefault: %u) " , " testnet/regtest only; " , defaultChainParams - > RequireStandard ( ) ) ) ;
2016-12-13 19:43:35 +01:00
strUsage + = HelpMessageOpt ( " -incrementalrelayfee=<amt> " , strprintf ( " Fee rate (in %s/kB) used to define cost of relay, used for mempool limiting and BIP 125 replacement. (default: %s) " , CURRENCY_UNIT , FormatMoney ( DEFAULT_INCREMENTAL_RELAY_FEE ) ) ) ;
2017-07-17 12:52:09 +02:00
strUsage + = HelpMessageOpt ( " -dustrelayfee=<amt> " , strprintf ( " Fee rate (in %s/kB) used to defined dust, the value of an output such that it will cost more than its value in fees at this fee rate to spend it. (default: %s) " , CURRENCY_UNIT , FormatMoney ( DUST_RELAY_TX_FEE ) ) ) ;
2016-12-13 19:43:35 +01:00
}
2016-07-18 20:57:20 +02:00
strUsage + = HelpMessageOpt ( " -bytespersigop " , strprintf ( _ ( " Equivalent bytes per sigop in transactions for relay and mining (default: %u) " ) , DEFAULT_BYTES_PER_SIGOP ) ) ;
2015-11-09 19:16:38 +01:00
strUsage + = HelpMessageOpt ( " -datacarrier " , strprintf ( _ ( " Relay and mine data carrier transactions (default: %u) " ) , DEFAULT_ACCEPT_DATACARRIER ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -datacarriersize " , strprintf ( _ ( " Maximum size of data in data carrier transactions we relay and mine (default: %u) " ) , MAX_OP_RETURN_RELAY ) ) ;
2016-02-01 20:30:37 +01:00
strUsage + = HelpMessageOpt ( " -mempoolreplacement " , strprintf ( _ ( " Enable transaction replacement in the memory pool (default: %u) " ) , DEFAULT_ENABLE_REPLACEMENT ) ) ;
2017-03-26 21:00:38 +02:00
strUsage + = HelpMessageOpt ( " -minrelaytxfee=<amt> " , strprintf ( _ ( " Fees (in %s/kB) smaller than this are considered zero fee for relaying, mining and transaction creation (default: %s) " ) ,
CURRENCY_UNIT , FormatMoney ( DEFAULT_MIN_RELAY_TX_FEE ) ) ) ;
strUsage + = HelpMessageOpt ( " -whitelistrelay " , strprintf ( _ ( " Accept relayed transactions received from whitelisted peers even when not relaying transactions (default: %d) " ) , DEFAULT_WHITELISTRELAY ) ) ;
strUsage + = HelpMessageOpt ( " -whitelistforcerelay " , strprintf ( _ ( " Force relay of transactions from whitelisted peers even if they violate local relay policy (default: %d) " ) , DEFAULT_WHITELISTFORCERELAY ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageGroup ( _ ( " Block creation options: " ) ) ;
2016-07-18 19:28:26 +02:00
strUsage + = HelpMessageOpt ( " -blockmaxweight=<n> " , strprintf ( _ ( " Set maximum BIP141 block weight (default: %d) " ) , DEFAULT_BLOCK_MAX_WEIGHT ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -blockmaxsize=<n> " , strprintf ( _ ( " Set maximum block size in bytes (default: %d) " ) , DEFAULT_BLOCK_MAX_SIZE ) ) ;
2016-12-13 17:51:51 +01:00
strUsage + = HelpMessageOpt ( " -blockmintxfee=<amt> " , strprintf ( _ ( " Set lowest fee rate (in %s/kB) for transactions to be included in block creation. (default: %s) " ) , CURRENCY_UNIT , FormatMoney ( DEFAULT_BLOCK_MIN_TX_FEE ) ) ) ;
2015-06-10 11:59:23 +02:00
if ( showDebug )
2016-02-15 05:13:27 +01:00
strUsage + = HelpMessageOpt ( " -blockversion=<n> " , " Override block version to test forking scenarios " ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageGroup ( _ ( " RPC server options: " ) ) ;
strUsage + = HelpMessageOpt ( " -server " , _ ( " Accept command line and JSON-RPC commands " ) ) ;
2015-06-27 21:21:41 +02:00
strUsage + = HelpMessageOpt ( " -rest " , strprintf ( _ ( " Accept public REST requests (default: %u) " ) , DEFAULT_REST_ENABLE ) ) ;
2017-03-03 12:34:15 +01:00
strUsage + = HelpMessageOpt ( " -rpcbind=<addr>[:port] " , _ ( " Bind to given address to listen for JSON-RPC connections. This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -rpcport. Use [host]:port notation for IPv6. This option can be specified multiple times (default: 127.0.0.1 and ::1 i.e., localhost, or if -rpcallowip has been specified, 0.0.0.0 and :: i.e., all addresses) " ) ) ;
2016-01-04 18:50:17 +01:00
strUsage + = HelpMessageOpt ( " -rpccookiefile=<loc> " , _ ( " Location of the auth cookie (default: data dir) " ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -rpcuser=<user> " , _ ( " Username for JSON-RPC connections " ) ) ;
strUsage + = HelpMessageOpt ( " -rpcpassword=<pw> " , _ ( " Password for JSON-RPC connections " ) ) ;
2016-12-21 14:45:59 +01:00
strUsage + = HelpMessageOpt ( " -rpcauth=<userpw> " , _ ( " Username and hashed password for JSON-RPC connections. The field <userpw> comes in the format: <USERNAME>:<SALT>$<HASH>. A canonical python script is included in share/rpcuser. The client then connects normally using the rpcuser=<USERNAME>/rpcpassword=<PASSWORD> pair of arguments. This option can be specified multiple times " ) ) ;
2015-05-22 03:50:01 +02:00
strUsage + = HelpMessageOpt ( " -rpcport=<port> " , strprintf ( _ ( " Listen for JSON-RPC connections on <port> (default: %u or testnet: %u) " ) , defaultBaseParams - > RPCPort ( ) , testnetBaseParams - > RPCPort ( ) ) ) ;
2015-02-04 09:11:49 +01:00
strUsage + = HelpMessageOpt ( " -rpcallowip=<ip> " , _ ( " Allow JSON-RPC connections from specified source. Valid for <ip> are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24). This option can be specified multiple times " ) ) ;
2017-03-26 16:25:17 +02:00
strUsage + = HelpMessageOpt ( " -rpcserialversion " , strprintf ( _ ( " Sets the serialization of raw transaction or block hex returned in non-verbose mode, non-segwit(0) or segwit(1) (default: %d) " ) , DEFAULT_RPC_SERIALIZE_VERSION ) ) ;
2015-08-28 17:14:51 +02:00
strUsage + = HelpMessageOpt ( " -rpcthreads=<n> " , strprintf ( _ ( " Set the number of threads to service RPC calls (default: %d) " ) , DEFAULT_HTTP_THREADS ) ) ;
if ( showDebug ) {
strUsage + = HelpMessageOpt ( " -rpcworkqueue=<n> " , strprintf ( " Set the depth of the work queue to service RPC calls (default: %d) " , DEFAULT_HTTP_WORKQUEUE ) ) ;
2015-09-18 15:45:38 +02:00
strUsage + = HelpMessageOpt ( " -rpcservertimeout=<n> " , strprintf ( " Timeout during HTTP requests (default: %d) " , DEFAULT_HTTP_SERVER_TIMEOUT ) ) ;
2015-08-28 17:14:51 +02:00
}
2012-05-13 11:36:10 +02:00
return strUsage ;
}
2014-06-10 16:02:46 +02:00
std : : string LicenseInfo ( )
{
2016-06-16 10:10:03 +02:00
const std : : string URL_SOURCE_CODE = " <https://github.com/bitcoin/bitcoin> " ;
const std : : string URL_WEBSITE = " <https://bitcoincore.org> " ;
2016-06-12 05:11:24 +02:00
2016-02-03 06:38:27 +01:00
return CopyrightHolders ( strprintf ( _ ( " Copyright (C) %i-%i " ) , 2009 , COPYRIGHT_YEAR ) + " " ) + " \n " +
2014-06-10 16:02:46 +02:00
" \n " +
2016-06-16 10:10:03 +02:00
strprintf ( _ ( " Please contribute if you find %s useful. "
" Visit %s for further information about the software. " ) ,
PACKAGE_NAME , URL_WEBSITE ) +
" \n " +
strprintf ( _ ( " The source code is available from %s. " ) ,
URL_SOURCE_CODE ) +
2014-06-10 16:02:46 +02:00
" \n " +
2016-06-16 10:10:03 +02:00
" \n " +
_ ( " This is experimental software. " ) + " \n " +
2016-06-12 05:11:24 +02:00
strprintf ( _ ( " Distributed under the MIT software license, see the accompanying file %s or %s " ) , " COPYING " , " <https://opensource.org/licenses/MIT> " ) + " \n " +
2014-06-10 16:02:46 +02:00
" \n " +
2016-06-12 05:11:24 +02:00
strprintf ( _ ( " This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit %s and cryptographic software written by Eric Young and UPnP software written by Thomas Bernard. " ) , " <https://www.openssl.org> " ) +
2014-06-10 16:02:46 +02:00
" \n " ;
}
2015-11-26 15:48:26 +01:00
static void BlockNotifyCallback ( bool initialSync , const CBlockIndex * pBlockIndex )
2014-08-14 18:32:34 +02:00
{
2015-11-26 15:48:26 +01:00
if ( initialSync | | ! pBlockIndex )
return ;
2017-08-01 21:17:40 +02:00
std : : string strCmd = gArgs . GetArg ( " -blocknotify " , " " ) ;
2014-08-14 18:32:34 +02:00
2015-11-26 15:48:26 +01:00
boost : : replace_all ( strCmd , " %s " , pBlockIndex - > GetBlockHash ( ) . GetHex ( ) ) ;
2014-08-14 18:32:34 +02:00
boost : : thread t ( runCommand , strCmd ) ; // thread runs free
}
2016-07-22 15:57:25 +02:00
static bool fHaveGenesis = false ;
static boost : : mutex cs_GenesisWait ;
static CConditionVariable condvar_GenesisWait ;
static void BlockNotifyGenesisWait ( bool , const CBlockIndex * pBlockIndex )
{
2017-08-07 07:36:37 +02:00
if ( pBlockIndex ! = nullptr ) {
2016-07-22 15:57:25 +02:00
{
boost : : unique_lock < boost : : mutex > lock_GenesisWait ( cs_GenesisWait ) ;
fHaveGenesis = true ;
}
condvar_GenesisWait . notify_all ( ) ;
}
}
2012-10-22 22:45:26 +02:00
struct CImportingNow
{
CImportingNow ( ) {
assert ( fImporting = = false ) ;
fImporting = true ;
}
~ CImportingNow ( ) {
assert ( fImporting = = true ) ;
fImporting = false ;
}
} ;
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
// If we're using -prune with -reindex, then delete block files that will be ignored by the
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
2015-06-02 21:24:53 +02:00
// is missing, do the same here to delete any later block files after a gap. Also delete all
// rev files since they'll be rewritten by the reindex anyway. This ensures that vinfoBlockFile
// is in sync with what's actually on disk by the time we start downloading, so that pruning
// works correctly.
void CleanupBlockRevFiles ( )
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
{
2017-03-01 17:05:50 +01:00
std : : map < std : : string , fs : : path > mapBlockFiles ;
2015-06-02 21:24:53 +02:00
// Glob all blk?????.dat and rev?????.dat files from the blocks directory.
// Remove the rev files immediately and insert the blk file paths into an
// ordered map keyed by block file index.
LogPrintf ( " Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune \n " ) ;
2017-03-01 17:05:50 +01:00
fs : : path blocksdir = GetDataDir ( ) / " blocks " ;
for ( fs : : directory_iterator it ( blocksdir ) ; it ! = fs : : directory_iterator ( ) ; it + + ) {
2015-06-02 21:24:53 +02:00
if ( is_regular_file ( * it ) & &
it - > path ( ) . filename ( ) . string ( ) . length ( ) = = 12 & &
it - > path ( ) . filename ( ) . string ( ) . substr ( 8 , 4 ) = = " .dat " )
{
if ( it - > path ( ) . filename ( ) . string ( ) . substr ( 0 , 3 ) = = " blk " )
mapBlockFiles [ it - > path ( ) . filename ( ) . string ( ) . substr ( 3 , 5 ) ] = it - > path ( ) ;
else if ( it - > path ( ) . filename ( ) . string ( ) . substr ( 0 , 3 ) = = " rev " )
remove ( it - > path ( ) ) ;
}
}
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
2015-06-02 21:24:53 +02:00
// Remove all block files that aren't part of a contiguous set starting at
// zero by walking the ordered map (keys are block file indices) by
// keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
// start removing block files.
int nContigCounter = 0 ;
2017-06-02 03:28:42 +02:00
for ( const std : : pair < std : : string , fs : : path > & item : mapBlockFiles ) {
2015-06-02 21:24:53 +02:00
if ( atoi ( item . first ) = = nContigCounter ) {
nContigCounter + + ;
continue ;
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
}
2015-06-02 21:24:53 +02:00
remove ( item . second ) ;
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
}
}
2017-03-01 17:05:50 +01:00
void ThreadImport ( std : : vector < fs : : path > vImportFiles )
2013-03-07 04:31:26 +01:00
{
2015-04-17 14:40:24 +02:00
const CChainParams & chainparams = Params ( ) ;
2012-10-22 22:45:26 +02:00
RenameThread ( " bitcoin-loadblk " ) ;
2016-11-04 19:34:36 +01:00
{
2016-04-20 15:45:41 +02:00
CImportingNow imp ;
2012-10-21 21:23:13 +02:00
// -reindex
if ( fReindex ) {
int nFile = 0 ;
2013-03-07 04:31:26 +01:00
while ( true ) {
2012-12-03 10:14:54 +01:00
CDiskBlockPos pos ( nFile , 0 ) ;
2017-03-01 17:05:50 +01:00
if ( ! fs : : exists ( GetBlockPosFilename ( pos , " blk " ) ) )
2014-09-08 19:29:14 +02:00
break ; // No block files left to reindex
2012-10-21 21:23:13 +02:00
FILE * file = OpenBlockFile ( pos , true ) ;
if ( ! file )
2014-09-08 19:29:14 +02:00
break ; // This error is logged in OpenBlockFile
2013-09-18 12:38:08 +02:00
LogPrintf ( " Reindexing block file blk%05u.dat... \n " , ( unsigned int ) nFile ) ;
2015-04-17 14:40:24 +02:00
LoadExternalBlockFile ( chainparams , file , & pos ) ;
2012-10-21 21:23:13 +02:00
nFile + + ;
}
2013-03-07 04:31:26 +01:00
pblocktree - > WriteReindexing ( false ) ;
fReindex = false ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " Reindexing finished \n " ) ;
2013-03-07 04:31:26 +01:00
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
2017-07-07 01:57:20 +02:00
LoadGenesisBlock ( chainparams ) ;
2012-10-22 22:45:26 +02:00
}
// hardcoded $DATADIR/bootstrap.dat
2017-03-01 17:05:50 +01:00
fs : : path pathBootstrap = GetDataDir ( ) / " bootstrap.dat " ;
if ( fs : : exists ( pathBootstrap ) ) {
2017-03-01 17:28:39 +01:00
FILE * file = fsbridge : : fopen ( pathBootstrap , " rb " ) ;
2012-10-22 22:45:26 +02:00
if ( file ) {
2017-03-01 17:05:50 +01:00
fs : : path pathBootstrapOld = GetDataDir ( ) / " bootstrap.dat.old " ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " Importing bootstrap.dat... \n " ) ;
2015-04-17 14:40:24 +02:00
LoadExternalBlockFile ( chainparams , file ) ;
2012-10-22 22:45:26 +02:00
RenameOver ( pathBootstrap , pathBootstrapOld ) ;
2014-02-08 11:35:02 +01:00
} else {
LogPrintf ( " Warning: Could not open bootstrap file %s \n " , pathBootstrap . string ( ) ) ;
2012-10-22 22:45:26 +02:00
}
}
2012-10-21 21:23:13 +02:00
// -loadblock=
2017-06-02 03:18:57 +02:00
for ( const fs : : path & path : vImportFiles ) {
2017-03-01 17:28:39 +01:00
FILE * file = fsbridge : : fopen ( path , " rb " ) ;
2012-10-21 21:23:13 +02:00
if ( file ) {
2014-02-08 11:35:02 +01:00
LogPrintf ( " Importing blocks file %s... \n " , path . string ( ) ) ;
2015-04-17 14:40:24 +02:00
LoadExternalBlockFile ( chainparams , file ) ;
2014-02-08 11:35:02 +01:00
} else {
LogPrintf ( " Warning: Could not open blocks file %s \n " , path . string ( ) ) ;
2012-10-21 21:23:13 +02:00
}
}
2014-05-07 20:10:48 +02:00
2016-04-20 15:45:41 +02:00
// scan for better chains in the block chain database, that are not yet connected in the active best chain
CValidationState state ;
if ( ! ActivateBestChain ( state , chainparams ) ) {
LogPrintf ( " Failed to connect best block " ) ;
StartShutdown ( ) ;
}
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -stopafterblockimport " , DEFAULT_STOPAFTERBLOCKIMPORT ) ) {
2014-05-07 20:10:48 +02:00
LogPrintf ( " Stopping after block import \n " ) ;
StartShutdown ( ) ;
}
2016-11-04 19:34:36 +01:00
} // End scope of CImportingNow
2017-08-01 21:17:40 +02:00
if ( gArgs . GetArg ( " -persistmempool " , DEFAULT_PERSIST_MEMPOOL ) ) {
2017-03-09 23:12:25 +01:00
LoadMempool ( ) ;
fDumpMempoolLater = ! fRequestShutdown ;
}
2012-10-22 22:45:26 +02:00
}
2014-06-03 01:21:03 +02:00
/** Sanity checks
* Ensure that Bitcoin is running in a usable environment with all
* necessary library support .
*/
bool InitSanityCheck ( void )
{
if ( ! ECC_InitSanityCheck ( ) ) {
2015-07-28 20:11:20 +02:00
InitError ( " Elliptic curve cryptography sanity check failure. Aborting. " ) ;
2014-06-03 01:21:03 +02:00
return false ;
}
2017-02-22 08:02:50 +01:00
2014-06-14 01:23:01 +02:00
if ( ! glibc_sanity_test ( ) | | ! glibcxx_sanity_test ( ) )
return false ;
2014-06-03 01:21:03 +02:00
2017-02-22 08:02:50 +01:00
if ( ! Random_SanityCheck ( ) ) {
InitError ( " OS cryptographic RNG sanity check failure. Aborting. " ) ;
return false ;
}
2014-06-03 01:21:03 +02:00
return true ;
}
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
bool AppInitServers ( boost : : thread_group & threadGroup )
{
2016-09-02 03:55:21 +02:00
RPCServer : : OnStarted ( & OnRPCStarted ) ;
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
RPCServer : : OnStopped ( & OnRPCStopped ) ;
RPCServer : : OnPreCommand ( & OnRPCPreCommand ) ;
2015-08-28 16:55:16 +02:00
if ( ! InitHTTPServer ( ) )
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
return false ;
if ( ! StartRPC ( ) )
return false ;
if ( ! StartHTTPRPC ( ) )
return false ;
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -rest " , DEFAULT_REST_ENABLE ) & & ! StartREST ( ) )
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
return false ;
2015-11-11 17:34:10 +01:00
if ( ! StartHTTPServer ( ) )
2015-08-28 16:55:16 +02:00
return false ;
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
return true ;
}
2015-10-08 09:58:31 +02:00
// Parameter interaction based on rules
void InitParameterInteraction ( )
{
// when specifying an explicit binding address, you want to listen on it
// even when -connect or -proxy is specified
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -bind " ) ) {
if ( gArgs . SoftSetBoolArg ( " -listen " , true ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -bind set -> setting -listen=1 \n " , __func__ ) ;
}
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -whitebind " ) ) {
if ( gArgs . SoftSetBoolArg ( " -listen " , true ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -whitebind set -> setting -listen=1 \n " , __func__ ) ;
}
2017-05-07 19:37:54 +02:00
if ( gArgs . IsArgSet ( " -connect " ) ) {
2015-10-08 09:58:31 +02:00
// when only connecting to trusted nodes, do not seed via DNS, or listen by default
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -dnsseed " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -connect set -> setting -dnsseed=0 \n " , __func__ ) ;
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -listen " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -connect set -> setting -listen=0 \n " , __func__ ) ;
}
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -proxy " ) ) {
2015-10-08 09:58:31 +02:00
// to protect privacy, do not listen by default if a default proxy server is specified
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -listen " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -proxy set -> setting -listen=0 \n " , __func__ ) ;
// to protect privacy, do not use UPNP when a proxy is set. The user may still specify -listen=1
// to listen locally, so don't rely on this happening through -listen below.
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -upnp " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -proxy set -> setting -upnp=0 \n " , __func__ ) ;
// to protect privacy, do not discover addresses by default
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -discover " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -proxy set -> setting -discover=0 \n " , __func__ ) ;
}
2017-08-01 21:17:40 +02:00
if ( ! gArgs . GetBoolArg ( " -listen " , DEFAULT_LISTEN ) ) {
2015-10-08 09:58:31 +02:00
// do not map ports or try to retrieve public IP when not listening (pointless)
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -upnp " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -listen=0 -> setting -upnp=0 \n " , __func__ ) ;
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -discover " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -listen=0 -> setting -discover=0 \n " , __func__ ) ;
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -listenonion " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -listen=0 -> setting -listenonion=0 \n " , __func__ ) ;
}
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -externalip " ) ) {
2015-10-08 09:58:31 +02:00
// if an explicit public IP is specified, do not try to find others
2017-08-01 21:17:40 +02:00
if ( gArgs . SoftSetBoolArg ( " -discover " , false ) )
2015-10-08 09:58:31 +02:00
LogPrintf ( " %s: parameter interaction: -externalip set -> setting -discover=0 \n " , __func__ ) ;
}
2016-11-12 10:23:57 +01:00
// disable whitelistrelay in blocksonly mode
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -blocksonly " , DEFAULT_BLOCKSONLY ) ) {
if ( gArgs . SoftSetBoolArg ( " -whitelistrelay " , false ) )
2015-11-26 00:00:23 +01:00
LogPrintf ( " %s: parameter interaction: -blocksonly=1 -> setting -whitelistrelay=0 \n " , __func__ ) ;
2015-10-08 10:01:29 +02:00
}
2015-11-26 00:00:23 +01:00
// Forcing relay from whitelisted hosts implies we will accept relays from them in the first place.
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -whitelistforcerelay " , DEFAULT_WHITELISTFORCERELAY ) ) {
if ( gArgs . SoftSetBoolArg ( " -whitelistrelay " , true ) )
2015-11-26 00:00:23 +01:00
LogPrintf ( " %s: parameter interaction: -whitelistforcerelay=1 -> setting -whitelistrelay=1 \n " , __func__ ) ;
}
2015-10-08 09:58:31 +02:00
}
2015-12-26 09:01:55 +01:00
static std : : string ResolveErrMsg ( const char * const optname , const std : : string & strBind )
{
return strprintf ( _ ( " Cannot resolve -%s address: '%s' " ) , optname , strBind ) ;
}
2015-11-26 14:03:27 +01:00
void InitLogging ( )
{
2017-08-01 21:17:40 +02:00
fPrintToConsole = gArgs . GetBoolArg ( " -printtoconsole " , false ) ;
fLogTimestamps = gArgs . GetBoolArg ( " -logtimestamps " , DEFAULT_LOGTIMESTAMPS ) ;
fLogTimeMicros = gArgs . GetBoolArg ( " -logtimemicros " , DEFAULT_LOGTIMEMICROS ) ;
fLogIPs = gArgs . GetBoolArg ( " -logips " , DEFAULT_LOGIPS ) ;
2015-11-26 14:03:27 +01:00
LogPrintf ( " \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n " ) ;
2016-06-09 11:09:21 +02:00
LogPrintf ( " Bitcoin version %s \n " , FormatFullVersion ( ) ) ;
2015-11-26 14:03:27 +01:00
}
2016-10-25 11:34:27 +02:00
namespace { // Variables internal to initialization process only
ServiceFlags nRelevantServices = NODE_NETWORK ;
int nMaxConnections ;
int nUserMaxConnections ;
int nFD ;
ServiceFlags nLocalServices = NODE_NETWORK ;
2017-05-31 22:21:25 +02:00
} // namespace
2016-10-25 11:34:27 +02:00
2017-02-25 00:20:03 +01:00
[[noreturn]] static void new_handler_terminate ( )
{
// Rather than throwing std::bad-alloc if allocation fails, terminate
// immediately to (try to) avoid chain corruption.
// Since LogPrintf may itself allocate memory, set the handler directly
// to terminate first.
std : : set_new_handler ( std : : terminate ) ;
LogPrintf ( " Error: Out of memory. Terminating. \n " ) ;
// The log was successful, terminate now.
std : : terminate ( ) ;
} ;
2016-10-25 11:34:27 +02:00
bool AppInitBasicSetup ( )
2011-05-14 20:10:21 +02:00
{
2012-05-21 16:47:29 +02:00
// ********************************************************* Step 1: setup
2011-05-14 20:10:21 +02:00
# ifdef _MSC_VER
2012-07-26 02:48:39 +02:00
// Turn off Microsoft heap dump noise
2011-05-14 20:10:21 +02:00
_CrtSetReportMode ( _CRT_WARN , _CRTDBG_MODE_FILE ) ;
2017-08-07 07:36:37 +02:00
_CrtSetReportFile ( _CRT_WARN , CreateFileA ( " NUL " , GENERIC_WRITE , 0 , nullptr , OPEN_EXISTING , 0 , 0 ) ) ;
2012-07-26 02:48:39 +02:00
// Disable confusing "helpful" text message on abort, Ctrl-C
2011-05-14 20:10:21 +02:00
_set_abort_behavior ( 0 , _WRITE_ABORT_MSG | _CALL_REPORTFAULT ) ;
# endif
2012-07-20 08:45:49 +02:00
# ifdef WIN32
// Enable Data Execution Prevention (DEP)
// Minimum supported OS versions: WinXP SP3, WinVista >= SP1, Win Server 2008
// A failure is non-critical and needs no further attention!
# ifndef PROCESS_DEP_ENABLE
2013-10-01 09:47:16 +02:00
// We define this here, because GCCs winbase.h limits this to _WIN32_WINNT >= 0x0601 (Windows 7),
// which is not correct. Can be removed, when GCCs winbase.h is fixed!
2012-07-20 08:45:49 +02:00
# define PROCESS_DEP_ENABLE 0x00000001
# endif
typedef BOOL ( WINAPI * PSETPROCDEPPOL ) ( DWORD ) ;
PSETPROCDEPPOL setProcDEPPol = ( PSETPROCDEPPOL ) GetProcAddress ( GetModuleHandleA ( " Kernel32.dll " ) , " SetProcessDEPPolicy " ) ;
2017-08-07 07:36:37 +02:00
if ( setProcDEPPol ! = nullptr ) setProcDEPPol ( PROCESS_DEP_ENABLE ) ;
2011-05-14 20:10:21 +02:00
# endif
2014-06-04 13:16:07 +02:00
2015-09-02 16:18:16 +02:00
if ( ! SetupNetworking ( ) )
2015-10-22 18:09:19 +02:00
return InitError ( " Initializing networking failed " ) ;
2015-09-02 16:18:16 +02:00
# ifndef WIN32
2017-08-01 21:17:40 +02:00
if ( ! gArgs . GetBoolArg ( " -sysperms " , false ) ) {
2014-06-04 13:16:07 +02:00
umask ( 077 ) ;
}
2012-07-20 08:45:49 +02:00
2011-05-14 20:10:21 +02:00
// Clean shutdown on SIGTERM
2017-03-22 21:29:41 +01:00
registerSignalHandler ( SIGTERM , HandleSIGTERM ) ;
registerSignalHandler ( SIGINT , HandleSIGTERM ) ;
2012-03-02 20:31:16 +01:00
// Reopen debug.log on SIGHUP
2017-03-22 21:29:41 +01:00
registerSignalHandler ( SIGHUP , HandleSIGHUP ) ;
2013-05-05 07:37:03 +02:00
2015-09-15 01:39:12 +02:00
// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
2013-05-05 07:37:03 +02:00
signal ( SIGPIPE , SIG_IGN ) ;
2011-05-14 20:10:21 +02:00
# endif
2017-02-25 00:20:03 +01:00
std : : set_new_handler ( new_handler_terminate ) ;
2016-10-25 11:34:27 +02:00
return true ;
}
2011-05-14 20:10:21 +02:00
2016-10-25 11:34:27 +02:00
bool AppInitParameterInteraction ( )
{
2015-04-09 15:58:34 +02:00
const CChainParams & chainparams = Params ( ) ;
2016-10-25 11:34:27 +02:00
// ********************************************************* Step 2: parameter interactions
2014-11-13 15:15:53 +01:00
2015-11-28 22:28:21 +01:00
// also see: InitParameterInteraction()
2014-02-14 17:33:07 +01:00
2016-09-19 15:52:48 +02:00
// if using block pruning, then disallow txindex
2017-08-01 21:17:40 +02:00
if ( gArgs . GetArg ( " -prune " , 0 ) ) {
if ( gArgs . GetBoolArg ( " -txindex " , DEFAULT_TXINDEX ) )
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
return InitError ( _ ( " Prune mode is incompatible with -txindex. " ) ) ;
}
2015-11-14 13:46:23 +01:00
2017-06-01 12:34:02 +02:00
// -bind and -whitebind can't be set when not listening
2017-03-29 17:51:41 +02:00
size_t nUserBind = gArgs . GetArgs ( " -bind " ) . size ( ) + gArgs . GetArgs ( " -whitebind " ) . size ( ) ;
2017-06-01 12:34:02 +02:00
if ( nUserBind ! = 0 & & ! gArgs . GetBoolArg ( " -listen " , DEFAULT_LISTEN ) ) {
return InitError ( " Cannot set -bind or -whitebind together with -listen=0 " ) ;
}
2014-11-16 12:19:23 +01:00
// Make sure enough file descriptors are available
2017-06-01 12:34:02 +02:00
int nBind = std : : max ( nUserBind , size_t ( 1 ) ) ;
2017-08-01 21:17:40 +02:00
nUserMaxConnections = gArgs . GetArg ( " -maxconnections " , DEFAULT_MAX_PEER_CONNECTIONS ) ;
2016-10-25 11:34:27 +02:00
nMaxConnections = std : : max ( nUserMaxConnections , 0 ) ;
2014-11-16 12:19:23 +01:00
// Trim requested connection counts, to fit into system limitations
2016-12-11 05:39:26 +01:00
nMaxConnections = std : : max ( std : : min ( nMaxConnections , ( int ) ( FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS ) ) , 0 ) ;
nFD = RaiseFileDescriptorLimit ( nMaxConnections + MIN_CORE_FILEDESCRIPTORS + MAX_ADDNODE_CONNECTIONS ) ;
2014-11-16 12:19:23 +01:00
if ( nFD < MIN_CORE_FILEDESCRIPTORS )
return InitError ( _ ( " Not enough file descriptors available. " ) ) ;
2016-12-11 05:39:26 +01:00
nMaxConnections = std : : min ( nFD - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS , nMaxConnections ) ;
2014-11-16 12:19:23 +01:00
if ( nMaxConnections < nUserMaxConnections )
InitWarning ( strprintf ( _ ( " Reducing -maxconnections from %d to %d, because of system limitations. " ) , nUserMaxConnections , nMaxConnections ) ) ;
2012-05-21 16:47:29 +02:00
// ********************************************************* Step 3: parameter-to-internal-flags
2017-05-07 19:37:54 +02:00
if ( gArgs . IsArgSet ( " -debug " ) ) {
2016-12-25 21:19:40 +01:00
// Special-case: if -debug=0/-nodebug is set, turn off debugging messages
2017-05-07 19:37:54 +02:00
const std : : vector < std : : string > categories = gArgs . GetArgs ( " -debug " ) ;
2016-12-25 21:19:40 +01:00
2017-04-05 01:18:12 +02:00
if ( find ( categories . begin ( ) , categories . end ( ) , std : : string ( " 0 " ) ) = = categories . end ( ) ) {
2016-12-25 21:19:40 +01:00
for ( const auto & cat : categories ) {
2017-04-04 20:59:35 +02:00
uint32_t flag = 0 ;
2016-12-25 21:19:40 +01:00
if ( ! GetLogCategory ( & flag , & cat ) ) {
2017-04-03 01:56:03 +02:00
InitWarning ( strprintf ( _ ( " Unsupported logging category %s=%s. " ) , " -debug " , cat ) ) ;
2017-04-04 20:59:35 +02:00
continue ;
2016-12-25 21:19:40 +01:00
}
logCategories | = flag ;
}
}
2016-11-30 01:50:49 +01:00
}
2013-10-08 12:09:40 +02:00
2017-04-03 01:56:03 +02:00
// Now remove the logging categories which were explicitly excluded
2017-03-29 17:51:41 +02:00
for ( const std : : string & cat : gArgs . GetArgs ( " -debugexclude " ) ) {
2017-06-27 03:10:39 +02:00
uint32_t flag = 0 ;
if ( ! GetLogCategory ( & flag , & cat ) ) {
InitWarning ( strprintf ( _ ( " Unsupported logging category %s=%s. " ) , " -debugexclude " , cat ) ) ;
continue ;
}
logCategories & = ~ flag ;
2017-04-03 01:56:03 +02:00
}
2014-06-27 14:41:11 +02:00
// Check for -debugnet
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -debugnet " , false ) )
2015-10-22 18:09:19 +02:00
InitWarning ( _ ( " Unsupported argument -debugnet ignored, use -debug=net. " ) ) ;
2014-06-16 08:48:32 +02:00
// Check for -socks - as this is a privacy risk to continue, exit here
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -socks " ) )
2015-10-22 18:09:19 +02:00
return InitError ( _ ( " Unsupported argument -socks found. Setting SOCKS version isn't possible anymore, only SOCKS5 proxies are supported. " ) ) ;
2014-05-05 20:32:56 +02:00
// Check for -tor - as this is a privacy risk to continue, exit here
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -tor " , false ) )
2015-10-22 18:09:19 +02:00
return InitError ( _ ( " Unsupported argument -tor found, use -onion. " ) ) ;
2013-10-08 12:09:40 +02:00
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -benchmark " , false ) )
2015-10-22 18:09:19 +02:00
InitWarning ( _ ( " Unsupported argument -benchmark ignored, use -debug=bench. " ) ) ;
2014-07-26 22:49:17 +02:00
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -whitelistalwaysrelay " , false ) )
2016-01-31 12:59:18 +01:00
InitWarning ( _ ( " Unsupported argument -whitelistalwaysrelay ignored, use -whitelistrelay and/or -whitelistforcerelay. " ) ) ;
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -blockminsize " ) )
2016-07-05 21:50:48 +02:00
InitWarning ( " Unsupported argument -blockminsize ignored. " ) ;
2015-03-13 17:25:34 +01:00
// Checkmempool and checkblockindex default to true in regtest mode
2017-08-01 21:17:40 +02:00
int ratio = std : : min < int > ( std : : max < int > ( gArgs . GetArg ( " -checkmempool " , chainparams . DefaultConsistencyChecks ( ) ? 1 : 0 ) , 0 ) , 1000000 ) ;
2015-10-07 23:34:55 +02:00
if ( ratio ! = 0 ) {
mempool . setSanityCheck ( 1.0 / ratio ) ;
}
2017-08-01 21:17:40 +02:00
fCheckBlockIndex = gArgs . GetBoolArg ( " -checkblockindex " , chainparams . DefaultConsistencyChecks ( ) ) ;
fCheckpointsEnabled = gArgs . GetBoolArg ( " -checkpoints " , DEFAULT_CHECKPOINTS_ENABLED ) ;
2012-06-22 19:11:57 +02:00
2017-08-01 21:17:40 +02:00
hashAssumeValid = uint256S ( gArgs . GetArg ( " -assumevalid " , chainparams . GetConsensus ( ) . defaultAssumeValid . GetHex ( ) ) ) ;
2017-01-06 12:49:59 +01:00
if ( ! hashAssumeValid . IsNull ( ) )
LogPrintf ( " Assuming ancestors of block %s have valid signatures. \n " , hashAssumeValid . GetHex ( ) ) ;
else
LogPrintf ( " Validating signatures for all blocks. \n " ) ;
2015-11-19 21:48:02 +01:00
// mempool limits
2017-08-01 21:17:40 +02:00
int64_t nMempoolSizeMax = gArgs . GetArg ( " -maxmempool " , DEFAULT_MAX_MEMPOOL_SIZE ) * 1000000 ;
int64_t nMempoolSizeMin = gArgs . GetArg ( " -limitdescendantsize " , DEFAULT_DESCENDANT_SIZE_LIMIT ) * 1000 * 40 ;
2015-11-19 21:48:02 +01:00
if ( nMempoolSizeMax < 0 | | nMempoolSizeMax < nMempoolSizeMin )
2015-11-27 16:44:30 +01:00
return InitError ( strprintf ( _ ( " -maxmempool must be at least %d MB " ) , std : : ceil ( nMempoolSizeMin / 1000000.0 ) ) ) ;
2017-02-06 15:16:18 +01:00
// incremental relay fee sets the minimum feerate increase necessary for BIP 125 replacement in the mempool
2016-12-13 19:43:35 +01:00
// and the amount the mempool min fee increases above the feerate of txs evicted due to mempool limiting.
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -incrementalrelayfee " ) )
2016-12-13 19:43:35 +01:00
{
CAmount n = 0 ;
2017-08-01 21:17:40 +02:00
if ( ! ParseMoney ( gArgs . GetArg ( " -incrementalrelayfee " , " " ) , n ) )
return InitError ( AmountErrMsg ( " incrementalrelayfee " , gArgs . GetArg ( " -incrementalrelayfee " , " " ) ) ) ;
2016-12-13 19:43:35 +01:00
incrementalRelayFee = CFeeRate ( n ) ;
}
2015-10-02 23:19:55 +02:00
2012-12-01 23:04:14 +01:00
// -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency
2017-08-01 21:17:40 +02:00
nScriptCheckThreads = gArgs . GetArg ( " -par " , DEFAULT_SCRIPTCHECK_THREADS ) ;
2013-04-29 19:35:47 +02:00
if ( nScriptCheckThreads < = 0 )
2015-07-01 17:38:15 +02:00
nScriptCheckThreads + = GetNumCores ( ) ;
2013-01-18 15:07:05 +01:00
if ( nScriptCheckThreads < = 1 )
2012-12-01 23:04:14 +01:00
nScriptCheckThreads = 0 ;
else if ( nScriptCheckThreads > MAX_SCRIPTCHECK_THREADS )
nScriptCheckThreads = MAX_SCRIPTCHECK_THREADS ;
2015-09-11 23:31:30 +02:00
// block pruning; get the amount of disk space (in MiB) to allot for block & undo files
2017-08-01 21:17:40 +02:00
int64_t nPruneArg = gArgs . GetArg ( " -prune " , 0 ) ;
2016-11-29 18:39:19 +01:00
if ( nPruneArg < 0 ) {
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
return InitError ( _ ( " Prune cannot be configured with a negative value. " ) ) ;
}
2016-11-29 18:39:19 +01:00
nPruneTarget = ( uint64_t ) nPruneArg * 1024 * 1024 ;
if ( nPruneArg = = 1 ) { // manual pruning: -prune=1
LogPrintf ( " Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files. \n " ) ;
nPruneTarget = std : : numeric_limits < uint64_t > : : max ( ) ;
fPruneMode = true ;
} else if ( nPruneTarget ) {
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
if ( nPruneTarget < MIN_DISK_SPACE_FOR_BLOCK_FILES ) {
2015-09-11 23:31:30 +02:00
return InitError ( strprintf ( _ ( " Prune configured below the minimum of %d MiB. Please use a higher number. " ) , MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024 ) ) ;
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
}
LogPrintf ( " Prune configured to target %uMiB on disk for block and undo files. \n " , nPruneTarget / 1024 / 1024 ) ;
fPruneMode = true ;
}
2016-03-29 19:43:02 +02:00
RegisterAllCoreRPCCommands ( tableRPC ) ;
2013-11-29 16:04:29 +01:00
# ifdef ENABLE_WALLET
2016-09-19 16:09:38 +02:00
RegisterWalletRPCCommands ( tableRPC ) ;
2013-11-29 16:04:29 +01:00
# endif
2011-05-14 20:10:21 +02:00
2017-08-01 21:17:40 +02:00
nConnectTimeout = gArgs . GetArg ( " -timeout " , DEFAULT_CONNECT_TIMEOUT ) ;
2014-09-25 09:01:54 +02:00
if ( nConnectTimeout < = 0 )
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT ;
2012-05-21 16:47:29 +02:00
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -minrelaytxfee " ) ) {
2014-04-23 00:46:19 +02:00
CAmount n = 0 ;
2017-08-01 21:17:40 +02:00
if ( ! ParseMoney ( gArgs . GetArg ( " -minrelaytxfee " , " " ) , n ) ) {
return InitError ( AmountErrMsg ( " minrelaytxfee " , gArgs . GetArg ( " -minrelaytxfee " , " " ) ) ) ;
2017-01-20 03:48:10 +01:00
}
2016-01-08 01:14:09 +01:00
// High fee check is done afterward in CWallet::ParameterInteraction()
: : minRelayTxFee = CFeeRate ( n ) ;
2016-12-13 19:43:35 +01:00
} else if ( incrementalRelayFee > : : minRelayTxFee ) {
// Allow only setting incrementalRelayFee to control both
: : minRelayTxFee = incrementalRelayFee ;
LogPrintf ( " Increasing minrelaytxfee to %s to match incrementalrelayfee \n " , : : minRelayTxFee . ToString ( ) ) ;
2013-04-26 02:11:27 +02:00
}
2012-05-21 16:47:29 +02:00
2016-12-13 17:51:51 +01:00
// Sanity check argument for min fee for including tx in block
// TODO: Harmonize which arguments need sanity checking and where that happens
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -blockmintxfee " ) )
2016-12-13 17:51:51 +01:00
{
CAmount n = 0 ;
2017-08-01 21:17:40 +02:00
if ( ! ParseMoney ( gArgs . GetArg ( " -blockmintxfee " , " " ) , n ) )
return InitError ( AmountErrMsg ( " blockmintxfee " , gArgs . GetArg ( " -blockmintxfee " , " " ) ) ) ;
2016-12-13 17:51:51 +01:00
}
2016-12-13 22:19:17 +01:00
// Feerate used to define dust. Shouldn't be changed lightly as old
// implementations may inadvertently create non-standard transactions
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -dustrelayfee " ) )
2016-12-13 22:19:17 +01:00
{
CAmount n = 0 ;
2017-08-01 21:17:40 +02:00
if ( ! ParseMoney ( gArgs . GetArg ( " -dustrelayfee " , " " ) , n ) | | 0 = = n )
return InitError ( AmountErrMsg ( " dustrelayfee " , gArgs . GetArg ( " -dustrelayfee " , " " ) ) ) ;
2016-12-13 22:19:17 +01:00
dustRelayFee = CFeeRate ( n ) ;
2013-04-26 02:11:27 +02:00
}
2012-05-21 16:47:29 +02:00
2017-08-01 21:17:40 +02:00
fRequireStandard = ! gArgs . GetBoolArg ( " -acceptnonstdtxn " , ! chainparams . RequireStandard ( ) ) ;
2016-10-19 04:33:53 +02:00
if ( chainparams . RequireStandard ( ) & & ! fRequireStandard )
2015-06-24 05:36:22 +02:00
return InitError ( strprintf ( " acceptnonstdtxn is not currently supported for %s chain " , chainparams . NetworkIDString ( ) ) ) ;
2017-08-01 21:17:40 +02:00
nBytesPerSigOp = gArgs . GetArg ( " -bytespersigop " , nBytesPerSigOp ) ;
2015-06-24 05:36:22 +02:00
2013-12-13 16:14:48 +01:00
# ifdef ENABLE_WALLET
2016-09-19 16:09:38 +02:00
if ( ! CWallet : : ParameterInteraction ( ) )
2016-03-15 10:30:37 +01:00
return false ;
2016-09-19 16:09:38 +02:00
# endif
2014-07-17 02:04:04 +02:00
2017-08-01 21:17:40 +02:00
fIsBareMultisigStd = gArgs . GetBoolArg ( " -permitbaremultisig " , DEFAULT_PERMIT_BAREMULTISIG ) ;
fAcceptDatacarrier = gArgs . GetBoolArg ( " -datacarrier " , DEFAULT_ACCEPT_DATACARRIER ) ;
nMaxDatacarrierBytes = gArgs . GetArg ( " -datacarriersize " , nMaxDatacarrierBytes ) ;
2014-07-17 02:04:04 +02:00
2015-06-19 16:42:39 +02:00
// Option to startup with mocktime set (used for regression testing):
2017-08-01 21:17:40 +02:00
SetMockTime ( gArgs . GetArg ( " -mocktime " , 0 ) ) ; // SetMockTime(0) is a no-op
2015-06-19 16:42:39 +02:00
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -peerbloomfilters " , DEFAULT_PEERBLOOMFILTERS ) )
2016-06-08 19:12:22 +02:00
nLocalServices = ServiceFlags ( nLocalServices | NODE_BLOOM ) ;
2015-08-21 06:15:27 +02:00
2017-08-01 21:17:40 +02:00
if ( gArgs . GetArg ( " -rpcserialversion " , DEFAULT_RPC_SERIALIZE_VERSION ) < 0 )
2016-11-20 15:54:51 +01:00
return InitError ( " rpcserialversion must be non-negative. " ) ;
2017-08-01 21:17:40 +02:00
if ( gArgs . GetArg ( " -rpcserialversion " , DEFAULT_RPC_SERIALIZE_VERSION ) > 1 )
2016-12-06 08:26:32 +01:00
return InitError ( " unknown rpcserialversion requested. " ) ;
2017-08-01 21:17:40 +02:00
nMaxTipAge = gArgs . GetArg ( " -maxtipage " , DEFAULT_MAX_TIP_AGE ) ;
2016-01-28 06:27:25 +01:00
2017-08-01 21:17:40 +02:00
fEnableReplacement = gArgs . GetBoolArg ( " -mempoolreplacement " , DEFAULT_ENABLE_REPLACEMENT ) ;
if ( ( ! fEnableReplacement ) & & gArgs . IsArgSet ( " -mempoolreplacement " ) ) {
2016-01-28 06:27:25 +01:00
// Minimal effort at forwards compatibility
2017-08-01 21:17:40 +02:00
std : : string strReplacementModeList = gArgs . GetArg ( " -mempoolreplacement " , " " ) ; // default is impossible
2016-01-28 06:27:25 +01:00
std : : vector < std : : string > vstrReplacementModes ;
boost : : split ( vstrReplacementModes , strReplacementModeList , boost : : is_any_of ( " , " ) ) ;
2016-02-01 20:30:37 +01:00
fEnableReplacement = ( std : : find ( vstrReplacementModes . begin ( ) , vstrReplacementModes . end ( ) , " fee " ) ! = vstrReplacementModes . end ( ) ) ;
2016-01-28 06:27:25 +01:00
}
2015-12-14 13:23:45 +01:00
2017-05-05 05:21:34 +02:00
if ( gArgs . IsArgSet ( " -vbparams " ) ) {
// Allow overriding version bits parameters for testing
2016-10-19 04:33:53 +02:00
if ( ! chainparams . MineBlocksOnDemand ( ) ) {
2017-05-05 05:21:34 +02:00
return InitError ( " Version bits parameters may only be overridden on regtest. " ) ;
2016-07-25 23:22:37 +02:00
}
2017-05-05 05:21:34 +02:00
for ( const std : : string & strDeployment : gArgs . GetArgs ( " -vbparams " ) ) {
2016-07-25 23:22:37 +02:00
std : : vector < std : : string > vDeploymentParams ;
2017-05-07 19:37:54 +02:00
boost : : split ( vDeploymentParams , strDeployment , boost : : is_any_of ( " : " ) ) ;
2016-07-25 23:22:37 +02:00
if ( vDeploymentParams . size ( ) ! = 3 ) {
2017-05-05 05:21:34 +02:00
return InitError ( " Version bits parameters malformed, expecting deployment:start:end " ) ;
2016-07-25 23:22:37 +02:00
}
int64_t nStartTime , nTimeout ;
if ( ! ParseInt64 ( vDeploymentParams [ 1 ] , & nStartTime ) ) {
return InitError ( strprintf ( " Invalid nStartTime (%s) " , vDeploymentParams[1])) ;
}
if ( ! ParseInt64 ( vDeploymentParams [ 2 ] , & nTimeout ) ) {
return InitError ( strprintf ( " Invalid nTimeout (%s) " , vDeploymentParams[2])) ;
}
bool found = false ;
2016-08-02 10:26:54 +02:00
for ( int j = 0 ; j < ( int ) Consensus : : MAX_VERSION_BITS_DEPLOYMENTS ; + + j )
2016-07-25 23:22:37 +02:00
{
2016-08-02 10:26:54 +02:00
if ( vDeploymentParams [ 0 ] . compare ( VersionBitsDeploymentInfo [ j ] . name ) = = 0 ) {
2017-05-05 05:21:34 +02:00
UpdateVersionBitsParameters ( Consensus : : DeploymentPos ( j ) , nStartTime , nTimeout ) ;
2016-07-25 23:22:37 +02:00
found = true ;
2017-05-05 05:21:34 +02:00
LogPrintf ( " Setting version bits activation parameters for %s to start=%ld, timeout=%ld \n " , vDeploymentParams [ 0 ] , nStartTime , nTimeout ) ;
2016-07-25 23:22:37 +02:00
break ;
}
}
if ( ! found ) {
return InitError ( strprintf ( " Invalid deployment (%s) " , vDeploymentParams[0])) ;
}
}
}
2016-10-25 11:34:27 +02:00
return true ;
}
2016-07-25 23:22:37 +02:00
2016-10-25 12:03:22 +02:00
static bool LockDataDirectory ( bool probeOnly )
2016-10-25 11:34:27 +02:00
{
2012-10-12 03:09:05 +02:00
std : : string strDataDir = GetDataDir ( ) . string ( ) ;
2016-03-15 10:30:37 +01:00
2012-05-21 16:47:29 +02:00
// Make sure only a single Bitcoin process is using the data directory.
2017-03-01 17:05:50 +01:00
fs : : path pathLockFile = GetDataDir ( ) / " .lock " ;
2017-03-01 17:28:39 +01:00
FILE * file = fsbridge : : fopen ( pathLockFile , " a " ) ; // empty lock file; created if it doesn't exist.
2012-05-21 16:47:29 +02:00
if ( file ) fclose ( file ) ;
2015-05-19 01:37:43 +02:00
try {
static boost : : interprocess : : file_lock lock ( pathLockFile . string ( ) . c_str ( ) ) ;
2016-10-25 11:34:27 +02:00
if ( ! lock . try_lock ( ) ) {
2015-12-09 11:53:12 +01:00
return InitError ( strprintf ( _ ( " Cannot obtain a lock on data directory %s. %s is probably already running. " ) , strDataDir , _ ( PACKAGE_NAME ) ) ) ;
2016-10-25 11:34:27 +02:00
}
2016-10-25 12:03:22 +02:00
if ( probeOnly ) {
lock . unlock ( ) ;
}
2015-05-19 01:37:43 +02:00
} catch ( const boost : : interprocess : : interprocess_exception & e ) {
2015-12-09 11:53:12 +01:00
return InitError ( strprintf ( _ ( " Cannot obtain a lock on data directory %s. %s is probably already running. " ) + " %s. " , strDataDir , _ ( PACKAGE_NAME ) , e . what ( ) ) ) ;
2015-05-19 01:37:43 +02:00
}
2016-10-25 11:34:27 +02:00
return true ;
}
2016-10-25 12:03:22 +02:00
bool AppInitSanityChecks ( )
{
// ********************************************************* Step 4: sanity checks
// Initialize elliptic curve code
2017-07-14 08:26:04 +02:00
std : : string sha256_algo = SHA256AutoDetect ( ) ;
LogPrintf ( " Using the '%s' SHA256 implementation \n " , sha256_algo ) ;
2017-05-10 00:13:00 +02:00
RandomInit ( ) ;
2016-10-25 12:03:22 +02:00
ECC_Start ( ) ;
globalVerifyHandle . reset ( new ECCVerifyHandle ( ) ) ;
// Sanity check
if ( ! InitSanityCheck ( ) )
return InitError ( strprintf ( _ ( " Initialization sanity check failed. %s is shutting down. " ) , _ ( PACKAGE_NAME ) ) ) ;
// Probe the data directory lock to give an early error message, if possible
2017-07-15 10:46:06 +02:00
// We cannot hold the data directory lock here, as the forking for daemon() hasn't yet happened,
// and a fork will cause weird behavior to it.
2016-10-25 12:03:22 +02:00
return LockDataDirectory ( true ) ;
}
2017-07-15 10:46:06 +02:00
bool AppInitLockDataDirectory ( )
2016-10-25 11:34:27 +02:00
{
2016-10-25 12:03:22 +02:00
// After daemonization get the data directory lock again and hold on to it until exit
// This creates a slight window for a race condition to happen, however this condition is harmless: it
// will at most make us exit without printing a message to console.
if ( ! LockDataDirectory ( false ) ) {
// Detailed error printed inside LockDataDirectory
return false ;
}
2017-07-15 10:46:06 +02:00
return true ;
}
2015-05-19 01:37:43 +02:00
2017-07-15 10:46:06 +02:00
bool AppInitMain ( boost : : thread_group & threadGroup , CScheduler & scheduler )
{
const CChainParams & chainparams = Params ( ) ;
// ********************************************************* Step 4a: application initialization
2014-09-20 10:56:25 +02:00
# ifndef WIN32
CreatePidFile ( GetPidFile ( ) , getpid ( ) ) ;
# endif
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -shrinkdebugfile " , logCategories = = BCLog : : NONE ) ) {
2017-01-24 18:46:01 +01:00
// Do this first since it both loads a bunch of debug.log into memory,
// and because this needs to happen before any other debug.log printing
2011-05-14 20:10:21 +02:00
ShrinkDebugFile ( ) ;
2017-01-24 18:46:01 +01:00
}
2015-05-15 21:31:14 +02:00
if ( fPrintToDebugLog )
OpenDebugLog ( ) ;
2012-09-04 18:24:08 +02:00
if ( ! fLogTimestamps )
2014-01-16 16:15:27 +01:00
LogPrintf ( " Startup time: %s \n " , DateTimeStrFormat ( " %Y-%m-%d %H:%M:%S " , GetTime ( ) ) ) ;
LogPrintf ( " Default data directory %s \n " , GetDefaultDataDir ( ) . string ( ) ) ;
2016-10-25 11:34:27 +02:00
LogPrintf ( " Using data directory %s \n " , GetDataDir ( ) . string ( ) ) ;
2017-08-01 21:17:40 +02:00
LogPrintf ( " Using config file %s \n " , GetConfigFile ( gArgs . GetArg ( " -conf " , BITCOIN_CONF_FILENAME ) ) . string ( ) ) ;
2016-12-11 05:39:26 +01:00
LogPrintf ( " Using at most %i automatic connections (%i file descriptors available) \n " , nMaxConnections , nFD ) ;
2011-05-14 20:10:21 +02:00
2016-10-05 22:58:47 +02:00
InitSignatureCache ( ) ;
Cache full script execution results in addition to signatures
This adds a new CuckooCache in validation, caching whether all of a
transaction's scripts were valid with a given set of script flags.
Unlike previous attempts at caching an entire transaction's
validity, which have nearly universally introduced consensus
failures, this only caches the validity of a transaction's
scriptSigs. As these are pure functions of the transaction and
data it commits to, this should be much safer.
This is somewhat duplicative with the sigcache, as entries in the
new cache will also have several entries in the sigcache. However,
the sigcache is kept both as ATMP relies on it and because it
prevents malleability-based DoS attacks on the new higher-level
cache. Instead, the -sigcachesize option is re-used - cutting the
sigcache size in half and using the newly freed memory for the
script execution cache.
Transactions which match the script execution cache never even have
entries in the script check thread's workqueue created.
Note that the cache is indexed only on the script execution flags
and the transaction's witness hash. While this is sufficient to
make the CScriptCheck() calls pure functions, this introduces
dependancies on the mempool calculating things such as the
PrecomputedTransactionData object, filling the CCoinsViewCache, etc
in the exact same way as ConnectBlock. I belive this is a reasonable
assumption, but should be noted carefully.
In a rather naive benchmark (reindex-chainstate up to block 284k
with cuckoocache always returning true for contains(),
-assumevalid=0 and a very large dbcache), this connected blocks
~1.7x faster.
2017-04-11 22:46:39 +02:00
InitScriptExecutionCache ( ) ;
2016-10-05 22:58:47 +02:00
2014-11-06 08:47:48 +01:00
LogPrintf ( " Using %u threads for script verification \n " , nScriptCheckThreads ) ;
2012-12-01 23:04:14 +01:00
if ( nScriptCheckThreads ) {
for ( int i = 0 ; i < nScriptCheckThreads - 1 ; i + + )
2013-03-07 04:31:26 +01:00
threadGroup . create_thread ( & ThreadScriptCheck ) ;
2012-12-01 23:04:14 +01:00
}
2015-04-03 17:50:06 +02:00
// Start the lightweight task scheduler thread
CScheduler : : Function serviceLoop = boost : : bind ( & CScheduler : : serviceQueue , & scheduler ) ;
threadGroup . create_thread ( boost : : bind ( & TraceThread < CScheduler : : Function > , " scheduler " , serviceLoop ) ) ;
2017-01-19 22:49:22 +01:00
GetMainSignals ( ) . RegisterBackgroundSignalScheduler ( scheduler ) ;
2014-10-29 18:08:31 +01:00
/* Start the RPC server already. It will be started in "warmup" mode
* and not really process calls already ( but it will signify connections
* that the server is there and will be ready later ) . Warmup mode will
* be disabled when initialisation is finished .
*/
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -server " , false ) )
2014-10-29 18:08:31 +01:00
{
uiInterface . InitMessage . connect ( SetRPCWarmupStatus ) ;
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.
- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.
- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral
- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.
- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.
By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.
What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.
Configuration options:
- `-rpcthreads`: repurposed as "number of work handler threads". Still
defaults to 4.
- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.
- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.
- `-debug=http`: low-level http activity logging
2015-01-23 07:53:17 +01:00
if ( ! AppInitServers ( threadGroup ) )
return InitError ( _ ( " Unable to start HTTP server. See debug log for details. " ) ) ;
2014-10-29 18:08:31 +01:00
}
2013-04-13 07:13:08 +02:00
int64_t nStart ;
2012-05-21 16:47:29 +02:00
2013-01-11 23:18:00 +01:00
// ********************************************************* Step 5: verify wallet database integrity
2013-11-29 16:04:29 +01:00
# ifdef ENABLE_WALLET
2016-09-19 16:09:38 +02:00
if ( ! CWallet : : Verify ( ) )
return false ;
# endif
2012-09-18 20:30:47 +02:00
// ********************************************************* Step 6: network initialization
2016-10-02 17:29:35 +02:00
// Note that we absolutely cannot open any actual connections
// until the very end ("start node") as the UTXO/block state
// is not yet setup and may end up being set up twice if we
// need to reindex later.
2012-05-21 16:47:29 +02:00
2016-04-16 20:47:18 +02:00
assert ( ! g_connman ) ;
2016-09-09 12:48:10 +02:00
g_connman = std : : unique_ptr < CConnman > ( new CConnman ( GetRand ( std : : numeric_limits < uint64_t > : : max ( ) ) , GetRand ( std : : numeric_limits < uint64_t > : : max ( ) ) ) ) ;
2016-04-16 20:47:18 +02:00
CConnman & connman = * g_connman ;
2016-10-04 19:49:44 +02:00
peerLogic . reset ( new PeerLogicValidation ( & connman ) ) ;
RegisterValidationInterface ( peerLogic . get ( ) ) ;
2013-06-06 05:21:41 +02:00
RegisterNodeSignals ( GetNodeSignals ( ) ) ;
2013-05-07 15:16:25 +02:00
2015-09-09 14:24:56 +02:00
// sanitize comments per BIP-0014, format user agent and check total size
2017-01-27 09:43:41 +01:00
std : : vector < std : : string > uacomments ;
2017-03-29 17:51:41 +02:00
for ( const std : : string & cmt : gArgs . GetArgs ( " -uacomment " ) ) {
2017-06-27 03:10:39 +02:00
if ( cmt ! = SanitizeString ( cmt , SAFE_CHARS_UA_COMMENT ) )
return InitError ( strprintf ( _ ( " User Agent comment (%s) contains unsafe characters . " ), cmt)) ;
uacomments . push_back ( cmt ) ;
2015-09-09 14:24:56 +02:00
}
strSubVersion = FormatSubVersion ( CLIENT_NAME , CLIENT_VERSION , uacomments ) ;
2015-07-31 18:05:42 +02:00
if ( strSubVersion . size ( ) > MAX_SUBVERSION_LENGTH ) {
2015-10-22 18:09:19 +02:00
return InitError ( strprintf ( _ ( " Total length of network version string (%i) exceeds maximum length (%i). Reduce the number or size of uacomments. " ) ,
2015-07-31 18:05:42 +02:00
strSubVersion . size ( ) , MAX_SUBVERSION_LENGTH ) ) ;
}
2017-05-07 18:54:43 +02:00
if ( gArgs . IsArgSet ( " -onlynet " ) ) {
2012-05-21 16:47:29 +02:00
std : : set < enum Network > nets ;
2017-06-02 03:18:57 +02:00
for ( const std : : string & snet : gArgs . GetArgs ( " -onlynet " ) ) {
2012-05-21 16:47:29 +02:00
enum Network net = ParseNetwork ( snet ) ;
if ( net = = NET_UNROUTABLE )
2014-01-16 16:15:27 +01:00
return InitError ( strprintf ( _ ( " Unknown network specified in -onlynet: '%s' " ) , snet ) ) ;
2012-05-21 16:47:29 +02:00
nets . insert ( net ) ;
}
for ( int n = 0 ; n < NET_MAX ; n + + ) {
enum Network net = ( enum Network ) n ;
if ( ! nets . count ( net ) )
SetLimited ( net ) ;
}
}
2017-02-16 02:08:36 +01:00
// Check for host lookup allowed before parsing any network related parameters
2017-08-01 21:17:40 +02:00
fNameLookup = gArgs . GetBoolArg ( " -dns " , DEFAULT_NAME_LOOKUP ) ;
2017-02-16 02:08:36 +01:00
2017-08-01 21:17:40 +02:00
bool proxyRandomize = gArgs . GetBoolArg ( " -proxyrandomize " , DEFAULT_PROXYRANDOMIZE ) ;
2015-06-10 09:19:13 +02:00
// -proxy sets a proxy for all outgoing network traffic
// -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
2017-08-01 21:17:40 +02:00
std : : string proxyArg = gArgs . GetArg ( " -proxy " , " " ) ;
2016-02-18 07:44:32 +01:00
SetLimited ( NET_TOR ) ;
2015-06-10 09:19:13 +02:00
if ( proxyArg ! = " " & & proxyArg ! = " 0 " ) {
2017-02-16 02:08:36 +01:00
CService proxyAddr ;
if ( ! Lookup ( proxyArg . c_str ( ) , proxyAddr , 9050 , fNameLookup ) ) {
return InitError ( strprintf ( _ ( " Invalid -proxy address or hostname: '%s' " ) , proxyArg ) ) ;
}
proxyType addrProxy = proxyType ( proxyAddr , proxyRandomize ) ;
2012-05-24 19:02:21 +02:00
if ( ! addrProxy . IsValid ( ) )
2017-02-16 02:08:36 +01:00
return InitError ( strprintf ( _ ( " Invalid -proxy address or hostname: '%s' " ) , proxyArg ) ) ;
2012-05-24 19:02:21 +02:00
2014-11-24 00:12:50 +01:00
SetProxy ( NET_IPV4 , addrProxy ) ;
SetProxy ( NET_IPV6 , addrProxy ) ;
2015-06-10 09:19:13 +02:00
SetProxy ( NET_TOR , addrProxy ) ;
2014-06-11 13:20:59 +02:00
SetNameProxy ( addrProxy ) ;
2016-02-18 07:44:32 +01:00
SetLimited ( NET_TOR , false ) ; // by default, -proxy sets onion as reachable, unless -noonion later
2012-05-01 21:04:07 +02:00
}
2015-06-10 09:19:13 +02:00
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
// -noonion (or -onion=0) disables connecting to .onion entirely
// An empty string is used to not override the onion proxy (in which case it defaults to -proxy set above, or none)
2017-08-01 21:17:40 +02:00
std : : string onionArg = gArgs . GetArg ( " -onion " , " " ) ;
2015-06-10 09:19:13 +02:00
if ( onionArg ! = " " ) {
if ( onionArg = = " 0 " ) { // Handle -noonion/-onion=0
2016-02-18 07:44:32 +01:00
SetLimited ( NET_TOR ) ; // set onions as unreachable
2015-06-10 09:19:13 +02:00
} else {
2017-02-16 02:08:36 +01:00
CService onionProxy ;
if ( ! Lookup ( onionArg . c_str ( ) , onionProxy , 9050 , fNameLookup ) ) {
return InitError ( strprintf ( _ ( " Invalid -onion address or hostname: '%s' " ) , onionArg ) ) ;
}
proxyType addrOnion = proxyType ( onionProxy , proxyRandomize ) ;
2015-06-10 09:19:13 +02:00
if ( ! addrOnion . IsValid ( ) )
2017-02-16 02:08:36 +01:00
return InitError ( strprintf ( _ ( " Invalid -onion address or hostname: '%s' " ) , onionArg ) ) ;
2015-06-10 09:19:13 +02:00
SetProxy ( NET_TOR , addrOnion ) ;
2016-02-18 07:44:32 +01:00
SetLimited ( NET_TOR , false ) ;
2015-06-10 09:19:13 +02:00
}
2012-05-24 19:02:21 +02:00
}
// see Step 2: parameter interactions for more information about these
2017-08-01 21:17:40 +02:00
fListen = gArgs . GetBoolArg ( " -listen " , DEFAULT_LISTEN ) ;
fDiscover = gArgs . GetBoolArg ( " -discover " , true ) ;
fRelayTxes = ! gArgs . GetBoolArg ( " -blocksonly " , DEFAULT_BLOCKSONLY ) ;
2012-05-17 04:11:19 +02:00
2017-03-29 17:51:41 +02:00
for ( const std : : string & strAddr : gArgs . GetArgs ( " -externalip " ) ) {
2017-06-27 03:10:39 +02:00
CService addrLocal ;
if ( Lookup ( strAddr . c_str ( ) , addrLocal , GetListenPort ( ) , fNameLookup ) & & addrLocal . IsValid ( ) )
AddLocal ( addrLocal , LOCAL_MANUAL ) ;
else
return InitError ( ResolveErrMsg ( " externalip " , strAddr ) ) ;
2012-05-21 16:47:29 +02:00
}
2014-11-18 18:06:32 +01:00
# if ENABLE_ZMQ
2016-11-30 03:43:29 +01:00
pzmqNotificationInterface = CZMQNotificationInterface : : Create ( ) ;
2014-11-18 18:06:32 +01:00
if ( pzmqNotificationInterface ) {
RegisterValidationInterface ( pzmqNotificationInterface ) ;
}
# endif
2016-09-12 21:04:20 +02:00
uint64_t nMaxOutboundLimit = 0 ; //unlimited unless -maxuploadtarget is set
uint64_t nMaxOutboundTimeframe = MAX_UPLOAD_TIMEFRAME ;
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -maxuploadtarget " ) ) {
nMaxOutboundLimit = gArgs . GetArg ( " -maxuploadtarget " , DEFAULT_MAX_UPLOAD_TARGET ) * 1024 * 1024 ;
2015-09-02 17:03:27 +02:00
}
2014-11-18 18:06:32 +01:00
2012-10-05 19:22:21 +02:00
// ********************************************************* Step 7: load block chain
2012-05-21 16:47:29 +02:00
2017-08-01 21:17:40 +02:00
fReindex = gArgs . GetBoolArg ( " -reindex " , false ) ;
bool fReindexChainState = gArgs . GetBoolArg ( " -reindex-chainstate " , false ) ;
2012-10-21 21:23:13 +02:00
2012-11-04 17:11:48 +01:00
// cache size calculations
2017-08-01 21:17:40 +02:00
int64_t nTotalCache = ( gArgs . GetArg ( " -dbcache " , nDefaultDbCache ) < < 20 ) ;
2015-05-04 01:56:42 +02:00
nTotalCache = std : : max ( nTotalCache , nMinDbCache < < 20 ) ; // total cache cannot be less than nMinDbCache
2016-08-13 19:21:13 +02:00
nTotalCache = std : : min ( nTotalCache , nMaxDbCache < < 20 ) ; // total cache cannot be greater than nMaxDbcache
2015-05-04 01:56:42 +02:00
int64_t nBlockTreeDBCache = nTotalCache / 8 ;
2017-08-01 21:17:40 +02:00
nBlockTreeDBCache = std : : min ( nBlockTreeDBCache , ( gArgs . GetBoolArg ( " -txindex " , DEFAULT_TXINDEX ) ? nMaxBlockDBAndTxIndexCache : nMaxBlockDBCache ) < < 20 ) ;
2012-11-04 17:11:48 +01:00
nTotalCache - = nBlockTreeDBCache ;
2015-05-04 01:56:42 +02:00
int64_t nCoinDBCache = std : : min ( nTotalCache / 2 , ( nTotalCache / 4 ) + ( 1 < < 23 ) ) ; // use 25%-50% of the remainder for disk cache
2016-06-27 15:39:28 +02:00
nCoinDBCache = std : : min ( nCoinDBCache , nMaxCoinsDBCache < < 20 ) ; // cap total coins db cache
2012-11-04 17:11:48 +01:00
nTotalCache - = nCoinDBCache ;
2015-05-04 01:56:42 +02:00
nCoinCacheUsage = nTotalCache ; // the rest goes to in-memory cache
2017-08-01 21:17:40 +02:00
int64_t nMempoolSizeMax = gArgs . GetArg ( " -maxmempool " , DEFAULT_MAX_MEMPOOL_SIZE ) * 1000000 ;
2015-05-04 01:56:42 +02:00
LogPrintf ( " Cache configuration: \n " ) ;
LogPrintf ( " * Using %.1fMiB for block index database \n " , nBlockTreeDBCache * ( 1.0 / 1024 / 1024 ) ) ;
LogPrintf ( " * Using %.1fMiB for chain state database \n " , nCoinDBCache * ( 1.0 / 1024 / 1024 ) ) ;
2016-12-22 04:15:13 +01:00
LogPrintf ( " * Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of unused mempool space) \n " , nCoinCacheUsage * ( 1.0 / 1024 / 1024 ) , nMempoolSizeMax * ( 1.0 / 1024 / 1024 ) ) ;
2012-11-04 17:11:48 +01:00
2013-02-16 17:58:45 +01:00
bool fLoaded = false ;
2017-06-23 09:33:31 +02:00
while ( ! fLoaded & & ! fRequestShutdown ) {
2013-02-16 17:58:45 +01:00
bool fReset = fReindex ;
std : : string strLoadError ;
2013-01-11 22:57:22 +01:00
2013-02-16 17:58:45 +01:00
uiInterface . InitMessage ( _ ( " Loading block index... " ) ) ;
2012-07-06 16:33:34 +02:00
2013-02-16 17:58:45 +01:00
nStart = GetTimeMillis ( ) ;
do {
try {
UnloadBlockIndex ( ) ;
delete pcoinsTip ;
delete pcoinsdbview ;
2015-01-08 14:38:06 +01:00
delete pcoinscatcher ;
2013-02-16 17:58:45 +01:00
delete pblocktree ;
2017-07-24 00:08:50 +02:00
pblocktree = new CBlockTreeDB ( nBlockTreeDBCache , false , fReset ) ;
2013-02-16 17:58:45 +01:00
2017-07-24 00:08:50 +02:00
if ( fReset ) {
2013-02-16 17:58:45 +01:00
pblocktree - > WriteReindexing ( true ) ;
2015-06-02 21:24:53 +02:00
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
if ( fPruneMode )
2015-06-02 21:24:53 +02:00
CleanupBlockRevFiles ( ) ;
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
}
2017-07-07 03:32:08 +02:00
2017-06-23 09:33:31 +02:00
if ( fRequestShutdown ) break ;
2013-02-16 17:58:45 +01:00
2017-07-07 03:32:08 +02:00
// LoadBlockIndex will load fTxIndex from the db, or set it if
// we're reindexing. It will also load fHavePruned if we've
// ever removed a block file from disk.
2017-07-24 00:08:50 +02:00
// Note that it also sets fReindex based on the disk flag!
// From here on out fReindex and fReset mean something different!
2016-10-25 16:35:26 +02:00
if ( ! LoadBlockIndex ( chainparams ) ) {
2013-02-16 17:58:45 +01:00
strLoadError = _ ( " Error loading block database " ) ;
break ;
}
2013-05-12 12:03:32 +02:00
// If the loaded chain has a wrong genesis, bail out immediately
// (we're likely using a testnet datadir, or the other way around).
2015-04-09 15:58:34 +02:00
if ( ! mapBlockIndex . empty ( ) & & mapBlockIndex . count ( chainparams . GetConsensus ( ) . hashGenesisBlock ) = = 0 )
2013-05-12 12:03:32 +02:00
return InitError ( _ ( " Incorrect or no genesis block found. Wrong datadir for network? " ) ) ;
2013-06-22 16:03:11 +02:00
// Check for changed -txindex state
2017-08-01 21:17:40 +02:00
if ( fTxIndex ! = gArgs . GetBoolArg ( " -txindex " , DEFAULT_TXINDEX ) ) {
2016-04-21 14:14:37 +02:00
strLoadError = _ ( " You need to rebuild the database using -reindex-chainstate to change -txindex " ) ;
2013-06-22 16:03:11 +02:00
break ;
}
Add block pruning functionality
This adds a -prune=N option to bitcoind, which if set to N>0 will enable block
file pruning. When pruning is enabled, block and undo files will be deleted to
try to keep total space used by those files to below the prune target (N, in
MB) specified by the user, subject to some constraints:
- The last 288 blocks on the main chain are always kept (MIN_BLOCKS_TO_KEEP),
- N must be at least 550MB (chosen as a value for the target that could
reasonably be met, with some assumptions about block sizes, orphan rates,
etc; see comment in main.h),
- No blocks are pruned until chainActive is at least 100,000 blocks long (on
mainnet; defined separately for mainnet, testnet, and regtest in chainparams
as nPruneAfterHeight).
This unsets NODE_NETWORK if pruning is enabled.
Also included is an RPC test for pruning (pruning.py).
Thanks to @rdponticelli for earlier work on this feature; this is based in
part off that work.
2015-02-23 20:27:44 +01:00
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks
// in the past, but is now trying to run unpruned.
if ( fHavePruned & & ! fPruneMode ) {
strLoadError = _ ( " You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain " ) ;
break ;
}
2017-07-07 03:32:08 +02:00
// At this point blocktree args are consistent with what's on disk.
2017-08-01 23:02:10 +02:00
// If we're not mid-reindex (based on disk + args), add a genesis block on disk
// (otherwise we use the one already on disk).
// This is called again in ThreadImport after the reindex completes.
2017-07-07 03:32:08 +02:00
if ( ! fReindex & & ! LoadGenesisBlock ( chainparams ) ) {
strLoadError = _ ( " Error initializing block database " ) ;
break ;
}
// At this point we're either in reindex or we've loaded a useful
// block tree into mapBlockIndex!
2017-07-24 00:08:50 +02:00
pcoinsdbview = new CCoinsViewDB ( nCoinDBCache , false , fReset | | fReindexChainState ) ;
2017-07-07 03:32:08 +02:00
pcoinscatcher = new CCoinsViewErrorCatcher ( pcoinsdbview ) ;
// If necessary, upgrade from older database format.
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
if ( ! pcoinsdbview - > Upgrade ( ) ) {
strLoadError = _ ( " Error upgrading chainstate database " ) ;
break ;
}
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
2017-04-19 18:34:30 +02:00
if ( ! ReplayBlocks ( chainparams , pcoinsdbview ) ) {
strLoadError = _ ( " Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate. " ) ;
break ;
}
2017-07-07 03:32:08 +02:00
// The on-disk coinsdb is now in a good state, create the cache
2017-04-17 17:41:10 +02:00
pcoinsTip = new CCoinsViewCache ( pcoinscatcher ) ;
2017-04-19 18:34:30 +02:00
2017-07-26 20:41:50 +02:00
bool is_coinsview_empty = fReset | | fReindexChainState | | pcoinsTip - > GetBestBlock ( ) . IsNull ( ) ;
if ( ! is_coinsview_empty ) {
2017-07-07 02:00:11 +02:00
// LoadChainTip sets chainActive based on pcoinsTip's best block
if ( ! LoadChainTip ( chainparams ) ) {
strLoadError = _ ( " Error initializing block database " ) ;
break ;
}
2017-08-07 07:36:37 +02:00
assert ( chainActive . Tip ( ) ! = nullptr ) ;
2017-07-07 02:00:11 +02:00
}
2017-04-19 18:34:30 +02:00
2017-07-24 00:08:50 +02:00
if ( ! fReset ) {
2017-07-07 03:29:46 +02:00
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
// It both disconnects blocks based on chainActive, and drops block data in
// mapBlockIndex based on lack of available witness data.
2016-03-18 17:20:12 +01:00
uiInterface . InitMessage ( _ ( " Rewinding blocks... " ) ) ;
if ( ! RewindBlockIndex ( chainparams ) ) {
strLoadError = _ ( " Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain " ) ;
break ;
}
}
2017-07-26 20:41:50 +02:00
if ( ! is_coinsview_empty ) {
2017-07-07 03:32:08 +02:00
uiInterface . InitMessage ( _ ( " Verifying blocks... " ) ) ;
2017-08-01 21:17:40 +02:00
if ( fHavePruned & & gArgs . GetArg ( " -checkblocks " , DEFAULT_CHECKBLOCKS ) > MIN_BLOCKS_TO_KEEP ) {
2017-07-07 03:32:08 +02:00
LogPrintf ( " Prune: pruned datadir may not have more than %d blocks; only checking available blocks " ,
MIN_BLOCKS_TO_KEEP ) ;
}
2015-07-28 23:42:03 +02:00
2017-07-07 03:32:08 +02:00
{
LOCK ( cs_main ) ;
CBlockIndex * tip = chainActive . Tip ( ) ;
RPCNotifyBlockChange ( true , tip ) ;
if ( tip & & tip - > nTime > GetAdjustedTime ( ) + 2 * 60 * 60 ) {
strLoadError = _ ( " The block database contains a block which appears to be from the future. "
" This may be due to your computer's date and time being set incorrectly. "
" Only rebuild the block database if you are sure that your computer's date and time are correct " ) ;
break ;
}
2015-07-28 23:42:03 +02:00
}
2017-08-01 21:17:40 +02:00
if ( ! CVerifyDB ( ) . VerifyDB ( chainparams , pcoinsdbview , gArgs . GetArg ( " -checklevel " , DEFAULT_CHECKLEVEL ) ,
gArgs . GetArg ( " -checkblocks " , DEFAULT_CHECKBLOCKS ) ) ) {
2017-07-07 03:32:08 +02:00
strLoadError = _ ( " Corrupted block database detected " ) ;
break ;
}
2013-02-16 17:58:45 +01:00
}
2014-12-07 13:29:06 +01:00
} catch ( const std : : exception & e ) {
2016-12-25 21:19:40 +01:00
LogPrintf ( " %s \n " , e . what ( ) ) ;
2013-02-16 17:58:45 +01:00
strLoadError = _ ( " Error opening block database " ) ;
break ;
}
2013-01-03 15:29:07 +01:00
2013-02-16 17:58:45 +01:00
fLoaded = true ;
} while ( false ) ;
2017-06-23 09:33:31 +02:00
if ( ! fLoaded & & ! fRequestShutdown ) {
2013-02-16 17:58:45 +01:00
// first suggest a reindex
if ( ! fReset ) {
2016-06-24 16:35:21 +02:00
bool fRet = uiInterface . ThreadSafeQuestion (
2013-05-13 08:26:29 +02:00
strLoadError + " . \n \n " + _ ( " Do you want to rebuild the block database now? " ) ,
2016-06-24 16:35:21 +02:00
strLoadError + " . \n Please restart with -reindex or -reindex-chainstate to recover. " ,
2013-02-16 17:58:45 +01:00
" " , CClientUIInterface : : MSG_ERROR | CClientUIInterface : : BTN_ABORT ) ;
if ( fRet ) {
fReindex = true ;
fRequestShutdown = false ;
} else {
2013-09-18 12:38:08 +02:00
LogPrintf ( " Aborted block database rebuild. Exiting. \n " ) ;
2013-02-16 17:58:45 +01:00
return false ;
}
} else {
return InitError ( strLoadError ) ;
}
}
}
2012-04-18 13:30:24 +02:00
2013-12-16 23:36:22 +01:00
// As LoadBlockIndex can take several minutes, it's possible the user
// requested to kill the GUI during the last operation. If so, exit.
2012-04-18 13:30:24 +02:00
// As the program has not fully started yet, Shutdown() is possibly overkill.
if ( fRequestShutdown )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " Shutdown requested. Exiting. \n " ) ;
2012-04-18 13:30:24 +02:00
return false ;
}
2017-07-02 21:57:24 +02:00
if ( fLoaded ) {
LogPrintf ( " block index %15dms \n " , GetTimeMillis ( ) - nStart ) ;
}
2011-05-14 20:10:21 +02:00
2017-03-01 17:05:50 +01:00
fs : : path est_path = GetDataDir ( ) / FEE_ESTIMATES_FILENAME ;
2017-03-01 17:28:39 +01:00
CAutoFile est_filein ( fsbridge : : fopen ( est_path , " rb " ) , SER_DISK , CLIENT_VERSION ) ;
2014-06-27 14:41:11 +02:00
// Allowed to fail as this file IS missing on first startup.
2014-10-20 12:45:50 +02:00
if ( ! est_filein . IsNull ( ) )
2017-02-15 21:48:48 +01:00
: : feeEstimator . Read ( est_filein ) ;
2014-09-18 14:08:43 +02:00
fFeeEstimatesInitialized = true ;
2014-03-17 13:19:54 +01:00
2012-09-18 20:30:47 +02:00
// ********************************************************* Step 8: load wallet
2013-11-29 16:04:29 +01:00
# ifdef ENABLE_WALLET
2016-09-19 16:09:38 +02:00
if ( ! CWallet : : InitLoadWallet ( ) )
return false ;
# else
2015-04-15 14:35:35 +02:00
LogPrintf ( " No wallet support compiled in! \n " ) ;
2016-09-19 16:09:38 +02:00
# endif
2015-06-30 19:22:48 +02:00
// ********************************************************* Step 9: data directory maintenance
// if pruning, unset the service bit and perform the initial blockstore prune
// after any wallet rescanning has taken place.
if ( fPruneMode ) {
LogPrintf ( " Unsetting NODE_NETWORK on prune mode \n " ) ;
2016-06-08 19:12:22 +02:00
nLocalServices = ServiceFlags ( nLocalServices & ~ NODE_NETWORK ) ;
2015-06-30 19:22:48 +02:00
if ( ! fReindex ) {
2015-10-19 20:43:04 +02:00
uiInterface . InitMessage ( _ ( " Pruning blockstore... " ) ) ;
2015-06-30 19:22:48 +02:00
PruneAndFlush ( ) ;
}
}
2016-10-19 04:33:53 +02:00
if ( chainparams . GetConsensus ( ) . vDeployments [ Consensus : : DEPLOYMENT_SEGWIT ] . nTimeout ! = 0 ) {
2017-01-18 16:15:37 +01:00
// Only advertise witness capabilities if they have a reasonable start time.
2015-11-17 00:20:49 +01:00
// This allows us to have the code merged without a defined softfork, by setting its
// end time to 0.
// Note that setting NODE_WITNESS is never required: the only downside from not
// doing so is that after activation, no upgraded nodes will fetch from you.
nLocalServices = ServiceFlags ( nLocalServices | NODE_WITNESS ) ;
// Only care about others providing witness capabilities if there is a softfork
// defined.
nRelevantServices = ServiceFlags ( nRelevantServices | NODE_WITNESS ) ;
}
2015-06-30 19:22:48 +02:00
// ********************************************************* Step 10: import blocks
2012-01-03 16:14:22 +01:00
2016-07-22 16:01:51 +02:00
if ( ! CheckDiskSpace ( ) )
return false ;
2016-07-22 15:57:25 +02:00
// Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
// No locking, as this happens before any background thread is started.
2017-08-07 07:36:37 +02:00
if ( chainActive . Tip ( ) = = nullptr ) {
2016-07-22 15:57:25 +02:00
uiInterface . NotifyBlockTip . connect ( BlockNotifyGenesisWait ) ;
} else {
fHaveGenesis = true ;
}
2017-08-01 21:17:40 +02:00
if ( gArgs . IsArgSet ( " -blocknotify " ) )
2014-08-14 18:32:34 +02:00
uiInterface . NotifyBlockTip . connect ( BlockNotifyCallback ) ;
2017-03-01 17:05:50 +01:00
std : : vector < fs : : path > vImportFiles ;
2017-03-29 17:51:41 +02:00
for ( const std : : string & strFile : gArgs . GetArgs ( " -loadblock " ) ) {
vImportFiles . push_back ( strFile ) ;
2012-09-24 19:37:03 +02:00
}
2016-07-22 15:57:25 +02:00
2013-03-07 04:31:26 +01:00
threadGroup . create_thread ( boost : : bind ( & ThreadImport , vImportFiles ) ) ;
2016-05-17 05:33:32 +02:00
// Wait for genesis block to be processed
2016-07-22 15:57:25 +02:00
{
boost : : unique_lock < boost : : mutex > lock ( cs_GenesisWait ) ;
while ( ! fHaveGenesis ) {
condvar_GenesisWait . wait ( lock ) ;
2016-05-17 05:33:32 +02:00
}
2016-07-22 15:57:25 +02:00
uiInterface . NotifyBlockTip . disconnect ( BlockNotifyGenesisWait ) ;
2014-11-08 08:18:21 +01:00
}
2012-09-24 19:37:03 +02:00
2015-06-30 19:22:48 +02:00
// ********************************************************* Step 11: start node
2011-05-14 20:10:21 +02:00
2012-05-21 16:47:29 +02:00
//// debug print
2014-05-06 15:25:01 +02:00
LogPrintf ( " mapBlockIndex.size() = %u \n " , mapBlockIndex . size ( ) ) ;
2013-10-10 23:07:44 +02:00
LogPrintf ( " nBestHeight = %d \n " , chainActive . Height ( ) ) ;
2017-08-01 21:17:40 +02:00
if ( gArgs . GetBoolArg ( " -listenonion " , DEFAULT_LISTEN_ONION ) )
2015-08-25 20:12:08 +02:00
StartTorControl ( threadGroup , scheduler ) ;
2016-05-27 05:29:39 +02:00
Discover ( threadGroup ) ;
// Map ports with UPnP
2017-08-01 21:17:40 +02:00
MapPort ( gArgs . GetBoolArg ( " -upnp " , DEFAULT_UPNP ) ) ;
2016-05-27 05:29:39 +02:00
2016-05-27 05:53:08 +02:00
CConnman : : Options connOptions ;
connOptions . nLocalServices = nLocalServices ;
connOptions . nRelevantServices = nRelevantServices ;
connOptions . nMaxConnections = nMaxConnections ;
connOptions . nMaxOutbound = std : : min ( MAX_OUTBOUND_CONNECTIONS , connOptions . nMaxConnections ) ;
2016-12-11 05:39:26 +01:00
connOptions . nMaxAddnode = MAX_ADDNODE_CONNECTIONS ;
2016-08-31 19:17:28 +02:00
connOptions . nMaxFeeler = 1 ;
2016-05-27 05:53:08 +02:00
connOptions . nBestHeight = chainActive . Height ( ) ;
connOptions . uiInterface = & uiInterface ;
2017-08-01 21:17:40 +02:00
connOptions . nSendBufferMaxSize = 1000 * gArgs . GetArg ( " -maxsendbuffer " , DEFAULT_MAXSENDBUFFER ) ;
connOptions . nReceiveFloodSize = 1000 * gArgs . GetArg ( " -maxreceivebuffer " , DEFAULT_MAXRECEIVEBUFFER ) ;
2016-05-27 05:53:08 +02:00
2016-09-12 21:04:20 +02:00
connOptions . nMaxOutboundTimeframe = nMaxOutboundTimeframe ;
connOptions . nMaxOutboundLimit = nMaxOutboundLimit ;
2017-03-29 17:51:41 +02:00
for ( const std : : string & strBind : gArgs . GetArgs ( " -bind " ) ) {
2017-06-27 03:10:39 +02:00
CService addrBind ;
if ( ! Lookup ( strBind . c_str ( ) , addrBind , GetListenPort ( ) , false ) ) {
return InitError ( ResolveErrMsg ( " bind " , strBind ) ) ;
}
connOptions . vBinds . push_back ( addrBind ) ;
2017-06-01 12:34:02 +02:00
}
2017-03-29 17:51:41 +02:00
for ( const std : : string & strBind : gArgs . GetArgs ( " -whitebind " ) ) {
2017-06-27 03:10:39 +02:00
CService addrBind ;
if ( ! Lookup ( strBind . c_str ( ) , addrBind , 0 , false ) ) {
return InitError ( ResolveErrMsg ( " whitebind " , strBind ) ) ;
}
if ( addrBind . GetPort ( ) = = 0 ) {
return InitError ( strprintf ( _ ( " Need to specify a port with -whitebind: '%s' " ) , strBind ) ) ;
}
connOptions . vWhiteBinds . push_back ( addrBind ) ;
2017-06-01 12:34:02 +02:00
}
2017-03-29 17:51:41 +02:00
for ( const auto & net : gArgs . GetArgs ( " -whitelist " ) ) {
2017-06-27 03:10:39 +02:00
CSubNet subnet ;
LookupSubNet ( net . c_str ( ) , subnet ) ;
if ( ! subnet . IsValid ( ) )
return InitError ( strprintf ( _ ( " Invalid netmask specified in -whitelist: '%s' " ) , net ) ) ;
connOptions . vWhitelistedRange . push_back ( subnet ) ;
2017-06-01 11:07:08 +02:00
}
2017-05-27 12:00:37 +02:00
if ( gArgs . IsArgSet ( " -seednode " ) ) {
connOptions . vSeedNodes = gArgs . GetArgs ( " -seednode " ) ;
}
2017-06-01 12:34:02 +02:00
if ( ! connman . Start ( scheduler , connOptions ) ) {
return false ;
}
2011-05-14 20:10:21 +02:00
2015-09-11 23:31:30 +02:00
// ********************************************************* Step 12: finished
2012-05-21 16:47:29 +02:00
2014-10-29 18:08:31 +01:00
SetRPCWarmupFinished ( ) ;
2012-05-21 16:47:29 +02:00
uiInterface . InitMessage ( _ ( " Done loading " ) ) ;
2013-11-29 16:04:29 +01:00
# ifdef ENABLE_WALLET
2016-09-09 10:42:30 +02:00
for ( CWalletRef pwallet : vpwallets ) {
pwallet - > postInitProcess ( scheduler ) ;
}
2013-11-29 16:04:29 +01:00
# endif
2011-05-14 20:10:21 +02:00
2013-03-09 18:02:57 +01:00
return ! fRequestShutdown ;
2011-05-14 20:10:21 +02:00
}