@@ -198,68 +198,33 @@ impl StateMachine {
198
198
SignerState :: ReadyToSign {
199
199
epoch,
200
200
last_signed_entity_type,
201
- } => {
202
- fn is_same_signed_entity_type (
203
- last_signed_entity_type : & Option < SignedEntityType > ,
204
- pending_certificate : & CertificatePending ,
205
- ) -> bool {
206
- Some ( & pending_certificate. signed_entity_type )
207
- == last_signed_entity_type. as_ref ( )
208
- }
209
-
210
- if let Some ( new_epoch) = self . has_epoch_changed ( * epoch) . await ? {
201
+ } => match self . has_epoch_changed ( * epoch) . await ? {
202
+ Some ( new_epoch) => {
211
203
info ! ( "→ Epoch has changed, transiting to UNREGISTERED" ) ;
212
204
* state = self
213
205
. transition_from_ready_to_sign_to_unregistered ( new_epoch)
214
206
. await ?;
215
- } else {
216
- match self . runner . get_pending_certificate ( ) . await . map_err ( |e| {
217
- RuntimeError :: KeepState {
218
- message : "could not fetch the pending certificate" . to_string ( ) ,
219
- nested_error : Some ( e) ,
220
- }
221
- } ) ? {
222
- Some ( pending_certificate)
223
- if is_same_signed_entity_type (
224
- last_signed_entity_type,
225
- & pending_certificate,
226
- ) =>
227
- {
228
- info ! ( " ⋅ same entity type, cannot sign pending certificate, waiting…" ) ;
229
- }
230
- Some ( pending_certificate)
231
- if self
232
- . runner
233
- . can_sign_signed_entity_type (
234
- & pending_certificate. signed_entity_type ,
235
- )
236
- . await =>
237
- {
238
- info ! (
239
- " ⋅ Epoch has NOT changed but there is a pending certificate" ;
240
- "pending_certificate" => ?pending_certificate
241
- ) ;
242
- info ! ( " → we can sign this certificate, transiting to READY_TO_SIGN" ) ;
243
- * state = self
244
- . transition_from_ready_to_sign_to_ready_to_sign (
245
- * epoch,
246
- & pending_certificate,
247
- )
248
- . await ?;
249
- }
250
- Some ( pending_certificate) => {
251
- info ! (
252
- " ⋅ Epoch has NOT changed but there is a pending certificate" ;
253
- "pending_certificate" => ?pending_certificate
254
- ) ;
255
- info ! ( " ⋅ cannot sign this pending certificate, waiting…" ) ;
256
- }
257
- None => {
258
- info ! ( " ⋅ no pending certificate, waiting…" ) ;
259
- }
207
+ }
208
+ None => {
209
+ let pending_certificate =
210
+ self . runner . get_pending_certificate ( ) . await . map_err ( |e| {
211
+ RuntimeError :: KeepState {
212
+ message : "could not fetch the pending certificate" . to_string ( ) ,
213
+ nested_error : Some ( e) ,
214
+ }
215
+ } ) ?;
216
+ if let Some ( new_state) = self
217
+ . ready_to_sign_certificate_next_state (
218
+ pending_certificate,
219
+ last_signed_entity_type,
220
+ epoch,
221
+ )
222
+ . await ?
223
+ {
224
+ * state = new_state
260
225
}
261
226
}
262
- }
227
+ } ,
263
228
} ;
264
229
265
230
self . metrics_service
@@ -268,6 +233,59 @@ impl StateMachine {
268
233
Ok ( ( ) )
269
234
}
270
235
236
+ async fn ready_to_sign_certificate_next_state (
237
+ & self ,
238
+ certificate_pending : Option < CertificatePending > ,
239
+ last_signed_entity : & Option < SignedEntityType > ,
240
+ epoch : & Epoch ,
241
+ ) -> Result < Option < SignerState > , RuntimeError > {
242
+ fn is_same_signed_entity_type (
243
+ signed_entity_type : & Option < SignedEntityType > ,
244
+ certificate : & CertificatePending ,
245
+ ) -> bool {
246
+ Some ( & certificate. signed_entity_type ) == signed_entity_type. as_ref ( )
247
+ }
248
+
249
+ async fn can_sign_signed_entity_type (
250
+ s : & StateMachine ,
251
+ certificate : & CertificatePending ,
252
+ ) -> bool {
253
+ s. runner
254
+ . can_sign_signed_entity_type ( & certificate. signed_entity_type )
255
+ . await
256
+ }
257
+
258
+ match certificate_pending {
259
+ Some ( certificate) if is_same_signed_entity_type ( last_signed_entity, & certificate) => {
260
+ info ! ( " ⋅ same entity type, cannot sign pending certificate, waiting…" ) ;
261
+ Ok ( None )
262
+ }
263
+ Some ( certificate) if can_sign_signed_entity_type ( self , & certificate) . await => {
264
+ info ! (
265
+ " ⋅ Epoch has NOT changed but there is a pending certificate" ;
266
+ "pending_certificate" => ?certificate,
267
+ ) ;
268
+ info ! ( " → we can sign this certificate, transiting to READY_TO_SIGN" ) ;
269
+ Ok ( Some (
270
+ self . transition_from_ready_to_sign_to_ready_to_sign ( * epoch, & certificate)
271
+ . await ?,
272
+ ) )
273
+ }
274
+ Some ( certificate) => {
275
+ info ! (
276
+ " ⋅ Epoch has NOT changed but there is a pending certificate" ;
277
+ "pending_certificate" => ?certificate
278
+ ) ;
279
+ info ! ( " ⋅ cannot sign this pending certificate, waiting…" ) ;
280
+ Ok ( None )
281
+ }
282
+ None => {
283
+ info ! ( " ⋅ no pending certificate, waiting…" ) ;
284
+ Ok ( None )
285
+ }
286
+ }
287
+ }
288
+
271
289
/// Return the new epoch if the epoch is different than the given one.
272
290
async fn has_epoch_changed ( & self , epoch : Epoch ) -> Result < Option < Epoch > , RuntimeError > {
273
291
let current_time_point = self
0 commit comments