Skip to content

Commit 84ce18c

Browse files
committed
Remove unnecessary dependencies for bitcoin-cli
This commit removes all the unnecessary dependencies (key, core, netbase, sync, ...) from bitcoin-cli. To do this it shards the chain parameters into BaseParams, which contains just the RPC port and data directory (as used by utils and bitcoin-cli) and Params, with the rest.
1 parent 14f888c commit 84ce18c

19 files changed

+232
-89
lines changed

src/Makefile.am

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ endif
2020
BITCOIN_CONFIG_INCLUDES=-I$(builddir)/config
2121
BITCOIN_INCLUDES=-I$(builddir) -I$(builddir)/obj $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS)
2222

23+
LIBBITCOIN_SERVER=libbitcoin_server.a
24+
LIBBITCOIN_WALLET=libbitcoin_wallet.a
25+
LIBBITCOIN_COMMON=libbitcoin_common.a
26+
LIBBITCOIN_CLI=libbitcoin_cli.a
27+
LIBBITCOIN_UTIL=libbitcoin_util.a
28+
LIBBITCOIN_CRYPTO=crypto/libbitcoin_crypto.a
29+
LIBBITCOINQT=qt/libbitcoinqt.a
30+
2331
noinst_LIBRARIES = \
2432
libbitcoin_server.a \
2533
libbitcoin_common.a \
2634
libbitcoin_cli.a \
35+
libbitcoin_util.a \
2736
crypto/libbitcoin_crypto.a
2837
if ENABLE_WALLET
2938
BITCOIN_INCLUDES += $(BDB_CPPFLAGS)
@@ -50,6 +59,7 @@ BITCOIN_CORE_H = \
5059
base58.h \
5160
bloom.h \
5261
chainparams.h \
62+
chainparamsbase.h \
5363
checkpoints.h \
5464
checkqueue.h \
5565
clientversion.h \
@@ -107,8 +117,9 @@ obj/build.h: FORCE
107117
@$(MKDIR_P) $(builddir)/obj
108118
@$(top_srcdir)/share/genbuild.sh $(abs_top_builddir)/src/obj/build.h \
109119
$(abs_top_srcdir)
110-
libbitcoin_common_a-version.$(OBJEXT): obj/build.h
120+
libbitcoin_util_a-version.$(OBJEXT): obj/build.h
111121

122+
# server: shared between bitcoind and bitcoin-qt
112123
libbitcoin_server_a_CPPFLAGS = $(BITCOIN_INCLUDES)
113124
libbitcoin_server_a_SOURCES = \
114125
addrman.cpp \
@@ -136,6 +147,8 @@ libbitcoin_server_a_SOURCES = \
136147
$(JSON_H) \
137148
$(BITCOIN_CORE_H)
138149

150+
# wallet: shared between bitcoind and bitcoin-qt, but only linked
151+
# when wallet enabled
139152
libbitcoin_wallet_a_CPPFLAGS = $(BITCOIN_INCLUDES)
140153
libbitcoin_wallet_a_SOURCES = \
141154
db.cpp \
@@ -146,6 +159,7 @@ libbitcoin_wallet_a_SOURCES = \
146159
walletdb.cpp \
147160
$(BITCOIN_CORE_H)
148161

162+
# crypto primitives library
149163
crypto_libbitcoin_crypto_a_CPPFLAGS = $(BITCOIN_CONFIG_INCLUDES)
150164
crypto_libbitcoin_crypto_a_SOURCES = \
151165
crypto/sha1.cpp \
@@ -156,6 +170,7 @@ crypto_libbitcoin_crypto_a_SOURCES = \
156170
crypto/sha1.h \
157171
crypto/ripemd160.h
158172

173+
# common: shared between bitcoind, and bitcoin-qt and non-server tools
159174
libbitcoin_common_a_CPPFLAGS = $(BITCOIN_INCLUDES)
160175
libbitcoin_common_a_SOURCES = \
161176
base58.cpp \
@@ -166,8 +181,16 @@ libbitcoin_common_a_SOURCES = \
166181
key.cpp \
167182
netbase.cpp \
168183
protocol.cpp \
169-
rpcprotocol.cpp \
170184
script.cpp \
185+
$(BITCOIN_CORE_H)
186+
187+
# util: shared between all executables.
188+
# This library *must* be included to make sure that the glibc
189+
# backward-compatibility objects and their sanity checks are linked.
190+
libbitcoin_util_a_CPPFLAGS = $(BITCOIN_INCLUDES)
191+
libbitcoin_util_a_SOURCES = \
192+
chainparamsbase.cpp \
193+
rpcprotocol.cpp \
171194
sync.cpp \
172195
util.cpp \
173196
version.cpp \
@@ -176,22 +199,24 @@ libbitcoin_common_a_SOURCES = \
176199
$(BITCOIN_CORE_H)
177200

178201
if GLIBC_BACK_COMPAT
179-
libbitcoin_common_a_SOURCES += compat/glibc_compat.cpp
180-
libbitcoin_common_a_SOURCES += compat/glibcxx_compat.cpp
202+
libbitcoin_util_a_SOURCES += compat/glibc_compat.cpp
203+
libbitcoin_util_a_SOURCES += compat/glibcxx_compat.cpp
181204
endif
182205

206+
# cli: shared between bitcoin-cli and bitcoin-qt
183207
libbitcoin_cli_a_SOURCES = \
184208
rpcclient.cpp \
185209
$(BITCOIN_CORE_H)
186210

187-
nodist_libbitcoin_common_a_SOURCES = $(srcdir)/obj/build.h
211+
nodist_libbitcoin_util_a_SOURCES = $(srcdir)/obj/build.h
188212
#
189213

190214
# bitcoind binary #
191215
bitcoind_LDADD = \
192-
libbitcoin_server.a \
193-
libbitcoin_common.a \
194-
crypto/libbitcoin_crypto.a \
216+
$(LIBBITCOIN_SERVER) \
217+
$(LIBBITCOIN_COMMON) \
218+
$(LIBBITCOIN_UTIL) \
219+
$(LIBBITCOIN_CRYPTO) \
195220
$(LIBLEVELDB) \
196221
$(LIBMEMENV)
197222
if ENABLE_WALLET
@@ -209,11 +234,13 @@ bitcoind_CPPFLAGS = $(BITCOIN_INCLUDES)
209234

210235
# bitcoin-cli binary #
211236
bitcoin_cli_LDADD = \
212-
libbitcoin_cli.a \
213-
libbitcoin_common.a \
214-
crypto/libbitcoin_crypto.a \
237+
$(LIBBITCOIN_CLI) \
238+
$(LIBBITCOIN_COMMON) \
239+
$(LIBBITCOIN_UTIL) \
240+
$(LIBBITCOIN_CRYPTO) \
215241
$(BOOST_LIBS)
216-
bitcoin_cli_SOURCES = bitcoin-cli.cpp
242+
bitcoin_cli_SOURCES = \
243+
bitcoin-cli.cpp
217244
bitcoin_cli_CPPFLAGS = $(BITCOIN_INCLUDES)
218245
#
219246

@@ -244,13 +271,6 @@ clean-local:
244271
@test -f $(PROTOC)
245272
$(AM_V_GEN) $(PROTOC) --cpp_out=$(@D) --proto_path=$(abspath $(<D) $<)
246273

247-
LIBBITCOIN_SERVER=libbitcoin_server.a
248-
LIBBITCOIN_WALLET=libbitcoin_wallet.a
249-
LIBBITCOIN_COMMON=libbitcoin_common.a
250-
LIBBITCOIN_CLI=libbitcoin_cli.a
251-
LIBBITCOIN_CRYPTO=crypto/libbitcoin_crypto.a
252-
LIBBITCOINQT=qt/libbitcoinqt.a
253-
254274
if ENABLE_TESTS
255275
include Makefile.test.include
256276
endif

src/Makefile.qt.include

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ qt_bitcoin_qt_LDADD = qt/libbitcoinqt.a $(LIBBITCOIN_SERVER)
355355
if ENABLE_WALLET
356356
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_WALLET)
357357
endif
358-
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) $(LIBMEMENV) \
358+
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) $(LIBMEMENV) \
359359
$(BOOST_LIBS) $(QT_LIBS) $(QT_DBUS_LIBS) $(QR_LIBS) $(PROTOBUF_LIBS) $(BDB_LIBS)
360360
qt_bitcoin_qt_LDFLAGS = $(QT_LDFLAGS)
361361

@@ -364,7 +364,7 @@ QT_QM=$(QT_TS:.ts=.qm)
364364

365365
.SECONDARY: $(QT_QM)
366366

367-
qt/bitcoinstrings.cpp: $(libbitcoin_server_a_SOURCES) $(libbitcoin_common_a_SOURCES) $(libbitcoin_cli_a_SOURCES)
367+
qt/bitcoinstrings.cpp: $(libbitcoin_server_a_SOURCES) $(libbitcoin_common_a_SOURCES) $(libbitcoin_cli_a_SOURCES) $(libbitcoin_util_a_SOURCES)
368368
@test -n $(XGETTEXT) || echo "xgettext is required for updating translations"
369369
$(AM_V_GEN) cd $(top_srcdir); XGETTEXT=$(XGETTEXT) share/qt/extract_strings_qt.py
370370

src/Makefile.qttest.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ qt_test_test_bitcoin_qt_LDADD = $(LIBBITCOINQT) $(LIBBITCOIN_SERVER)
3030
if ENABLE_WALLET
3131
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_WALLET)
3232
endif
33-
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) \
33+
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) \
3434
$(LIBMEMENV) $(BOOST_LIBS) $(QT_DBUS_LIBS) $(QT_TEST_LIBS) $(QT_LIBS) \
3535
$(QR_LIBS) $(PROTOBUF_LIBS) $(BDB_LIBS)
3636
qt_test_test_bitcoin_qt_LDFLAGS = $(QT_LDFLAGS)

src/Makefile.test.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ endif
6363

6464
test_test_bitcoin_SOURCES = $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
6565
test_test_bitcoin_CPPFLAGS = $(BITCOIN_INCLUDES) -I$(builddir)/test/ $(TESTDEFS)
66-
test_test_bitcoin_LDADD = $(LIBBITCOIN_SERVER) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) $(LIBMEMENV) \
66+
test_test_bitcoin_LDADD = $(LIBBITCOIN_SERVER) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) $(LIBMEMENV) \
6767
$(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB)
6868
if ENABLE_WALLET
6969
test_test_bitcoin_LDADD += $(LIBBITCOIN_WALLET)

src/bitcoin-cli.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "rpcclient.h"
99
#include "rpcprotocol.h"
1010
#include "ui_interface.h" /* for _(...) */
11-
#include "chainparams.h"
11+
#include "chainparamsbase.h"
1212

1313
#include <boost/filesystem/operations.hpp>
1414

@@ -60,12 +60,11 @@ static bool AppInitRPC(int argc, char* argv[])
6060
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
6161
return false;
6262
}
63-
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
64-
if (!SelectParamsFromCommandLine()) {
63+
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
64+
if (!SelectBaseParamsFromCommandLine()) {
6565
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
6666
return false;
6767
}
68-
6968
if (argc<2 || mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version"))
7069
{
7170
std::string strUsage = _("Bitcoin Core RPC client version") + " " + FormatFullVersion() + "\n";
@@ -104,7 +103,7 @@ Object CallRPC(const string& strMethod, const Array& params)
104103

105104
bool fWait = GetBoolArg("-rpcwait", false); // -rpcwait means try until server has started
106105
do {
107-
bool fConnected = d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", itostr(Params().RPCPort())));
106+
bool fConnected = d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", itostr(BaseParams().RPCPort())));
108107
if (fConnected) break;
109108
if (fWait)
110109
MilliSleep(1000);

src/chainparams.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ unsigned int pnSeed[] =
9999
class CMainParams : public CChainParams {
100100
public:
101101
CMainParams() {
102-
networkID = CChainParams::MAIN;
102+
networkID = CBaseChainParams::MAIN;
103103
strNetworkID = "main";
104104
// The message start string is designed to be unlikely to occur in normal data.
105105
// The characters are rarely used upper ASCII, not valid as UTF-8, and produce
@@ -110,7 +110,6 @@ class CMainParams : public CChainParams {
110110
pchMessageStart[3] = 0xd9;
111111
vAlertPubKey = ParseHex("04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284");
112112
nDefaultPort = 8333;
113-
nRPCPort = 8332;
114113
bnProofOfWorkLimit = ~uint256(0) >> 32;
115114
nSubsidyHalvingInterval = 210000;
116115
nEnforceBlockUpgradeMajority = 750;
@@ -191,7 +190,7 @@ static CMainParams mainParams;
191190
class CTestNetParams : public CMainParams {
192191
public:
193192
CTestNetParams() {
194-
networkID = CChainParams::TESTNET;
193+
networkID = CBaseChainParams::TESTNET;
195194
strNetworkID = "test";
196195
// The message start string is designed to be unlikely to occur in normal data.
197196
// The characters are rarely used upper ASCII, not valid as UTF-8, and produce
@@ -202,14 +201,12 @@ class CTestNetParams : public CMainParams {
202201
pchMessageStart[3] = 0x07;
203202
vAlertPubKey = ParseHex("04302390343f91cc401d56d68b123028bf52e5fca1939df127f63c6467cdf9c8e2c14b61104cf817d0b780da337893ecc4aaff1309e536162dabbdb45200ca2b0a");
204203
nDefaultPort = 18333;
205-
nRPCPort = 18332;
206204
nEnforceBlockUpgradeMajority = 51;
207205
nRejectBlockOutdatedMajority = 75;
208206
nToCheckBlockUpgradeMajority = 100;
209207
nMinerThreads = 0;
210208
nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
211209
nTargetSpacing = 10 * 60;
212-
strDataDir = "testnet3";
213210

214211
// Modify the testnet genesis block so the timestamp is valid for a later start.
215212
genesis.nTime = 1296688602;
@@ -245,7 +242,7 @@ static CTestNetParams testNetParams;
245242
class CRegTestParams : public CTestNetParams {
246243
public:
247244
CRegTestParams() {
248-
networkID = CChainParams::REGTEST;
245+
networkID = CBaseChainParams::REGTEST;
249246
strNetworkID = "regtest";
250247
pchMessageStart[0] = 0xfa;
251248
pchMessageStart[1] = 0xbf;
@@ -264,7 +261,6 @@ class CRegTestParams : public CTestNetParams {
264261
genesis.nNonce = 2;
265262
hashGenesisBlock = genesis.GetHash();
266263
nDefaultPort = 18444;
267-
strDataDir = "regtest";
268264
assert(hashGenesisBlock == uint256("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
269265

270266
vSeeds.clear(); // Regtest mode doesn't have any DNS seeds.
@@ -279,21 +275,23 @@ class CRegTestParams : public CTestNetParams {
279275
};
280276
static CRegTestParams regTestParams;
281277

282-
static CChainParams *pCurrentParams = &mainParams;
278+
static CChainParams *pCurrentParams = 0;
283279

284280
const CChainParams &Params() {
281+
assert(pCurrentParams);
285282
return *pCurrentParams;
286283
}
287284

288-
void SelectParams(CChainParams::Network network) {
285+
void SelectParams(CBaseChainParams::Network network) {
286+
SelectBaseParams(network);
289287
switch (network) {
290-
case CChainParams::MAIN:
288+
case CBaseChainParams::MAIN:
291289
pCurrentParams = &mainParams;
292290
break;
293-
case CChainParams::TESTNET:
291+
case CBaseChainParams::TESTNET:
294292
pCurrentParams = &testNetParams;
295293
break;
296-
case CChainParams::REGTEST:
294+
case CBaseChainParams::REGTEST:
297295
pCurrentParams = &regTestParams;
298296
break;
299297
default:
@@ -303,19 +301,9 @@ void SelectParams(CChainParams::Network network) {
303301
}
304302

305303
bool SelectParamsFromCommandLine() {
306-
bool fRegTest = GetBoolArg("-regtest", false);
307-
bool fTestNet = GetBoolArg("-testnet", false);
308-
309-
if (fTestNet && fRegTest) {
304+
if (!SelectBaseParamsFromCommandLine())
310305
return false;
311-
}
312306

313-
if (fRegTest) {
314-
SelectParams(CChainParams::REGTEST);
315-
} else if (fTestNet) {
316-
SelectParams(CChainParams::TESTNET);
317-
} else {
318-
SelectParams(CChainParams::MAIN);
319-
}
307+
SelectParams(BaseParams().NetworkID());
320308
return true;
321309
}

src/chainparams.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define BITCOIN_CHAIN_PARAMS_H
88

99
#include "core.h"
10+
#include "chainparamsbase.h"
1011
#include "protocol.h"
1112
#include "uint256.h"
1213

@@ -29,14 +30,6 @@ struct CDNSSeedData {
2930
class CChainParams
3031
{
3132
public:
32-
enum Network {
33-
MAIN,
34-
TESTNET,
35-
REGTEST,
36-
37-
MAX_NETWORK_TYPES
38-
};
39-
4033
enum Base58Type {
4134
PUBKEY_ADDRESS,
4235
SCRIPT_ADDRESS,
@@ -73,17 +66,15 @@ class CChainParams
7366
int64_t TargetTimespan() const { return nTargetTimespan; }
7467
int64_t TargetSpacing() const { return nTargetSpacing; }
7568
int64_t Interval() const { return nTargetTimespan / nTargetSpacing; }
76-
const std::string& DataDir() const { return strDataDir; }
7769
/* Make miner stop after a block is found. In RPC, don't return
7870
* until nGenProcLimit blocks are generated */
7971
bool MineBlocksOnDemand() const { return fMineBlocksOnDemand; }
80-
Network NetworkID() const { return networkID; }
72+
CBaseChainParams::Network NetworkID() const { return networkID; }
8173
/* Return the BIP70 network string (main, test or regtest) */
8274
std::string NetworkIDString() const { return strNetworkID; }
8375
const std::vector<CDNSSeedData>& DNSSeeds() const { return vSeeds; }
8476
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
8577
const std::vector<CAddress>& FixedSeeds() const { return vFixedSeeds; }
86-
int RPCPort() const { return nRPCPort; }
8778
protected:
8879
CChainParams() {}
8980

@@ -92,19 +83,17 @@ class CChainParams
9283
// Raw pub key bytes for the broadcast alert signing key.
9384
std::vector<unsigned char> vAlertPubKey;
9485
int nDefaultPort;
95-
int nRPCPort;
9686
uint256 bnProofOfWorkLimit;
9787
int nSubsidyHalvingInterval;
9888
int nEnforceBlockUpgradeMajority;
9989
int nRejectBlockOutdatedMajority;
10090
int nToCheckBlockUpgradeMajority;
10191
int64_t nTargetTimespan;
10292
int64_t nTargetSpacing;
103-
std::string strDataDir;
10493
int nMinerThreads;
10594
std::vector<CDNSSeedData> vSeeds;
10695
std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
107-
Network networkID;
96+
CBaseChainParams::Network networkID;
10897
std::string strNetworkID;
10998
CBlock genesis;
11099
std::vector<CAddress> vFixedSeeds;
@@ -123,7 +112,7 @@ class CChainParams
123112
const CChainParams &Params();
124113

125114
/** Sets the params returned by Params() to those for the given network. */
126-
void SelectParams(CChainParams::Network network);
115+
void SelectParams(CBaseChainParams::Network network);
127116

128117
/**
129118
* Looks for -regtest or -testnet and then calls SelectParams as appropriate.

0 commit comments

Comments
 (0)