Skip to content

mining: spurious template creation after testnet 3/4 difficulty reset #36983

Backlog

Description

@Sjors

Noticed while reviewing #36870.

After 20 minutes of not finding a block,testnet3 and testnet4 drop the difficulty to 1. At that point waitNext() creates a new template.

The problem happens afterwards: it keeps making a new template every second, even when fees don't rise.

This probably never happens in practice, because someone always immediately produces a block after the reset. But it should be fixed.

E.g. on top of #36870:

diff --git a/src/node/block_template_manager.cpp b/src/node/block_template_manager.cpp
index 109dfbd8d6..59cbc86ece 100644
--- a/src/node/block_template_manager.cpp
+++ b/src/node/block_template_manager.cpp
@@ -153,5 +153,12 @@ std::unique_ptr<CBlockTemplate> BlockTemplateManager::WaitAndCreateNewBlock(
         if (!tip_changed && allow_min_difficulty) {
             const NodeClock::time_point tip_time{std::chrono::seconds{m_chainman.ActiveChain().Tip()->GetBlockTime()}};
-            if (NodeClock::now() >= tip_time + 20min + 1s) {
+            const NodeClock::time_point min_difficulty_time{tip_time + 20min + 1s};
+            // Return one template with a timestamp past that point, so
+            // miners can roll nTime further (see getBlockHeader()). Skip
+            // templates already past it, or a stale tip would produce a new
+            // template every tick. Don't compare nBits to the minimum
+            // instead: the rule doesn't apply at difficulty adjustment
+            // heights.
+            if (block_template->block.Time() < min_difficulty_time && NodeClock::now() >= min_difficulty_time) {
                 tip_changed = true;
             }
diff --git a/src/test/testnet4_miner_tests.cpp b/src/test/testnet4_miner_tests.cpp
index ecaec1d18c..c738286ce3 100644
--- a/src/test/testnet4_miner_tests.cpp
+++ b/src/test/testnet4_miner_tests.cpp
@@ -68,4 +68,10 @@ BOOST_AUTO_TEST_CASE(MiningInterface)
     block_template = block_template->waitNext(wait_options);
     BOOST_REQUIRE(block_template);
+
+    // The new template's timestamp is already past that point, so waiting
+    // again does not return another template.
+    clock += 10min;
+    should_be_nullptr = block_template->waitNext(wait_options);
+    BOOST_REQUIRE(should_be_nullptr == nullptr);
 }

This returns a new template every 20 minutes. This is good, because PR 36870 tells miners to not roll nTime for more than 20 minutes on these networks, so we need to give them a fresh template after that.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions