|
| 1 | +use charon_crypto::types::{PUBLIC_KEY_LENGTH, PublicKey}; |
| 2 | +use serde::{Deserialize, Serialize}; |
1 | 3 |
|
| 4 | +use crate::{deposit::DepositData, helpers::EthHex, registration::BuilderRegistration}; |
| 5 | +use serde_with::serde_as; |
| 6 | + |
| 7 | +/// DistValidator is a distributed validator managed by the cluster. |
| 8 | +#[serde_as] |
| 9 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 10 | +pub struct DistValidator { |
| 11 | + /// PubKey is the distributed validator group public key. |
| 12 | + #[serde(rename = "distributed_public_key")] |
| 13 | + #[serde_as(as = "EthHex")] |
| 14 | + pub pub_key: Vec<u8>, |
| 15 | + |
| 16 | + /// PubShares are the public keys corresponding to each node's secret key |
| 17 | + /// share. It can be used to verify a partial signature created by any |
| 18 | + /// node in the cluster. |
| 19 | + #[serde(rename = "public_shares")] |
| 20 | + #[serde_as(as = "Vec<EthHex>")] |
| 21 | + pub pub_shares: Vec<Vec<u8>>, |
| 22 | + |
| 23 | + /// PartialDepositData is the list of partial deposit data. |
| 24 | + pub partial_deposit_data: Vec<DepositData>, |
| 25 | + |
| 26 | + /// BuilderRegistration is the pre-generated signed validator builder |
| 27 | + /// registration. |
| 28 | + pub builder_registration: BuilderRegistration, |
| 29 | +} |
| 30 | + |
| 31 | +/// DistValidatorError is an error type for DistValidator operations. |
| 32 | +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] |
| 33 | +pub enum DistValidatorError { |
| 34 | + /// Invalid public key length. |
| 35 | + #[error("invalid public key length: got {0}, want {1}")] |
| 36 | + InvalidPublicKeyLength(usize, usize), |
| 37 | + /// Invalid public share index. |
| 38 | + #[error("invalid public share index: got {0}, want less than {1}")] |
| 39 | + InvalidPublicShareIndex(usize, usize), |
| 40 | +} |
| 41 | + |
| 42 | +impl DistValidator { |
| 43 | + /// PublicKey returns the distributed validator group public key. |
| 44 | + pub fn public_key(&self) -> Result<PublicKey, DistValidatorError> { |
| 45 | + if self.pub_key.len() != PUBLIC_KEY_LENGTH { |
| 46 | + return Err(DistValidatorError::InvalidPublicKeyLength( |
| 47 | + self.pub_key.len(), |
| 48 | + PUBLIC_KEY_LENGTH, |
| 49 | + )); |
| 50 | + } |
| 51 | + let mut pub_key = [0u8; PUBLIC_KEY_LENGTH]; |
| 52 | + pub_key.copy_from_slice(&self.pub_key); |
| 53 | + Ok(pub_key) |
| 54 | + } |
| 55 | + |
| 56 | + /// PublicKeyHex returns the validator hex group public key. |
| 57 | + pub fn public_key_hex(&self) -> Result<String, DistValidatorError> { |
| 58 | + let pub_key = self.public_key()?; |
| 59 | + Ok(format!("0x{}", hex::encode(pub_key))) |
| 60 | + } |
| 61 | + |
| 62 | + /// PublicShare returns a peer's threshold BLS public share. |
| 63 | + pub fn public_share(&self, index: usize) -> Result<PublicKey, DistValidatorError> { |
| 64 | + if index >= self.pub_shares.len() { |
| 65 | + return Err(DistValidatorError::InvalidPublicShareIndex( |
| 66 | + index, |
| 67 | + self.pub_shares.len(), |
| 68 | + )); |
| 69 | + } |
| 70 | + if self.pub_shares[index].len() != PUBLIC_KEY_LENGTH { |
| 71 | + return Err(DistValidatorError::InvalidPublicKeyLength( |
| 72 | + self.pub_shares[index].len(), |
| 73 | + PUBLIC_KEY_LENGTH, |
| 74 | + )); |
| 75 | + } |
| 76 | + let mut pub_share = [0u8; PUBLIC_KEY_LENGTH]; |
| 77 | + pub_share.copy_from_slice(&self.pub_shares[index]); |
| 78 | + Ok(pub_share) |
| 79 | + } |
| 80 | + |
| 81 | + /// ZeroRegistration returns a true if the validator has zero valued |
| 82 | + /// registration. |
| 83 | + pub fn zero_registration(&self) -> bool { |
| 84 | + self.builder_registration.signature.is_empty() |
| 85 | + && self.builder_registration.message.fee_recipient.is_empty() |
| 86 | + && self.builder_registration.message.gas_limit == 0 |
| 87 | + && self.builder_registration.message.timestamp.timestamp() == 0 |
| 88 | + && self.builder_registration.message.pub_key.is_empty() |
| 89 | + } |
| 90 | + |
| 91 | + /// Eth2Registration returns the validator's Eth2 registration. |
| 92 | + pub fn eth2_registration(&self) -> Result<(), DistValidatorError> { |
| 93 | + unimplemented!( |
| 94 | + "Eth2 registration requires to have ethereum types library which is not yet integrated in charon-cluster" |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
0 commit comments