UpdateTime() needs to account for timewarp fix on testnet4 #30614

issue Sjors openend this issue on August 9, 2024
  1. Sjors commented at 7:16 am on August 9, 2024: member

    In (miner.cpp) we set the block (template) timestamp as follows:

     0pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
     1...
     2UpdateTime(pblock, ...)
     3
     4
     5int64_t UpdateTime(CBlockHeader* pblock, ...)
     6{
     7    int64_t nOldTime = pblock->nTime;
     8    int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
     9
    10    if (nOldTime < nNewTime) {
    11        pblock->nTime = nNewTime;
    12    }
    

    In other words, we pick the system time, and if needed bump it to MTP + 1.

    This doesn’t take the timewarp mitigation for testnet4 into account. The first block must have a timestamp no less than 7200 seconds (2 hours) before the last block. There’s discussion about lowering that to 600 from the original proposal.

    An adversarial miner could set the timestamp 2 hours in the future. The current code would then set the next block at the current time. With the current value of 7200 this should not cause an issue, even if we immediately find a block. But it seems safer to explicitly account for this and increase the timestamp if needed, especially if we ever lower the value.

    We should also add a functional test that uses fake time to test the limit case: we don’t want to end up accepting a block exactly 2 hours in the future and then failing to generate a template because there’s no possible valid timestamp due to some off-by-one error. (I don’t think this can happen though, haven’t thought it through)

    See also https://github.com/bitcoin/bips/pull/1658#issuecomment-2275393864

  2. Sjors commented at 7:22 am on August 9, 2024: member
    Although I think we should fix this, it seems potentially problematic to expect other node / miner implementations to also fix this. That’s something to consider in the discussion of whether to change the 7200 value.
  3. maflcko added the label Tests on Aug 9, 2024
  4. maflcko added the label Brainstorming on Aug 9, 2024
  5. garlonicon commented at 11:31 am on September 2, 2024: none

    If someone uses 600 seconds as a MAX_TIMEWARP, then that node can be stuck on block 42335. More information: https://bitcointalk.org/index.php?topic=5499150.msg64488234#msg64488234

    I wonder, if all blocks after 42335 will be reorged, or if MAX_TIMEWARP will be bigger than 600. I guess v28.0 was released with that value, and some nodes are stuck, because of that.

  6. Sjors commented at 11:50 am on September 2, 2024: member

    This was fixed in #30681.

    However someone should have tried a full IBD before changing the testnet4 consensus rules. An incompatible block in the past won’t be retroactively rejected by miners, but it will be by new noes.

    I’ll try that, and open a new issue if needed.

  7. Sjors commented at 12:13 pm on September 2, 2024: member

    However someone should have tried a full IBD before changing the testnet4 consensus rules.

    I just did. It gets stuck at block 42,335 from yesterday. So at least there was no historical block violating the rule at the time this “soft fork” was introduced.

    Opening a fresh issue…

  8. Sjors closed this on Sep 2, 2024


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-21 09:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me