Skip to content

Commit 6452217

Browse files
authored
Merge branch 'main' into dkg_nodesigs
2 parents 454c17e + 8cd8665 commit 6452217

12 files changed

Lines changed: 908 additions & 103 deletions

File tree

crates/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ hex.workspace = true
1818
humantime.workspace = true
1919
tokio.workspace = true
2020
pluto-app.workspace = true
21+
pluto-eth1wrap.workspace = true
2122
pluto-cluster.workspace = true
2223
pluto-crypto.workspace = true
2324
pluto-relay-server.workspace = true
2425
pluto-tracing.workspace = true
2526
pluto-core.workspace = true
2627
pluto-p2p.workspace = true
27-
pluto-eth1wrap.workspace = true
2828
pluto-eth2api.workspace = true
2929
pluto-eth2util.workspace = true
3030
pluto-k1util.workspace = true

crates/cli/src/cli.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clap::{Parser, Subcommand};
44

55
use crate::commands::{
66
create_cluster::CreateClusterArgs,
7+
create_dkg::CreateDkgArgs,
78
create_enr::CreateEnrArgs,
89
enr::EnrArgs,
910
relay::RelayArgs,
@@ -133,6 +134,10 @@ pub struct CreateArgs {
133134
/// Create subcommands
134135
#[derive(Subcommand)]
135136
pub enum CreateCommands {
137+
/// Create a cluster definition file for a new Distributed Key Generation
138+
/// ceremony
139+
Dkg(Box<CreateDkgArgs>),
140+
136141
/// Create an Ethereum Node Record (ENR) private key to identify this charon
137142
/// client
138143
Enr(CreateEnrArgs),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#[derive(thiserror::Error, Debug)]
2+
pub enum AddressValidationError {
3+
/// Value exceeds usize::MAX.
4+
#[error("Value {value} exceeds usize::MAX")]
5+
ValueExceedsUsize {
6+
/// The value that exceeds usize::MAX.
7+
value: u64,
8+
},
9+
10+
/// Mismatching number of fee recipient addresses.
11+
#[error(
12+
"mismatching --num-validators and --fee-recipient-addresses: num_validators={num_validators}, addresses={addresses}"
13+
)]
14+
MismatchingFeeRecipientAddresses {
15+
/// Number of validators.
16+
num_validators: u64,
17+
/// Number of addresses.
18+
addresses: usize,
19+
},
20+
21+
/// Mismatching number of withdrawal addresses.
22+
#[error(
23+
"mismatching --num-validators and --withdrawal-addresses: num_validators={num_validators}, addresses={addresses}"
24+
)]
25+
MismatchingWithdrawalAddresses {
26+
/// Number of validators.
27+
num_validators: u64,
28+
/// Number of addresses.
29+
addresses: usize,
30+
},
31+
}
32+
33+
type Result<T> = std::result::Result<T, AddressValidationError>;
34+
35+
/// Validates that addresses match the number of validators.
36+
/// If only one address is provided, it fills the slice to match num_validators.
37+
///
38+
/// Returns an error if the number of addresses doesn't match and isn't exactly
39+
/// 1.
40+
pub(super) fn validate_addresses(
41+
num_validators: u64,
42+
fee_recipient_addrs: &[String],
43+
withdrawal_addrs: &[String],
44+
) -> Result<(Vec<String>, Vec<String>)> {
45+
let num_validators_usize =
46+
usize::try_from(num_validators).map_err(|_| AddressValidationError::ValueExceedsUsize {
47+
value: num_validators,
48+
})?;
49+
50+
if fee_recipient_addrs.len() != num_validators_usize && fee_recipient_addrs.len() != 1 {
51+
return Err(AddressValidationError::MismatchingFeeRecipientAddresses {
52+
num_validators,
53+
addresses: fee_recipient_addrs.len(),
54+
});
55+
}
56+
57+
if withdrawal_addrs.len() != num_validators_usize && withdrawal_addrs.len() != 1 {
58+
return Err(AddressValidationError::MismatchingWithdrawalAddresses {
59+
num_validators,
60+
addresses: withdrawal_addrs.len(),
61+
});
62+
}
63+
64+
let mut fee_addrs = fee_recipient_addrs.to_vec();
65+
let mut withdraw_addrs = withdrawal_addrs.to_vec();
66+
67+
if fee_addrs.len() == 1 {
68+
let addr = fee_addrs[0].clone();
69+
fee_addrs = vec![addr; num_validators_usize];
70+
}
71+
72+
if withdraw_addrs.len() == 1 {
73+
let addr = withdraw_addrs[0].clone();
74+
withdraw_addrs = vec![addr; num_validators_usize];
75+
}
76+
77+
Ok((fee_addrs, withdraw_addrs))
78+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub(super) const DEFAULT_NETWORK: &str = "mainnet";
2+
pub(super) const ZERO_ADDRESS: &str = "0x0000000000000000000000000000000000000000";
3+
pub(crate) const MIN_NODES: u64 = 3;
4+
pub(crate) const MIN_THRESHOLD: u64 = 2;

crates/cli/src/commands/create_cluster.rs

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,17 @@ use rand::rngs::OsRng;
4646
use tracing::{debug, info, warn};
4747

4848
use crate::{
49-
commands::create_dkg,
49+
commands::{
50+
address_validation::validate_addresses,
51+
constants::{MIN_NODES, MIN_THRESHOLD},
52+
create_dkg,
53+
},
5054
error::{
5155
CliError, CreateClusterError, InvalidNetworkConfigError, Result as CliResult,
5256
ThresholdError,
5357
},
5458
};
5559

56-
/// Minimum number of nodes required in a cluster.
57-
pub const MIN_NODES: u64 = 3;
58-
/// Minimum threshold value.
59-
pub const MIN_THRESHOLD: u64 = 2;
60-
/// Zero ethereum address (not allowed on mainnet/gnosis).
61-
pub const ZERO_ADDRESS: &str = "0x0000000000000000000000000000000000000000";
6260
/// HTTP scheme.
6361
const HTTP_SCHEME: &str = "http";
6462
/// HTTPS scheme.
@@ -1210,52 +1208,6 @@ async fn load_definition(
12101208
Ok(def)
12111209
}
12121210

1213-
/// Validates that addresses match the number of validators.
1214-
/// If only one address is provided, it fills the slice to match num_validators.
1215-
///
1216-
/// Returns an error if the number of addresses doesn't match and isn't exactly
1217-
/// 1.
1218-
fn validate_addresses(
1219-
num_validators: u64,
1220-
fee_recipient_addrs: &[String],
1221-
withdrawal_addrs: &[String],
1222-
) -> Result<(Vec<String>, Vec<String>)> {
1223-
let num_validators_usize =
1224-
usize::try_from(num_validators).map_err(|_| CreateClusterError::ValueExceedsUsize {
1225-
value: num_validators,
1226-
})?;
1227-
1228-
if fee_recipient_addrs.len() != num_validators_usize && fee_recipient_addrs.len() != 1 {
1229-
return Err(CreateClusterError::MismatchingFeeRecipientAddresses {
1230-
num_validators,
1231-
addresses: fee_recipient_addrs.len(),
1232-
});
1233-
}
1234-
1235-
if withdrawal_addrs.len() != num_validators_usize && withdrawal_addrs.len() != 1 {
1236-
return Err(CreateClusterError::MismatchingWithdrawalAddresses {
1237-
num_validators,
1238-
addresses: withdrawal_addrs.len(),
1239-
});
1240-
}
1241-
1242-
let mut fee_addrs = fee_recipient_addrs.to_vec();
1243-
let mut withdraw_addrs = withdrawal_addrs.to_vec();
1244-
1245-
// Expand single address to match num_validators
1246-
if fee_addrs.len() == 1 {
1247-
let addr = fee_addrs[0].clone();
1248-
fee_addrs = vec![addr; num_validators_usize];
1249-
}
1250-
1251-
if withdraw_addrs.len() == 1 {
1252-
let addr = withdraw_addrs[0].clone();
1253-
withdraw_addrs = vec![addr; num_validators_usize];
1254-
}
1255-
1256-
Ok((fee_addrs, withdraw_addrs))
1257-
}
1258-
12591211
/// Returns the safe threshold, logging a warning if a non-standard threshold is
12601212
/// provided.
12611213
fn safe_threshold(num_nodes: u64, threshold: Option<u64>) -> u64 {

0 commit comments

Comments
 (0)