Skip to content

Commit

Permalink
refactor: remove ethereum-utils and use ethereum sub-folder (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjermundgaraba authored Dec 19, 2024
1 parent 8d1f655 commit 3b81a89
Show file tree
Hide file tree
Showing 53 changed files with 61 additions and 72 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
[workspace]
members = [
"packages/*",
"packages/ethereum/ethereum-light-client",
"packages/ethereum/ethereum-trie-db",
"packages/ethereum/ethereum-types",
"packages/ethereum/tree_hash",
"packages/solidity",
"packages/relayer-lib",
"packages/sp1-ics07-tendermint-prover",
"packages/sp1-ics07-tendermint-utils",

"programs/relayer",
"programs/operator",
Expand All @@ -23,11 +30,10 @@ sp1-ics07-tendermint-prover = { path = "packages/sp1-ics07-tendermint-pro
sp1-ics07-tendermint-utils = { path = "packages/sp1-ics07-tendermint-utils", default-features = false }
sp1-ics07-tendermint-update-client = { path = "programs/sp1-programs/update-client", default-features = false }
sp1-ics07-tendermint-membership = { path = "programs/sp1-programs/membership", default-features = false }
ethereum-trie-db = { path = "packages/ethereum-trie-db", default-features = false }
ethereum-light-client = { path = "packages/ethereum-light-client", default-features = false }
ethereum-types = { path = "packages/ethereum-types", default-features = false }
ethereum-utils = { path = "packages/ethereum-utils", default-features = false }
tree_hash = { path = "packages/tree_hash", default-features = false }
ethereum-trie-db = { path = "packages/ethereum/ethereum-trie-db", default-features = false }
ethereum-light-client = { path = "packages/ethereum/ethereum-light-client", default-features = false }
ethereum-types = { path = "packages/ethereum/ethereum-types", default-features = false }
tree_hash = { path = "packages/ethereum/tree_hash", default-features = false }

serde = { version = "1.0", default-features = false }
serde_json = { version = "1.0", default-features = false }
Expand Down
7 changes: 0 additions & 7 deletions packages/ethereum-utils/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions packages/ethereum-utils/README.md

This file was deleted.

20 changes: 0 additions & 20 deletions packages/ethereum-utils/src/lib.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ path = "src/bin/generate_json_schema.rs"

[dependencies]
ethereum-trie-db = { workspace = true }
ethereum-utils = { workspace = true }
ethereum-types = { workspace = true }

alloy-primitives = { workspace = true, features = ["serde", "hex-compat"] }
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum EthereumIBCError {
VerifyStorageProof(String),

#[error("insufficient number of sync committee participants ({0})")]
InsufficientSyncCommitteeParticipants(usize),
InsufficientSyncCommitteeParticipants(u64),

#[error("update header contains deneb specific information")]
MustBeDeneb,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
unused_crate_dependencies
)]

/// Ensure that a condition is true, otherwise return an error.
/// This macro is used for precondition checks in the light client logic for readability.
macro_rules! ensure {
($cond:expr, $err:expr) => {
if !$cond {
return Err($err);
}
};
}

pub mod client_state;
pub mod consensus_state;
pub mod error;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use ethereum_types::consensus::{
slot::compute_timestamp_at_slot, sync_committee::compute_sync_committee_period_at_slot,
};
use ethereum_utils::ensure;

use crate::{
client_state::ClientState, consensus_state::ConsensusState, error::EthereumIBCError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use ethereum_types::consensus::{
slot::{compute_epoch_at_slot, compute_slot_at_timestamp, GENESIS_SLOT},
sync_committee::compute_sync_committee_period_at_slot,
};
use ethereum_utils::ensure;
use tree_hash::TreeHash;

use crate::{
Expand Down Expand Up @@ -133,11 +132,9 @@ pub fn validate_light_client_update<V: BlsVerify>(
) -> Result<(), EthereumIBCError> {
// Verify sync committee has sufficient participants
ensure!(
update.sync_aggregate.num_sync_committe_participants()
>= client_state
.min_sync_committee_participants
.try_into()
.unwrap(),
update
.sync_aggregate
.has_sufficient_participants(client_state.min_sync_committee_participants),
EthereumIBCError::InsufficientSyncCommitteeParticipants(
update.sync_aggregate.num_sync_committe_participants(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ edition = { workspace = true }
repository = { workspace = true }

[dependencies]
ethereum-utils = { workspace = true }

hex = { workspace = true }
trie-db = { workspace = true, features = ["std"] }
hash-db = { workspace = true }
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Defines the account trie and the account type.
use alloy_primitives::{Address, B256};
use ethereum_utils::ensure;
use hash_db::HashDB;
use memory_db::{HashKey, MemoryDB};
use primitive_types::{H160, H256, U256};
Expand Down Expand Up @@ -49,13 +48,12 @@ pub fn verify_account_storage_root(
Some(account) => {
let account =
rlp::decode::<Account>(account.as_ref()).map_err(TrieDBError::RlpDecode)?;
ensure!(
account.storage_root == storage_root,
TrieDBError::ValueMismatch {
if account.storage_root != storage_root {
return Err(TrieDBError::ValueMismatch {
expected: storage_root.as_ref().into(),
actual: account.storage_root.as_ref().into(),
}
);
});
}
Ok(())
}
None => Err(TrieDBError::ValueMissing {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ pub struct SyncAggregate {
impl SyncAggregate {
/// Returns the number of bits that are set to `true`.
#[must_use]
pub fn num_sync_committe_participants(&self) -> usize {
pub fn num_sync_committe_participants(&self) -> u64 {
self.sync_committee_bits
.iter()
.map(|byte| byte.count_ones())
.sum::<u32>() as usize
.map(|byte| u64::from(byte.count_ones()))
.sum()
}

/// Returns the size of the sync committee.
pub fn sync_committee_size(&self) -> usize {
self.sync_committee_bits.len() * 8
pub fn sync_committee_size(&self) -> u64 {
self.sync_committee_bits.len() as u64 * 8
}

/// Returns if at least 2/3 of the sync committee signed
Expand All @@ -53,6 +53,11 @@ impl SyncAggregate {
pub fn validate_signature_supermajority(&self) -> bool {
self.num_sync_committe_participants() * 3 >= self.sync_committee_size() * 2
}

/// Returns if the sync committee has sufficient participants
pub fn has_sufficient_participants(&self, min_sync_committee_participants: u64) -> bool {
self.num_sync_committe_participants() >= min_sync_committee_participants
}
}

/// Returns the sync committee period at a given `epoch`.
Expand Down Expand Up @@ -180,4 +185,21 @@ mod test {
assert_eq!(sync_aggregate.sync_committee_size(), 32);
assert!(sync_aggregate.validate_signature_supermajority());
}

#[test]
fn test_has_sufficient_participants() {
let sync_aggregate = SyncAggregate {
sync_committee_bits: vec![0b00000001].into(),
sync_committee_signature: BlsSignature::default(),
};
assert!(sync_aggregate.has_sufficient_participants(1));
assert!(!sync_aggregate.has_sufficient_participants(2));

let sync_aggregate = SyncAggregate {
sync_committee_bits: vec![0b11111111, 0b11111111, 0b11111111, 0b11111111].into(),
sync_committee_signature: BlsSignature::default(),
};
assert!(sync_aggregate.has_sufficient_participants(32));
assert!(!sync_aggregate.has_sufficient_participants(33));
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion programs/cw-ics08-wasm-eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ crate-type = ["cdylib", "rlib"]
ibc-proto = { workspace = true }
ethereum-light-client = { workspace = true }
ethereum-types = { workspace = true }
ethereum-utils = { workspace = true }

alloy-primitives = { workspace = true, default-features = false }

Expand Down
10 changes: 4 additions & 6 deletions programs/cw-ics08-wasm-eth/src/custom_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use alloy_primitives::B256;
use cosmwasm_std::{Binary, CustomQuery, QuerierWrapper, QueryRequest};
use ethereum_light_client::verify::BlsVerify;
use ethereum_types::consensus::bls::{BlsPublicKey, BlsSignature};
use ethereum_utils::ensure;
use thiserror::Error;

/// The custom query for the Ethereum light client
Expand Down Expand Up @@ -81,14 +80,13 @@ impl BlsVerify for BlsVerifier<'_> {
.query(&request)
.map_err(|e| BlsVerifierError::FastAggregateVerify(e.to_string()))?;

ensure!(
is_valid,
BlsVerifierError::InvalidSignature {
if !is_valid {
return Err(BlsVerifierError::InvalidSignature {
public_keys: public_keys.to_vec(),
msg,
signature,
}
);
});
}

Ok(())
}
Expand Down

0 comments on commit 3b81a89

Please sign in to comment.