execution_time.rs
Overview
This file provides utilities for managing and adjusting execution time limits and production timeouts within a system that processes blocks and messages. It contains logic to dynamically correct production timeout values based on recent execution durations, and it encapsulates rules for determining execution deadlines and message timeouts during block production and verification phases.
The file primarily defines two structs:
ProductionTimeoutCorrection: Maintains and updates a correction value to adjust production timeouts adaptively.ExecutionTimeLimits: Represents time constraints and deadlines for block processing and message execution, including support for alternative timeout values for specific messages.
Structs and Their Functionality
ProductionTimeoutCorrection
Manages a correction factor for production timeouts, which adjusts dynamically based on the actual duration of the last production cycle.
Fields
last_production_duration: i64
Stores the duration of the last production cycle in milliseconds.correction: i64
An adjustment value (in milliseconds) applied to the desired production timeout. It typically holds negative values to reduce the timeout.
Implementations
Default
Initializes withlast_production_durationset to 0 andcorrectioninitialized to -50 milliseconds.
Methods
report_last_production(&mut self, duration: Duration)
Records the duration of the last production cycle.
Parameters:duration: The actual duration of the last production, as astd::time::Duration.
Usage Example:
let mut correction = ProductionTimeoutCorrection::default(); correction.report_last_production(Duration::from_millis(120));get_production_timeout(&mut self, desired: Duration) -> Duration
Calculates the adjusted production timeout by applying the current correction to the desired timeout. The correction is updated based on whether the last production was longer or shorter than the desired duration. The correction will not exceed 90% of the desired duration in the negative direction.
Parameters:desired: The requested production timeout duration.
Returns:An adjusted
Durationreflecting the corrected timeout value.
Usage Example:
let desired_timeout = Duration::from_millis(200); let adjusted_timeout = correction.get_production_timeout(desired_timeout);get_correction(&self) -> i64
Returns the current correction value in milliseconds.
Note: This method is crate-private.
Implementation Details
The correction logic attempts to keep the production timeout aligned with the actual production durations observed. If the last duration exceeds the desired timeout, the correction is decreased by 10 ms, lowering the timeout. If the production was significantly shorter (more than 20 ms less than desired), the correction is increased by 5 ms, allowing more time. The correction is capped so it cannot reduce the timeout by more than 90% of the desired duration, preventing overly aggressive timeout reductions.
ExecutionTimeLimits
Encapsulates time limits related to block processing and message execution, including deadlines for block production and optional differentiated timeouts for specific "alternative" messages.
Fields
block_deadline: Option<Instant>
An optional absolute deadline by which block processing should be completed.default_message_timeout: Option<Duration>
The standard timeout duration for message execution.alternative_message_timeout: Option<Duration>
An alternative timeout duration used for a subset of messages, typically those identified as requiring different timing constraints.alternative_messages: Option<HashSet<UInt256>>
An optional set of message hashes for which the alternative timeout applies.
Constants
NO_LIMITS
A constant instance representing no time constraints (all fields set toNone).
Methods
new(block_deadline: Option<Instant>, default_message_timeout: Option<Duration>, alternative_message_timeout: Option<Duration>) -> Self
Creates a newExecutionTimeLimitsinstance with specified deadlines and timeouts.alternative_messagesis initialized toNone.
Parameters:block_deadline: Optional deadline for block completion.default_message_timeout: Optional default timeout for messages.alternative_message_timeout: Optional alternative timeout for specific messages.
production(block_timeout: Duration, config: &Config) -> Self
Constructs anExecutionTimeLimitsinstance configured for block production, setting the block deadline to now plusblock_timeout, and setting the default message timeout from the configuration. No alternative message timeout is set.
Parameters:block_timeout: Duration representing the maximum allowed block production time.config: Reference to aConfiginstance providing global timing parameters.
verification(config: &Config) -> Self
Constructs anExecutionTimeLimitsinstance configured for block verification, setting the block deadline and message timeouts based on values from the configuration. Both default and alternative message timeouts are set according to configuration fields.
Parameters:config: Reference to aConfiginstance providing verification timing parameters.
add_alternative_message(&mut self, message_hash: UInt256)
Adds a message hash to the set of alternative messages, which are subject to the alternative message timeout. If the set does not exist, it is created.
Parameters:message_hash: The hash of the message (typeUInt256) to mark as alternative.
block_deadline(&self) -> Option<Instant>
Returns the optional block deadline.get_message_timeout(&self, message_hash: &UInt256) -> Option<Duration>
Returns the timeout duration that applies to a given message hash. If the hash is in the alternative messages set, the alternative timeout is returned; otherwise, the default timeout is returned.
Parameters:message_hash: Reference to the message hash to query.
Implementation Details
This struct facilitates flexible timeout management by allowing differentiated timeouts for specific messages, useful in scenarios where some messages require special handling or extended/shortened execution windows. The use of Instant for deadlines allows efficient comparison with the current time to enforce deadlines.
Interaction with Other System Components
The
ProductionTimeoutCorrectionstruct is intended for use in components that manage block production timing, allowing them to dynamically adjust timeouts based on recent production duration history.The
ExecutionTimeLimitsstruct relies on configuration data (Config) which presumably encapsulates system-wide timing parameters. This integration ensures that the timing constraints reflect current system settings and policies.The use of
UInt256for message hashes indicates interaction with components handling message identification, likely in a blockchain or distributed ledger context.The
ExecutionTimeLimitsstruct’s methods provide interfaces to query timeouts and deadlines, which would be consumed by execution engines or schedulers responsible for enforcing these limits.
Visual Diagram
classDiagram
class ProductionTimeoutCorrection {
-last_production_duration: i64
-correction: i64
+report_last_production()
+get_production_timeout()
+get_correction()
}
class ExecutionTimeLimits {
-block_deadline: Option<Instant>
-default_message_timeout: Option<Duration>
-alternative_message_timeout: Option<Duration>
-alternative_messages: Option<HashSet<UInt256>>
+new()
+production()
+verification()
+add_alternative_message()
+block_deadline()
+get_message_timeout()
}
This diagram illustrates the two main structs with their core fields and exposed methods, highlighting their encapsulated responsibilities related to execution timing and timeout adjustment.