Skip to content

Commit 1c8bbc3

Browse files
authored
fix: finish eth2registration method in cluster/distvalidator (#298)
* fix: todo in eth2registration * fix: review comments * fix: review comments * fix: comment
1 parent 408f8f3 commit 1c8bbc3

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

crates/cluster/src/distvalidator.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
use pluto_crypto::types::{PUBLIC_KEY_LENGTH, PublicKey};
2+
use pluto_eth2api::{
3+
spec::{
4+
BuilderVersion,
5+
phase0::{BLS_PUBKEY_LEN, BLS_SIGNATURE_LEN},
6+
},
7+
v1,
8+
versioned::VersionedSignedValidatorRegistration,
9+
};
210
use pluto_ssz::serde_utils::Hex0x;
311
use serde::{Deserialize, Serialize};
412

5-
use crate::{deposit::DepositData, registration::BuilderRegistration};
13+
use crate::{definition::ADDRESS_LEN, deposit::DepositData, registration::BuilderRegistration};
614
use serde_with::{
715
base64::{Base64, Standard},
816
serde_as,
@@ -41,6 +49,9 @@ pub enum DistValidatorError {
4149
/// Invalid public share index.
4250
#[error("invalid public share index: got {0}, want less than {1}")]
4351
InvalidPublicShareIndex(usize, usize),
52+
/// Invalid builder registration.
53+
#[error("invalid registration")]
54+
InvalidRegistration,
4455
}
4556

4657
impl DistValidator {
@@ -83,20 +94,41 @@ impl DistValidator {
8394
}
8495

8596
/// True if the validator has zero valued registration.
86-
/// registration.
8797
pub fn zero_registration(&self) -> bool {
88-
self.builder_registration.signature.is_empty()
89-
&& self.builder_registration.message.fee_recipient.is_empty()
98+
self.builder_registration.signature == [0u8; BLS_SIGNATURE_LEN]
99+
&& self.builder_registration.message.fee_recipient == [0u8; ADDRESS_LEN]
90100
&& self.builder_registration.message.gas_limit == 0
91101
&& self.builder_registration.message.timestamp.timestamp() == 0
92-
&& self.builder_registration.message.pub_key.is_empty()
102+
&& self.builder_registration.message.pub_key == [0u8; BLS_PUBKEY_LEN]
93103
}
94104

95105
/// Validator's Eth2 registration.
96-
pub fn eth2_registration(&self) -> Result<(), DistValidatorError> {
97-
unimplemented!(
98-
"Eth2 registration requires to have ethereum types library which is not yet integrated in pluto-cluster"
99-
)
106+
pub fn eth2_registration(
107+
&self,
108+
) -> Result<VersionedSignedValidatorRegistration, DistValidatorError> {
109+
if self.zero_registration() {
110+
return Err(DistValidatorError::InvalidRegistration);
111+
}
112+
113+
let reg = &self.builder_registration;
114+
115+
Ok(VersionedSignedValidatorRegistration {
116+
version: BuilderVersion::V1,
117+
v1: Some(v1::SignedValidatorRegistration {
118+
message: v1::ValidatorRegistration {
119+
fee_recipient: reg.message.fee_recipient,
120+
gas_limit: reg.message.gas_limit,
121+
timestamp: reg
122+
.message
123+
.timestamp
124+
.timestamp()
125+
.try_into()
126+
.map_err(|_| DistValidatorError::InvalidRegistration)?,
127+
pubkey: reg.message.pub_key,
128+
},
129+
signature: reg.signature,
130+
}),
131+
})
100132
}
101133
}
102134

0 commit comments

Comments
 (0)