Skip to content

Commit 209cfa8

Browse files
authored
Merge pull request duality-solutions#256 from crackcomm/split-miner
Use separate miner wallets
2 parents 829b6cb + 06814a1 commit 209cfa8

File tree

10 files changed

+116
-138
lines changed

10 files changed

+116
-138
lines changed

src/miner/internal/hash-rate-counter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include "utiltime.h"
88

99

10-
HashRateCounter::HashRateCounter() : _parent(nullptr){};
11-
HashRateCounter::HashRateCounter(HashRateCounterRef parent) : _parent(parent){};
12-
13-
/** Hash rate counter increment implementation. */
1410
void HashRateCounter::Increment(int64_t amount)
1511
{
1612
// Set start time if not set and return

src/miner/internal/hash-rate-counter.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,22 @@ struct HashRateCounter : public std::enable_shared_from_this<HashRateCounter> {
2424
HashRateCounterRef _parent;
2525

2626
public:
27-
HashRateCounter();
28-
explicit HashRateCounter(HashRateCounterRef counter);
27+
explicit HashRateCounter() : _parent(nullptr){};
28+
explicit HashRateCounter(HashRateCounterRef parent) : _parent(parent){};
2929

30-
/** Returns hash rate per second */
30+
// Returns hash rate per second
3131
operator int64_t() { return _count_per_sec; };
3232

33-
/** Creates new counter */
34-
static HashRateCounterRef Make() { return std::make_shared<HashRateCounter>(); }
35-
36-
/** Creates new child counter */
33+
// Creates new child counter
3734
HashRateCounterRef MakeChild() { return std::make_shared<HashRateCounter>(shared_from_this()); }
3835

39-
/** Increments counter */
36+
// Increments counter
4037
void Increment(int64_t amount);
4138

42-
/** Resets counter and timer */
39+
// Resets counter and timer
4340
void Reset();
4441

45-
/** Returns start time */
42+
// Returns start time
4643
int64_t start() const { return _timer_start; };
4744
};
4845

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: 10 additions & 5 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.
@@ -21,20 +22,20 @@ class MinerBase
2122
MinerBase(MinerContextRef ctx, std::size_t device_index);
2223
virtual ~MinerBase() = default;
2324

24-
/** Starts miner loop */
25+
// Starts miner loop
2526
void Loop();
2627

27-
/** Starts miner loop */
28+
// Starts miner loop
2829
void operator()() { Loop(); };
2930

30-
/** Returns miner device name */
31+
// Returns miner device name
3132
virtual const char* DeviceName() = 0;
3233

3334
protected:
34-
/** Processes a new found solution */
35+
// Processes a new found solution
3536
void ProcessFoundSolution(const CBlock& block, const uint256& hash);
3637

37-
/** tries to mine a block */
38+
// tries to mine a block
3839
virtual int64_t TryMineBlock(CBlock& block) = 0;
3940

4041
// Solution must be lower or equal to
@@ -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: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
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"
128

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-
}
9+
MinerContext::MinerContext(const CChainParams& chainparams_, CConnman& connman_)
10+
: counter(std::make_shared<HashRateCounter>()),
11+
shared(std::make_shared<MinerSharedContext>(chainparams_, connman_)){};
12+
13+
MinerContext::MinerContext(MinerSharedContextRef shared_, HashRateCounterRef counter_)
14+
: counter(counter_), shared(shared_){};
2415

2516
void MinerSharedContext::RecreateBlock()
2617
{
@@ -31,5 +22,5 @@ void MinerSharedContext::RecreateBlock()
3122
// for the new block to be created
3223
boost::unique_lock<boost::shared_mutex> guard(_mutex);
3324
_chain_tip = chainActive.Tip();
34-
_block_template = CreateNewBlock(chainparams, _coinbase_script->reserveScript);
25+
_block_template = CreateNewBlock(chainparams);
3526
}

src/miner/internal/miner-context.h

Lines changed: 21 additions & 29 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
};
@@ -76,32 +79,21 @@ class MinerContext
7679
HashRateCounterRef counter;
7780
MinerSharedContextRef shared;
7881

79-
MinerContext(const CChainParams& chainparams_, CConnman& connman_)
80-
: counter(HashRateCounter::Make()), shared(std::make_shared<MinerSharedContext>(chainparams_, connman_)){};
82+
MinerContext(const CChainParams& chainparams_, CConnman& connman_);
83+
MinerContext(MinerSharedContextRef shared_, HashRateCounterRef counter_);
8184

82-
MinerContext(MinerSharedContextRef shared_, HashRateCounterRef counter_)
83-
: counter(counter_), shared(shared_){};
84-
85-
/* Constructs child context */
85+
// Constructs child context
8686
explicit MinerContext(const MinerContext* ctx_)
8787
: MinerContext(ctx_->shared, ctx_->counter->MakeChild()){};
8888

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

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-
92+
// Connection manager
9893
CConnman& connman() const { return shared->connman; }
94+
95+
// Chain parameters
9996
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(); }
10597
};
10698

10799
#endif // DYNAMIC_INTERNAL_MINER_CONTEXT_H

src/miner/internal/miners-controller.cpp

Lines changed: 2 additions & 4 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

@@ -76,8 +75,7 @@ void MinersController::NotifyBlock(const CBlockIndex* index_new, const CBlockInd
7675
// Create new block template for miners
7776
_last_sync_time = GetTime();
7877
_last_txn_time = mempool.GetTransactionsUpdated();
79-
_downloaded = initial_download || _downloaded;
80-
_ctx->RecreateBlock();
78+
_ctx->shared->RecreateBlock();
8179
// start miners
8280
if (can_start()) {
8381
_group_cpu.Start();
@@ -95,6 +93,6 @@ void MinersController::NotifyTransaction(const CTransaction& txn, const CBlockIn
9593
}
9694
if (GetTime() - _last_txn_time > 60) {
9795
_last_txn_time = latest_txn;
98-
_ctx->RecreateBlock();
96+
_ctx->shared->RecreateBlock();
9997
}
10098
};

src/miner/internal/miners-controller.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ class MinersController
4242
int64_t _last_sync_time = 0;
4343

4444
public:
45-
MinersController(MinerContextRef ctx);
4645
MinersController(const CChainParams& chainparams, CConnman& connman);
46+
MinersController(MinerContextRef ctx);
4747
virtual ~MinersController() = default;
4848

49-
/** Starts miners */
49+
// Starts miners
5050
void Start();
5151

52-
/** Shuts down all miner threads */
52+
// Shuts down all miner threads
5353
void Shutdown();
5454

55-
/** Gets combined hash rate of GPU and CPU */
55+
// Gets combined hash rate of GPU and CPU
5656
int64_t GetHashRate() const;
5757

58-
/** Returns CPU miners thread group */
58+
// Returns CPU miners thread group
5959
MinersThreadGroup<CPUMiner>& group_cpu() { return _group_cpu; }
6060

6161
#ifdef ENABLE_GPU
62-
/** Returns GPU miners thread group */
62+
// Returns GPU miners thread group
6363
MinersThreadGroup<GPUMiner>& group_gpu()
6464
{
6565
return _group_gpu;
@@ -68,25 +68,24 @@ class MinersController
6868

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

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

7675
protected:
77-
/** Returns shared miner context */
76+
// Returns shared miner context
7877
MinerContextRef ctx() const { return _ctx; }
7978

80-
/** Handles new node connection */
79+
// Handles new node connection
8180
virtual void NotifyNode(const CNode* node);
8281

83-
/** Handles updated blockchain tip */
82+
// Handles updated blockchain tip
8483
virtual void NotifyBlock(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload);
8584

86-
/** Handles new transaction */
85+
// Handles new transaction
8786
virtual void NotifyTransaction(const CTransaction& tx, const CBlockIndex* pindex, int posInBlock);
8887

89-
/** Connects signals to controller */
88+
// Connects signals to controller
9089
friend void ConnectMinerSignals(MinersController*);
9190
};
9291

0 commit comments

Comments
 (0)