Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions src/herder/HerderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,16 +1229,69 @@ HerderImpl::setupTriggerNextLedger()
// bootstrap with a pessimistic estimate of when
// the ballot protocol started last
auto now = mApp.getClock().now();
auto lastBallotStart = now - milliseconds;
auto lastStart = mHerderSCPDriver.getPrepareStart(lastIndex);
if (lastStart)
auto lastLedgerStatingPoint = now - milliseconds;

#ifdef BUILD_TESTS
if (mApp.getConfig().EXPERIMENTAL_TRIGGER_TIMER &&
mApp.getClock().getMode() == VirtualClock::REAL_TIME)
{
auto consensusCloseTime = trackingConsensusCloseTime();

// Bootstrap to pessimistic estimate on startup
if (consensusCloseTime == 0)
{
CLOG_WARNING(
Herder,
"Consensus close time is 0, using pessimistic estimate");
// Keep the existing lastLedgerStatingPoint
}
else
{
// The externalized close time is a unix timestamp. We convert it to
// steady_clock time by:
// 1. Converting unix timestamp to system_clock::time_point (wall
// clock time)
// 2. Calculating how long ago that was from current system time
// 3. Subtracting that duration from current steady_clock time
auto externalizedSystemTime =
VirtualClock::from_time_t(consensusCloseTime);
auto currentSystemTime = mApp.getClock().system_now();

// Handle clock drift: if externalized time is in the future,
// fall back to pessimistic estimate
if (externalizedSystemTime >= currentSystemTime)
{
CLOG_WARNING(Herder,
"Externalized closeTime {} is in the future "
"(current time {}), "
"using pessimistic estimate",
consensusCloseTime,
VirtualClock::to_time_t(currentSystemTime));
// Keep the existing lastLedgerStatingPoint which is already set
// to the pessimistic estimate (now - milliseconds)
}
else
{
// Calculate how long ago the externalized closeTime was
auto timeSinceExternalized =
currentSystemTime - externalizedSystemTime;
lastLedgerStatingPoint = now - timeSinceExternalized;
}
}
}
else
#endif
{
lastBallotStart = *lastStart;
auto lastStart = mHerderSCPDriver.getPrepareStart(lastIndex);
if (lastStart)
{
lastLedgerStatingPoint = *lastStart;
}
}

// Adjust trigger time in case node's clock has drifted.
// This ensures that next value to nominate is valid
auto triggerTime = lastBallotStart + milliseconds;
auto triggerTime = lastLedgerStatingPoint + milliseconds;

if (triggerTime < now)
{
Expand Down
3 changes: 3 additions & 0 deletions src/main/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ Config::Config() : NODE_SEED(SecretKey::random())
CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING = false;
MODE_USES_IN_MEMORY_LEDGER = false;
SKIP_HIGH_CRITICAL_VALIDATOR_CHECKS_FOR_TESTING = false;
EXPERIMENTAL_TRIGGER_TIMER = false;
#endif

#ifdef BEST_OFFER_DEBUGGING
Expand Down Expand Up @@ -1131,6 +1132,8 @@ Config::processConfig(std::shared_ptr<cpptoml::table> t)
EXPERIMENTAL_BACKGROUND_TX_SIG_VERIFICATION =
readBool(item);
}},
{"EXPERIMENTAL_TRIGGER_TIMER",
[&]() { EXPERIMENTAL_TRIGGER_TIMER = readBool(item); }},
{"ARTIFICIALLY_DELAY_LEDGER_CLOSE_FOR_TESTING",
[&]() {
ARTIFICIALLY_DELAY_LEDGER_CLOSE_FOR_TESTING =
Expand Down
5 changes: 5 additions & 0 deletions src/main/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,11 @@ class Config : public std::enable_shared_from_this<Config>
size_t TESTING_MAX_SOROBAN_BYTE_ALLOWANCE;
size_t TESTING_MAX_CLASSIC_BYTE_ALLOWANCE;

// Experimental flag to use externalized close time for trigger timer
// calculation instead of prepare start time. Should only be used for
// testing.
bool EXPERIMENTAL_TRIGGER_TIMER;

// Set QUORUM_SET using automatic quorum set configuration based on
// `validators`.
void
Expand Down
1 change: 1 addition & 0 deletions src/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ getTestConfig(int instanceNumber, Config::TestDbMode mode)
thisConfig.MANUAL_CLOSE = true;

thisConfig.TEST_CASES_ENABLED = true;
thisConfig.EXPERIMENTAL_TRIGGER_TIMER = true;

thisConfig.PEER_PORT =
static_cast<unsigned short>(DEFAULT_PEER_PORT + instanceNumber * 2);
Expand Down