Skip to content

Commit

Permalink
miner: Reorg Testnet4 minimum difficulty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
fjahr committed Oct 13, 2024
1 parent 48cf3da commit 495afa0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class Mining
//! Returns the hash and height for the tip of this chain
virtual std::optional<BlockRef> getTip() = 0;

virtual std::vector<uint256> rollback() = 0;
virtual void reconsider(std::vector<uint256> invalidated) = 0;

/**
* Waits for the connected tip to change. If the tip was not connected on
* startup, this will wait.
Expand Down
27 changes: 27 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <policy/settings.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <rpc/blockchain.h>
#include <rpc/protocol.h>
#include <rpc/server.h>
#include <support/allocators/secure.h>
Expand Down Expand Up @@ -969,6 +970,32 @@ class MinerImpl : public Mining
return BlockRef{tip->GetBlockHash(), tip->nHeight};
}

std::vector<uint256> rollback() override
{
LOCK(::cs_main);
std::vector<uint256> result;
CBlockIndex* tip{chainman().ActiveChain().Tip()};
if (!tip) return {};
if (chainman().GetConsensus().enforce_BIP94) {
while (tip->nBits == 0x1d00ffff) {
InvalidateBlock(chainman(), tip->GetBlockHash());
result.emplace_back(tip->GetBlockHash());
tip = tip->pprev;
}
}
return result;
}

void reconsider(std::vector<uint256> invalidated) override
{
LOCK(::cs_main);
if (!chainman().GetConsensus().enforce_BIP94 || invalidated.empty()) return;

for (uint256 bh : invalidated) {
ReconsiderBlock(chainman(), bh);
}
}

BlockRef waitTipChanged(uint256 current_tip, MillisecondsDouble timeout) override
{
if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
Expand Down
4 changes: 4 additions & 0 deletions src/rpc/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ UniValue CreateUTXOSnapshot(
std::optional<int> GetPruneHeight(const node::BlockManager& blockman, const CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
void CheckBlockDataAvailability(node::BlockManager& blockman, const CBlockIndex& blockindex, bool check_for_undo) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

void ReconsiderBlock(ChainstateManager& chainman, uint256 block_hash);
void InvalidateBlock(ChainstateManager& chainman, const uint256 block_hash);


#endif // BITCOIN_RPC_BLOCKCHAIN_H
3 changes: 3 additions & 0 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ static RPCHelpMan getblocktemplate()
ChainstateManager& chainman = EnsureChainman(node);
Mining& miner = EnsureMining(node);
LOCK(cs_main);
std::vector<uint256> invalidated{miner.rollback()};
uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};

std::string strMode = "template";
Expand Down Expand Up @@ -966,6 +967,8 @@ static RPCHelpMan getblocktemplate()
result.pushKV("default_witness_commitment", HexStr(block_template->getCoinbaseCommitment()));
}

miner.reconsider(invalidated);

return result;
},
};
Expand Down

0 comments on commit 495afa0

Please sign in to comment.