Skip to content
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

refactor(light-client): Replace ssz-rs with ethereum_ssz in Light Client #1588

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 9 additions & 47 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion light-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ 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
ethereum_ssz.workspace = true

[lib]
name = "light_client"
Expand Down
9 changes: 5 additions & 4 deletions light-client/src/consensus/consensus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy::primitives::B256;
use anyhow::{anyhow, ensure, Result};
use chrono::Duration;
use milagro_bls::PublicKey;
use ssz_rs::prelude::*;
use ssz::TryFromIter;
use tracing::{debug, info, warn};

use super::{rpc::ConsensusRpc, types::*, utils::*};
Expand Down Expand Up @@ -568,10 +568,11 @@ fn compute_committee_sign_root(
genesis_root: &[u8],
header: Bytes32,
fork_version: &[u8],
) -> Result<Node> {
) -> Result<B256> {
let genesis_root = genesis_root.to_vec().try_into()?;
let domain_type = &hex::decode("07000000")?[..];
let fork_version = Vector::from_iter(fork_version.to_vec());
let fork_version = FixedVector::try_from_iter(fork_version.to_vec())
.expect("should convert fork version to fixed vector");
let domain = compute_domain(domain_type, fork_version, genesis_root)?;
compute_signing_root(header, domain)
}
Expand Down Expand Up @@ -617,7 +618,7 @@ fn verify_sync_committee_signature(

Ok(is_aggregate_valid(
signature,
signing_root.r#as_bytes(),
signing_root.as_slice(),
&public_keys,
))
})();
Expand Down
33 changes: 18 additions & 15 deletions light-client/src/consensus/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{types::Bytes32, utils::bytes32_to_node};
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 serde::Serialize;
use ssz::*;
use ssz_types::{typenum, FixedVector};
use tree_hash::TreeHash;

pub fn calc_sync_period(slot: u64) -> u64 {
Expand All @@ -26,7 +29,7 @@ pub fn is_proof_valid<L: TreeHash>(
index: usize,
) -> bool {
let res: Result<bool> = (move || {
let leaf_hash = Node::from_bytes(<[u8; 32]>::from(leaf_object.tree_hash_root()));
let leaf_hash = B256::from_ssz_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"),
Expand All @@ -40,19 +43,19 @@ pub fn is_proof_valid<L: TreeHash>(
res.unwrap_or_default()
}

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

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

pub fn compute_signing_root(object_root: Bytes32, domain: Bytes32) -> Result<Node> {
pub fn compute_signing_root(object_root: Bytes32, domain: Bytes32) -> Result<B256> {
let mut data = SigningData {
object_root,
domain,
Expand All @@ -62,30 +65,30 @@ pub fn compute_signing_root(object_root: Bytes32, domain: Bytes32) -> Result<Nod

pub fn compute_domain(
domain_type: &[u8],
fork_version: Vector<u8, 4>,
fork_version: FixedVector<u8, typenum::U4>,
genesis_root: Bytes32,
) -> Result<Bytes32> {
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()?)
}

fn compute_fork_data_root(
current_version: Vector<u8, 4>,
pub fn compute_fork_data_root(
current_version: FixedVector<u8, typenum::U4>,
genesis_validator_root: Bytes32,
) -> Result<Node> {
) -> Result<B256> {
let mut 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>> {
pub fn branch_to_nodes(branch: Vec<Bytes32>) -> Result<Vec<B256>> {
branch
.iter()
.map(bytes32_to_node)
.collect::<Result<Vec<Node>>>()
.collect::<Result<Vec<B256>>>()
}
4 changes: 2 additions & 2 deletions light-client/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use ssz_rs::Vector;
use ssz_types::{typenum::U32, FixedVector};

pub type Bytes32 = Vector<u8, 32>;
pub type Bytes32 = FixedVector<u8, U32>;
12 changes: 8 additions & 4 deletions light-client/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::types::Bytes32;
use alloy::primitives::B256;
use anyhow::Result;
use ssz_rs::{Node, Vector};
use ssz::{Decode, TryFromIter};
use ssz_types::FixedVector;

pub fn bytes_to_bytes32(bytes: &[u8]) -> Bytes32 {
Vector::from_iter(bytes.to_vec())
FixedVector::try_from_iter(bytes.to_vec()).expect("should convert bytes to fixed vector")
}

pub fn bytes32_to_node(bytes: &Bytes32) -> Result<Node> {
Ok(Node::from_bytes(bytes.as_slice().try_into()?))
pub fn bytes32_to_node(bytes: &Bytes32) -> Result<B256> {
let array = <[u8; 32]>::try_from(bytes.as_ref())
.map_err(|_| anyhow::anyhow!("Failed to convert bytes to array"))?;
B256::from_ssz_bytes(&array).map_err(|_| anyhow::anyhow!("Failed to decode SSZ bytes"))
}

pub fn u64_to_hex_string(val: u64) -> String {
Expand Down
Loading