Skip to content

Commit 3ba4a1d

Browse files
authored
Beacon chain types refactor (#3995)
massive refactor to our beacon chain types. we are now aware of all forks and forced to handle them. also versioned the ethereum client state, potentially will do the same to the consensus state? includes the changes from #3878 as well. TODO: - [ ] docs on new libraries - [ ] json spec tests for the beacon types (not a hard requirement for merging this pr)
2 parents 102d37f + 539bdf5 commit 3ba4a1d

134 files changed

Lines changed: 3695 additions & 1563 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ members = [
6666
"lib/arbitrum-verifier",
6767
"lib/cometbls-groth16-verifier",
6868
"lib/ethereum-sync-protocol",
69+
"lib/ethereum-sync-protocol-types",
6970
"lib/evm-storage-verifier",
7071
"lib/scroll-verifier",
7172
"lib/tendermint-verifier",
@@ -192,6 +193,7 @@ members = [
192193
"cosmwasm/ucs03-zkgm-token-minter-api",
193194
"cosmwasm/cw20-base",
194195
"lib/scroll-types",
196+
"lib/fork-schedules",
195197
]
196198

197199
[workspace.package]
@@ -216,13 +218,15 @@ opt-level = 3
216218
[workspace.dependencies]
217219
aptos-move-ibc = { path = "generated/rust/aptos-move-ibc", default-features = false }
218220
aptos-verifier = { path = "lib/aptos-verifier", default-features = false }
219-
beacon-api = { path = "lib/beacon-api", default-features = false }
220-
beacon-api-types = { path = "lib/beacon-api-types", default-features = false }
221221
cometbft-rpc = { path = "lib/cometbft-rpc", default-features = false }
222222
cometbft-types = { path = "lib/cometbft-types", default-features = false }
223223
concurrent-keyring = { path = "lib/concurrent-keyring", default-features = false }
224224
cosmos-client = { path = "lib/cosmos-client", default-features = false }
225225

226+
beacon-api = { path = "lib/beacon-api", default-features = false }
227+
beacon-api-types = { path = "lib/beacon-api-types", default-features = false }
228+
fork-schedules = { path = "lib/fork-schedules", default-features = false }
229+
226230
state-lens-light-client-types = { path = "lib/state-lens-light-client-types", default-features = false }
227231

228232
arbitrum-light-client-types = { path = "lib/arbitrum-light-client-types", default-features = false }
@@ -234,10 +238,11 @@ cometbls-light-client-types = { path = "lib/cometbls-light-client-types", defaul
234238

235239
scroll-light-client-types = { path = "lib/scroll-light-client-types", default-features = false }
236240

237-
ethereum-light-client = { path = "cosmwasm/ibc-union/lightclient/ethereum", default-features = false }
238-
ethereum-light-client-types = { path = "lib/ethereum-light-client-types", default-features = false }
239-
ethereum-sync-protocol = { path = "lib/ethereum-sync-protocol", default-features = false }
240-
evm-storage-verifier = { path = "lib/evm-storage-verifier", default-features = false }
241+
ethereum-light-client = { path = "cosmwasm/ibc-union/lightclient/ethereum", default-features = false }
242+
ethereum-light-client-types = { path = "lib/ethereum-light-client-types", default-features = false }
243+
ethereum-sync-protocol = { path = "lib/ethereum-sync-protocol", default-features = false }
244+
ethereum-sync-protocol-types = { path = "lib/ethereum-sync-protocol-types", default-features = false }
245+
evm-storage-verifier = { path = "lib/evm-storage-verifier", default-features = false }
241246

242247
berachain-light-client-types = { path = "lib/berachain-light-client-types", default-features = false }
243248
scroll-api = { path = "lib/scroll-api", default-features = false }

cosmwasm/ibc-union/lightclient/berachain/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use beacon_api_types::{ExecutionPayloadHeaderSsz, Mainnet};
1+
use beacon_api_types::{chain_spec::Mainnet, deneb};
22
use berachain_light_client_types::{ClientState, ConsensusState, Header};
33
use cosmwasm_std::Empty;
44
use ethereum_light_client_types::StorageProof;
@@ -122,7 +122,7 @@ impl IbcClient for BerachainLightClient {
122122
b"beacon".to_vec(),
123123
[LATEST_EXECUTION_PAYLOAD_HEADER_PREFIX].to_vec(),
124124
],
125-
ExecutionPayloadHeaderSsz::<Mainnet>::try_from(header.execution_header.clone())
125+
deneb::ExecutionPayloadHeaderSsz::<Mainnet>::try_from(header.execution_header.clone())
126126
.map_err(Into::<Error>::into)?
127127
.encode_as::<Ssz>(),
128128
)

cosmwasm/ibc-union/lightclient/berachain/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub enum Error {
4242
TrustedValidatorsMismatch(#[from] TrustedValidatorsMismatch),
4343

4444
#[error(transparent)]
45-
ExecutionPayloadHeader(#[from] beacon_api_types::execution_payload_header::ssz::Error),
45+
ExecutionPayloadHeader(#[from] beacon_api_types::deneb::execution_payload_header::ssz::Error),
4646

4747
#[error(transparent)]
4848
MigrateClientStore(#[from] MigrateClientStoreError),

cosmwasm/ibc-union/lightclient/ethereum/src/client.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use beacon_api_types::{ChainSpec, Mainnet, Minimal, PresetBaseKind};
1+
use beacon_api_types::chain_spec::{ChainSpec, Mainnet, Minimal, PresetBaseKind};
22
use cosmwasm_std::Empty;
33
use ethereum_light_client_types::{
4-
ClientState, ConsensusState, Header, LightClientUpdate, Misbehaviour, StorageProof,
4+
ClientState, ClientStateV1, ConsensusState, Header, LightClientUpdate, Misbehaviour,
5+
StorageProof,
56
};
67
use ethereum_sync_protocol::{
78
utils::{
@@ -81,17 +82,26 @@ impl IbcClient for EthereumLightClient {
8182
}
8283

8384
fn get_latest_height(client_state: &Self::ClientState) -> u64 {
84-
client_state.latest_height
85+
match client_state {
86+
ClientState::V1(cs) => cs.latest_height,
87+
}
8588
}
8689

8790
fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
88-
client_state.chain_id.to_string()
91+
match client_state {
92+
ClientState::V1(cs) => cs.chain_id.to_string(),
93+
}
8994
}
9095

9196
fn status(ctx: IbcClientCtx<Self>, client_state: &Self::ClientState) -> Status {
9297
let _ = ctx;
9398

94-
if client_state.frozen_height.height() != 0 {
99+
if match client_state {
100+
ClientState::V1(cs) => cs.frozen_height,
101+
}
102+
.height()
103+
!= 0
104+
{
95105
Status::Frozen
96106
} else {
97107
Status::Active
@@ -110,7 +120,7 @@ impl IbcClient for EthereumLightClient {
110120
header: Header,
111121
_caller: cosmwasm_std::Addr,
112122
) -> Result<(u64, Self::ClientState, Self::ConsensusState), IbcClientError<Self>> {
113-
let client_state = ctx.read_self_client_state()?;
123+
let ClientState::V1(client_state) = ctx.read_self_client_state()?;
114124
let consensus_state = ctx.read_self_consensus_state(header.trusted_height.height())?;
115125

116126
match client_state.chain_spec {
@@ -131,7 +141,7 @@ impl IbcClient for EthereumLightClient {
131141
let consensus_state =
132142
ctx.read_self_consensus_state(misbehaviour.trusted_height.height())?;
133143

134-
let mut client_state = ctx.read_self_client_state()?;
144+
let ClientState::V1(mut client_state) = ctx.read_self_client_state()?;
135145

136146
match client_state.chain_spec {
137147
PresetBaseKind::Minimal => {
@@ -144,7 +154,7 @@ impl IbcClient for EthereumLightClient {
144154

145155
client_state.frozen_height = Height::new(1);
146156

147-
Ok(client_state)
157+
Ok(ClientState::V1(client_state))
148158
}
149159
}
150160

@@ -214,7 +224,7 @@ pub fn check_commitment_key(path: H256, key: U256) -> Result<(), Error> {
214224

215225
pub fn verify_header<C: ChainSpec>(
216226
ctx: &IbcClientCtx<EthereumLightClient>,
217-
client_state: ClientState,
227+
client_state: ClientStateV1,
218228
consensus_state: ConsensusState,
219229
header: Header,
220230
) -> Result<(u64, ClientState, ConsensusState), Error> {
@@ -242,13 +252,13 @@ pub fn verify_header<C: ChainSpec>(
242252
}
243253

244254
validate_light_client_update::<C, _>(
245-
&header.consensus_update.clone().into(),
255+
client_state.chain_id,
256+
&header.consensus_update.clone().into_light_client_update(),
246257
current_sync_committee,
247258
next_sync_committee,
248259
current_slot,
249260
consensus_state.slot,
250261
client_state.genesis_validators_root,
251-
&client_state.fork_parameters,
252262
VerificationContext { deps: ctx.deps },
253263
)
254264
.map_err(Error::ValidateLightClient)?;
@@ -275,7 +285,7 @@ pub fn verify_header<C: ChainSpec>(
275285
}
276286

277287
fn update_state<C: ChainSpec>(
278-
mut client_state: ClientState,
288+
mut client_state: ClientStateV1,
279289
mut consensus_state: ConsensusState,
280290
header: Header,
281291
) -> Result<(u64, ClientState, ConsensusState), Error> {
@@ -313,12 +323,16 @@ fn update_state<C: ChainSpec>(
313323
}
314324
}
315325

316-
Ok((updated_height, client_state, consensus_state))
326+
Ok((
327+
updated_height,
328+
ClientState::V1(client_state),
329+
consensus_state,
330+
))
317331
}
318332

319333
pub fn verify_misbehaviour<C: ChainSpec>(
320334
ctx: &IbcClientCtx<EthereumLightClient>,
321-
client_state: &ClientState,
335+
client_state: &ClientStateV1,
322336
consensus_state: ConsensusState,
323337
misbehaviour: Misbehaviour,
324338
) -> Result<(), Error> {
@@ -357,13 +371,13 @@ pub fn verify_misbehaviour<C: ChainSpec>(
357371

358372
// Make sure both headers would have been accepted by the light client
359373
validate_light_client_update::<C, VerificationContext>(
360-
&misbehaviour.update_1.clone().into(),
374+
client_state.chain_id,
375+
&misbehaviour.update_1.clone().into_light_client_update(),
361376
current_sync_committee,
362377
next_sync_committee,
363378
current_slot,
364379
consensus_state.slot,
365380
client_state.genesis_validators_root,
366-
&client_state.fork_parameters,
367381
VerificationContext { deps: ctx.deps },
368382
)
369383
.map_err(Error::ValidateLightClient)?;
@@ -384,13 +398,13 @@ pub fn verify_misbehaviour<C: ChainSpec>(
384398
misbehaviour.update_2.currently_trusted_sync_committee();
385399

386400
validate_light_client_update::<C, VerificationContext>(
387-
&misbehaviour.update_2.clone().into(),
401+
client_state.chain_id,
402+
&misbehaviour.update_1.into_light_client_update(),
388403
current_sync_committee,
389404
next_sync_committee,
390405
current_slot,
391406
consensus_state.slot,
392407
client_state.genesis_validators_root,
393-
&client_state.fork_parameters,
394408
VerificationContext { deps: ctx.deps },
395409
)
396410
.map_err(Error::ValidateLightClient)?;

cosmwasm/ibc-union/lightclient/ethereum/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use beacon_api_types::Slot;
1+
use beacon_api_types::slot::Slot;
22
use cosmwasm_std::{StdError, VerificationError};
33
use ibc_union_light_client::IbcClientError;
44
use unionlabs::primitives::{H256, U256};

cosmwasm/ibc-union/lightclient/ethereum/src/verification.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ pub fn check_aggregate_pubkey(
7676

7777
#[cfg(test)]
7878
mod tests {
79-
use beacon_api_types::SyncCommittee;
79+
use beacon_api_types::altair;
8080
use cosmwasm_std::testing::mock_dependencies;
8181

8282
use super::*;
8383

8484
#[test]
8585
fn test_check_aggregate_pubkey() {
86-
let sync_committee: SyncCommittee = serde_json::from_str(
86+
let sync_committee: altair::SyncCommittee = serde_json::from_str(
8787
r#"{"pubkeys":["0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac","0xb7e6e187ed813d950a9a17d1e70c03e4de2903596c4c5ff326848515c985deee38198efebc265300cd4f1d6bd7b5d264","0x914b56f41c411fbfca9dc9763f44daf253c103b162457d07954fd0af768b5e74692b4639c22455fb81d71f7ed6144514","0x8b47707a1f563d3b1034e20be2a663587f17fece6581fca156cf660575fde4b8de4d45f1fda7ade9167b953d4c93417d","0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b","0x9314c6de0386635e2799af798884c2ea09c63b9f079e572acc00b06a7faccce501ea4dfc0b1a23b8603680a5e3481327","0xb0af0bfa83f0922e6cbfd2bc8ec19ff0f692fcb87c4e35f30e1353b342ae2fdaea6056bc2759970fc2a1f561826f564e","0x8bb9e1693eab1496d7583bf22fb1f2a475934c63b4d94118940617aa187bc277f738223e0ec1ce8a5566035d9bcc5470","0xb242e56475dca34fe92de09daee3951d647c04ed7a483a5c5c5613676f5ca88d54ec64d1aee81fb0f085aa67c88ee6db","0x8be11e9ead2e1bb5be7e2ec066ff83589558a5d9373666b3fc518a6a6639b3baecb87f8f34895f63e8d09d270d93ce04","0xafe6eface52fb6de91055a81abf9aa6e42ce2ef36fd8ae0d09aec6e5d8bd40a065dfccda6104af94df3f7a5854559ef4","0x87bbd5574c17dbf80463d11f812a77306f67913c510b1b234f5bd80478c7da8e69476cd6711cd1f4c0e228a4e2e99636","0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b","0x83802cd575a3cea7e3e38fc1a73d94a9e4fdb999b8494e7929309c009d79a23edb1ba091ac02588f130e0585fb106540","0x8ce551755078927147bae52f683f962ca09cd68e2a14dc7444f98739fe5d27e3596314d78deedc87beb705bcf9532182","0x963528adb5322c2e2c54dc296ffddd2861bb103cbf64646781dfa8a3c2d8a8eda7079d2b3e95600028c44365afbf8879","0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e","0x96746aaba64dc87835ba709332f4d5d7837ada092b439c49d251aecf92aab5dc132e917bf6f59799bc093f976a7bc021","0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c","0xb41a0d9f8f19be13395aa09711b492d20eaf4a56d2360cd6daa2fd665532d852cb9224a5a39e5abff389882f961f12a6","0x96ef954b331a534199f4f113d993a50ec7a781fc5aa2a181ea0bdbfd4c5c557abfebfcc02604d5aef52ba64afbe0ff18","0xaecc56f2b1c4011d450214d3e1254479d583a6a5c2c06fbc049512731f76227d140df9f36a3f76b4ccb4df1342403573","0x9893413c00283a3f9ed9fd9845dda1cea38228d22567f9541dccc357e54a2d6a6e204103c92564cbc05f4905ac7c493a","0xb08f7feb86786c37661afb9951a959c9b465fd11ca98fcbc908fcf49144084051f6c363e2eb4459da2c2d03d84175692","0xad01d0f23cb74fcc4c39a2d0827d22f4722f02076196350dff5dcc6be765009c66e29001001959d77b277c2f0fba0425","0xa1c76af1545d7901214bb6be06be5d9e458f8e989c19373a920f0018327c83982f6a2ac138260b8def732cb366411ddc","0xb300303a03b8eff26a25449169d1946b208d5240f011ca6f5db23cd7f2c004b63f60afe3c9e047b67f9e4c8970c71cf0","0xae4d49364e4a36760cc74a675500055b9aed99bc19d31abb953ea156bb5a76dcf36769d15341b850114a30ffc8057780","0x8d58f7e2e58471b46d20a66a61f4cde3c78ab6c0505517c615e08d8ef5adf59b65fa2b01ea2395c84584a6f10d6cee2f","0xaa241b2afbb33f92a5d281aec9c8bac8997c1dddc051455fc0f334de48320f160b5029b552495aed21ed9ce252aab499","0x8a298ee1ac0466ecaa04d5798048c6e192409af63217f32fd7e07794cfcdcd8deca055b9782dd1ad45a578a9ec10606c","0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e"],"aggregate_pubkey":"0xb99ecc9ee5b6954058d69d0f4bb18e19b5732057133229919233dcfb5626efbc68b2d4fc6663063dfc762479520312bd"}"#,
8888
).unwrap();
8989

lib/beacon-api-types/src/altair.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub use crate::altair::{
2+
light_client_bootstrap::LightClientBootstrap,
3+
light_client_finality_update::LightClientFinalityUpdate,
4+
light_client_header::LightClientHeader, light_client_update::LightClientUpdate,
5+
sync_aggregate::SyncAggregate, sync_committee::SyncCommittee,
6+
};
7+
#[cfg(feature = "ssz")]
8+
pub use crate::altair::{sync_aggregate::SyncAggregateSsz, sync_committee::SyncCommitteeSsz};
9+
10+
/// <https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientbootstrap>
11+
pub mod light_client_bootstrap;
12+
13+
/// <https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientfinalityupdate>
14+
pub mod light_client_finality_update;
15+
16+
/// <https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientheader>
17+
pub mod light_client_header;
18+
19+
/// <https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientupdate>
20+
pub mod light_client_update;
21+
22+
/// <https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/beacon-chain.md#syncaggregate>
23+
pub mod sync_aggregate;
24+
25+
/// <https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/beacon-chain.md#synccommittee>
26+
pub mod sync_committee;

lib/beacon-api-types/src/light_client_bootstrap.rs renamed to lib/beacon-api-types/src/altair/light_client_bootstrap.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use unionlabs::primitives::H256;
22

33
use crate::{
4-
consts::{floorlog2, CURRENT_SYNC_COMMITTEE_INDEX},
5-
light_client_header::LightClientHeader,
6-
sync_committee::SyncCommittee,
4+
altair::{LightClientHeader, SyncCommittee},
5+
consts::{floorlog2, CURRENT_SYNC_COMMITTEE_GINDEX},
76
};
87

98
#[derive(Debug, Clone, PartialEq)]
@@ -12,5 +11,5 @@ pub struct LightClientBootstrap {
1211
pub header: LightClientHeader,
1312
/// Current sync committee corresponding to `beacon_header.state_root`
1413
pub current_sync_committee: SyncCommittee,
15-
pub current_sync_committee_branch: [H256; floorlog2(CURRENT_SYNC_COMMITTEE_INDEX)],
14+
pub current_sync_committee_branch: [H256; floorlog2(CURRENT_SYNC_COMMITTEE_GINDEX)],
1615
}

lib/beacon-api-types/src/light_client_finality_update.rs renamed to lib/beacon-api-types/src/altair/light_client_finality_update.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use unionlabs::primitives::H256;
22

33
use crate::{
4-
consts::{floorlog2, FINALIZED_ROOT_INDEX},
5-
light_client_header::LightClientHeader,
6-
Slot, SyncAggregate,
4+
altair::{LightClientHeader, SyncAggregate},
5+
consts::{floorlog2, FINALIZED_ROOT_GINDEX},
6+
slot::Slot,
77
};
88

99
#[derive(Debug, Clone, PartialEq)]
@@ -13,7 +13,7 @@ pub struct LightClientFinalityUpdate {
1313
pub attested_header: LightClientHeader,
1414
/// Finalized header corresponding to `attested_header.state_root`
1515
pub finalized_header: LightClientHeader,
16-
pub finality_branch: [H256; floorlog2(FINALIZED_ROOT_INDEX)],
16+
pub finality_branch: [H256; floorlog2(FINALIZED_ROOT_GINDEX)],
1717
/// Sync committee aggregate signature
1818
pub sync_aggregate: SyncAggregate,
1919
/// Slot at which the aggregate signature was created (untrusted)

0 commit comments

Comments
 (0)