@@ -10,29 +10,35 @@ use database::NetworkState;
1010use reqwest:: Client ;
1111use sensitive_url:: SensitiveUrl ;
1212use ssv_types:: { ClusterId , ENCRYPTED_KEY_LENGTH , OperatorId , Share , ValidatorMetadata } ;
13+ use thiserror:: Error ;
1314use tower:: ServiceBuilder ;
1415use tracing:: debug;
1516use types:: { Graffiti , PublicKeyBytes , Signature } ;
1617
17- use crate :: { error:: ExecutionError , sync:: MAX_OPERATORS } ;
18+ use crate :: { error:: ExecutionError , sync:: MAX_OPERATORS , util :: ShareParseError :: InvalidLength } ;
1819
1920// phase0.SignatureLength
2021const SIGNATURE_LENGTH : usize = 96 ;
2122// phase0.PublicKeyLength
2223const PUBLIC_KEY_LENGTH : usize = 48 ;
2324
24- // Simple wrapper to make String compatible with Error trait
25- #[ derive( Debug ) ]
26- struct StringError ( String ) ;
27-
28- impl std:: fmt:: Display for StringError {
29- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
30- write ! ( f, "{}" , self . 0 )
31- }
25+ /// Errors that can occur during share parsing
26+ #[ derive( Error , Debug ) ]
27+ pub enum ShareParseError {
28+ #[ error( "Share data has invalid length: expected {expected}, got {actual}" ) ]
29+ InvalidLength { expected : usize , actual : usize } ,
30+
31+ #[ error( "Failed to create public key: {0}" ) ]
32+ PublicKeyCreation ( String ) ,
33+
34+ #[ error(
35+ "Encrypted key has wrong length: expected {}, got {}" ,
36+ ENCRYPTED_KEY_LENGTH ,
37+ "actual"
38+ ) ]
39+ EncryptedKeyLength ,
3240}
3341
34- impl std:: error:: Error for StringError { }
35-
3642// Parses shares from a ValidatorAdded event
3743// Event contains a bytes stream of the form
3844// [signature | public keys | encrypted keys].
@@ -41,7 +47,7 @@ pub fn parse_shares(
4147 operator_ids : & [ OperatorId ] ,
4248 cluster_id : & ClusterId ,
4349 validator_pubkey : & PublicKeyBytes ,
44- ) -> Result < ( Vec < u8 > , Vec < Share > ) , String > {
50+ ) -> Result < ( Vec < u8 > , Vec < Share > ) , ShareParseError > {
4551 let operator_count = operator_ids. len ( ) ;
4652
4753 // Calculate offsets for different components within the shares
@@ -51,11 +57,10 @@ pub fn parse_shares(
5157
5258 // Validate total length of shares
5359 if shares_expected_length != shares. len ( ) {
54- return Err ( format ! (
55- "Share data has invalid length: expected {}, got {}" ,
56- shares_expected_length,
57- shares. len( )
58- ) ) ;
60+ return Err ( InvalidLength {
61+ expected : shares_expected_length,
62+ actual : shares. len ( ) ,
63+ } ) ;
5964 }
6065
6166 // Extract all of the components
@@ -78,12 +83,12 @@ pub fn parse_shares(
7883
7984 // Create public key
8085 let share_pubkey = PublicKeyBytes :: from_str ( & public_key_hex)
81- . map_err ( |e| format ! ( "Failed to create public key: {e}" ) ) ?;
86+ . map_err ( ShareParseError :: PublicKeyCreation ) ?;
8287
8388 // Convert encrypted key into fixed array
8489 let encrypted_array: [ u8 ; 256 ] = encrypted
8590 . try_into ( )
86- . map_err ( |_| "Encrypted key has wrong length" . to_string ( ) ) ?;
91+ . map_err ( |_| ShareParseError :: EncryptedKeyLength ) ?;
8792
8893 Ok ( Share {
8994 validator_pubkey : * validator_pubkey,
@@ -93,7 +98,7 @@ pub fn parse_shares(
9398 encrypted_private_key : encrypted_array,
9499 } )
95100 } )
96- . collect :: < Result < Vec < _ > , String > > ( ) ?;
101+ . collect :: < Result < Vec < _ > , ShareParseError > > ( ) ?;
97102
98103 Ok ( ( signature, shares) )
99104}
@@ -216,18 +221,15 @@ pub fn validate_operators(
216221}
217222
218223/// Helper function to parse validator public keys
219- pub fn parse_validator_pubkey ( pubkey : & Bytes ) -> Result < PublicKeyBytes , ExecutionError > {
224+ pub fn parse_validator_pubkey ( pubkey : & Bytes ) -> Result < PublicKeyBytes , ShareParseError > {
220225 let pubkey_str = pubkey. to_string ( ) ;
221226 PublicKeyBytes :: from_str ( & pubkey_str) . map_err ( |e| {
222227 debug ! (
223228 validator_pubkey = %pubkey_str,
224229 error = %e,
225230 "Failed to parse validator public key"
226231 ) ;
227- ExecutionError :: invalid_event (
228- format ! ( "Failed to parse validator public key: {e}" ) ,
229- Some ( Box :: new ( StringError ( e. to_string ( ) ) ) ) ,
230- )
232+ ShareParseError :: PublicKeyCreation ( e)
231233 } )
232234}
233235
0 commit comments