Skip to content

Commit 3b7f7fe

Browse files
committed
unify bls
1 parent 50e72fa commit 3b7f7fe

45 files changed

Lines changed: 1301 additions & 657 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 541 additions & 66 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ axum-extra = { version = "0.10.0", features = ["typed-header"] }
2424
base64 = "0.22.1"
2525
bimap = { version = "0.6.3", features = ["serde"] }
2626
blsful = "2.5"
27-
blst = "0.3.11"
2827
cb-cli = { path = "crates/cli" }
2928
cb-common = { path = "crates/common" }
3029
cb-metrics = { path = "crates/metrics" }
@@ -37,7 +36,6 @@ ctr = "0.9.2"
3736
derive_more = { version = "2.0.1", features = ["deref", "display", "from", "into"] }
3837
docker-compose-types = "0.16.0"
3938
docker-image = "0.2.1"
40-
eth2_keystore = { git = "https://github.com/sigp/lighthouse", tag = "v7.0.1" }
4139
ethereum_serde_utils = "0.7.0"
4240
ethereum_ssz = "0.8"
4341
ethereum_ssz_derive = "0.8"
@@ -47,6 +45,8 @@ headers = "0.4.0"
4745
indexmap = "2.2.6"
4846
jsonwebtoken = { version = "9.3.1", default-features = false }
4947
lazy_static = "1.5.0"
48+
lh_eth2_keystore = { package = "eth2_keystore", git = "https://github.com/sigp/lighthouse", tag = "v7.1.0" }
49+
lh_types = { package = "types", git = "https://github.com/sigp/lighthouse", tag = "v7.1.0" }
5050
parking_lot = "0.12.3"
5151
pbkdf2 = "0.12.2"
5252
prometheus = "0.13.4"

benches/pbs/src/main.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::time::{Duration, Instant};
22

3-
use alloy::{primitives::B256, rpc::types::beacon::BlsPublicKey};
3+
use alloy::{hex, primitives::B256};
44
use cb_common::{
55
config::RelayConfig,
6-
pbs::{GetHeaderResponse, RelayClient, RelayEntry},
7-
signer::BlsSecretKey,
6+
pbs::{BlsPublicKey, BlsSecretKey, GetHeaderResponse, RelayClient, RelayEntry},
87
types::Chain,
9-
utils::blst_pubkey_to_alloy,
108
};
119
use cb_tests::mock_relay::{start_mock_relay_service, MockRelayState};
1210
use comfy_table::Table;
@@ -19,7 +17,7 @@ fn get_random_hash() -> B256 {
1917
B256::from(rand::random::<[u8; 32]>())
2018
}
2119
fn get_random_pubkey() -> BlsPublicKey {
22-
BlsPublicKey::ZERO
20+
BlsPublicKey::deserialize(&hex!("0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")).unwrap()
2321
}
2422

2523
#[tokio::main]
@@ -47,7 +45,7 @@ async fn main() {
4745
for slot in 0..config.benchmark.n_slots {
4846
let parent_hash = get_random_hash();
4947
let validator_pubkey = get_random_pubkey();
50-
let url = mock_validator.get_header_url(slot, parent_hash, validator_pubkey).unwrap();
48+
let url = mock_validator.get_header_url(slot, &parent_hash, &validator_pubkey).unwrap();
5149

5250
for _ in 0..config.benchmark.headers_per_slot {
5351
let url = url.clone();
@@ -138,8 +136,8 @@ const MOCK_RELAY_SECRET: [u8; 32] = [
138136
152, 98, 59, 240, 181, 131, 47, 1, 180, 255, 245,
139137
];
140138
async fn start_mock_relay(chain: Chain, relay_config: RelayConfig) {
141-
let signer = BlsSecretKey::key_gen(&MOCK_RELAY_SECRET, &[]).unwrap();
142-
let pubkey: BlsPublicKey = blst_pubkey_to_alloy(&signer.sk_to_pk());
139+
let signer = BlsSecretKey::deserialize(&MOCK_RELAY_SECRET).unwrap();
140+
let pubkey: BlsPublicKey = signer.public_key();
143141

144142
assert_eq!(relay_config.entry.pubkey, pubkey, "Expected relay pubkey to be 0xb060572f535ba5615b874ebfef757fbe6825352ad257e31d724e57fe25a067a13cfddd0f00cb17bf3a3d2e901a380c17");
145143

@@ -152,7 +150,7 @@ async fn start_mock_relay(chain: Chain, relay_config: RelayConfig) {
152150
}
153151

154152
fn get_mock_validator(bench: BenchConfig) -> RelayClient {
155-
let entry = RelayEntry { id: bench.id, pubkey: BlsPublicKey::default(), url: bench.url };
153+
let entry = RelayEntry { id: bench.id, pubkey: get_random_pubkey(), url: bench.url };
156154
let config = RelayConfig {
157155
entry,
158156
id: None,

bin/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ pub mod prelude {
99
load_builder_module_config, load_commit_module_config, load_pbs_config,
1010
load_pbs_custom_config, LogsSettings, StartCommitModuleConfig, PBS_MODULE_NAME,
1111
},
12-
pbs::{BuilderEvent, BuilderEventClient, OnBuilderApiEvent},
13-
signer::{BlsPublicKey, BlsSignature, EcdsaSignature},
12+
pbs::{BlsPublicKey, BlsSignature, BuilderEvent, BuilderEventClient, OnBuilderApiEvent},
13+
signer::EcdsaSignature,
1414
types::Chain,
1515
utils::{initialize_tracing_log, utcnow_ms, utcnow_ns, utcnow_sec, utcnow_us},
1616
};

crates/common/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ async-trait.workspace = true
1212
axum.workspace = true
1313
base64.workspace = true
1414
bimap.workspace = true
15-
blst.workspace = true
1615
cipher.workspace = true
1716
ctr.workspace = true
1817
derive_more.workspace = true
1918
docker-image.workspace = true
20-
eth2_keystore.workspace = true
2119
ethereum_serde_utils.workspace = true
2220
ethereum_ssz.workspace = true
2321
ethereum_ssz_derive.workspace = true
2422
eyre.workspace = true
2523
jsonwebtoken.workspace = true
24+
lh_eth2_keystore.workspace = true
25+
lh_types.workspace = true
2626
pbkdf2.workspace = true
2727
rand.workspace = true
2828
rayon.workspace = true

crates/common/src/commit/client.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::{Duration, Instant};
22

3-
use alloy::{primitives::Address, rpc::types::beacon::BlsSignature};
3+
use alloy::primitives::Address;
44
use eyre::WrapErr;
55
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
66
use serde::Deserialize;
@@ -16,7 +16,8 @@ use super::{
1616
};
1717
use crate::{
1818
constants::SIGNER_JWT_EXPIRATION,
19-
signer::{BlsPublicKey, EcdsaSignature},
19+
pbs::{BlsPublicKey, BlsSignature},
20+
signer::EcdsaSignature,
2021
types::{Jwt, ModuleId},
2122
utils::create_jwt,
2223
DEFAULT_REQUEST_TIMEOUT,

crates/common/src/commit/request.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,37 @@ use std::{
66
use alloy::{
77
hex,
88
primitives::{Address, B256},
9-
rpc::types::beacon::BlsSignature,
109
};
1110
use derive_more::derive::From;
1211
use serde::{Deserialize, Serialize};
1312
use tree_hash::TreeHash;
1413
use tree_hash_derive::TreeHash;
1514

1615
use crate::{
17-
constants::COMMIT_BOOST_DOMAIN, error::BlstErrorWrapper, signature::verify_signed_message,
18-
signer::BlsPublicKey, types::Chain,
16+
constants::COMMIT_BOOST_DOMAIN,
17+
pbs::{BlsPublicKey, BlsSignature},
18+
signature::verify_signed_message,
19+
types::Chain,
1920
};
2021

21-
pub trait ProxyId: AsRef<[u8]> + Debug + Clone + Copy + TreeHash + Display {}
22+
pub trait ProxyId: Debug + Clone + TreeHash + Display {
23+
fn to_bytes(&self) -> Vec<u8>;
24+
}
2225

23-
impl ProxyId for Address {}
26+
impl ProxyId for Address {
27+
fn to_bytes(&self) -> Vec<u8> {
28+
self.0.as_slice().to_vec()
29+
}
30+
}
2431

25-
impl ProxyId for BlsPublicKey {}
32+
impl ProxyId for BlsPublicKey {
33+
fn to_bytes(&self) -> Vec<u8> {
34+
self.serialize().to_vec()
35+
}
36+
}
2637

2738
// GENERIC PROXY DELEGATION
28-
#[derive(Debug, Clone, Copy, Serialize, Deserialize, TreeHash)]
39+
#[derive(Debug, Clone, Serialize, Deserialize, TreeHash)]
2940
pub struct ProxyDelegation<T: ProxyId> {
3041
pub delegator: BlsPublicKey,
3142
pub proxy: T,
@@ -40,7 +51,7 @@ impl<T: ProxyId> fmt::Display for ProxyDelegation<T> {
4051
}
4152
}
4253

43-
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
54+
#[derive(Debug, Clone, Serialize, Deserialize)]
4455
pub struct SignedProxyDelegation<T: ProxyId> {
4556
pub message: ProxyDelegation<T>,
4657
/// Signature of message with the delegator keypair
@@ -51,7 +62,7 @@ pub type SignedProxyDelegationBls = SignedProxyDelegation<BlsPublicKey>;
5162
pub type SignedProxyDelegationEcdsa = SignedProxyDelegation<Address>;
5263

5364
impl<T: ProxyId> SignedProxyDelegation<T> {
54-
pub fn validate(&self, chain: Chain) -> Result<(), BlstErrorWrapper> {
65+
pub fn validate(&self, chain: Chain) -> bool {
5566
verify_signed_message(
5667
chain,
5768
&self.message.delegator,

crates/common/src/config/mux.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
use alloy::{
88
primitives::{address, Address, U256},
99
providers::ProviderBuilder,
10-
rpc::types::beacon::BlsPublicKey,
1110
sol,
1211
};
1312
use eyre::{bail, ensure, Context};
@@ -16,7 +15,11 @@ use tracing::{debug, info};
1615
use url::Url;
1716

1817
use super::{load_optional_env_var, PbsConfig, RelayConfig, MUX_PATH_ENV};
19-
use crate::{config::remove_duplicate_keys, pbs::RelayClient, types::Chain};
18+
use crate::{
19+
config::remove_duplicate_keys,
20+
pbs::{BlsPublicKey, RelayClient},
21+
types::Chain,
22+
};
2023

2124
#[derive(Debug, Deserialize, Serialize)]
2225
pub struct PbsMuxes {
@@ -93,8 +96,8 @@ impl PbsMuxes {
9396
let config = Arc::new(config);
9497

9598
let runtime_config = RuntimeMuxConfig { id: mux.id, config, relays: relay_clients };
96-
for pubkey in mux.validator_pubkeys.iter() {
97-
configs.insert(*pubkey, runtime_config.clone());
99+
for pubkey in mux.validator_pubkeys.into_iter() {
100+
configs.insert(pubkey, runtime_config.clone());
98101
}
99102
}
100103

@@ -254,7 +257,7 @@ async fn fetch_lido_registry_keys(
254257
debug!("fetching {total_keys} total keys");
255258

256259
const CALL_BATCH_SIZE: u64 = 250u64;
257-
const BLS_PK_LEN: usize = BlsPublicKey::len_bytes();
260+
const BLS_PK_LEN: usize = 48;
258261

259262
let mut keys = vec![];
260263
let mut offset = 0;
@@ -275,7 +278,10 @@ async fn fetch_lido_registry_keys(
275278
);
276279

277280
for chunk in pubkeys.chunks(BLS_PK_LEN) {
278-
keys.push(BlsPublicKey::try_from(chunk)?);
281+
keys.push(
282+
BlsPublicKey::deserialize(chunk)
283+
.map_err(|_| eyre::eyre!("invalid BLS public key"))?,
284+
);
279285
}
280286

281287
offset += limit;
@@ -319,10 +325,13 @@ async fn fetch_ssv_pubkeys(
319325
.json::<SSVResponse>()
320326
.await?;
321327

322-
pubkeys.extend(response.validators.iter().map(|v| v.pubkey).collect::<Vec<BlsPublicKey>>());
328+
let fetched = response.validators.len();
329+
pubkeys.extend(
330+
response.validators.into_iter().map(|v| v.pubkey).collect::<Vec<BlsPublicKey>>(),
331+
);
323332
page += 1;
324333

325-
if response.validators.len() < MAX_PER_PAGE {
334+
if fetched < MAX_PER_PAGE {
326335
ensure!(
327336
pubkeys.len() == response.pagination.total,
328337
"expected {} keys, got {}",
@@ -383,8 +392,11 @@ mod tests {
383392
.pubkeys;
384393

385394
let mut vec = vec![];
386-
for chunk in pubkeys.chunks(BlsPublicKey::len_bytes()) {
387-
vec.push(BlsPublicKey::try_from(chunk)?);
395+
for chunk in pubkeys.chunks(48) {
396+
vec.push(
397+
BlsPublicKey::deserialize(chunk)
398+
.map_err(|_| eyre::eyre!("invalid BLS public key"))?,
399+
);
388400
}
389401

390402
assert_eq!(vec.len(), LIMIT);

crates/common/src/config/pbs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{
99
use alloy::{
1010
primitives::{utils::format_ether, U256},
1111
providers::{Provider, ProviderBuilder},
12-
rpc::types::beacon::BlsPublicKey,
1312
};
1413
use eyre::{ensure, Result};
1514
use serde::{de::DeserializeOwned, Deserialize, Serialize};
@@ -26,8 +25,8 @@ use crate::{
2625
SIGNER_URL_ENV,
2726
},
2827
pbs::{
29-
BuilderEventPublisher, DefaultTimeout, RelayClient, RelayEntry, DEFAULT_PBS_PORT,
30-
LATE_IN_SLOT_TIME_MS, REGISTER_VALIDATOR_RETRY_LIMIT,
28+
BlsPublicKey, BuilderEventPublisher, DefaultTimeout, RelayClient, RelayEntry,
29+
DEFAULT_PBS_PORT, LATE_IN_SLOT_TIME_MS, REGISTER_VALIDATOR_RETRY_LIMIT,
3130
},
3231
types::{Chain, Jwt, ModuleId},
3332
utils::{

crates/common/src/config/utils.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::{collections::HashMap, path::Path};
22

3-
use alloy::rpc::types::beacon::BlsPublicKey;
43
use eyre::{bail, Context, Result};
54
use serde::de::DeserializeOwned;
65

76
use super::JWTS_ENV;
8-
use crate::types::ModuleId;
7+
use crate::{pbs::BlsPublicKey, types::ModuleId};
98

109
pub fn load_env_var(env: &str) -> Result<String> {
1110
std::env::var(env).wrap_err(format!("{env} is not set"))
@@ -37,7 +36,7 @@ pub fn remove_duplicate_keys(keys: Vec<BlsPublicKey>) -> Vec<BlsPublicKey> {
3736
let mut key_set = std::collections::HashSet::new();
3837

3938
for key in keys {
40-
if key_set.insert(key) {
39+
if key_set.insert(key.clone()) {
4140
unique_keys.push(key);
4241
}
4342
}
@@ -75,9 +74,9 @@ mod tests {
7574

7675
#[test]
7776
fn test_remove_duplicate_keys() {
78-
let key1 = BlsPublicKey::from([1; 48]);
79-
let key2 = BlsPublicKey::from([2; 48]);
80-
let keys = vec![key1, key2, key1];
77+
let key1 = BlsPublicKey::deserialize(&[1; 48]).unwrap();
78+
let key2 = BlsPublicKey::deserialize(&[2; 48]).unwrap();
79+
let keys = vec![key1.clone(), key2.clone(), key1.clone()];
8180

8281
let unique_keys = remove_duplicate_keys(keys);
8382
assert_eq!(unique_keys.len(), 2);

0 commit comments

Comments
 (0)