Define 7200 second timestamp window constant

This commit is contained in:
Russell Yanofsky 2017-03-02 12:20:34 -05:00
parent 3fabae7425
commit e57a1fd899
8 changed files with 27 additions and 13 deletions

View File

@ -56,7 +56,7 @@ class Variant(collections.namedtuple("Variant", "call data rescan prune")):
"scriptPubKey": { "scriptPubKey": {
"address": self.address["address"] "address": self.address["address"]
}, },
"timestamp": timestamp + RESCAN_WINDOW + (1 if self.rescan == Rescan.late_timestamp else 0), "timestamp": timestamp + TIMESTAMP_WINDOW + (1 if self.rescan == Rescan.late_timestamp else 0),
"pubkeys": [self.address["pubkey"]] if self.data == Data.pub else [], "pubkeys": [self.address["pubkey"]] if self.data == Data.pub else [],
"keys": [self.key] if self.data == Data.priv else [], "keys": [self.key] if self.data == Data.priv else [],
"label": self.label, "label": self.label,
@ -108,7 +108,7 @@ ImportNode = collections.namedtuple("ImportNode", "prune rescan")
IMPORT_NODES = [ImportNode(*fields) for fields in itertools.product((False, True), repeat=2)] IMPORT_NODES = [ImportNode(*fields) for fields in itertools.product((False, True), repeat=2)]
# Rescans start at the earliest block up to 2 hours before the key timestamp. # Rescans start at the earliest block up to 2 hours before the key timestamp.
RESCAN_WINDOW = 2 * 60 * 60 TIMESTAMP_WINDOW = 2 * 60 * 60
class ImportRescanTest(BitcoinTestFramework): class ImportRescanTest(BitcoinTestFramework):
@ -141,7 +141,7 @@ class ImportRescanTest(BitcoinTestFramework):
self.nodes[0].generate(1) self.nodes[0].generate(1)
assert_equal(self.nodes[0].getrawmempool(), []) assert_equal(self.nodes[0].getrawmempool(), [])
timestamp = self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"] timestamp = self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"]
set_node_times(self.nodes, timestamp + RESCAN_WINDOW + 1) set_node_times(self.nodes, timestamp + TIMESTAMP_WINDOW + 1)
self.nodes[0].generate(1) self.nodes[0].generate(1)
sync_blocks(self.nodes) sync_blocks(self.nodes)

View File

@ -19,7 +19,7 @@ MIN_BLOCKS_TO_KEEP = 288
# Rescans start at the earliest block up to 2 hours before a key timestamp, so # Rescans start at the earliest block up to 2 hours before a key timestamp, so
# the manual prune RPC avoids pruning blocks in the same window to be # the manual prune RPC avoids pruning blocks in the same window to be
# compatible with pruning based on key creation time. # compatible with pruning based on key creation time.
RESCAN_WINDOW = 2 * 60 * 60 TIMESTAMP_WINDOW = 2 * 60 * 60
def calc_usage(blockdir): def calc_usage(blockdir):
@ -242,7 +242,7 @@ class PruneTest(BitcoinTestFramework):
def height(index): def height(index):
if use_timestamp: if use_timestamp:
return node.getblockheader(node.getblockhash(index))["time"] + RESCAN_WINDOW return node.getblockheader(node.getblockhash(index))["time"] + TIMESTAMP_WINDOW
else: else:
return index return index

View File

@ -14,6 +14,20 @@
#include <vector> #include <vector>
/**
* Maximum amount of time that a block timestamp is allowed to exceed the
* current network-adjusted time before the block will be accepted.
*/
static const int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
/**
* Timestamp window used as a grace period by code that compares external
* timestamps (such as timestamps passed to RPCs, or wallet key creation times)
* to block timestamps. This should be set at least as high as
* MAX_FUTURE_BLOCK_TIME.
*/
static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
class CBlockFileInfo class CBlockFileInfo
{ {
public: public:

View File

@ -846,7 +846,7 @@ UniValue pruneblockchain(const JSONRPCRequest& request)
// too low to be a block time (corresponds to timestamp from Sep 2001). // too low to be a block time (corresponds to timestamp from Sep 2001).
if (heightParam > 1000000000) { if (heightParam > 1000000000) {
// Add a 2 hour buffer to include blocks which might have had old timestamps // Add a 2 hour buffer to include blocks which might have had old timestamps
CBlockIndex* pindex = chainActive.FindEarliestAtLeast(heightParam - 7200); CBlockIndex* pindex = chainActive.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW);
if (!pindex) { if (!pindex) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Could not find block with at least the specified timestamp."); throw JSONRPCError(RPC_INTERNAL_ERROR, "Could not find block with at least the specified timestamp.");
} }

View File

@ -2983,7 +2983,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early"); return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
// Check timestamp // Check timestamp
if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60) if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future"); return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
// Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded: // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:

View File

@ -497,7 +497,7 @@ UniValue importwallet(const JSONRPCRequest& request)
pwalletMain->ShowProgress("", 100); // hide progress dialog in GUI pwalletMain->ShowProgress("", 100); // hide progress dialog in GUI
CBlockIndex *pindex = chainActive.Tip(); CBlockIndex *pindex = chainActive.Tip();
while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200) while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - TIMESTAMP_WINDOW)
pindex = pindex->pprev; pindex = pindex->pprev;
pwalletMain->UpdateTimeFirstKey(nTimeBegin); pwalletMain->UpdateTimeFirstKey(nTimeBegin);
@ -1073,7 +1073,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
} }
if (fRescan && fRunScan && requests.size()) { if (fRescan && fRunScan && requests.size()) {
CBlockIndex* pindex = nLowestTimestamp > minimumTimestamp ? chainActive.FindEarliestAtLeast(std::max<int64_t>(nLowestTimestamp - 7200, 0)) : chainActive.Genesis(); CBlockIndex* pindex = nLowestTimestamp > minimumTimestamp ? chainActive.FindEarliestAtLeast(std::max<int64_t>(nLowestTimestamp - TIMESTAMP_WINDOW, 0)) : chainActive.Genesis();
CBlockIndex* scannedRange = nullptr; CBlockIndex* scannedRange = nullptr;
if (pindex) { if (pindex) {
scannedRange = pwalletMain->ScanForWalletTransactions(pindex, true); scannedRange = pwalletMain->ScanForWalletTransactions(pindex, true);
@ -1090,7 +1090,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
// range, or if the import result already has an error set, let // range, or if the import result already has an error set, let
// the result stand unmodified. Otherwise replace the result // the result stand unmodified. Otherwise replace the result
// with an error message. // with an error message.
if (GetImportTimestamp(request, now) - 7200 >= scannedRange->GetBlockTimeMax() || results.at(i).exists("error")) { if (GetImportTimestamp(request, now) - TIMESTAMP_WINDOW >= scannedRange->GetBlockTimeMax() || results.at(i).exists("error")) {
response.push_back(results.at(i)); response.push_back(results.at(i));
} else { } else {
UniValue result = UniValue(UniValue::VOBJ); UniValue result = UniValue(UniValue::VOBJ);

View File

@ -415,7 +415,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
CKey futureKey; CKey futureKey;
futureKey.MakeNewKey(true); futureKey.MakeNewKey(true);
key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(futureKey.GetPubKey()))); key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(futureKey.GetPubKey())));
key.pushKV("timestamp", newTip->GetBlockTimeMax() + 7200); key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW);
key.pushKV("internal", UniValue(true)); key.pushKV("internal", UniValue(true));
keys.push_back(key); keys.push_back(key);
JSONRPCRequest request; JSONRPCRequest request;

View File

@ -1562,7 +1562,7 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
// no need to read and scan block, if block was created before // no need to read and scan block, if block was created before
// our wallet birthday (as adjusted for block time variability) // our wallet birthday (as adjusted for block time variability)
while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200))) while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - TIMESTAMP_WINDOW)))
pindex = chainActive.Next(pindex); pindex = chainActive.Next(pindex);
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
@ -3490,7 +3490,7 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
// Extract block timestamps for those keys // Extract block timestamps for those keys
for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++) for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off mapKeyBirth[it->first] = it->second->GetBlockTime() - TIMESTAMP_WINDOW; // block times can be 2h off
} }
bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value) bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)