Skip to content

Commit

Permalink
Update consensus that blocks must be in chronological order (#2933)
Browse files Browse the repository at this point in the history
* Update consensus that blocks must be in chronological order

* Add option to test forward staking

* Rename arg

* Rename arg
  • Loading branch information
Bushstar authored Jul 5, 2024
1 parent 6096226 commit 44ab45a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ void SetupServerArgs()
gArgs.AddArg("-blocktimeordering", strprintf("(Deprecated) Whether to order transactions by time, otherwise ordered by fee (default: %u)", false), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-txordering", strprintf("Whether to order transactions by entry time, fee or both randomly (0: mixed, 1: fee based, 2: entry time) (default: %u)", DEFAULT_TX_ORDERING), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-ethstartstate", strprintf("Initialise Ethereum state trie using JSON input"), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-ascendingstaketime", strprintf("Test staking forward in time from the current block"), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#ifdef USE_UPNP
#if USE_UPNP
gArgs.AddArg("-upnp", "Use UPnP to map the listening port (default: 1 when listening and no -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
Expand Down
10 changes: 8 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,8 +1177,14 @@ namespace pos {
nLastCoinStakeSearchTime = tip->GetMedianTimePast() + 1;
}
} else {
// Plus one to avoid time-too-old error on exact median time.
nLastCoinStakeSearchTime = tip->GetMedianTimePast() + 1;
if (gArgs.GetBoolArg("-ascendingstaketime", false) ||
blockHeight >= static_cast<int64_t>(Params().GetConsensus().DF24Height)) {
// Set time to last block time. New blocks must be after the last block.
nLastCoinStakeSearchTime = tip->GetBlockTime();
} else {
// Plus one to avoid time-too-old error on exact median time.
nLastCoinStakeSearchTime = tip->GetMedianTimePast() + 1;
}
}

lastBlockSeen = tip->GetBlockHash();
Expand Down
23 changes: 16 additions & 7 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5047,13 +5047,22 @@ static bool ContextualCheckBlockHeader(const CBlockHeader &block,
}

// Check timestamp against prev
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
return state.Invalid(ValidationInvalidReason::BLOCK_INVALID_HEADER,
false,
"time-too-old",
strprintf("block's timestamp is too early. Block time: %d Min time: %d",
block.GetBlockTime(),
pindexPrev->GetMedianTimePast()));
if (nHeight >= consensusParams.DF24Height) {
if (block.GetBlockTime() <= pindexPrev->GetBlockTime()) {
return state.Invalid(ValidationInvalidReason::BLOCK_INVALID_HEADER,
false,
"time-before-prev",
"block's timestamp cannot be the same or before previous block's timestamp");
}
} else {
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
return state.Invalid(ValidationInvalidReason::BLOCK_INVALID_HEADER,
false,
"time-too-old",
strprintf("block's timestamp is too early. Block time: %d Min time: %d",
block.GetBlockTime(),
pindexPrev->GetMedianTimePast()));
}
}

// Check timestamp
Expand Down

0 comments on commit 44ab45a

Please sign in to comment.