3
3
//! These wrappers allows keeping mithril-stm agnostic to Cardano, while providing some
4
4
//! guarantees that mithril-stm will not be misused in the context of Cardano.
5
5
6
- use crate :: crypto_helper:: {
7
- cardano:: { ParseError , SerDeShelleyFileFormat } ,
8
- types:: {
9
- ProtocolParameters , ProtocolPartyId , ProtocolSignerVerificationKey ,
10
- ProtocolSignerVerificationKeySignature , ProtocolStakeDistribution ,
6
+ use crate :: {
7
+ crypto_helper:: {
8
+ cardano:: SerDeShelleyFileFormat ,
9
+ types:: {
10
+ ProtocolParameters , ProtocolPartyId , ProtocolSignerVerificationKey ,
11
+ ProtocolSignerVerificationKeySignature , ProtocolStakeDistribution ,
12
+ } ,
13
+ ProtocolOpCert ,
11
14
} ,
12
- ProtocolOpCert ,
15
+ StdError , StdResult ,
13
16
} ;
14
17
15
18
use mithril_stm:: key_reg:: { ClosedKeyReg , KeyReg } ;
16
19
use mithril_stm:: stm:: { Stake , StmInitializer , StmParameters , StmSigner , StmVerificationKeyPoP } ;
17
20
use mithril_stm:: RegisterError ;
18
21
19
22
use crate :: crypto_helper:: cardano:: Sum6KesBytes ;
23
+ use anyhow:: { anyhow, Context } ;
20
24
use blake2:: {
21
25
digest:: { consts:: U32 , FixedOutput } ,
22
26
Blake2b , Digest ,
@@ -36,7 +40,7 @@ type D = Blake2b<U32>;
36
40
pub type KESPeriod = u32 ;
37
41
38
42
/// New registration error
39
- #[ derive( Error , Debug , PartialEq , Eq ) ]
43
+ #[ derive( Error , Debug ) ]
40
44
pub enum ProtocolRegistrationErrorWrapper {
41
45
/// Error raised when a party id is needed but not provided
42
46
// TODO: Should be removed once the signer certification is fully deployed
@@ -72,16 +76,16 @@ pub enum ProtocolRegistrationErrorWrapper {
72
76
PoolAddressEncoding ,
73
77
74
78
/// Error raised when a core registration error occurs
75
- #[ error( "core registration error: '{0}'" ) ]
76
- CoreRegister ( # [ from ] RegisterError ) ,
79
+ #[ error( "core registration error: '{0:? }'" ) ]
80
+ CoreRegister ( StdError ) ,
77
81
}
78
82
79
83
/// New initializer error
80
84
#[ derive( Error , Debug ) ]
81
85
pub enum ProtocolInitializerErrorWrapper {
82
- /// Error raised when a codec parse error occurs
83
- #[ error( "codec parse error: '{0}' " ) ]
84
- Codec ( # [ from ] ParseError ) ,
86
+ /// Error raised when the underlying protocol initializer fails
87
+ #[ error( "protocol initializer error {0:?} " ) ]
88
+ ProtocolInitializer ( StdError ) ,
85
89
86
90
/// Error raised when a KES update error occurs
87
91
#[ error( "KES key cannot be updated for period {0}" ) ]
@@ -122,19 +126,22 @@ impl StmInitializerWrapper {
122
126
kes_period : Option < KESPeriod > ,
123
127
stake : Stake ,
124
128
rng : & mut R ,
125
- ) -> Result < Self , ProtocolInitializerErrorWrapper > {
129
+ ) -> StdResult < Self > {
126
130
let stm_initializer = StmInitializer :: setup ( params, stake, rng) ;
127
131
let kes_signature = if let Some ( kes_sk_path) = kes_sk_path {
128
- let mut kes_sk_bytes = Sum6KesBytes :: from_file ( kes_sk_path) ?;
132
+ let mut kes_sk_bytes = Sum6KesBytes :: from_file ( kes_sk_path)
133
+ . map_err ( |e| anyhow ! ( e) )
134
+ . with_context ( || "StmInitializerWrapper can not read KES secret key from file" ) ?;
129
135
let mut kes_sk = Sum6Kes :: try_from ( & mut kes_sk_bytes)
130
- . map_err ( ProtocolInitializerErrorWrapper :: Codec ) ?;
136
+ . map_err ( |e| ProtocolInitializerErrorWrapper :: ProtocolInitializer ( anyhow ! ( e) ) )
137
+ . with_context ( || "StmInitializerWrapper can not use KES secret key" ) ?;
131
138
let kes_sk_period = kes_sk. get_period ( ) ;
132
139
let provided_period = kes_period. unwrap_or_default ( ) ;
133
140
if kes_sk_period > provided_period {
134
- return Err ( ProtocolInitializerErrorWrapper :: KesMismatch (
141
+ return Err ( anyhow ! ( ProtocolInitializerErrorWrapper :: KesMismatch (
135
142
kes_sk_period,
136
143
provided_period,
137
- ) ) ;
144
+ ) ) ) ;
138
145
}
139
146
140
147
// We need to perform the evolutions
@@ -192,7 +199,10 @@ impl StmInitializerWrapper {
192
199
self ,
193
200
closed_reg : ClosedKeyReg < D > ,
194
201
) -> Result < StmSigner < D > , ProtocolRegistrationErrorWrapper > {
195
- Ok ( self . stm_initializer . new_signer ( closed_reg) ?)
202
+ self . stm_initializer
203
+ . new_signer ( closed_reg)
204
+ . with_context ( || "StmInitializerWrapper can not create a new signer" )
205
+ . map_err ( |e| ProtocolRegistrationErrorWrapper :: CoreRegister ( anyhow ! ( e) ) )
196
206
}
197
207
198
208
/// Convert to bytes
@@ -279,7 +289,8 @@ impl KeyRegWrapper {
279
289
if let Some ( & stake) = self . stake_distribution . get ( & pool_id_bech32) {
280
290
self . stm_key_reg
281
291
. register ( stake, pk. into ( ) )
282
- . map_err ( ProtocolRegistrationErrorWrapper :: CoreRegister ) ?;
292
+ . with_context ( || format ! ( "KeyRegWrapper can not register pool {pool_id_bech32}" ) )
293
+ . map_err ( |e| ProtocolRegistrationErrorWrapper :: CoreRegister ( anyhow ! ( e) ) ) ?;
283
294
return Ok ( pool_id_bech32) ;
284
295
}
285
296
Err ( ProtocolRegistrationErrorWrapper :: PartyIdNonExisting )
0 commit comments