Skip to main content

hotshot_contract_adapter/
sol_types.rs

1//! Solidity types for interacting with contracts
2//! Re-export types that are used, sometimes renamed to avoid collision.
3//!
4//! TODO: (alex) Due to <https://github.com/foundry-rs/foundry/issues/10153>,
5//! try to re-export the same type from the "youngest" child contract since that is the contract whose functions are being called,
6//! thus from whom the rust bindings are expected.
7//! E.g. Both PlonkVerifier and LightClient, and LightClientV2 depends on BN254. The inheritance relationship is:
8//!   BN254 <- PlonkVerifier <- LIghtClient <- LightClientV2
9//! Most of the time, we interact with PlonkVerifier's function via LightClientV2, thus import BN254.G1Point from `bindings::plonkverifierv2`.
10//! When we need to directly interact with PlonkVerifier's method, implement stupid plain `From<lc2::BN254::G1Point> for pv::BN254::G1Point`.
11//! If you are lazy, you can even use unsafe memory transmute since they are literally the same representation, duplicated in different modules,
12//! thus treated by the rust type systems as distinct types.
13//!
14//! Another usage is in the differential testing in Solidity tests. In those cases, the actual types don't matter, since they will all `abi_encode()`
15//! into the exact same bytes before being communicated over to contract via FFI. Thus using any one of them is fine.
16
17use alloy::sol;
18
19/// # What to re-export, what to hide?
20/// - export contract struct itself, but try to avoid export instance type (instead, use ::new() to get a handle)
21/// - avoid exporting `xxCall` and `xxReturn` types, they usually can be converted/transmuted from existing struct
22/// - Event types should be exported
23/// - structs should be exported and renamed with `xxSol` suffix to avoid confusion with other rust types
24///   - see module doc for more explanation on types duplication issue in alloy
25pub use crate::bindings::{
26    access_control_upgradeable::AccessControlUpgradeable,
27    erc1967_proxy::ERC1967Proxy,
28    esp_token::EspToken,
29    esp_token_v2::EspTokenV2,
30    fee_contract::FeeContract::{self, Deposit},
31    light_client::{
32        BN254::G1Point as G1PointSol,
33        IPlonkVerifier::{PlonkProof as PlonkProofSol, VerifyingKey as VerifyingKeySol},
34        LightClient::{
35            self, LightClientErrors, LightClientInstance, LightClientState as LightClientStateSol,
36            StakeTableState as StakeTableStateSol,
37        },
38    },
39    light_client_mock::{self, LightClientMock},
40    light_client_v2::{self, LightClientV2},
41    light_client_v2_mock::{self, LightClientV2Mock},
42    light_client_v3::{self, LightClientV3},
43    light_client_v3_mock::{self, LightClientV3Mock},
44    ops_timelock::OpsTimelock,
45    ownable_upgradeable::OwnableUpgradeable,
46    plonk_verifier::PlonkVerifier,
47    plonk_verifier_v2::PlonkVerifierV2,
48    plonk_verifier_v3::PlonkVerifierV3,
49    reward_claim::RewardClaim,
50    safe_exit_timelock::SafeExitTimelock,
51    stake_table::StakeTable,
52    stake_table_v2::{self, StakeTableV2},
53    stake_table_v3::{
54        self, BN254::G2Point as G2PointSol, EdOnBN254::EdOnBN254Point as EdOnBN254PointSol,
55        StakeTableV3,
56    },
57};
58
59// For types that we need to interact with some functions but their bindings are not generated
60// we manually declare them there. It's possible that they get included in the future commits,
61// at which point, the rust type system will complain and we simply remove the manual declaration
62// and re-export the type from bindings instead.
63sol! {
64    /// types in src/legacy/Transcript.sol
65    struct TranscriptDataSol {
66        bytes32 state;
67        bytes transcript;
68    }
69
70    /// types in src/libraries/PlonkVerifierV2.sol
71    struct ChallengesSol {
72        uint256 alpha;
73        uint256 alpha2;
74        uint256 alpha3;
75        uint256 beta;
76        uint256 gamma;
77        uint256 zeta;
78        uint256 v;
79        uint256 u;
80    }
81}
82
83// Due to <https://github.com/foundry-rs/foundry/issues/10153> the rust bindings contain duplicate types for our solidity types.
84// In order to avoid writing a lot of boilerplate code we use transmute to convert between these duplicated types.
85// Since all the types we transmute between are generated by foundry from the same underlying solidity type
86// we expect that the order of fields and types of fields are always the same.
87impl From<LightClient::genesisStateReturn> for LightClientStateSol {
88    fn from(v: LightClient::genesisStateReturn) -> Self {
89        unsafe { std::mem::transmute(v) }
90    }
91}
92
93impl From<light_client_mock::LightClient::LightClientState> for LightClientStateSol {
94    fn from(v: light_client_mock::LightClient::LightClientState) -> Self {
95        unsafe { std::mem::transmute(v) }
96    }
97}
98impl From<light_client_mock::LightClientMock::finalizedStateReturn> for LightClientStateSol {
99    fn from(v: light_client_mock::LightClientMock::finalizedStateReturn) -> Self {
100        unsafe { std::mem::transmute(v) }
101    }
102}
103
104impl From<LightClientStateSol> for light_client_mock::LightClient::LightClientState {
105    fn from(v: LightClientStateSol) -> Self {
106        unsafe { std::mem::transmute(v) }
107    }
108}
109
110impl From<PlonkProofSol> for light_client_mock::IPlonkVerifier::PlonkProof {
111    fn from(v: PlonkProofSol) -> Self {
112        unsafe { std::mem::transmute(v) }
113    }
114}
115
116impl From<light_client_mock::LightClientMock::genesisStateReturn> for LightClientStateSol {
117    fn from(v: light_client_mock::LightClientMock::genesisStateReturn) -> Self {
118        unsafe { std::mem::transmute(v) }
119    }
120}
121
122impl From<LightClientV2::finalizedStateReturn> for LightClientStateSol {
123    fn from(v: LightClientV2::finalizedStateReturn) -> Self {
124        unsafe { std::mem::transmute(v) }
125    }
126}
127
128impl From<LightClientV2::votingStakeTableStateReturn> for StakeTableStateSol {
129    fn from(v: LightClientV2::votingStakeTableStateReturn) -> Self {
130        unsafe { std::mem::transmute(v) }
131    }
132}
133
134impl From<light_client_v2_mock::LightClient::LightClientState> for LightClientStateSol {
135    fn from(v: light_client_v2_mock::LightClient::LightClientState) -> Self {
136        unsafe { std::mem::transmute(v) }
137    }
138}
139impl From<LightClientStateSol> for light_client_v2_mock::LightClient::LightClientState {
140    fn from(v: LightClientStateSol) -> Self {
141        unsafe { std::mem::transmute(v) }
142    }
143}
144impl From<LightClientStateSol> for light_client_v2::LightClient::LightClientState {
145    fn from(v: LightClientStateSol) -> Self {
146        unsafe { std::mem::transmute(v) }
147    }
148}
149
150impl From<StakeTableStateSol> for light_client_v2::LightClient::StakeTableState {
151    fn from(v: StakeTableStateSol) -> Self {
152        unsafe { std::mem::transmute(v) }
153    }
154}
155impl From<StakeTableStateSol> for light_client_v2_mock::LightClient::StakeTableState {
156    fn from(v: StakeTableStateSol) -> Self {
157        unsafe { std::mem::transmute(v) }
158    }
159}
160
161impl From<LightClientV2Mock::genesisStateReturn> for LightClientStateSol {
162    fn from(v: LightClientV2Mock::genesisStateReturn) -> Self {
163        unsafe { std::mem::transmute(v) }
164    }
165}
166
167impl From<LightClientV2Mock::finalizedStateReturn> for LightClientStateSol {
168    fn from(v: LightClientV2Mock::finalizedStateReturn) -> Self {
169        unsafe { std::mem::transmute(v) }
170    }
171}
172
173impl From<PlonkProofSol> for light_client_v2::IPlonkVerifier::PlonkProof {
174    fn from(v: PlonkProofSol) -> Self {
175        unsafe { std::mem::transmute(v) }
176    }
177}
178
179impl From<LightClientV2Mock::votingStakeTableStateReturn> for StakeTableStateSol {
180    fn from(v: LightClientV2Mock::votingStakeTableStateReturn) -> Self {
181        unsafe { std::mem::transmute(v) }
182    }
183}
184
185impl From<stake_table_v3::BN254::G1Point> for G1PointSol {
186    fn from(v: stake_table_v3::BN254::G1Point) -> Self {
187        Self { x: v.x, y: v.y }
188    }
189}
190
191impl From<G1PointSol> for stake_table_v3::BN254::G1Point {
192    fn from(v: G1PointSol) -> Self {
193        Self { x: v.x, y: v.y }
194    }
195}
196
197// Transmute conversion functions for LightClientV3
198impl From<LightClientV3::finalizedStateReturn> for LightClientStateSol {
199    fn from(v: LightClientV3::finalizedStateReturn) -> Self {
200        unsafe { std::mem::transmute(v) }
201    }
202}
203
204impl From<LightClientV3::votingStakeTableStateReturn> for StakeTableStateSol {
205    fn from(v: LightClientV3::votingStakeTableStateReturn) -> Self {
206        unsafe { std::mem::transmute(v) }
207    }
208}
209
210impl From<LightClientStateSol> for light_client_v3::LightClient::LightClientState {
211    fn from(v: LightClientStateSol) -> Self {
212        unsafe { std::mem::transmute(v) }
213    }
214}
215
216impl From<StakeTableStateSol> for light_client_v3::LightClient::StakeTableState {
217    fn from(v: StakeTableStateSol) -> Self {
218        unsafe { std::mem::transmute(v) }
219    }
220}
221
222impl From<PlonkProofSol> for light_client_v3::IPlonkVerifier::PlonkProof {
223    fn from(v: PlonkProofSol) -> Self {
224        unsafe { std::mem::transmute(v) }
225    }
226}
227
228// Transmute conversion functions for LightClientV3Mock
229impl From<light_client_v3_mock::LightClient::LightClientState> for LightClientStateSol {
230    fn from(v: light_client_v3_mock::LightClient::LightClientState) -> Self {
231        unsafe { std::mem::transmute(v) }
232    }
233}
234
235impl From<LightClientStateSol> for light_client_v3_mock::LightClient::LightClientState {
236    fn from(v: LightClientStateSol) -> Self {
237        unsafe { std::mem::transmute(v) }
238    }
239}
240
241impl From<StakeTableStateSol> for light_client_v3_mock::LightClient::StakeTableState {
242    fn from(v: StakeTableStateSol) -> Self {
243        unsafe { std::mem::transmute(v) }
244    }
245}
246
247impl From<LightClientV3Mock::genesisStateReturn> for LightClientStateSol {
248    fn from(v: LightClientV3Mock::genesisStateReturn) -> Self {
249        unsafe { std::mem::transmute(v) }
250    }
251}
252
253impl From<LightClientV3Mock::finalizedStateReturn> for LightClientStateSol {
254    fn from(v: LightClientV3Mock::finalizedStateReturn) -> Self {
255        unsafe { std::mem::transmute(v) }
256    }
257}
258
259impl From<LightClientV3Mock::votingStakeTableStateReturn> for StakeTableStateSol {
260    fn from(v: LightClientV3Mock::votingStakeTableStateReturn) -> Self {
261        unsafe { std::mem::transmute(v) }
262    }
263}
264
265#[cfg(test)]
266mod tests {
267    use alloy::{primitives::U256, sol_types::private::Address};
268
269    use crate::sol_types::StakeTableV3::CommissionUpdated;
270
271    #[test]
272    fn test_commission_updated_serde_roundtrip() {
273        let original = CommissionUpdated {
274            validator: Address::random(),
275            timestamp: U256::from(999),
276            oldCommission: 123,
277            newCommission: 456,
278        };
279
280        let serialized = bincode::serialize(&original).expect("Failed to serialize");
281        let deserialized: CommissionUpdated =
282            bincode::deserialize(&serialized).expect("Failed to deserialize");
283
284        assert_eq!(original.validator, deserialized.validator);
285        assert_eq!(original.timestamp, deserialized.timestamp);
286        assert_eq!(original.oldCommission, deserialized.oldCommission);
287        assert_eq!(original.newCommission, deserialized.newCommission);
288    }
289}