@@ -25,27 +25,28 @@ void MinerBase::Loop()
25
25
RenameThread (tfm::format (" dynamic-%s-miner-%d" , DeviceName (), _device_index).data ());
26
26
27
27
CBlock block;
28
- CBlockIndex* current_tip = nullptr ;
28
+ CBlockIndex* chain_tip = nullptr ;
29
+ std::uint64_t block_flag = 0 ;
29
30
std::shared_ptr<CBlockTemplate> block_template = {nullptr };
30
31
31
32
try {
32
33
while (true ) {
33
- // Get blockchain tip
34
- CBlockIndex* next_tip = _ctx->tip ();
35
- // Get shared block template
36
- auto next_template = _ctx->block_template ();
37
- assert (next_template != nullptr );
38
- CBlock& next_block = next_template->block ;
39
34
// Update block and tip if changed
40
- if (current_tip != next_tip || (next_block.nBits != block.nBits && next_block.nTime > block.nTime )) {
41
- block = next_block;
42
- block_template = next_template;
43
- current_tip = next_tip;
35
+ if (block_flag != _ctx->block_flag ()) {
36
+ // set new block template
37
+ block_template = _ctx->block_template ();
38
+ block = block_template->block ;
39
+ // set block flag only after template
40
+ // so we've waited for RecreateBlock
41
+ block_flag = _ctx->block_flag ();
42
+ // block chain tip
43
+ chain_tip = _ctx->tip ();
44
44
}
45
45
// Make sure we have a tip
46
- assert (current_tip != nullptr );
46
+ assert (chain_tip != nullptr );
47
+ assert (block_template != nullptr );
47
48
// Increment nonce
48
- IncrementExtraNonce (block, current_tip , _extra_nonce);
49
+ IncrementExtraNonce (block, chain_tip , _extra_nonce);
49
50
LogPrintf (" DynamicMiner -- Running miner on device %s#%d with %u transactions in block (%u bytes)\n " , DeviceName (), _device_index, block.vtx .size (),
50
51
GetSerializeSize (block, SER_NETWORK, PROTOCOL_VERSION));
51
52
// set loop start for counter
@@ -58,12 +59,17 @@ void MinerBase::Loop()
58
59
_ctx->counter ->Increment (hashes);
59
60
// Check for stop or if block needs to be rebuilt
60
61
boost::this_thread::interruption_point ();
62
+ // Check if block was recreated
63
+ if (block_flag != _ctx->block_flag ()) {
64
+ break ;
65
+ }
66
+ // Recreate block if nonce too big
61
67
if (block.nNonce >= 0xffff0000 ) {
62
68
_ctx->RecreateBlock ();
63
69
break ;
64
70
}
65
71
// Update block time
66
- if (UpdateTime (block, _ctx->chainparams ().GetConsensus (), current_tip ) < 0 ) {
72
+ if (UpdateTime (block, _ctx->chainparams ().GetConsensus (), chain_tip ) < 0 ) {
67
73
// Recreate the block if the clock has run backwards,
68
74
// so that we can use the correct time.
69
75
_ctx->RecreateBlock ();
0 commit comments