Skip to content

Commit a3975c5

Browse files
Mike Hearnrecursive-rat4
authored andcommitted
Introduce a CChainParameters singleton class and regtest mode.
The new class is accessed via the Params() method and holds most things that vary between main, test and regtest networks. The regtest mode has two purposes, one is to run the bitcoind/bitcoinj comparison tool which compares two separate implementations of the Bitcoin protocol looking for divergence. The other is that when run, you get a local node which can mine a single block instantly, which is highly convenient for testing apps during development as there's no need to wait 10 minutes for a block on the testnet. Conflicts: blackcoin-qt.pro src/alert.cpp src/base58.h src/bitcoinrpc.cpp src/checkpoints.cpp src/init.cpp src/main.cpp src/main.h src/makefile.unix src/net.cpp src/protocol.h src/rpcmining.cpp src/rpcwallet.cpp src/txdb.cpp src/util.cpp
1 parent 62fe466 commit a3975c5

31 files changed

Lines changed: 457 additions & 302 deletions

blackcoin-qt.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ HEADERS += src/qt/bitcoingui.h \
154154
src/addrman.h \
155155
src/base58.h \
156156
src/bignum.h \
157+
src/chainparams.h \
157158
src/chainparamsseeds.h \
158159
src/checkpoints.h \
159160
src/compat.h \
@@ -233,6 +234,7 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
233234
src/qt/editaddressdialog.cpp \
234235
src/qt/bitcoinaddressvalidator.cpp \
235236
src/alert.cpp \
237+
src/chainparams.cpp \
236238
src/version.cpp \
237239
src/sync.cpp \
238240
src/util.cpp \

src/alert.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ using namespace std;
1919
map<uint256, CAlert> mapAlerts;
2020
CCriticalSection cs_mapAlerts;
2121

22-
static const char* pszMainKey = "0486bce1bac0d543f104cbff2bd23680056a3b9ea05e1137d2ff90eeb5e08472eb500322593a2cb06fbf8297d7beb6cd30cb90f98153b5b7cce1493749e41e0284";
23-
24-
// TestNet alerts pubKey
25-
static const char* pszTestKey = "0471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496";
26-
27-
// TestNet alerts private key
28-
// "308201130201010420b665cff1884e53da26376fd1b433812c9a5a8a4d5221533b15b9629789bb7e42a081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a1440342000471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496"
29-
3022
void CUnsignedAlert::SetNull()
3123
{
3224
nVersion = 1;
@@ -149,7 +141,7 @@ bool CAlert::RelayTo(CNode* pnode) const
149141

150142
bool CAlert::CheckSignature() const
151143
{
152-
CPubKey key(ParseHex(fTestNet ? pszTestKey : pszMainKey));
144+
CPubKey key(Params().AlertKey());
153145
if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
154146
return error("CAlert::CheckSignature() : verify signature failed");
155147

src/base58.h

Lines changed: 23 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <string>
1919
#include <vector>
2020

21+
#include "chainparams.h"
2122
#include "bignum.h"
2223
#include "key.h"
2324
#include "script.h"
@@ -271,21 +272,13 @@ class CBitcoinAddressVisitor : public boost::static_visitor<bool>
271272
class CBitcoinAddress : public CBase58Data
272273
{
273274
public:
274-
enum
275-
{
276-
PUBKEY_ADDRESS = 25,
277-
SCRIPT_ADDRESS = 85,
278-
PUBKEY_ADDRESS_TEST = 111,
279-
SCRIPT_ADDRESS_TEST = 196,
280-
};
281-
282275
bool Set(const CKeyID &id) {
283-
SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20);
276+
SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);
284277
return true;
285278
}
286279

287280
bool Set(const CScriptID &id) {
288-
SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20);
281+
SetData(Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS), &id, 20);
289282
return true;
290283
}
291284

@@ -296,32 +289,10 @@ class CBitcoinAddress : public CBase58Data
296289

297290
bool IsValid() const
298291
{
299-
unsigned int nExpectedSize = 20;
300-
bool fExpectTestNet = false;
301-
switch(nVersion)
302-
{
303-
case PUBKEY_ADDRESS:
304-
nExpectedSize = 20; // Hash of public key
305-
fExpectTestNet = false;
306-
break;
307-
case SCRIPT_ADDRESS:
308-
nExpectedSize = 20; // Hash of CScript
309-
fExpectTestNet = false;
310-
break;
311-
312-
case PUBKEY_ADDRESS_TEST:
313-
nExpectedSize = 20;
314-
fExpectTestNet = true;
315-
break;
316-
case SCRIPT_ADDRESS_TEST:
317-
nExpectedSize = 20;
318-
fExpectTestNet = true;
319-
break;
320-
321-
default:
322-
return false;
323-
}
324-
return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
292+
bool fCorrectSize = vchData.size() == 20;
293+
bool fKnownVersion = nVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS) ||
294+
nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
295+
return fCorrectSize && fKnownVersion;
325296
}
326297

327298
CBitcoinAddress()
@@ -346,48 +317,27 @@ class CBitcoinAddress : public CBase58Data
346317
CTxDestination Get() const {
347318
if (!IsValid())
348319
return CNoDestination();
349-
switch (nVersion) {
350-
case PUBKEY_ADDRESS:
351-
case PUBKEY_ADDRESS_TEST: {
352-
uint160 id;
353-
memcpy(&id, &vchData[0], 20);
320+
uint160 id;
321+
memcpy(&id, &vchData[0], 20);
322+
if (nVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
354323
return CKeyID(id);
355-
}
356-
case SCRIPT_ADDRESS:
357-
case SCRIPT_ADDRESS_TEST: {
358-
uint160 id;
359-
memcpy(&id, &vchData[0], 20);
324+
else if (nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
360325
return CScriptID(id);
361-
}
362-
}
363-
return CNoDestination();
326+
else
327+
return CNoDestination();
364328
}
365329

366330
bool GetKeyID(CKeyID &keyID) const {
367-
if (!IsValid())
331+
if (!IsValid() || nVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
368332
return false;
369-
switch (nVersion) {
370-
case PUBKEY_ADDRESS:
371-
case PUBKEY_ADDRESS_TEST: {
372-
uint160 id;
373-
memcpy(&id, &vchData[0], 20);
374-
keyID = CKeyID(id);
375-
return true;
376-
}
377-
default: return false;
378-
}
333+
uint160 id;
334+
memcpy(&id, &vchData[0], 20);
335+
keyID = CKeyID(id);
336+
return true;
379337
}
380338

381339
bool IsScript() const {
382-
if (!IsValid())
383-
return false;
384-
switch (nVersion) {
385-
case SCRIPT_ADDRESS:
386-
case SCRIPT_ADDRESS_TEST: {
387-
return true;
388-
}
389-
default: return false;
390-
}
340+
return IsValid() && nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
391341
}
392342
};
393343

@@ -402,7 +352,7 @@ class CBitcoinSecret : public CBase58Data
402352
void SetKey(const CKey& vchSecret)
403353
{
404354
assert(vchSecret.IsValid());
405-
SetData(128 + (fTestNet ? CBitcoinAddress::PUBKEY_ADDRESS_TEST : CBitcoinAddress::PUBKEY_ADDRESS), vchSecret.begin(), vchSecret.size());
355+
SetData(Params().Base58Prefix(CChainParams::SECRET_KEY), vchSecret.begin(), vchSecret.size());
406356
if (vchSecret.IsCompressed())
407357
vchData.push_back(1);
408358
}
@@ -416,20 +366,9 @@ class CBitcoinSecret : public CBase58Data
416366

417367
bool IsValid() const
418368
{
419-
bool fExpectTestNet = false;
420-
switch(nVersion)
421-
{
422-
case (128 + CBitcoinAddress::PUBKEY_ADDRESS):
423-
break;
424-
425-
case (128 + CBitcoinAddress::PUBKEY_ADDRESS_TEST):
426-
fExpectTestNet = true;
427-
break;
428-
429-
default:
430-
return false;
431-
}
432-
return fExpectTestNet == fTestNet && (vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1));
369+
bool fExpectedFormat = vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1);
370+
bool fCorrectVersion = nVersion == Params().Base58Prefix(CChainParams::SECRET_KEY);
371+
return fExpectedFormat && fCorrectVersion;
433372
}
434373

435374
bool SetString(const char* pszSecret)

src/bitcoind.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ bool AppInit(int argc, char* argv[])
7070

7171
if (fCommandLine)
7272
{
73+
if (!SelectParamsFromCommandLine()) {
74+
fprintf(stderr, "Error: invalid combination of -regtest and -testnet.\n");
75+
return false;
76+
}
7377
int ret = CommandLineRPC(argc, argv);
7478
exit(ret);
7579
}

src/bitcoinrpc.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Distributed under the MIT/X11 software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
#include "chainparams.h"
67
#include "init.h"
78
#include "util.h"
89
#include "sync.h"
@@ -40,11 +41,6 @@ static map<string, boost::shared_ptr<deadline_timer> > deadlineTimers;
4041
static ssl::context* rpc_ssl_context = NULL;
4142
static boost::thread_group* rpc_worker_group = NULL;
4243

43-
static inline unsigned short GetDefaultRPCPort()
44-
{
45-
return GetBoolArg("-testnet", false) ? 25715 : 15715;
46-
}
47-
4844
Object JSONRPCError(int code, const string& message)
4945
{
5046
Object error;
@@ -764,8 +760,8 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol,
764760
void StartRPCThreads()
765761
{
766762
strRPCUserColonPass = mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"];
767-
if ((mapArgs["-rpcpassword"] == "") ||
768-
(mapArgs["-rpcuser"] == mapArgs["-rpcpassword"]))
763+
if (((mapArgs["-rpcpassword"] == "") ||
764+
(mapArgs["-rpcuser"] == mapArgs["-rpcpassword"])) && Params().RequireRPCPassword())
769765
{
770766
unsigned char rand_pwd[32];
771767
RAND_bytes(rand_pwd, 32);
@@ -819,7 +815,7 @@ void StartRPCThreads()
819815
// Try a dual IPv6/IPv4 socket, falling back to separate IPv4 and IPv6 sockets
820816
const bool loopback = !mapArgs.count("-rpcallowip");
821817
asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any();
822-
ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", GetDefaultRPCPort()));
818+
ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", Params().RPCPort()));
823819
boost::system::error_code v6_only_error;
824820
boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(*rpc_io_service));
825821

@@ -1112,7 +1108,7 @@ Object CallRPC(const string& strMethod, const Array& params)
11121108
asio::ssl::stream<asio::ip::tcp::socket> sslStream(io_service, context);
11131109
SSLIOStreamDevice<asio::ip::tcp> d(sslStream, fUseSSL);
11141110
iostreams::stream< SSLIOStreamDevice<asio::ip::tcp> > stream(d);
1115-
if (!d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", itostr(GetDefaultRPCPort()))))
1111+
if (!d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", itostr(Params().RPCPort()))))
11161112
throw runtime_error("couldn't connect to server");
11171113

11181114
// HTTP basic authentication

0 commit comments

Comments
 (0)