Skip to content

Commit a2989a9

Browse files
authored
refactor(ssz): move ssz to standalone crate (#289)
* refactor(ssz): move ssz to standalone crate * fix: use directly pluto_ssz
1 parent b671b43 commit a2989a9

40 files changed

Lines changed: 735 additions & 812 deletions

Cargo.lock

Lines changed: 19 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ members = [
1313
"crates/k1util",
1414
"crates/relay-server",
1515
"crates/p2p",
16+
"crates/ssz",
1617
"crates/testutil",
1718
"crates/tracing",
1819
"crates/peerinfo",
@@ -106,6 +107,7 @@ pluto-eth2util = { path = "crates/eth2util" }
106107
pluto-eth1wrap = { path = "crates/eth1wrap" }
107108
pluto-k1util = { path = "crates/k1util" }
108109
pluto-relay-server = { path = "crates/relay-server" }
110+
pluto-ssz = { path = "crates/ssz" }
109111
pluto-testutil = { path = "crates/testutil" }
110112
pluto-tracing = { path = "crates/tracing" }
111113
pluto-p2p = { path = "crates/p2p" }

crates/app/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tempfile.workspace = true
3434
pluto-cluster.workspace = true
3535
pluto-k1util.workspace = true
3636
pluto-crypto.workspace = true
37+
pluto-ssz.workspace = true
3738

3839
[build-dependencies]
3940
pluto-build-proto.workspace = true

crates/app/src/obolapi/error.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub enum Error {
4545

4646
/// SSZ hashing error from [`pluto_cluster`].
4747
#[error("SSZ hashing error: {0}")]
48-
Ssz(#[from] pluto_cluster::ssz::SSZError<pluto_cluster::ssz_hasher::Hasher>),
48+
Ssz(#[from] pluto_cluster::ssz::SSZError<pluto_ssz::Hasher>),
4949

5050
/// K1 signing error.
5151
#[error("K1 signing error: {0}")]
@@ -73,5 +73,11 @@ pub enum Error {
7373

7474
/// SSZ hasher error.
7575
#[error("SSZ hasher error: {0}")]
76-
HasherError(#[from] pluto_cluster::ssz_hasher::HasherError),
76+
HasherError(#[from] pluto_ssz::HasherError),
77+
}
78+
79+
impl From<pluto_ssz::Error<pluto_ssz::HasherError>> for Error {
80+
fn from(error: pluto_ssz::Error<pluto_ssz::HasherError>) -> Self {
81+
Self::Ssz(pluto_cluster::ssz::SSZError::from(error))
82+
}
7783
}

crates/app/src/obolapi/exit.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use serde::{Deserialize, Serialize};
1111
use pluto_cluster::{
1212
helpers::to_0x_hex,
1313
ssz::{SSZ_LEN_BLS_SIG, SSZ_LEN_PUB_KEY},
14-
ssz_hasher::{HashWalker, Hasher},
1514
};
1615
use pluto_eth2api::types::{
1716
GetPoolVoluntaryExitsResponseResponseDatum, Phase0SignedVoluntaryExitMessage,
1817
};
18+
use pluto_ssz::{HashWalker, Hasher, put_bytes_n};
1919

2020
use crate::obolapi::{
2121
client::Client,
@@ -26,9 +26,6 @@ use crate::obolapi::{
2626
/// Type alias for signed voluntary exit from eth2api.
2727
pub type SignedVoluntaryExit = GetPoolVoluntaryExitsResponseResponseDatum;
2828

29-
// TODO: Unify SSZ hashing across the workspace. `pluto-cluster` already has
30-
// SSZ hashing utilities. Consider extracting a shared SSZ crate (or promoting
31-
// the existing hasher) so all crates share one SSZ interface and error type.
3229
/// Trait for types that can be hashed using SSZ hash tree root.
3330
pub trait SszHashable {
3431
/// Hashes this value into the provided hasher.
@@ -48,7 +45,7 @@ impl SszHashable for SignedVoluntaryExit {
4845

4946
self.message.hash_with(hh)?;
5047
let sig_bytes = from_0x(&self.signature, SSZ_LEN_BLS_SIG)?;
51-
pluto_cluster::helpers::put_bytes_n(hh, &sig_bytes, SSZ_LEN_BLS_SIG)?;
48+
put_bytes_n(hh, &sig_bytes, SSZ_LEN_BLS_SIG)?;
5249

5350
hh.merkleize(index)?;
5451
Ok(())
@@ -236,7 +233,7 @@ impl SszHashable for FullExitAuthBlob {
236233
let index = hh.index();
237234

238235
hh.put_bytes(&self.lock_hash)?;
239-
pluto_cluster::helpers::put_bytes_n(hh, &self.validator_pubkey, SSZ_LEN_PUB_KEY)?;
236+
put_bytes_n(hh, &self.validator_pubkey, SSZ_LEN_PUB_KEY)?;
240237
hh.put_uint64(self.share_index)?;
241238

242239
hh.merkleize(index)?;

crates/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pluto-core.workspace = true
2525
pluto-p2p.workspace = true
2626
pluto-eth2util.workspace = true
2727
pluto-k1util.workspace = true
28+
pluto-ssz.workspace = true
2829
libp2p.workspace = true
2930
tokio-util.workspace = true
3031
tracing.workspace = true

crates/cli/src/commands/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ use crate::{
3232

3333
use k256::SecretKey;
3434
use pluto_app::obolapi::{Client, ClientOptions};
35-
use pluto_cluster::ssz_hasher::{HashWalker, Hasher};
3635
use pluto_eth2util::enr::Record;
3736
use pluto_k1util::{load, sign};
37+
use pluto_ssz::{HashWalker, Hasher};
3838
use reqwest::{Method, StatusCode, header::CONTENT_TYPE};
3939
use serde_with::{base64::Base64, serde_as};
4040
use std::os::unix::fs::PermissionsExt as _;

crates/cli/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub enum CliError {
7373

7474
/// SSZ hasher error.
7575
#[error("Hasher error: {0}")]
76-
HasherError(#[from] pluto_cluster::ssz_hasher::HasherError),
76+
HasherError(#[from] pluto_ssz::HasherError),
7777

7878
/// HTTP request error.
7979
#[error("HTTP request error: {0}")]

crates/cluster/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pluto-p2p.workspace = true
2525
pluto-eth2util.workspace = true
2626
pluto-eth1wrap.workspace = true
2727
pluto-k1util.workspace = true
28+
pluto-ssz.workspace = true
2829
k256.workspace = true
2930
tokio.workspace = true
3031
reqwest = { workspace = true, features = ["json"] }

crates/cluster/src/definition.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::collections::HashSet;
22

3+
use pluto_ssz::{Hasher, serde_utils::Hex0x};
4+
35
use crate::{
46
eip712sigs::{
57
EIP712Error, digest_eip712, eip712_creator_config_hash, eip712_enr,
68
get_operator_eip712_type,
79
},
8-
helpers::{EthHex, from_0x_hex_str},
10+
helpers::from_0x_hex_str,
911
operator::{Operator, OperatorV1X1, OperatorV1X2OrLater},
1012
ssz::{SSZError, hash_definition},
11-
ssz_hasher::Hasher,
1213
version::{CURRENT_VERSION, DKG_ALGO, versions::*},
1314
};
1415
use chrono::{DateTime, Timelike, Utc};
@@ -771,7 +772,7 @@ pub struct Creator {
771772
/// The Ethereum address of the creator
772773
pub address: String,
773774
/// The creator's signature over the config hash
774-
#[serde_as(as = "EthHex")]
775+
#[serde_as(as = "Hex0x")]
775776
pub config_signature: Vec<u8>,
776777
}
777778

@@ -817,7 +818,7 @@ pub struct DefinitionV1x0or1 {
817818
pub dkg_algorithm: String,
818819
/// Cluster's 4 byte beacon chain fork version
819820
/// (network/chain identifier).
820-
#[serde_as(as = "EthHex")]
821+
#[serde_as(as = "Hex0x")]
821822
pub fork_version: Vec<u8>,
822823
/// Config hash uniquely identifies a cluster definition excluding operator
823824
/// ENRs and signatures.
@@ -928,15 +929,15 @@ pub struct DefinitionV1x2or3 {
928929
pub dkg_algorithm: String,
929930
/// Cluster's 4 byte beacon chain fork version
930931
/// (network/chain identifier).
931-
#[serde_as(as = "EthHex")]
932+
#[serde_as(as = "Hex0x")]
932933
pub fork_version: Vec<u8>,
933934
/// Config hash uniquely identifies a cluster definition excluding operator
934935
/// ENRs and signatures.
935-
#[serde_as(as = "EthHex")]
936+
#[serde_as(as = "Hex0x")]
936937
pub config_hash: Vec<u8>,
937938
/// Definition hash uniquely identifies a cluster definition including
938939
/// operator ENRs and signatures.
939-
#[serde_as(as = "EthHex")]
940+
#[serde_as(as = "Hex0x")]
940941
pub definition_hash: Vec<u8>,
941942
}
942943

@@ -1042,15 +1043,15 @@ pub struct DefinitionV1x4 {
10421043
pub dkg_algorithm: String,
10431044
/// Cluster's 4 byte beacon chain fork version
10441045
/// (network/chain identifier).
1045-
#[serde_as(as = "EthHex")]
1046+
#[serde_as(as = "Hex0x")]
10461047
pub fork_version: Vec<u8>,
10471048
/// Config hash uniquely identifies a cluster definition excluding operator
10481049
/// ENRs and signatures.
1049-
#[serde_as(as = "EthHex")]
1050+
#[serde_as(as = "Hex0x")]
10501051
pub config_hash: Vec<u8>,
10511052
/// Definition hash uniquely identifies a cluster definition including
10521053
/// operator ENRs and signatures.
1053-
#[serde_as(as = "EthHex")]
1054+
#[serde_as(as = "Hex0x")]
10541055
pub definition_hash: Vec<u8>,
10551056
}
10561057

@@ -1154,15 +1155,15 @@ pub struct DefinitionV1x5to7 {
11541155
pub dkg_algorithm: String,
11551156
/// Cluster's 4 byte beacon chain fork version
11561157
/// (network/chain identifier).
1157-
#[serde_as(as = "EthHex")]
1158+
#[serde_as(as = "Hex0x")]
11581159
pub fork_version: Vec<u8>,
11591160
/// Config hash uniquely identifies a cluster definition excluding operator
11601161
/// ENRs and signatures.
1161-
#[serde_as(as = "EthHex")]
1162+
#[serde_as(as = "Hex0x")]
11621163
pub config_hash: Vec<u8>,
11631164
/// Definition hash uniquely identifies a cluster definition including
11641165
/// operator ENRs and signatures.
1165-
#[serde_as(as = "EthHex")]
1166+
#[serde_as(as = "Hex0x")]
11661167
pub definition_hash: Vec<u8>,
11671168
}
11681169

@@ -1251,19 +1252,19 @@ pub struct DefinitionV1x8 {
12511252
pub dkg_algorithm: String,
12521253
/// ForkVersion defines the cluster's 4 byte beacon chain fork version
12531254
/// (network/chain identifier).
1254-
#[serde_as(as = "EthHex")]
1255+
#[serde_as(as = "Hex0x")]
12551256
pub fork_version: Vec<u8>,
12561257
/// DepositAmounts specifies partial deposit amounts that sum up to at least
12571258
/// 32ETH.
12581259
#[serde_as(as = "DefaultOnNull<Vec<PickFirst<(DisplayFromStr, _)>>>")]
12591260
pub deposit_amounts: Vec<u64>,
12601261
/// ConfigHash uniquely identifies a cluster definition excluding operator
12611262
/// ENRs and signatures.
1262-
#[serde_as(as = "EthHex")]
1263+
#[serde_as(as = "Hex0x")]
12631264
pub config_hash: Vec<u8>,
12641265
/// DefinitionHash uniquely identifies a cluster definition including
12651266
/// operator ENRs and signatures.
1266-
#[serde_as(as = "EthHex")]
1267+
#[serde_as(as = "Hex0x")]
12671268
pub definition_hash: Vec<u8>,
12681269
}
12691270

@@ -1353,7 +1354,7 @@ pub struct DefinitionV1x9 {
13531354
pub dkg_algorithm: String,
13541355
/// ForkVersion defines the cluster's 4 byte beacon chain fork version
13551356
/// (network/chain identifier).
1356-
#[serde_as(as = "EthHex")]
1357+
#[serde_as(as = "Hex0x")]
13571358
pub fork_version: Vec<u8>,
13581359
/// DepositAmounts specifies partial deposit amounts that sum up to at least
13591360
/// 32ETH.
@@ -1364,11 +1365,11 @@ pub struct DefinitionV1x9 {
13641365
pub consensus_protocol: String,
13651366
/// ConfigHash uniquely identifies a cluster definition excluding operator
13661367
/// ENRs and signatures.
1367-
#[serde_as(as = "EthHex")]
1368+
#[serde_as(as = "Hex0x")]
13681369
pub config_hash: Vec<u8>,
13691370
/// DefinitionHash uniquely identifies a cluster definition including
13701371
/// operator ENRs and signatures.
1371-
#[serde_as(as = "EthHex")]
1372+
#[serde_as(as = "Hex0x")]
13721373
pub definition_hash: Vec<u8>,
13731374
}
13741375

@@ -1460,7 +1461,7 @@ pub struct DefinitionV1x10 {
14601461
pub dkg_algorithm: String,
14611462
/// Cluster's 4 byte beacon chain fork version
14621463
/// (network/chain identifier).
1463-
#[serde_as(as = "EthHex")]
1464+
#[serde_as(as = "Hex0x")]
14641465
pub fork_version: Vec<u8>,
14651466
/// Partial deposit amounts that sum up to at least
14661467
/// 32ETH.
@@ -1476,11 +1477,11 @@ pub struct DefinitionV1x10 {
14761477
pub compounding: bool,
14771478
/// Config hash uniquely identifies a cluster definition excluding operator
14781479
/// ENRs and signatures.
1479-
#[serde_as(as = "EthHex")]
1480+
#[serde_as(as = "Hex0x")]
14801481
pub config_hash: Vec<u8>,
14811482
/// Definition hash uniquely identifies a cluster definition including
14821483
/// operator ENRs and signatures.
1483-
#[serde_as(as = "EthHex")]
1484+
#[serde_as(as = "Hex0x")]
14841485
pub definition_hash: Vec<u8>,
14851486
}
14861487

0 commit comments

Comments
 (0)