Skip to content

Commit c6afad9

Browse files
committed
Simplify block transaction sync
1 parent 0283404 commit c6afad9

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

src/miner/internal/miner-base.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ void MinerBase::Loop()
3838

3939
CBlock block;
4040
CBlockIndex* chain_tip = nullptr;
41-
std::uint64_t block_flag = 0;
41+
int64_t block_time = 0;
4242
std::shared_ptr<CBlockTemplate> block_template = {nullptr};
4343

4444
try {
4545
while (true) {
4646
// Update block and tip if changed
47-
if (block_flag != _ctx->shared->block_flag()) {
47+
if (block_time != _ctx->shared->block_time()) {
4848
// set new block template
4949
block_template = _ctx->shared->block_template();
5050
block = block_template->block;
5151
// set block reserve script
5252
SetBlockPubkeyScript(block, _coinbase_script->reserveScript);
5353
// set block flag only after template
5454
// so we've waited for RecreateBlock
55-
block_flag = _ctx->shared->block_flag();
55+
block_time = _ctx->shared->block_time();
5656
// block template chain tip
5757
chain_tip = _ctx->shared->tip();
5858
}
@@ -74,7 +74,7 @@ void MinerBase::Loop()
7474
// Check for stop or if block needs to be rebuilt
7575
boost::this_thread::interruption_point();
7676
// Check if block was recreated
77-
if (block_flag != _ctx->shared->block_flag()) {
77+
if (block_time != _ctx->shared->block_time()) {
7878
break;
7979
}
8080
// Recreate block if nonce too big

src/miner/internal/miner-context.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "miner/internal/miner-context.h"
66
#include "miner/miner-util.h"
7+
#include "txmempool.h"
78
#include "validation.h"
89

910
MinerContext::MinerContext(const CChainParams& chainparams_, CConnman& connman_)
@@ -15,12 +16,15 @@ MinerContext::MinerContext(MinerSharedContextRef shared_, HashRateCounterRef cou
1516

1617
void MinerSharedContext::RecreateBlock()
1718
{
18-
// First we increment flag
19-
// so all miner threads know new block is coming
20-
_block_flag += 1;
2119
// Then we acquire unique lock so that miners wait
2220
// for the new block to be created
2321
boost::unique_lock<boost::shared_mutex> guard(_mutex);
22+
uint32_t txn_time = mempool.GetTransactionsUpdated();
23+
// pass if nothing changed
24+
if (_chain_tip == chainActive.Tip() && _last_txn == txn_time)
25+
return;
2426
_chain_tip = chainActive.Tip();
27+
_block_time = GetTime();
2528
_block_template = CreateNewBlock(chainparams);
29+
_last_txn = txn_time;
2630
}

src/miner/internal/miner-context.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ struct MinerSharedContext {
3737
// Returns chain tip of current block template
3838
CBlockIndex* tip() const { return _chain_tip; }
3939

40-
// Returns miner block flag
41-
// It's incremented every time new template is generated
42-
uint64_t block_flag() const { return _block_flag; }
40+
// Returns miner block template creation time
41+
int64_t block_time() const { return _block_time; }
4342

44-
// Returns true if block was created
45-
bool has_block() const { return _block_template != nullptr; }
43+
// Returns time of last transaction in the block
44+
uint32_t last_txn() const { return _last_txn; }
4645

4746
// Returns miner block template
4847
std::shared_ptr<CBlockTemplate> block_template()
@@ -63,7 +62,9 @@ struct MinerSharedContext {
6362
// current block chain tip
6463
std::atomic<CBlockIndex*> _chain_tip{nullptr};
6564
// atomic flag incremented on recreated block
66-
std::atomic<std::uint64_t> _block_flag{0};
65+
std::atomic<int64_t> _block_time{0};
66+
// last transaction update time
67+
std::atomic<uint32_t> _last_txn{0};
6768
// shared block template for miners
6869
std::shared_ptr<CBlockTemplate> _block_template{nullptr};
6970
// mutex protecting multiple threads recreating block

src/miner/internal/miners-controller.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "miner/internal/miner-context.h"
88
#include "miner/miner-util.h"
99
#include "net.h"
10-
#include "txmempool.h"
1110
#include "validation.h"
1211
#include "validationinterface.h"
1312

@@ -27,6 +26,9 @@ void MinersController::Start()
2726
{
2827
_enable_start = true;
2928
_signals = std::make_shared<MinerSignals>(this);
29+
// initialize block template
30+
_ctx->shared->RecreateBlock();
31+
3032
if (can_start()) {
3133
_group_cpu.Start();
3234
#ifdef ENABLE_GPU
@@ -78,8 +80,6 @@ void MinerSignals::NotifyBlock(const CBlockIndex* index_new, const CBlockIndex*
7880
if (index_new != chainActive.Tip())
7981
return;
8082
// Create new block template for miners
81-
_ctr->_last_sync_time = GetTime();
82-
_ctr->_last_txn_time = mempool.GetTransactionsUpdated();
8383
_ctr->_ctx->shared->RecreateBlock();
8484
// start miners
8585
if (_ctr->can_start()) {
@@ -95,13 +95,7 @@ void MinerSignals::NotifyTransaction(const CTransaction& txn, const CBlockIndex*
9595
// check if blockchain has synced, has more than 1 peer and is enabled before recreating blocks
9696
if (IsInitialBlockDownload() || !_ctr->can_start())
9797
return;
98-
99-
const int64_t latest_txn = mempool.GetTransactionsUpdated();
100-
if (latest_txn == _ctr->_last_txn_time) {
101-
return;
102-
}
103-
if (GetTime() - _ctr->_last_txn_time > 60) {
104-
_ctr->_last_txn_time = latest_txn;
98+
if (GetTime() - _ctr->_ctx->shared->last_txn() > 60) {
10599
_ctr->_ctx->shared->RecreateBlock();
106100
}
107101
};

src/miner/internal/miners-controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class MinersController
6363
void StartIfEnabled();
6464

6565
// Returns true if enabled, connected and has block.
66-
bool can_start() const { return _connected && _enable_start && _ctx->shared->has_block(); }
66+
bool can_start() const { return _connected && _enable_start && _ctx->shared->block_template(); }
6767

6868
// Miner signals class
6969
friend class MinerSignals;

0 commit comments

Comments
 (0)