kona_engine/
versions.rs

1//! Engine API version selection based on Optimism hardfork activations.
2//!
3//! Automatically selects the appropriate Engine API method versions based on
4//! the rollup configuration and block timestamps. Different Optimism hardforks
5//! require different Engine API versions to support new features.
6//!
7//! # Version Mapping
8//!
9//! - **Bedrock, Canyon, Delta** → V2 methods
10//! - **Ecotone (Cancun)** → V3 methods
11//! - **Isthmus** → V4 methods
12//!
13//! Adapted from the [OP Node version providers](https://github.com/ethereum-optimism/optimism/blob/develop/op-node/rollup/types.go#L546).
14
15use kona_genesis::RollupConfig;
16
17/// Engine API version for `engine_forkchoiceUpdated` method calls.
18///
19/// Selects between V2 and V3 based on hardfork activation. V3 is required
20/// for Ecotone/Cancun and later hardforks to support new consensus features.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub enum EngineForkchoiceVersion {
23    /// Version 2: Used for Bedrock, Canyon, and Delta hardforks.
24    V2,
25    /// Version 3: Required for Ecotone/Cancun and later hardforks.
26    V3,
27}
28
29impl EngineForkchoiceVersion {
30    /// Returns the appropriate [`EngineForkchoiceVersion`] for the chain at the given attributes.
31    ///
32    /// Uses the [`RollupConfig`] to check which hardfork is active at the given timestamp.
33    pub fn from_cfg(cfg: &RollupConfig, timestamp: u64) -> Self {
34        if cfg.is_ecotone_active(timestamp) {
35            // Cancun+
36            Self::V3
37        } else {
38            // Bedrock, Canyon, Delta
39            Self::V2
40        }
41    }
42}
43
44/// Engine API version for `engine_newPayload` method calls.
45///
46/// Progressive version selection based on hardfork activation:
47/// - V2: Basic payload processing
48/// - V3: Adds Cancun/Ecotone support
49/// - V4: Adds Isthmus hardfork features
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
51pub enum EngineNewPayloadVersion {
52    /// Version 2: Basic payload processing for early hardforks.
53    V2,
54    /// Version 3: Adds Cancun/Ecotone consensus features.
55    V3,
56    /// Version 4: Adds Isthmus hardfork support.
57    V4,
58}
59
60impl EngineNewPayloadVersion {
61    /// Returns the appropriate [`EngineNewPayloadVersion`] for the chain at the given timestamp.
62    ///
63    /// Uses the [`RollupConfig`] to check which hardfork is active at the given timestamp.
64    pub fn from_cfg(cfg: &RollupConfig, timestamp: u64) -> Self {
65        if cfg.is_isthmus_active(timestamp) {
66            Self::V4
67        } else if cfg.is_ecotone_active(timestamp) {
68            // Cancun
69            Self::V3
70        } else {
71            Self::V2
72        }
73    }
74}
75
76/// Engine API version for `engine_getPayload` method calls.
77///
78/// Matches the payload version used for retrieval with the version
79/// used during payload construction, ensuring API compatibility.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
81pub enum EngineGetPayloadVersion {
82    /// Version 2: Basic payload retrieval.
83    V2,
84    /// Version 3: Enhanced payload data for Cancun/Ecotone.
85    V3,
86    /// Version 4: Extended payload format for Isthmus.
87    V4,
88}
89
90impl EngineGetPayloadVersion {
91    /// Returns the appropriate [`EngineGetPayloadVersion`] for the chain at the given timestamp.
92    ///
93    /// Uses the [`RollupConfig`] to check which hardfork is active at the given timestamp.
94    pub fn from_cfg(cfg: &RollupConfig, timestamp: u64) -> Self {
95        if cfg.is_isthmus_active(timestamp) {
96            Self::V4
97        } else if cfg.is_ecotone_active(timestamp) {
98            // Cancun
99            Self::V3
100        } else {
101            Self::V2
102        }
103    }
104}