Skip to content

Commit 7e10853

Browse files
committed
Use separate miner wallets
1 parent a9f9333 commit 7e10853

File tree

8 files changed

+84
-62
lines changed

8 files changed

+84
-62
lines changed

src/miner/internal/miner-base.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@
1616
#include <assert.h>
1717

1818

19-
MinerBase::MinerBase(MinerContextRef ctx, std::size_t device_index) : _ctx(ctx), _device_index(device_index){};
19+
MinerBase::MinerBase(MinerContextRef ctx, std::size_t device_index)
20+
: _ctx(ctx),
21+
_device_index(device_index)
22+
{
23+
GetMainSignals().ScriptForMining(_coinbase_script);
24+
// Throw an error if no script was provided. This can happen
25+
// due to some internal error but also if the keypool is empty.
26+
// In the latter case, already the pointer is NULL.
27+
if (!_coinbase_script || _coinbase_script->reserveScript.empty()) {
28+
throw std::runtime_error("No coinbase script available (mining requires a wallet)");
29+
}
30+
};
2031

2132
void MinerBase::Loop()
2233
{
@@ -32,15 +43,17 @@ void MinerBase::Loop()
3243
try {
3344
while (true) {
3445
// Update block and tip if changed
35-
if (block_flag != _ctx->block_flag()) {
46+
if (block_flag != _ctx->shared->block_flag()) {
3647
// set new block template
37-
block_template = _ctx->block_template();
48+
block_template = _ctx->shared->block_template();
3849
block = block_template->block;
50+
// set block reserve script
51+
SetBlockPubkeyScript(block, _coinbase_script->reserveScript);
3952
// set block flag only after template
4053
// so we've waited for RecreateBlock
41-
block_flag = _ctx->block_flag();
42-
// block chain tip
43-
chain_tip = _ctx->tip();
54+
block_flag = _ctx->shared->block_flag();
55+
// block template chain tip
56+
chain_tip = _ctx->shared->tip();
4457
}
4558
// Make sure we have a tip
4659
assert(chain_tip != nullptr);
@@ -60,19 +73,19 @@ void MinerBase::Loop()
6073
// Check for stop or if block needs to be rebuilt
6174
boost::this_thread::interruption_point();
6275
// Check if block was recreated
63-
if (block_flag != _ctx->block_flag()) {
76+
if (block_flag != _ctx->shared->block_flag()) {
6477
break;
6578
}
6679
// Recreate block if nonce too big
6780
if (block.nNonce >= 0xffff0000) {
68-
_ctx->RecreateBlock();
81+
_ctx->shared->RecreateBlock();
6982
break;
7083
}
7184
// Update block time
7285
if (UpdateTime(block, _ctx->chainparams().GetConsensus(), chain_tip) < 0) {
7386
// Recreate the block if the clock has run backwards,
7487
// so that we can use the correct time.
75-
_ctx->RecreateBlock();
88+
_ctx->shared->RecreateBlock();
7689
break;
7790
}
7891
if (_ctx->chainparams().GetConsensus().fPowAllowMinDifficultyBlocks) {
@@ -97,7 +110,7 @@ void MinerBase::ProcessFoundSolution(const CBlock& block, const uint256& hash)
97110
LogPrintf("DynamicMiner%s:\n proof-of-work found \n hash: %s \ntarget: %s\n", DeviceName(), hash.GetHex(), _hash_target.GetHex());
98111
ProcessBlockFound(block, _ctx->chainparams());
99112
SetThreadPriority(THREAD_PRIORITY_LOWEST);
100-
_ctx->coinbase_script()->KeepScript();
113+
_coinbase_script->KeepScript();
101114

102115
// TODO: it needs to close all miners
103116
// In regression test mode, stop mining after a block is found.

src/miner/internal/miner-base.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cstddef>
1212

1313
class CBlock;
14+
class CReserveScript;
1415

1516
/**
1617
* Base miner class for CPU and GPU miner.
@@ -49,6 +50,10 @@ class MinerBase
4950

5051
// Extra block nonce
5152
unsigned int _extra_nonce = 0;
53+
54+
// Miner coinbase script
55+
// Includes wallet payout key
56+
std::shared_ptr<CReserveScript> _coinbase_script{nullptr};
5257
};
5358

5459
#endif // DYNAMIC_INTERNAL_MINER_BASE_H

src/miner/internal/miner-context.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
// Copyright (c) 2016-2018 Duality Blockchain Solutions Developers
2-
// Copyright (c) 2014-2018 The Dash Core Developers
3-
// Copyright (c) 2009-2018 The Bitcoin Developers
4-
// Copyright (c) 2009-2018 Satoshi Nakamoto
52
// Distributed under the MIT/X11 software license, see the accompanying
63
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
74

85
#include "miner/internal/miner-context.h"
96
#include "miner/miner-util.h"
107
#include "validation.h"
11-
#include "validationinterface.h"
12-
13-
void MinerSharedContext::InitializeCoinbaseScript()
14-
{
15-
boost::unique_lock<boost::shared_mutex> guard(_mutex);
16-
GetMainSignals().ScriptForMining(_coinbase_script);
17-
// Throw an error if no script was provided. This can happen
18-
// due to some internal error but also if the keypool is empty.
19-
// In the latter case, already the pointer is NULL.
20-
if (!_coinbase_script || _coinbase_script->reserveScript.empty()) {
21-
throw std::runtime_error("No coinbase script available (mining requires a wallet)");
22-
}
23-
}
248

259
void MinerSharedContext::RecreateBlock()
2610
{
@@ -31,5 +15,5 @@ void MinerSharedContext::RecreateBlock()
3115
// for the new block to be created
3216
boost::unique_lock<boost::shared_mutex> guard(_mutex);
3317
_chain_tip = chainActive.Tip();
34-
_block_template = CreateNewBlock(chainparams, _coinbase_script->reserveScript);
18+
_block_template = CreateNewBlock(chainparams);
3519
}

src/miner/internal/miner-context.h

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class CBlock;
1616
class CChainParams;
1717
class CConnman;
1818
class CBlockIndex;
19-
class CReserveScript;
2019
struct CBlockTemplate;
2120

2221
class MinerBase;
@@ -34,33 +33,37 @@ struct MinerSharedContext {
3433
MinerSharedContext(const CChainParams& chainparams_, CConnman& connman_)
3534
: chainparams(chainparams_), connman(connman_){};
3635

37-
protected:
38-
friend class MinerContext;
39-
40-
// recreates miners block template
41-
void RecreateBlock();
42-
void InitializeCoinbaseScript();
43-
36+
// Returns chain tip of current block template
4437
CBlockIndex* tip() const { return _chain_tip; }
38+
39+
// Returns miner block flag
40+
// It's incremented every time new template is generated
4541
uint64_t block_flag() const { return _block_flag; }
46-
std::shared_ptr<CReserveScript> coinbase_script() const { return _coinbase_script; }
42+
43+
// Returns true if block was created
4744
bool has_block() const { return _block_template != nullptr; }
4845

46+
// Returns miner block template
4947
std::shared_ptr<CBlockTemplate> block_template()
5048
{
5149
boost::shared_lock<boost::shared_mutex> guard(_mutex);
5250
return _block_template;
5351
}
5452

53+
protected:
54+
friend class MinerBase;
55+
friend class MinersController;
56+
57+
// recreates miners block template
58+
void RecreateBlock();
59+
5560
private:
5661
// current block chain tip
5762
std::atomic<CBlockIndex*> _chain_tip{nullptr};
5863
// atomic flag incremented on recreated block
5964
std::atomic<std::uint64_t> _block_flag{0};
6065
// shared block template for miners
6166
std::shared_ptr<CBlockTemplate> _block_template{nullptr};
62-
// coinbase script for all miners
63-
std::shared_ptr<CReserveScript> _coinbase_script{nullptr};
6467
// mutex protecting multiple threads recreating block
6568
mutable boost::shared_mutex _mutex;
6669
};
@@ -82,26 +85,18 @@ class MinerContext
8285
MinerContext(MinerSharedContextRef shared_, HashRateCounterRef counter_)
8386
: counter(counter_), shared(shared_){};
8487

85-
/* Constructs child context */
88+
// Constructs child context
8689
explicit MinerContext(const MinerContext* ctx_)
8790
: MinerContext(ctx_->shared, ctx_->counter->MakeChild()){};
8891

89-
/** Creates child context for group or miner */
92+
// Creates child context for group or miner
9093
MinerContextRef MakeChild() const { return std::make_shared<MinerContext>(this); }
9194

92-
/** Recreates block for all miners */
93-
void RecreateBlock() { shared->RecreateBlock(); }
94-
95-
/** Initializes coinbase script for all miners */
96-
void InitializeCoinbaseScript() { shared->InitializeCoinbaseScript(); }
97-
95+
// Connection manager
9896
CConnman& connman() const { return shared->connman; }
97+
98+
// Chain parameters
9999
const CChainParams& chainparams() const { return shared->chainparams; }
100-
CBlockIndex* tip() const { return shared->tip(); }
101-
uint64_t block_flag() const { return shared->block_flag(); }
102-
std::shared_ptr<CBlockTemplate> block_template() const { return shared->block_template(); }
103-
std::shared_ptr<CReserveScript> coinbase_script() const { return shared->coinbase_script(); }
104-
bool has_block() const { return shared->has_block(); }
105100
};
106101

107102
#endif // DYNAMIC_INTERNAL_MINER_CONTEXT_H

src/miner/internal/miners-controller.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ MinersController::MinersController(MinerContextRef ctx)
3030
_connected(!_ctx->chainparams().MiningRequiresPeers()),
3131
_downloaded(!_ctx->chainparams().MiningRequiresPeers())
3232
{
33-
_ctx->InitializeCoinbaseScript();
3433
ConnectMinerSignals(this);
3534
};
3635

@@ -77,7 +76,7 @@ void MinersController::NotifyBlock(const CBlockIndex* index_new, const CBlockInd
7776
// Create new block template for miners
7877
_last_sync_time = GetTime();
7978
_last_txn_time = mempool.GetTransactionsUpdated();
80-
_ctx->RecreateBlock();
79+
_ctx->shared->RecreateBlock();
8180
// start miners
8281
if (can_start()) {
8382
_group_cpu.Start();
@@ -95,6 +94,6 @@ void MinersController::NotifyTransaction(const CTransaction& txn, const CBlockIn
9594
}
9695
if (GetTime() - _last_txn_time > 60) {
9796
_last_txn_time = latest_txn;
98-
_ctx->RecreateBlock();
97+
_ctx->shared->RecreateBlock();
9998
}
10099
};

src/miner/internal/miners-controller.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ class MinersController
6868

6969
private:
7070
void StartIfEnabled();
71-
void InitializeCoinbaseScript();
7271

7372
/** Returns true if can start */
74-
bool can_start() const { return _connected && _downloaded && _enable_start && _ctx->has_block(); }
73+
bool can_start() const { return _connected && _downloaded && _enable_start && _ctx->shared->has_block(); }
7574

7675
protected:
7776
/** Returns shared miner context */

src/miner/miner-util.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ScoreCompare
8585
uint64_t nLastBlockTx = 0;
8686
uint64_t nLastBlockSize = 0;
8787

88-
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainparams, const CScript& scriptPubKeyIn)
88+
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainparams, const CScript* scriptPubKeyIn)
8989
{
9090
// Create new block
9191
std::unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
@@ -279,7 +279,10 @@ std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainparams,
279279
bool areWeMinting = GetMintingInstructions(nHeight, fluidMint);
280280

281281
// Compute regular coinbase transaction.
282-
txNew.vout[0].scriptPubKey = scriptPubKeyIn;
282+
// HACK: move outside for split-miner
283+
if (scriptPubKeyIn) {
284+
txNew.vout[0].scriptPubKey = *scriptPubKeyIn;
285+
}
283286
txNew.vin[0].scriptSig = CScript() << nHeight << OP_0;
284287

285288
if (areWeMinting) {
@@ -340,6 +343,11 @@ std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainparams,
340343
return pblocktemplate;
341344
}
342345

346+
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainparams, const CScript& scriptPubKeyIn)
347+
{
348+
return CreateNewBlock(chainparams, &scriptPubKeyIn);
349+
}
350+
343351
void IncrementExtraNonce(CBlock& block, const CBlockIndex* indexPrev, unsigned int& nExtraNonce)
344352
{
345353
// Update nExtraNonce
@@ -348,13 +356,29 @@ void IncrementExtraNonce(CBlock& block, const CBlockIndex* indexPrev, unsigned i
348356
nExtraNonce = 0;
349357
hashPrevBlock = block.hashPrevBlock;
350358
}
359+
// Increment extra nonce
351360
++nExtraNonce;
352-
unsigned int nHeight = indexPrev->nHeight + 1; // Height first in coinbase required for block.version=2
353-
// TODO: ...
361+
// Height first in coinbase required for block.version=2
362+
unsigned int nHeight = indexPrev->nHeight + 1;
363+
// Create copied transaction
354364
CMutableTransaction txCoinbase(*block.vtx[0]);
365+
// Set extra nonce in script
355366
txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
367+
// Make sure script size is correct
368+
// NOTE: `100` should be a constant
356369
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
357-
370+
// Set new transaction in block
358371
block.vtx[0] = MakeTransactionRef(std::move(txCoinbase));
372+
// Generate merkle root hash
373+
block.hashMerkleRoot = BlockMerkleRoot(block);
374+
}
375+
376+
void SetBlockPubkeyScript(CBlock& block, const CScript& scriptPubKeyIn)
377+
{
378+
// Create copied transaction
379+
CMutableTransaction txCoinbase(*block.vtx[0]);
380+
// Set coinbase out address
381+
txCoinbase.vout[0].scriptPubKey = scriptPubKeyIn;
382+
// Generate merkle root hash
359383
block.hashMerkleRoot = BlockMerkleRoot(block);
360384
}

src/miner/miner-util.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ struct CBlockTemplate {
4040
std::vector<CTxOut> voutSuperblock; // dynode payment
4141
};
4242

43+
/** Set pubkey script in generated block */
44+
void SetBlockPubkeyScript(CBlock& block, const CScript& scriptPubKeyIn);
4345
/** Generate a new block, without valid proof-of-work */
44-
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainParams, const CScript& scriptPubKeyIn);
46+
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainparams, const CScript* scriptPubKeyIn = nullptr);
47+
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CChainParams& chainparams, const CScript& scriptPubKeyIn);
4548
/** Called by a miner when new block was found. */
4649
bool ProcessBlockFound(const CBlock& block, const CChainParams& chainparams);
4750

0 commit comments

Comments
 (0)