Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#31600: rpc: have getblocktemplate mintime accou…
Browse files Browse the repository at this point in the history
…nt for timewarp

e1676b0 doc: release notes (Sjors Provoost)
0082f6a rpc: have mintime account for timewarp rule (Sjors Provoost)
79d45b1 rpc: clarify BIP94 behavior for curtime (Sjors Provoost)
0713548 refactor: add GetMinimumTime() helper (Sjors Provoost)

Pull request description:

  #30681 fixed the `curtime` field of `getblocktemplate` to take the timewarp rule into account. However I forgot to do the same for the `mintime` field, which was hardcoded to use `pindexPrev->GetMedianTimePast()+1`.

  This PR adds a helper `GetMinimumTime()` and uses it for the `mintime` field.

  #31376 changed the `curtime` field to always account for the timewarp rule. This PR maintains that behavior.

  Note that `mintime` now always applies BIP94, including on mainnet. This makes future softfork activation safer.

  It could be backported to v28.

ACKs for top commit:
  fjahr:
    tACK e1676b0
  achow101:
    ACK e1676b0
  darosior:
    utACK e1676b0 on the code changes
  tdb3:
    brief code review re ACK e1676b0
  TheCharlatan:
    ACK e1676b0

Tree-SHA512: 0e322d8cc3b8ff770849bce211edcb5b6f55d04e5e0dee0657805049663d758f27423b047ee6363bd8f6c6fead13f974760f48b3321ea86f514f446e1b23231c
  • Loading branch information
achow101 committed Jan 31, 2025
2 parents 8fa10ed + e1676b0 commit 992f37f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
11 changes: 11 additions & 0 deletions doc/release-notes-31600.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Updated RPCs
---
- the `getblocktemplate` RPC `curtime` (BIP22) and `mintime` (BIP23) fields now
account for the timewarp fix proposed in BIP94 on all networks. This ensures
that, in the event a timewarp fix softfork activates on mainnet, un-upgraded
miners will not accidentally violate the timewarp rule. (#31376, #31600)

As a reminder, it's important that any software which uses the `getblocktemplate`
RPC takes these values into account (either `curtime` or `mintime` is fine).
Relying only on a clock can lead to invalid blocks under some circumstances,
especially once a timewarp fix is deployed.
21 changes: 15 additions & 6 deletions src/node/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@
#include <utility>

namespace node {
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
{
int64_t nOldTime = pblock->nTime;
int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};

int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
{
int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
// Height of block to be mined.
const int height{pindexPrev->nHeight + 1};
if (height % consensusParams.DifficultyAdjustmentInterval() == 0) {
nNewTime = std::max<int64_t>(nNewTime, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
// Account for BIP94 timewarp rule on all networks. This makes future
// activation safer.
if (height % difficulty_adjustment_interval == 0) {
min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
}
return min_time;
}

int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
{
int64_t nOldTime = pblock->nTime;
int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};

if (nOldTime < nNewTime) {
pblock->nTime = nNewTime;
Expand Down
7 changes: 7 additions & 0 deletions src/node/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ class BlockAssembler
void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
};

/**
* Get the minimum time a miner should use in the next block. This always
* accounts for the BIP94 timewarp rule, so does not necessarily reflect the
* consensus limit.
*/
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval);

int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);

/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
Expand Down
7 changes: 4 additions & 3 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
using interfaces::BlockTemplate;
using interfaces::Mining;
using node::BlockAssembler;
using node::GetMinimumTime;
using node::NodeContext;
using node::RegenerateCommitments;
using node::UpdateTime;
Expand Down Expand Up @@ -674,7 +675,7 @@ static RPCHelpMan getblocktemplate()
{RPCResult::Type::NUM, "coinbasevalue", "maximum allowable input to coinbase transaction, including the generation award and transaction fees (in satoshis)"},
{RPCResult::Type::STR, "longpollid", "an id to include with a request to longpoll on an update to this template"},
{RPCResult::Type::STR, "target", "The hash target"},
{RPCResult::Type::NUM_TIME, "mintime", "The minimum timestamp appropriate for the next block time, expressed in " + UNIX_EPOCH_TIME},
{RPCResult::Type::NUM_TIME, "mintime", "The minimum timestamp appropriate for the next block time, expressed in " + UNIX_EPOCH_TIME + ". Adjusted for the proposed BIP94 timewarp rule."},
{RPCResult::Type::ARR, "mutable", "list of ways the block template may be changed",
{
{RPCResult::Type::STR, "value", "A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock'"},
Expand All @@ -683,7 +684,7 @@ static RPCHelpMan getblocktemplate()
{RPCResult::Type::NUM, "sigoplimit", "limit of sigops in blocks"},
{RPCResult::Type::NUM, "sizelimit", "limit of block size"},
{RPCResult::Type::NUM, "weightlimit", /*optional=*/true, "limit of block weight"},
{RPCResult::Type::NUM_TIME, "curtime", "current timestamp in " + UNIX_EPOCH_TIME},
{RPCResult::Type::NUM_TIME, "curtime", "current timestamp in " + UNIX_EPOCH_TIME + ". Adjusted for the proposed BIP94 timewarp rule."},
{RPCResult::Type::STR, "bits", "compressed target of next block"},
{RPCResult::Type::NUM, "height", "The height of the next block"},
{RPCResult::Type::STR_HEX, "signet_challenge", /*optional=*/true, "Only on signet"},
Expand Down Expand Up @@ -977,7 +978,7 @@ static RPCHelpMan getblocktemplate()
result.pushKV("coinbasevalue", (int64_t)block.vtx[0]->vout[0].nValue);
result.pushKV("longpollid", tip.GetHex() + ToString(nTransactionsUpdatedLast));
result.pushKV("target", hashTarget.GetHex());
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
result.pushKV("mintime", GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()));
result.pushKV("mutable", std::move(aMutable));
result.pushKV("noncerange", "00000000ffffffff");
int64_t nSigOpLimit = MAX_BLOCK_SIGOPS_COST;
Expand Down
2 changes: 2 additions & 0 deletions test/functional/mining_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def test_timewarp(self):
# The template will have an adjusted timestamp, which we then modify
tmpl = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
assert_greater_than_or_equal(tmpl['curtime'], t + MAX_FUTURE_BLOCK_TIME - MAX_TIMEWARP)
# mintime and curtime should match
assert_equal(tmpl['mintime'], tmpl['curtime'])

block = CBlock()
block.nVersion = tmpl["version"]
Expand Down

0 comments on commit 992f37f

Please sign in to comment.