@@ -10,28 +10,30 @@ 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 ) ;
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 } ,
2730
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- }
32- }
31+ #[ error( "Failed to create public key: {0}" ) ]
32+ PublicKeyCreation ( String ) ,
3333
34- impl std:: error:: Error for StringError { }
34+ #[ error( "Encrypted key has wrong length" ) ]
35+ EncryptedKeyLength ,
36+ }
3537
3638// Parses shares from a ValidatorAdded event
3739// Event contains a bytes stream of the form
@@ -41,7 +43,7 @@ pub fn parse_shares(
4143 operator_ids : & [ OperatorId ] ,
4244 cluster_id : & ClusterId ,
4345 validator_pubkey : & PublicKeyBytes ,
44- ) -> Result < ( Vec < u8 > , Vec < Share > ) , String > {
46+ ) -> Result < ( Vec < u8 > , Vec < Share > ) , ShareParseError > {
4547 let operator_count = operator_ids. len ( ) ;
4648
4749 // Calculate offsets for different components within the shares
@@ -51,11 +53,10 @@ pub fn parse_shares(
5153
5254 // Validate total length of shares
5355 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- ) ) ;
56+ return Err ( InvalidLength {
57+ expected : shares_expected_length,
58+ actual : shares. len ( ) ,
59+ } ) ;
5960 }
6061
6162 // Extract all of the components
@@ -78,12 +79,12 @@ pub fn parse_shares(
7879
7980 // Create public key
8081 let share_pubkey = PublicKeyBytes :: from_str ( & public_key_hex)
81- . map_err ( |e| format ! ( "Failed to create public key: {e}" ) ) ?;
82+ . map_err ( ShareParseError :: PublicKeyCreation ) ?;
8283
8384 // Convert encrypted key into fixed array
8485 let encrypted_array: [ u8 ; 256 ] = encrypted
8586 . try_into ( )
86- . map_err ( |_| "Encrypted key has wrong length" . to_string ( ) ) ?;
87+ . map_err ( |_| ShareParseError :: EncryptedKeyLength ) ?;
8788
8889 Ok ( Share {
8990 validator_pubkey : * validator_pubkey,
@@ -93,7 +94,7 @@ pub fn parse_shares(
9394 encrypted_private_key : encrypted_array,
9495 } )
9596 } )
96- . collect :: < Result < Vec < _ > , String > > ( ) ?;
97+ . collect :: < Result < Vec < _ > , ShareParseError > > ( ) ?;
9798
9899 Ok ( ( signature, shares) )
99100}
@@ -216,18 +217,15 @@ pub fn validate_operators(
216217}
217218
218219/// Helper function to parse validator public keys
219- pub fn parse_validator_pubkey ( pubkey : & Bytes ) -> Result < PublicKeyBytes , ExecutionError > {
220+ pub fn parse_validator_pubkey ( pubkey : & Bytes ) -> Result < PublicKeyBytes , ShareParseError > {
220221 let pubkey_str = pubkey. to_string ( ) ;
221222 PublicKeyBytes :: from_str ( & pubkey_str) . map_err ( |e| {
222223 debug ! (
223224 validator_pubkey = %pubkey_str,
224225 error = %e,
225226 "Failed to parse validator public key"
226227 ) ;
227- ExecutionError :: invalid_event (
228- format ! ( "Failed to parse validator public key: {e}" ) ,
229- Some ( Box :: new ( StringError ( e. to_string ( ) ) ) ) ,
230- )
228+ ShareParseError :: PublicKeyCreation ( e)
231229 } )
232230}
233231
0 commit comments