Skip to content

chore: remove ssz_rs dependency #1671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 12 additions & 48 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion crates/light-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ alloy.workspace = true
anyhow.workspace = true
async-trait.workspace = true
chrono.workspace = true
ethereum_ssz.workspace = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't worked alphabetically

ethportal-api.workspace = true
figment = { version = "0.10.7", features = ["toml", "env"] }
futures.workspace = true
Expand All @@ -29,14 +30,15 @@ serde.workspace = true
serde-this-or-that.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "d09f55b4f8554491e3431e01af1c32347a8781cd" }
ssz_types.workspace = true
strum.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
tree_hash.workspace = true
tree_hash_derive.workspace = true
trin-validation.workspace = true

[lib]
name = "light_client"
Expand Down
35 changes: 10 additions & 25 deletions crates/light-client/src/consensus/consensus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use ethportal_api::{
utils::bytes::hex_encode,
};
use milagro_bls::PublicKey;
use ssz_rs::prelude::*;
use ssz_types::{typenum, BitVector, FixedVector};
use tracing::{debug, info, warn};
use tree_hash::TreeHash;
Expand All @@ -30,8 +29,6 @@ use crate::{
consensus::{
constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES, rpc::portal_rpc::expected_current_slot,
},
types::Bytes32,
utils::bytes_to_bytes32,
};

// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md
Expand Down Expand Up @@ -581,14 +578,14 @@ pub fn verify_generic_update(

fn compute_committee_sign_root(
genesis_root: &[u8],
header: Bytes32,
header: B256,
fork_version: &[u8],
) -> Result<Node> {
let genesis_root = genesis_root.to_vec().try_into()?;
) -> Result<B256> {
let genesis_root = B256::from_slice(genesis_root);
let domain_type = &hex::decode("07000000")?[..];
let fork_version = Vector::from_iter(fork_version.to_vec());
let fork_version = FixedVector::from(fork_version.to_vec());
let domain = compute_domain(domain_type, fork_version, genesis_root)?;
compute_signing_root(header, domain)
Ok(compute_signing_root(header, domain))
}

fn get_participating_keys(
Expand Down Expand Up @@ -627,12 +624,12 @@ fn verify_sync_committee_signature(
) -> bool {
let res: Result<bool> = (move || {
let public_keys: Vec<&PublicKey> = pks.iter().collect();
let header_root = bytes_to_bytes32(attested_header.tree_hash_root().as_slice());
let header_root = attested_header.tree_hash_root();
let signing_root = compute_committee_sign_root(genesis_root, header_root, fork_version)?;

Ok(is_aggregate_valid(
signature,
signing_root.r#as_bytes(),
signing_root.as_slice(),
&public_keys,
))
})();
Expand All @@ -645,26 +642,18 @@ fn is_finality_proof_valid(
finality_header: &mut BeaconBlockHeader,
finality_branch: &FixedVector<B256, FinalizedRootProofLen>,
) -> bool {
let finality_branch = finality_branch
.iter()
.map(|h| bytes_to_bytes32(h.as_slice()))
.collect::<Vec<_>>();
is_proof_valid(attested_header, finality_header, &finality_branch, 6, 41)
is_proof_valid(attested_header, finality_header, finality_branch, 6, 41)
}

fn is_next_committee_proof_valid(
attested_header: &BeaconBlockHeader,
next_committee: &mut SyncCommittee,
next_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLen>,
) -> bool {
let next_committee_branch = next_committee_branch
.iter()
.map(|h| bytes_to_bytes32(h.as_slice()))
.collect::<Vec<_>>();
is_proof_valid(
attested_header,
next_committee,
&next_committee_branch,
next_committee_branch,
5,
23,
)
Expand All @@ -675,14 +664,10 @@ fn is_current_committee_proof_valid(
current_committee: &mut SyncCommittee,
current_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLen>,
) -> bool {
let current_committee_branch = current_committee_branch
.iter()
.map(|h| bytes_to_bytes32(h.as_slice()))
.collect::<Vec<_>>();
is_proof_valid(
attested_header,
current_committee,
&current_committee_branch,
current_committee_branch,
5,
22,
)
Expand Down
73 changes: 30 additions & 43 deletions crates/light-client/src/consensus/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use alloy::primitives::B256;
use anyhow::Result;
use ethportal_api::consensus::{header::BeaconBlockHeader, signature::BlsSignature};
use milagro_bls::{AggregateSignature, PublicKey};
use ssz_rs::prelude::*;
use ssz_types::{typenum::U4, FixedVector};
use tree_hash::TreeHash;

use crate::{types::Bytes32, utils::bytes32_to_node};
use tree_hash_derive::TreeHash;
use trin_validation::merkle::proof::merkle_root_from_branch;

pub fn calc_sync_period(slot: u64) -> u64 {
let epoch = slot / 32; // 32 slots per epoch
Expand All @@ -21,72 +22,58 @@ pub fn is_aggregate_valid(sig_bytes: &BlsSignature, msg: &[u8], pks: &[&PublicKe

pub fn is_proof_valid<L: TreeHash>(
attested_header: &BeaconBlockHeader,
leaf_object: &mut L,
branch: &[Bytes32],
leaf_object: &L,
branch: &[B256],
depth: usize,
index: usize,
) -> bool {
let res: Result<bool> = (move || {
let leaf_hash = Node::from_bytes(<[u8; 32]>::from(leaf_object.tree_hash_root()));
let state_root = bytes32_to_node(
&Bytes32::try_from(attested_header.state_root.0.to_vec())
.expect("Unable to convert state root to bytes"),
)?;
let branch = branch_to_nodes(branch.to_vec())?;
let leaf_hash = leaf_object.tree_hash_root();
let state_root = attested_header.state_root;

let is_valid = is_valid_merkle_branch(&leaf_hash, branch.iter(), depth, index, &state_root);
Ok(is_valid)
})();
let root = merkle_root_from_branch(leaf_hash, branch, depth, index);

res.unwrap_or_default()
root == state_root
}

#[derive(SimpleSerialize, Default, Debug)]
#[derive(Default, Debug, TreeHash)]
struct SigningData {
object_root: Bytes32,
domain: Bytes32,
object_root: B256,
domain: B256,
}

#[derive(SimpleSerialize, Default, Debug)]
#[derive(Default, Debug, TreeHash)]
struct ForkData {
current_version: Vector<u8, 4>,
genesis_validator_root: Bytes32,
current_version: FixedVector<u8, U4>,
genesis_validator_root: B256,
}

pub fn compute_signing_root(object_root: Bytes32, domain: Bytes32) -> Result<Node> {
let mut data = SigningData {
pub fn compute_signing_root(object_root: B256, domain: B256) -> B256 {
let data = SigningData {
object_root,
domain,
};
Ok(data.hash_tree_root()?)
data.tree_hash_root()
}

pub fn compute_domain(
domain_type: &[u8],
fork_version: Vector<u8, 4>,
genesis_root: Bytes32,
) -> Result<Bytes32> {
let fork_data_root = compute_fork_data_root(fork_version, genesis_root)?;
fork_version: FixedVector<u8, U4>,
genesis_root: B256,
) -> Result<B256> {
let fork_data_root = compute_fork_data_root(fork_version, genesis_root);
let start = domain_type;
let end = &fork_data_root.as_bytes()[..28];
let end = &fork_data_root.as_slice()[..28];
let d = [start, end].concat();
Ok(d.to_vec().try_into()?)
Ok(B256::from_slice(&d))
}

fn compute_fork_data_root(
current_version: Vector<u8, 4>,
genesis_validator_root: Bytes32,
) -> Result<Node> {
let mut fork_data = ForkData {
current_version: FixedVector<u8, U4>,
genesis_validator_root: B256,
) -> B256 {
let fork_data = ForkData {
current_version,
genesis_validator_root,
};
Ok(fork_data.hash_tree_root()?)
}

pub fn branch_to_nodes(branch: Vec<Bytes32>) -> Result<Vec<Node>> {
branch
.iter()
.map(bytes32_to_node)
.collect::<Result<Vec<Node>>>()
fork_data.tree_hash_root()
}
1 change: 0 additions & 1 deletion crates/light-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ pub mod database;
pub mod errors;
pub mod node;
pub mod rpc;
pub mod types;
pub mod utils;
Loading
Loading