kona_engine/
versions.rs

1//! Provides engine api endpoint versions
2//!
3//! Adapted from the [op-node version providers][vp].
4//!
5//! [vp]: https://github.com/ethereum-optimism/optimism/blob/develop/op-node/rollup/types.go#L546
6
7use kona_genesis::RollupConfig;
8
9/// The method version for the `engine_forkchoiceUpdated` api.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum EngineForkchoiceVersion {
12    /// The `engine_forkchoiceUpdated` api version 1.
13    V1,
14    /// The `engine_forkchoiceUpdated` api version 2.
15    V2,
16    /// The `engine_forkchoiceUpdated` api version 3.
17    V3,
18}
19
20impl EngineForkchoiceVersion {
21    /// Returns the appropriate [`EngineForkchoiceVersion`] for the chain at the given attributes.
22    ///
23    /// Uses the [`RollupConfig`] to check which hardfork is active at the given timestamp.
24    pub fn from_cfg(cfg: &RollupConfig, timestamp: u64) -> Self {
25        if cfg.is_ecotone_active(timestamp) {
26            // Cancun
27            Self::V3
28        } else if cfg.is_canyon_active(timestamp) {
29            // Shanghai
30            Self::V2
31        } else {
32            // According to Ethereum engine API spec, we can use fcuV2 here,
33            // but Geth v1.13.11 does not accept V2 before Shanghai.
34            Self::V1
35        }
36    }
37}
38
39/// The method version for the `engine_newPayload` api.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
41pub enum EngineNewPayloadVersion {
42    /// The `engine_newPayload` api version 2.
43    V2,
44    /// The `engine_newPayload` api version 3.
45    V3,
46    /// The `engine_newPayload` api version 4.
47    V4,
48}
49
50impl EngineNewPayloadVersion {
51    /// Returns the appropriate [`EngineNewPayloadVersion`] for the chain at the given timestamp.
52    ///
53    /// Uses the [`RollupConfig`] to check which hardfork is active at the given timestamp.
54    pub fn from_cfg(cfg: &RollupConfig, timestamp: u64) -> Self {
55        if cfg.is_isthmus_active(timestamp) {
56            Self::V4
57        } else if cfg.is_ecotone_active(timestamp) {
58            // Cancun
59            Self::V3
60        } else {
61            Self::V2
62        }
63    }
64}
65
66/// The method version for the `engine_getPayload` api.
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
68pub enum EngineGetPayloadVersion {
69    /// The `engine_getPayload` api version 2.
70    V2,
71    /// The `engine_getPayload` api version 3.
72    V3,
73    /// The `engine_getPayload` api version 4.
74    V4,
75}
76
77impl EngineGetPayloadVersion {
78    /// Returns the appropriate [`EngineGetPayloadVersion`] for the chain at the given timestamp.
79    ///
80    /// Uses the [`RollupConfig`] to check which hardfork is active at the given timestamp.
81    pub fn from_cfg(cfg: &RollupConfig, timestamp: u64) -> Self {
82        if cfg.is_isthmus_active(timestamp) {
83            Self::V4
84        } else if cfg.is_ecotone_active(timestamp) {
85            // Cancun
86            Self::V3
87        } else {
88            Self::V2
89        }
90    }
91}