Skip to content

Commit 5a6af31

Browse files
committed
Merge #9908: Define 7200 second timestamp window constant
e57a1fd Define 7200 second timestamp window constant (Russell Yanofsky) Tree-SHA512: 449d20e4fd23905cd96be36f717c55a0a2360aba1002aaf55a3699cce4a41f6e94acc2fbe511a93c5cbe8f8e68386995a76cad67620ebb66ba9283e6080ab567
2 parents 56ab672 + e57a1fd commit 5a6af31

File tree

8 files changed

+27
-13
lines changed

8 files changed

+27
-13
lines changed

qa/rpc-tests/import-rescan.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def do_import(self, timestamp):
5656
"scriptPubKey": {
5757
"address": self.address["address"]
5858
},
59-
"timestamp": timestamp + RESCAN_WINDOW + (1 if self.rescan == Rescan.late_timestamp else 0),
59+
"timestamp": timestamp + TIMESTAMP_WINDOW + (1 if self.rescan == Rescan.late_timestamp else 0),
6060
"pubkeys": [self.address["pubkey"]] if self.data == Data.pub else [],
6161
"keys": [self.key] if self.data == Data.priv else [],
6262
"label": self.label,
@@ -108,7 +108,7 @@ def check(self, txid=None, amount=None, confirmations=None):
108108
IMPORT_NODES = [ImportNode(*fields) for fields in itertools.product((False, True), repeat=2)]
109109

110110
# Rescans start at the earliest block up to 2 hours before the key timestamp.
111-
RESCAN_WINDOW = 2 * 60 * 60
111+
TIMESTAMP_WINDOW = 2 * 60 * 60
112112

113113

114114
class ImportRescanTest(BitcoinTestFramework):
@@ -141,7 +141,7 @@ def run_test(self):
141141
self.nodes[0].generate(1)
142142
assert_equal(self.nodes[0].getrawmempool(), [])
143143
timestamp = self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"]
144-
set_node_times(self.nodes, timestamp + RESCAN_WINDOW + 1)
144+
set_node_times(self.nodes, timestamp + TIMESTAMP_WINDOW + 1)
145145
self.nodes[0].generate(1)
146146
sync_blocks(self.nodes)
147147

qa/rpc-tests/pruning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Rescans start at the earliest block up to 2 hours before a key timestamp, so
2020
# the manual prune RPC avoids pruning blocks in the same window to be
2121
# compatible with pruning based on key creation time.
22-
RESCAN_WINDOW = 2 * 60 * 60
22+
TIMESTAMP_WINDOW = 2 * 60 * 60
2323

2424

2525
def calc_usage(blockdir):
@@ -242,7 +242,7 @@ def manual_test(self, node_number, use_timestamp):
242242

243243
def height(index):
244244
if use_timestamp:
245-
return node.getblockheader(node.getblockhash(index))["time"] + RESCAN_WINDOW
245+
return node.getblockheader(node.getblockhash(index))["time"] + TIMESTAMP_WINDOW
246246
else:
247247
return index
248248

src/chain.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414

1515
#include <vector>
1616

17+
/**
18+
* Maximum amount of time that a block timestamp is allowed to exceed the
19+
* current network-adjusted time before the block will be accepted.
20+
*/
21+
static const int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
22+
23+
/**
24+
* Timestamp window used as a grace period by code that compares external
25+
* timestamps (such as timestamps passed to RPCs, or wallet key creation times)
26+
* to block timestamps. This should be set at least as high as
27+
* MAX_FUTURE_BLOCK_TIME.
28+
*/
29+
static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
30+
1731
class CBlockFileInfo
1832
{
1933
public:

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ UniValue pruneblockchain(const JSONRPCRequest& request)
846846
// too low to be a block time (corresponds to timestamp from Sep 2001).
847847
if (heightParam > 1000000000) {
848848
// Add a 2 hour buffer to include blocks which might have had old timestamps
849-
CBlockIndex* pindex = chainActive.FindEarliestAtLeast(heightParam - 7200);
849+
CBlockIndex* pindex = chainActive.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW);
850850
if (!pindex) {
851851
throw JSONRPCError(RPC_INTERNAL_ERROR, "Could not find block with at least the specified timestamp.");
852852
}

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2983,7 +2983,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
29832983
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
29842984

29852985
// Check timestamp
2986-
if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
2986+
if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
29872987
return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
29882988

29892989
// Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:

src/wallet/rpcdump.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ UniValue importwallet(const JSONRPCRequest& request)
513513
pwallet->ShowProgress("", 100); // hide progress dialog in GUI
514514

515515
CBlockIndex *pindex = chainActive.Tip();
516-
while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200)
516+
while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - TIMESTAMP_WINDOW)
517517
pindex = pindex->pprev;
518518

519519
pwallet->UpdateTimeFirstKey(nTimeBegin);
@@ -1095,7 +1095,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
10951095
}
10961096

10971097
if (fRescan && fRunScan && requests.size()) {
1098-
CBlockIndex* pindex = nLowestTimestamp > minimumTimestamp ? chainActive.FindEarliestAtLeast(std::max<int64_t>(nLowestTimestamp - 7200, 0)) : chainActive.Genesis();
1098+
CBlockIndex* pindex = nLowestTimestamp > minimumTimestamp ? chainActive.FindEarliestAtLeast(std::max<int64_t>(nLowestTimestamp - TIMESTAMP_WINDOW, 0)) : chainActive.Genesis();
10991099
CBlockIndex* scannedRange = nullptr;
11001100
if (pindex) {
11011101
scannedRange = pwallet->ScanForWalletTransactions(pindex, true);
@@ -1112,7 +1112,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
11121112
// range, or if the import result already has an error set, let
11131113
// the result stand unmodified. Otherwise replace the result
11141114
// with an error message.
1115-
if (GetImportTimestamp(request, now) - 7200 >= scannedRange->GetBlockTimeMax() || results.at(i).exists("error")) {
1115+
if (GetImportTimestamp(request, now) - TIMESTAMP_WINDOW >= scannedRange->GetBlockTimeMax() || results.at(i).exists("error")) {
11161116
response.push_back(results.at(i));
11171117
} else {
11181118
UniValue result = UniValue(UniValue::VOBJ);

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
415415
CKey futureKey;
416416
futureKey.MakeNewKey(true);
417417
key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(futureKey.GetPubKey())));
418-
key.pushKV("timestamp", newTip->GetBlockTimeMax() + 7200);
418+
key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW);
419419
key.pushKV("internal", UniValue(true));
420420
keys.push_back(key);
421421
JSONRPCRequest request;

src/wallet/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
15621562

15631563
// no need to read and scan block, if block was created before
15641564
// our wallet birthday (as adjusted for block time variability)
1565-
while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200)))
1565+
while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - TIMESTAMP_WINDOW)))
15661566
pindex = chainActive.Next(pindex);
15671567

15681568
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
@@ -3495,7 +3495,7 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
34953495

34963496
// Extract block timestamps for those keys
34973497
for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
3498-
mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
3498+
mapKeyBirth[it->first] = it->second->GetBlockTime() - TIMESTAMP_WINDOW; // block times can be 2h off
34993499
}
35003500

35013501
bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)

0 commit comments

Comments
 (0)