Skip to content

Commit bea5b2f

Browse files
authored
chore: remove ssz_rs dependency (#1671)
1 parent 19556c1 commit bea5b2f

File tree

8 files changed

+56
-135
lines changed

8 files changed

+56
-135
lines changed

Cargo.lock

+12-48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/light-client/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ alloy.workspace = true
1616
anyhow.workspace = true
1717
async-trait.workspace = true
1818
chrono.workspace = true
19+
ethereum_ssz.workspace = true
1920
ethportal-api.workspace = true
2021
figment = { version = "0.10.7", features = ["toml", "env"] }
2122
futures.workspace = true
@@ -29,14 +30,15 @@ serde.workspace = true
2930
serde-this-or-that.workspace = true
3031
serde_json.workspace = true
3132
serde_yaml.workspace = true
32-
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "d09f55b4f8554491e3431e01af1c32347a8781cd" }
3333
ssz_types.workspace = true
3434
strum.workspace = true
3535
thiserror.workspace = true
3636
tokio.workspace = true
3737
tracing.workspace = true
3838
tracing-subscriber.workspace = true
3939
tree_hash.workspace = true
40+
tree_hash_derive.workspace = true
41+
trin-validation.workspace = true
4042

4143
[lib]
4244
name = "light_client"

crates/light-client/src/consensus/consensus_client.rs

+10-25
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use ethportal_api::{
1919
utils::bytes::hex_encode,
2020
};
2121
use milagro_bls::PublicKey;
22-
use ssz_rs::prelude::*;
2322
use ssz_types::{typenum, BitVector, FixedVector};
2423
use tracing::{debug, info, warn};
2524
use tree_hash::TreeHash;
@@ -30,8 +29,6 @@ use crate::{
3029
consensus::{
3130
constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES, rpc::portal_rpc::expected_current_slot,
3231
},
33-
types::Bytes32,
34-
utils::bytes_to_bytes32,
3532
};
3633

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

582579
fn compute_committee_sign_root(
583580
genesis_root: &[u8],
584-
header: Bytes32,
581+
header: B256,
585582
fork_version: &[u8],
586-
) -> Result<Node> {
587-
let genesis_root = genesis_root.to_vec().try_into()?;
583+
) -> Result<B256> {
584+
let genesis_root = B256::from_slice(genesis_root);
588585
let domain_type = &hex::decode("07000000")?[..];
589-
let fork_version = Vector::from_iter(fork_version.to_vec());
586+
let fork_version = FixedVector::from(fork_version.to_vec());
590587
let domain = compute_domain(domain_type, fork_version, genesis_root)?;
591-
compute_signing_root(header, domain)
588+
Ok(compute_signing_root(header, domain))
592589
}
593590

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

633630
Ok(is_aggregate_valid(
634631
signature,
635-
signing_root.r#as_bytes(),
632+
signing_root.as_slice(),
636633
&public_keys,
637634
))
638635
})();
@@ -645,26 +642,18 @@ fn is_finality_proof_valid(
645642
finality_header: &mut BeaconBlockHeader,
646643
finality_branch: &FixedVector<B256, FinalizedRootProofLen>,
647644
) -> bool {
648-
let finality_branch = finality_branch
649-
.iter()
650-
.map(|h| bytes_to_bytes32(h.as_slice()))
651-
.collect::<Vec<_>>();
652-
is_proof_valid(attested_header, finality_header, &finality_branch, 6, 41)
645+
is_proof_valid(attested_header, finality_header, finality_branch, 6, 41)
653646
}
654647

655648
fn is_next_committee_proof_valid(
656649
attested_header: &BeaconBlockHeader,
657650
next_committee: &mut SyncCommittee,
658651
next_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLen>,
659652
) -> bool {
660-
let next_committee_branch = next_committee_branch
661-
.iter()
662-
.map(|h| bytes_to_bytes32(h.as_slice()))
663-
.collect::<Vec<_>>();
664653
is_proof_valid(
665654
attested_header,
666655
next_committee,
667-
&next_committee_branch,
656+
next_committee_branch,
668657
5,
669658
23,
670659
)
@@ -675,14 +664,10 @@ fn is_current_committee_proof_valid(
675664
current_committee: &mut SyncCommittee,
676665
current_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLen>,
677666
) -> bool {
678-
let current_committee_branch = current_committee_branch
679-
.iter()
680-
.map(|h| bytes_to_bytes32(h.as_slice()))
681-
.collect::<Vec<_>>();
682667
is_proof_valid(
683668
attested_header,
684669
current_committee,
685-
&current_committee_branch,
670+
current_committee_branch,
686671
5,
687672
22,
688673
)
+30-43
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use alloy::primitives::B256;
12
use anyhow::Result;
23
use ethportal_api::consensus::{header::BeaconBlockHeader, signature::BlsSignature};
34
use milagro_bls::{AggregateSignature, PublicKey};
4-
use ssz_rs::prelude::*;
5+
use ssz_types::{typenum::U4, FixedVector};
56
use tree_hash::TreeHash;
6-
7-
use crate::{types::Bytes32, utils::bytes32_to_node};
7+
use tree_hash_derive::TreeHash;
8+
use trin_validation::merkle::proof::merkle_root_from_branch;
89

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

2223
pub fn is_proof_valid<L: TreeHash>(
2324
attested_header: &BeaconBlockHeader,
24-
leaf_object: &mut L,
25-
branch: &[Bytes32],
25+
leaf_object: &L,
26+
branch: &[B256],
2627
depth: usize,
2728
index: usize,
2829
) -> bool {
29-
let res: Result<bool> = (move || {
30-
let leaf_hash = Node::from_bytes(<[u8; 32]>::from(leaf_object.tree_hash_root()));
31-
let state_root = bytes32_to_node(
32-
&Bytes32::try_from(attested_header.state_root.0.to_vec())
33-
.expect("Unable to convert state root to bytes"),
34-
)?;
35-
let branch = branch_to_nodes(branch.to_vec())?;
30+
let leaf_hash = leaf_object.tree_hash_root();
31+
let state_root = attested_header.state_root;
3632

37-
let is_valid = is_valid_merkle_branch(&leaf_hash, branch.iter(), depth, index, &state_root);
38-
Ok(is_valid)
39-
})();
33+
let root = merkle_root_from_branch(leaf_hash, branch, depth, index);
4034

41-
res.unwrap_or_default()
35+
root == state_root
4236
}
4337

44-
#[derive(SimpleSerialize, Default, Debug)]
38+
#[derive(Default, Debug, TreeHash)]
4539
struct SigningData {
46-
object_root: Bytes32,
47-
domain: Bytes32,
40+
object_root: B256,
41+
domain: B256,
4842
}
4943

50-
#[derive(SimpleSerialize, Default, Debug)]
44+
#[derive(Default, Debug, TreeHash)]
5145
struct ForkData {
52-
current_version: Vector<u8, 4>,
53-
genesis_validator_root: Bytes32,
46+
current_version: FixedVector<u8, U4>,
47+
genesis_validator_root: B256,
5448
}
5549

56-
pub fn compute_signing_root(object_root: Bytes32, domain: Bytes32) -> Result<Node> {
57-
let mut data = SigningData {
50+
pub fn compute_signing_root(object_root: B256, domain: B256) -> B256 {
51+
let data = SigningData {
5852
object_root,
5953
domain,
6054
};
61-
Ok(data.hash_tree_root()?)
55+
data.tree_hash_root()
6256
}
6357

6458
pub fn compute_domain(
6559
domain_type: &[u8],
66-
fork_version: Vector<u8, 4>,
67-
genesis_root: Bytes32,
68-
) -> Result<Bytes32> {
69-
let fork_data_root = compute_fork_data_root(fork_version, genesis_root)?;
60+
fork_version: FixedVector<u8, U4>,
61+
genesis_root: B256,
62+
) -> Result<B256> {
63+
let fork_data_root = compute_fork_data_root(fork_version, genesis_root);
7064
let start = domain_type;
71-
let end = &fork_data_root.as_bytes()[..28];
65+
let end = &fork_data_root.as_slice()[..28];
7266
let d = [start, end].concat();
73-
Ok(d.to_vec().try_into()?)
67+
Ok(B256::from_slice(&d))
7468
}
7569

7670
fn compute_fork_data_root(
77-
current_version: Vector<u8, 4>,
78-
genesis_validator_root: Bytes32,
79-
) -> Result<Node> {
80-
let mut fork_data = ForkData {
71+
current_version: FixedVector<u8, U4>,
72+
genesis_validator_root: B256,
73+
) -> B256 {
74+
let fork_data = ForkData {
8175
current_version,
8276
genesis_validator_root,
8377
};
84-
Ok(fork_data.hash_tree_root()?)
85-
}
86-
87-
pub fn branch_to_nodes(branch: Vec<Bytes32>) -> Result<Vec<Node>> {
88-
branch
89-
.iter()
90-
.map(bytes32_to_node)
91-
.collect::<Result<Vec<Node>>>()
78+
fork_data.tree_hash_root()
9279
}

crates/light-client/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ pub mod database;
99
pub mod errors;
1010
pub mod node;
1111
pub mod rpc;
12-
pub mod types;
1312
pub mod utils;

0 commit comments

Comments
 (0)