1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use async_trait::async_trait;
use slog::{debug, trace, warn, Logger};
use std::sync::Arc;

use mithril_common::entities::{
    Certificate, Epoch, ProtocolMessage, SignedEntityType, SignedEntityTypeDiscriminants,
    SingleSignatures,
};
use mithril_common::logging::LoggerExtensions;
use mithril_common::StdResult;

use crate::entities::OpenMessage;
use crate::services::{
    BufferedSingleSignatureStore, CertifierService, CertifierServiceError,
    SignatureRegistrationStatus,
};

/// A decorator of [CertifierService] that can buffer registration of single signatures
/// when the open message is not yet created.
///
/// When an open message is created, buffered single signatures for the open message type are
/// registered.
pub struct BufferedCertifierService {
    certifier_service: Arc<dyn CertifierService>,
    buffered_single_signature_store: Arc<dyn BufferedSingleSignatureStore>,
    logger: Logger,
}

impl BufferedCertifierService {
    /// Create a new instance of `BufferedCertifierService`.
    pub fn new(
        certifier_service: Arc<dyn CertifierService>,
        buffered_single_signature_store: Arc<dyn BufferedSingleSignatureStore>,
        logger: Logger,
    ) -> Self {
        Self {
            certifier_service,
            buffered_single_signature_store,
            logger: logger.new_with_component_name::<Self>(),
        }
    }

    async fn try_register_buffered_signatures_to_current_open_message(
        &self,
        signed_entity_type: &SignedEntityType,
    ) -> StdResult<()> {
        let discriminant: SignedEntityTypeDiscriminants = signed_entity_type.into();
        let buffered_signatures = self
            .buffered_single_signature_store
            .get_buffered_signatures(discriminant)
            .await?;
        let mut signatures_to_remove = vec![];

        for signature in buffered_signatures {
            match self
                .certifier_service
                .register_single_signature(signed_entity_type, &signature)
                .await
            {
                Ok(..) => {
                    signatures_to_remove.push(signature);
                }
                Err(error) => match error.downcast_ref::<CertifierServiceError>() {
                    Some(CertifierServiceError::InvalidSingleSignature(..)) => {
                        trace!(self.logger, "Skipping invalid signature for signed entity '{signed_entity_type:?}'";
                            "party_id" => &signature.party_id,
                            "error" => ?error,
                        );
                    }
                    _ => {
                        anyhow::bail!(error);
                    }
                },
            }
        }

        self.buffered_single_signature_store
            .remove_buffered_signatures(discriminant, signatures_to_remove)
            .await?;

        Ok(())
    }
}

#[async_trait]
impl CertifierService for BufferedCertifierService {
    async fn inform_epoch(&self, epoch: Epoch) -> StdResult<()> {
        self.certifier_service.inform_epoch(epoch).await
    }

    async fn register_single_signature(
        &self,
        signed_entity_type: &SignedEntityType,
        signature: &SingleSignatures,
    ) -> StdResult<SignatureRegistrationStatus> {
        match self
            .certifier_service
            .register_single_signature(signed_entity_type, signature)
            .await
        {
            Ok(res) => Ok(res),
            Err(error) => match error.downcast_ref::<CertifierServiceError>() {
                Some(CertifierServiceError::NotFound(..)) if signature.is_authenticated() => {
                    debug!(
                        self.logger, "No OpenMessage available for signed entity - Buffering single signature";
                        "signed_entity_type" => ?signed_entity_type,
                        "party_id" => &signature.party_id
                    );

                    self.buffered_single_signature_store
                        .buffer_signature(signed_entity_type.into(), signature)
                        .await?;

                    Ok(SignatureRegistrationStatus::Buffered)
                }
                _ => Err(error),
            },
        }
    }

    async fn create_open_message(
        &self,
        signed_entity_type: &SignedEntityType,
        protocol_message: &ProtocolMessage,
    ) -> StdResult<OpenMessage> {
        // IMPORTANT: this method should not fail if the open message creation succeeds
        // Otherwise, the state machine won't aggregate signatures for this open message.

        let creation_result = self
            .certifier_service
            .create_open_message(signed_entity_type, protocol_message)
            .await;

        if creation_result.is_ok() {
            if let Err(error) = self
                .try_register_buffered_signatures_to_current_open_message(signed_entity_type)
                .await
            {
                warn!(self.logger, "Failed to register buffered signatures to the new open message";
                    "signed_entity_type" => ?signed_entity_type,
                    "error" => ?error
                );
            }
        }

        creation_result
    }

    async fn get_open_message(
        &self,
        signed_entity_type: &SignedEntityType,
    ) -> StdResult<Option<OpenMessage>> {
        self.certifier_service
            .get_open_message(signed_entity_type)
            .await
    }

    async fn mark_open_message_if_expired(
        &self,
        signed_entity_type: &SignedEntityType,
    ) -> StdResult<Option<OpenMessage>> {
        self.certifier_service
            .mark_open_message_if_expired(signed_entity_type)
            .await
    }

    async fn create_certificate(
        &self,
        signed_entity_type: &SignedEntityType,
    ) -> StdResult<Option<Certificate>> {
        self.certifier_service
            .create_certificate(signed_entity_type)
            .await
    }

    async fn get_certificate_by_hash(&self, hash: &str) -> StdResult<Option<Certificate>> {
        self.certifier_service.get_certificate_by_hash(hash).await
    }

    async fn get_latest_certificates(&self, last_n: usize) -> StdResult<Vec<Certificate>> {
        self.certifier_service.get_latest_certificates(last_n).await
    }

    async fn verify_certificate_chain(&self, epoch: Epoch) -> StdResult<()> {
        self.certifier_service.verify_certificate_chain(epoch).await
    }
}

#[cfg(test)]
mod tests {
    use anyhow::anyhow;
    use mockall::predicate::eq;

    use mithril_common::entities::SignedEntityTypeDiscriminants::{
        CardanoTransactions, MithrilStakeDistribution,
    };
    use mithril_common::entities::SingleSignatureAuthenticationStatus;
    use mithril_common::test_utils::fake_data;

    use crate::database::repository::BufferedSingleSignatureRepository;
    use crate::database::test_helper::main_db_connection;
    use crate::services::{
        CertifierServiceError, MockBufferedSingleSignatureStore, MockCertifierService,
    };
    use crate::test_tools::TestLogger;

    use super::*;

    fn mock_certifier(
        certifier_mock_config: impl FnOnce(&mut MockCertifierService),
    ) -> Arc<MockCertifierService> {
        let mut certifier = MockCertifierService::new();
        certifier_mock_config(&mut certifier);
        Arc::new(certifier)
    }

    fn mock_store<F>(store_mock_config: F) -> Arc<MockBufferedSingleSignatureStore>
    where
        F: FnOnce(&mut MockBufferedSingleSignatureStore),
    {
        let mut store = MockBufferedSingleSignatureStore::new();
        store_mock_config(&mut store);
        Arc::new(store)
    }

    /// Run a scenario where we try to register a signature (using a fixed signed entity type).
    ///
    /// Return the registration result and the list of buffered signatures after the registration.
    async fn run_register_signature_scenario(
        decorated_certifier_mock_config: impl FnOnce(&mut MockCertifierService),
        signature_to_register: &SingleSignatures,
    ) -> (
        StdResult<SignatureRegistrationStatus>,
        Vec<SingleSignatures>,
    ) {
        let store = Arc::new(BufferedSingleSignatureRepository::new(Arc::new(
            main_db_connection().unwrap(),
        )));
        let certifier = BufferedCertifierService::new(
            mock_certifier(decorated_certifier_mock_config),
            store.clone(),
            TestLogger::stdout(),
        );

        let registration_result = certifier
            .register_single_signature(
                &SignedEntityType::MithrilStakeDistribution(Epoch(5)),
                signature_to_register,
            )
            .await;

        let buffered_signatures = store
            .get_buffered_signatures(MithrilStakeDistribution)
            .await
            .unwrap();

        (registration_result, buffered_signatures)
    }

    #[tokio::test]
    async fn when_registering_single_signature_dont_buffer_signature_if_decorated_certifier_succeed(
    ) {
        let (registration_result, buffered_signatures_after_registration) =
            run_register_signature_scenario(
                |mock_certifier| {
                    mock_certifier
                        .expect_register_single_signature()
                        .returning(|_, _| Ok(SignatureRegistrationStatus::Registered));
                },
                &SingleSignatures::fake("party_1", "a message"),
            )
            .await;

        let status = registration_result.expect("Registration should have succeed");
        assert_eq!(status, SignatureRegistrationStatus::Registered);
        assert_eq!(
            buffered_signatures_after_registration,
            Vec::<SingleSignatures>::new()
        );
    }

    mod when_registering_single_signature_if_decorated_certifier_as_no_opened_message {
        use super::*;

        #[tokio::test]
        async fn buffer_signature_if_authenticated() {
            let (registration_result, buffered_signatures_after_registration) =
                run_register_signature_scenario(
                    |mock_certifier| {
                        mock_certifier
                            .expect_register_single_signature()
                            .returning(|_, _| {
                                Err(CertifierServiceError::NotFound(
                                    SignedEntityType::MithrilStakeDistribution(Epoch(5)),
                                )
                                .into())
                            });
                    },
                    &SingleSignatures {
                        authentication_status: SingleSignatureAuthenticationStatus::Authenticated,
                        ..SingleSignatures::fake("party_1", "a message")
                    },
                )
                .await;

            let status = registration_result.expect("Registration should have succeed");
            assert_eq!(status, SignatureRegistrationStatus::Buffered);
            assert_eq!(
                buffered_signatures_after_registration,
                vec![SingleSignatures::fake("party_1", "a message")]
            );
        }

        #[tokio::test]
        async fn dont_buffer_signature_if_not_authenticated() {
            let (registration_result, buffered_signatures_after_registration) =
                run_register_signature_scenario(
                    |mock_certifier| {
                        mock_certifier
                            .expect_register_single_signature()
                            .returning(|_, _| {
                                Err(CertifierServiceError::NotFound(
                                    SignedEntityType::MithrilStakeDistribution(Epoch(5)),
                                )
                                .into())
                            });
                    },
                    &SingleSignatures {
                        authentication_status: SingleSignatureAuthenticationStatus::Unauthenticated,
                        ..SingleSignatures::fake("party_1", "a message")
                    },
                )
                .await;

            registration_result.expect_err("Registration should have failed");
            assert_eq!(
                buffered_signatures_after_registration,
                Vec::<SingleSignatures>::new()
            );
        }
    }

    #[tokio::test]
    async fn buffered_signatures_are_moved_to_newly_opened_message() {
        let store = Arc::new(BufferedSingleSignatureRepository::new(Arc::new(
            main_db_connection().unwrap(),
        )));
        for (signed_type, signature) in [
            (
                MithrilStakeDistribution,
                SingleSignatures::fake("party_1", "message 1"),
            ),
            (
                MithrilStakeDistribution,
                SingleSignatures::fake("party_2", "message 2"),
            ),
            (
                CardanoTransactions,
                SingleSignatures::fake("party_3", "message 3"),
            ),
        ] {
            store
                .buffer_signature(signed_type, &signature)
                .await
                .unwrap();
        }

        let certifier = BufferedCertifierService::new(
            mock_certifier(|mock| {
                mock.expect_create_open_message()
                    .returning(|_, _| Ok(OpenMessage::dummy()));

                // Those configuration Asserts that the buffered signatures are registered
                mock.expect_register_single_signature()
                    .with(
                        eq(SignedEntityType::MithrilStakeDistribution(Epoch(5))),
                        eq(SingleSignatures::fake("party_1", "message 1")),
                    )
                    .once()
                    .returning(|_, _| Ok(SignatureRegistrationStatus::Registered));
                mock.expect_register_single_signature()
                    .with(
                        eq(SignedEntityType::MithrilStakeDistribution(Epoch(5))),
                        eq(SingleSignatures::fake("party_2", "message 2")),
                    )
                    .once()
                    .returning(|_, _| Ok(SignatureRegistrationStatus::Registered));
            }),
            store.clone(),
            TestLogger::stdout(),
        );

        certifier
            .create_open_message(
                &SignedEntityType::MithrilStakeDistribution(Epoch(5)),
                &ProtocolMessage::new(),
            )
            .await
            .unwrap();

        let remaining_sigs = store
            .get_buffered_signatures(MithrilStakeDistribution)
            .await
            .unwrap();
        assert!(remaining_sigs.is_empty());
    }

    mod when_failing_to_transfer_buffered_signature_to_new_open_message {
        use mockall::predicate::always;

        use super::*;

        async fn run_scenario(
            certifier_mock_config: impl FnOnce(&mut MockCertifierService),
            store_mock_config: impl FnOnce(&mut MockBufferedSingleSignatureStore),
        ) {
            let store = mock_store(store_mock_config);
            let certifier = BufferedCertifierService::new(
                mock_certifier(certifier_mock_config),
                store,
                TestLogger::stdout(),
            );

            certifier
                .create_open_message(
                    &SignedEntityType::MithrilStakeDistribution(Epoch(5)),
                    &ProtocolMessage::new(),
                )
                .await
                .expect("Transferring buffered signatures to new open message should not fail");
        }

        #[tokio::test]
        async fn skip_invalid_signatures() {
            run_scenario(
                |mock| {
                    mock.expect_create_open_message()
                        .returning(|_, _| Ok(OpenMessage::dummy()));

                    mock.expect_register_single_signature()
                        .with(always(), eq(fake_data::single_signatures(vec![1])))
                        .returning(|_, _| Ok(SignatureRegistrationStatus::Registered))
                        .once();
                    mock.expect_register_single_signature()
                        .with(always(), eq(fake_data::single_signatures(vec![2])))
                        .returning(|_, _| {
                            Err(CertifierServiceError::InvalidSingleSignature(
                                OpenMessage::dummy().signed_entity_type,
                                anyhow!("Invalid signature"),
                            )
                            .into())
                        })
                        .once();
                    mock.expect_register_single_signature()
                        .with(always(), eq(fake_data::single_signatures(vec![3])))
                        .returning(|_, _| Ok(SignatureRegistrationStatus::Registered))
                        .once();
                },
                |mock| {
                    mock.expect_get_buffered_signatures().returning(|_| {
                        Ok(vec![
                            fake_data::single_signatures(vec![1]),
                            fake_data::single_signatures(vec![2]),
                            fake_data::single_signatures(vec![3]),
                        ])
                    });
                    mock.expect_remove_buffered_signatures()
                        // Only non-skipped signatures should be removed
                        .withf(|_, sig_to_remove| sig_to_remove.len() == 2)
                        .returning(|_, _| Ok(()));
                },
            )
            .await;
        }

        #[tokio::test]
        async fn do_not_return_an_error_if_getting_buffer_signatures_fail() {
            run_scenario(
                |mock| {
                    mock.expect_create_open_message()
                        .returning(|_, _| Ok(OpenMessage::dummy()));
                    mock.expect_register_single_signature()
                        .returning(|_, _| Ok(SignatureRegistrationStatus::Registered));
                },
                |mock| {
                    mock.expect_get_buffered_signatures()
                        .returning(|_| Err(anyhow!("get_buffered_signatures error")));
                },
            )
            .await;
        }

        #[tokio::test]
        async fn do_not_return_an_error_if_registering_signature_fail() {
            run_scenario(
                |mock| {
                    mock.expect_create_open_message()
                        .returning(|_, _| Ok(OpenMessage::dummy()));
                    mock.expect_register_single_signature()
                        .returning(|_, _| Err(anyhow!("register_single_signature error")));
                },
                |mock| {
                    mock.expect_get_buffered_signatures()
                        .returning(|_| Ok(vec![fake_data::single_signatures(vec![1])]));
                },
            )
            .await;
        }

        #[tokio::test]
        async fn do_not_return_an_error_if_removing_buffered_signatures_fail() {
            run_scenario(
                |mock| {
                    mock.expect_create_open_message()
                        .returning(|_, _| Ok(OpenMessage::dummy()));
                    mock.expect_register_single_signature()
                        .returning(|_, _| Ok(SignatureRegistrationStatus::Registered));
                },
                |mock| {
                    mock.expect_get_buffered_signatures()
                        .returning(|_| Ok(vec![fake_data::single_signatures(vec![1])]));
                    mock.expect_remove_buffered_signatures()
                        .returning(|_, _| Err(anyhow!("remove_buffered_signatures error")));
                },
            )
            .await;
        }
    }
}