Skip to content

Commit d47bbbd

Browse files
committed
Merge pull request #3368 from laanwj/2013_11_disable_wallet_mining
Allow mining RPCs with --disable-wallet
2 parents 955787f + fbc7a96 commit d47bbbd

File tree

8 files changed

+104
-82
lines changed

8 files changed

+104
-82
lines changed

doc/build-unix.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,7 @@ disable-wallet mode with:
187187
./configure --disable-wallet
188188

189189
In this case there is no dependency on Berkeley DB 4.8.
190+
191+
Mining is also possible in disable-wallet mode, but only using the `getblocktemplate` RPC
192+
call not `getwork`.
193+

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ libbitcoin_server_a_SOURCES = \
4848
keystore.cpp \
4949
leveldbwrapper.cpp \
5050
main.cpp \
51+
miner.cpp \
5152
net.cpp \
5253
noui.cpp \
5354
rpcblockchain.cpp \
55+
rpcmining.cpp \
5456
rpcnet.cpp \
5557
rpcrawtransaction.cpp \
5658
txdb.cpp \
@@ -61,9 +63,7 @@ libbitcoin_server_a_SOURCES = \
6163
libbitcoin_wallet_a_SOURCES = \
6264
db.cpp \
6365
crypter.cpp \
64-
miner.cpp \
6566
rpcdump.cpp \
66-
rpcmining.cpp \
6767
rpcwallet.cpp \
6868
wallet.cpp \
6969
walletdb.cpp \

src/init.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ void Shutdown()
113113
RenameThread("bitcoin-shutoff");
114114
mempool.AddTransactionsUpdated(1);
115115
StopRPCThreads();
116-
#ifdef ENABLE_WALLET
117116
ShutdownRPCMining();
117+
#ifdef ENABLE_WALLET
118118
if (pwalletMain)
119119
bitdb.Flush(false);
120120
GenerateBitcoins(false, NULL, 0);
@@ -1042,10 +1042,8 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer)
10421042
#endif
10431043

10441044
StartNode(threadGroup);
1045-
#ifdef ENABLE_WALLET
10461045
// InitRPCMining is needed here so getwork/getblocktemplate in the GUI debug console works properly.
10471046
InitRPCMining();
1048-
#endif
10491047
if (fServer)
10501048
StartRPCThreads();
10511049

src/miner.cpp

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include "net.h"
1111
#include "wallet.h"
1212

13-
double dHashesPerSec = 0.0;
14-
int64_t nHPSTimerStart = 0;
15-
1613
//////////////////////////////////////////////////////////////////////////////
1714
//
1815
// BitcoinMiner
@@ -54,41 +51,6 @@ void SHA256Transform(void* pstate, void* pinput, const void* pinit)
5451
((uint32_t*)pstate)[i] = ctx.h[i];
5552
}
5653

57-
//
58-
// ScanHash scans nonces looking for a hash with at least some zero bits.
59-
// It operates on big endian data. Caller does the byte reversing.
60-
// All input buffers are 16-byte aligned. nNonce is usually preserved
61-
// between calls, but periodically or if nNonce is 0xffff0000 or above,
62-
// the block is rebuilt and nNonce starts over at zero.
63-
//
64-
unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone)
65-
{
66-
unsigned int& nNonce = *(unsigned int*)(pdata + 12);
67-
for (;;)
68-
{
69-
// Crypto++ SHA256
70-
// Hash pdata using pmidstate as the starting state into
71-
// pre-formatted buffer phash1, then hash phash1 into phash
72-
nNonce++;
73-
SHA256Transform(phash1, pdata, pmidstate);
74-
SHA256Transform(phash, phash1, pSHA256InitState);
75-
76-
// Return the nonce if the hash has at least some zero bits,
77-
// caller will check if it has enough to reach the target
78-
if (((unsigned short*)phash)[14] == 0)
79-
return nNonce;
80-
81-
// If nothing found after trying for a while, return -1
82-
if ((nNonce & 0xffff) == 0)
83-
{
84-
nHashesDone = 0xffff+1;
85-
return (unsigned int) -1;
86-
}
87-
if ((nNonce & 0xfff) == 0)
88-
boost::this_thread::interruption_point();
89-
}
90-
}
91-
9254
// Some explaining would be appreciated
9355
class COrphan
9456
{
@@ -381,16 +343,6 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
381343
return pblocktemplate.release();
382344
}
383345

384-
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
385-
{
386-
CPubKey pubkey;
387-
if (!reservekey.GetReservedKey(pubkey))
388-
return NULL;
389-
390-
CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
391-
return CreateNewBlock(scriptPubKey);
392-
}
393-
394346
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
395347
{
396348
// Update nExtraNonce
@@ -454,6 +406,58 @@ void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash
454406
memcpy(phash1, &tmp.hash1, 64);
455407
}
456408

409+
#ifdef ENABLE_WALLET
410+
//////////////////////////////////////////////////////////////////////////////
411+
//
412+
// Internal miner
413+
//
414+
double dHashesPerSec = 0.0;
415+
int64_t nHPSTimerStart = 0;
416+
417+
//
418+
// ScanHash scans nonces looking for a hash with at least some zero bits.
419+
// It operates on big endian data. Caller does the byte reversing.
420+
// All input buffers are 16-byte aligned. nNonce is usually preserved
421+
// between calls, but periodically or if nNonce is 0xffff0000 or above,
422+
// the block is rebuilt and nNonce starts over at zero.
423+
//
424+
unsigned int static ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone)
425+
{
426+
unsigned int& nNonce = *(unsigned int*)(pdata + 12);
427+
for (;;)
428+
{
429+
// Crypto++ SHA256
430+
// Hash pdata using pmidstate as the starting state into
431+
// pre-formatted buffer phash1, then hash phash1 into phash
432+
nNonce++;
433+
SHA256Transform(phash1, pdata, pmidstate);
434+
SHA256Transform(phash, phash1, pSHA256InitState);
435+
436+
// Return the nonce if the hash has at least some zero bits,
437+
// caller will check if it has enough to reach the target
438+
if (((unsigned short*)phash)[14] == 0)
439+
return nNonce;
440+
441+
// If nothing found after trying for a while, return -1
442+
if ((nNonce & 0xffff) == 0)
443+
{
444+
nHashesDone = 0xffff+1;
445+
return (unsigned int) -1;
446+
}
447+
if ((nNonce & 0xfff) == 0)
448+
boost::this_thread::interruption_point();
449+
}
450+
}
451+
452+
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey)
453+
{
454+
CPubKey pubkey;
455+
if (!reservekey.GetReservedKey(pubkey))
456+
return NULL;
457+
458+
CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
459+
return CreateNewBlock(scriptPubKey);
460+
}
457461

458462
bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
459463
{
@@ -665,5 +669,5 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads)
665669
minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet));
666670
}
667671

668-
672+
#endif
669673

src/rpcmining.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
using namespace json_spirit;
2121
using namespace std;
2222

23-
// Key used by getwork/getblocktemplate miners.
23+
#ifdef ENABLE_WALLET
24+
// Key used by getwork miners.
2425
// Allocated in InitRPCMining, free'd in ShutdownRPCMining
2526
static CReserveKey* pMiningKey = NULL;
2627

@@ -40,6 +41,14 @@ void ShutdownRPCMining()
4041

4142
delete pMiningKey; pMiningKey = NULL;
4243
}
44+
#else
45+
void InitRPCMining()
46+
{
47+
}
48+
void ShutdownRPCMining()
49+
{
50+
}
51+
#endif
4352

4453
// Return average network hashes per second based on the last 'lookup' blocks,
4554
// or from the last difficulty change if 'lookup' is nonpositive.
@@ -99,7 +108,7 @@ Value getnetworkhashps(const Array& params, bool fHelp)
99108
return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
100109
}
101110

102-
111+
#ifdef ENABLE_WALLET
103112
Value getgenerate(const Array& params, bool fHelp)
104113
{
105114
if (fHelp || params.size() != 0)
@@ -197,7 +206,6 @@ Value setgenerate(const Array& params, bool fHelp)
197206
return Value::null;
198207
}
199208

200-
201209
Value gethashespersec(const Array& params, bool fHelp)
202210
{
203211
if (fHelp || params.size() != 0)
@@ -216,6 +224,7 @@ Value gethashespersec(const Array& params, bool fHelp)
216224
return (boost::int64_t)0;
217225
return (boost::int64_t)dHashesPerSec;
218226
}
227+
#endif
219228

220229

221230
Value getmininginfo(const Array& params, bool fHelp)
@@ -248,16 +257,19 @@ Value getmininginfo(const Array& params, bool fHelp)
248257
obj.push_back(Pair("currentblocktx", (uint64_t)nLastBlockTx));
249258
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
250259
obj.push_back(Pair("errors", GetWarnings("statusbar")));
251-
obj.push_back(Pair("generate", getgenerate(params, false)));
252260
obj.push_back(Pair("genproclimit", (int)GetArg("-genproclimit", -1)));
253-
obj.push_back(Pair("hashespersec", gethashespersec(params, false)));
254261
obj.push_back(Pair("networkhashps", getnetworkhashps(params, false)));
255262
obj.push_back(Pair("pooledtx", (uint64_t)mempool.size()));
256263
obj.push_back(Pair("testnet", TestNet()));
264+
#ifdef ENABLE_WALLET
265+
obj.push_back(Pair("generate", getgenerate(params, false)));
266+
obj.push_back(Pair("hashespersec", gethashespersec(params, false)));
267+
#endif
257268
return obj;
258269
}
259270

260271

272+
#ifdef ENABLE_WALLET
261273
Value getwork(const Array& params, bool fHelp)
262274
{
263275
if (fHelp || params.size() > 1)
@@ -381,7 +393,7 @@ Value getwork(const Array& params, bool fHelp)
381393
return CheckWork(pblock, *pwalletMain, *pMiningKey);
382394
}
383395
}
384-
396+
#endif
385397

386398
Value getblocktemplate(const Array& params, bool fHelp)
387399
{

src/rpcserver.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,14 @@ static const CRPCCommand vRPCCommands[] =
248248
{ "gettxout", &gettxout, true, false, false },
249249
{ "verifychain", &verifychain, true, false, false },
250250

251-
#ifdef ENABLE_WALLET
251+
/* Mining */
252252
{ "getnetworkhashps", &getnetworkhashps, true, false, false },
253-
{ "getgenerate", &getgenerate, true, false, false },
254-
{ "setgenerate", &setgenerate, true, true, false },
255-
{ "gethashespersec", &gethashespersec, true, false, false },
256253
{ "getmininginfo", &getmininginfo, true, false, false },
254+
{ "getblocktemplate", &getblocktemplate, true, false, false },
255+
{ "submitblock", &submitblock, false, false, false },
256+
257+
#ifdef ENABLE_WALLET
258+
/* Wallet */
257259
{ "getnewaddress", &getnewaddress, true, false, true },
258260
{ "getaccountaddress", &getaccountaddress, true, false, true },
259261
{ "getrawchangeaddress", &getrawchangeaddress, true, false, true },
@@ -283,10 +285,7 @@ static const CRPCCommand vRPCCommands[] =
283285
{ "listaddressgroupings", &listaddressgroupings, false, false, true },
284286
{ "signmessage", &signmessage, false, false, true },
285287
{ "verifymessage", &verifymessage, false, false, false },
286-
{ "getwork", &getwork, true, false, true },
287288
{ "listaccounts", &listaccounts, false, false, true },
288-
{ "getblocktemplate", &getblocktemplate, true, false, false },
289-
{ "submitblock", &submitblock, false, false, false },
290289
{ "listsinceblock", &listsinceblock, false, false, true },
291290
{ "dumpprivkey", &dumpprivkey, true, false, true },
292291
{ "dumpwallet", &dumpwallet, true, false, true },
@@ -295,6 +294,12 @@ static const CRPCCommand vRPCCommands[] =
295294
{ "listunspent", &listunspent, false, false, true },
296295
{ "lockunspent", &lockunspent, false, false, true },
297296
{ "listlockunspent", &listlockunspent, false, false, true },
297+
298+
/* Wallet-enabled mining */
299+
{ "getgenerate", &getgenerate, true, false, false },
300+
{ "setgenerate", &setgenerate, true, true, false },
301+
{ "gethashespersec", &gethashespersec, true, false, false },
302+
{ "getwork", &getwork, true, false, true },
298303
#endif // ENABLE_WALLET
299304
};
300305

src/test/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ test_bitcoin_SOURCES = alert_tests.cpp \
3131
allocator_tests.cpp base32_tests.cpp base58_tests.cpp base64_tests.cpp \
3232
bignum_tests.cpp bloom_tests.cpp canonical_tests.cpp checkblock_tests.cpp \
3333
Checkpoints_tests.cpp compress_tests.cpp DoS_tests.cpp getarg_tests.cpp \
34-
key_tests.cpp mruset_tests.cpp multisig_tests.cpp \
34+
key_tests.cpp miner_tests.cpp mruset_tests.cpp multisig_tests.cpp \
3535
netbase_tests.cpp pmt_tests.cpp rpc_tests.cpp script_P2SH_tests.cpp \
3636
script_tests.cpp serialize_tests.cpp sigopcount_tests.cpp test_bitcoin.cpp \
3737
transaction_tests.cpp uint160_tests.cpp uint256_tests.cpp util_tests.cpp \
3838
sighash_tests.cpp $(JSON_TEST_FILES) $(RAW_TEST_FILES)
3939

4040
if ENABLE_WALLET
41-
test_bitcoin_SOURCES += accounting_tests.cpp wallet_tests.cpp miner_tests.cpp rpc_wallet_tests.cpp
41+
test_bitcoin_SOURCES += accounting_tests.cpp wallet_tests.cpp rpc_wallet_tests.cpp
4242
endif
4343

4444
nodist_test_bitcoin_SOURCES = $(BUILT_SOURCES)

0 commit comments

Comments
 (0)