Skip to content

Commit

Permalink
Introduce waitFeesChanged() mining interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Sep 19, 2024
1 parent a9f4a60 commit fdcd009
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ class Mining
*/
virtual BlockRef waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;

/**
* Waits for fees in the next block to rise, a new tip or the timeout.
*
* @param[in] timeout how long to wait for a fee increase
* @param[in] tip block hash that the most recent template builds on
* @param[in] fee_delta currently ignored: how much total fees in the next block should rise.
* @param[in,out] fees_before currently ignored: fees for the most recent template
* @param[out] tip_changed whether a new tip arrived during the wait
*
* @returns true if fees increased, false if a new tip arrives or the timeout occurs
*/
virtual bool waitFeesChanged(MillisecondsDouble timeout, uint256 tip, CAmount fee_delta, CAmount& fees_before, bool& tip_changed) = 0;

/**
* Construct a new block template
*
Expand Down
22 changes: 22 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,28 @@ class MinerImpl : public Mining
return BlockRef{chainman().ActiveChain().Tip()->GetBlockHash(), chainman().ActiveChain().Tip()->nHeight};
}

bool waitFeesChanged(MillisecondsDouble timeout, uint256 tip, CAmount fee_delta, CAmount& fees_before, bool& tip_changed) override
{
Assume(getTip());
unsigned int last_mempool_update{context()->mempool->GetTransactionsUpdated()};

auto deadline = std::chrono::steady_clock::now() + timeout;
{
while (!chainman().m_interrupt && std::chrono::steady_clock::now() < deadline) {
std::this_thread::sleep_for(std::min(timeout, MillisecondsDouble(100)));
if (getTip().value().hash != tip) {
tip_changed = true;
return false;
}

// TODO: when cluster mempool is available, actually calculate
// fees for the next block. This is currently too expensive.
if (context()->mempool->GetTransactionsUpdated() > last_mempool_update) return true;
}
}
return false;
}

bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) override
{
return chainman().ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/new_block);
Expand Down

0 comments on commit fdcd009

Please sign in to comment.