-
Notifications
You must be signed in to change notification settings - Fork 35.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restrict timestamp when mining a diff-adjustment block to prev-600
This prepares us for a potential future timewarp-fixing softfork by ensuring that we always refuse to mine blocks which refuse to exploit timewarp, no matter the behavior of other miners. Note that we allow timestamp to go backwards by 600 seconds on the difficulty-adjustment blocks to avoid bricking any existing hardware which relies on the ability to roll nTime by up to 600 seconds. Note that it is possible that the eventual softfork to fix timewarp has a looser resetriction than the 600 seconds enforced here, however it seems unlikely we will apply a tighter one, and its fine if we restrict things further on the mining end than in consensus.
- Loading branch information
1 parent
0bf0b3c
commit bdb4372
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2014-2019 The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
"""Test getblocktemplate complies with potential future timewarp-fixing | ||
softforks (modulo 600 second nTime decrease). | ||
We first mine a chain up to the difficulty-adjustment block and set | ||
the last block's timestamp 2 hours in the future, then check the times | ||
returned by getblocktemplate. Note that we cannot check the actual | ||
retarget behavior as difficulty adjustments do not occur on regtest | ||
(though it still technically has a difficulty adjustment interval).""" | ||
|
||
from test_framework.test_framework import BitcoinTestFramework | ||
from test_framework.util import ( | ||
assert_equal, | ||
connect_nodes_bi, | ||
) | ||
|
||
|
||
class TimewarpMiningTest(BitcoinTestFramework): | ||
def set_test_params(self): | ||
self.num_nodes = 2 | ||
self.setup_clean_chain = True | ||
|
||
def run_test(self): | ||
self.log.info('Create some old blocks') | ||
block_1 = self.nodes[0].generate(1)[0] | ||
for _ in range(2013): | ||
self.nodes[0].generate(1) | ||
last_block_time = self.nodes[0].getblockheader(block_1)["time"] + 7199 | ||
|
||
self.log.info('Create a block 7199 seconds in the future') | ||
self.nodes[0].setmocktime(last_block_time) | ||
block_2015 = self.nodes[0].generate(1)[0] | ||
assert_equal(self.nodes[0].getblockheader(block_2015)["time"], last_block_time) | ||
|
||
mining_info = self.nodes[0].getmininginfo() | ||
assert_equal(mining_info['blocks'], 2015) | ||
assert_equal(mining_info['currentblocktx'], 0) | ||
assert_equal(mining_info['currentblockweight'], 4000) | ||
|
||
self.restart_node(0) | ||
connect_nodes_bi(self.nodes, 0, 1) | ||
|
||
self.log.info('Check that mintime and curtime are last-block - 600 seconds') | ||
# Now test that mintime and curtime are last_block_time - 600 | ||
template = self.nodes[0].getblocktemplate({'rules': ['segwit']}) | ||
assert_equal(template["curtime"], last_block_time - 600) | ||
assert_equal(template["mintime"], last_block_time - 600) | ||
|
||
self.log.info('Generate a 2016th block and check that the next block\'s time goes back to now') | ||
block_2016 = self.nodes[0].generate(1)[0] | ||
assert_equal(self.nodes[0].getblockheader(block_2016)["time"], last_block_time - 600) | ||
|
||
# Assume test doesn't take 2 hours and check that we let time jump backwards | ||
# now that we mined the difficulty-adjustment block | ||
block_2017 = self.nodes[0].generate(1)[0] | ||
assert(self.nodes[0].getblockheader(block_2017)["time"] < last_block_time - 600) | ||
|
||
if __name__ == '__main__': | ||
TimewarpMiningTest().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters