mithril_aggregator/services/signable_builder/
signable_seed_builder.rs

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
//! ## AggregatorSignableSeedBuilder
//!
//! This service is responsible for computing the seed protocol message
//! that is used by the [SignableBuilder] to compute the final protocol message.
//!
use anyhow::Context;
use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::RwLock;

use mithril_common::{
    entities::ProtocolMessagePartValue, signable_builder::SignableSeedBuilder, StdResult,
};

use crate::services::EpochService;

/// SignableSeedBuilder aggregator implementation
pub struct AggregatorSignableSeedBuilder {
    epoch_service: Arc<RwLock<dyn EpochService>>,
}

impl AggregatorSignableSeedBuilder {
    /// AggregatorSignableSeedBuilder factory
    pub fn new(epoch_service: Arc<RwLock<dyn EpochService>>) -> Self {
        Self { epoch_service }
    }
}

#[async_trait]
impl SignableSeedBuilder for AggregatorSignableSeedBuilder {
    async fn compute_next_aggregate_verification_key(&self) -> StdResult<ProtocolMessagePartValue> {
        let epoch_service = self.epoch_service.read().await;
        let next_aggregate_verification_key = (*epoch_service)
            .next_aggregate_verification_key()?
            .to_json_hex()
            .with_context(|| "convert next avk to json hex failure")?
            .to_string();

        Ok(next_aggregate_verification_key)
    }

    async fn compute_next_protocol_parameters(&self) -> StdResult<ProtocolMessagePartValue> {
        let epoch_service = self.epoch_service.read().await;
        let next_protocol_parameters = epoch_service.next_protocol_parameters()?.compute_hash();

        Ok(next_protocol_parameters)
    }

    async fn compute_current_epoch(&self) -> StdResult<ProtocolMessagePartValue> {
        let epoch_service = self.epoch_service.read().await;
        let current_epoch = epoch_service.epoch_of_current_data()?.to_string();

        Ok(current_epoch)
    }
}

#[cfg(test)]
mod tests {
    use mithril_common::{
        entities::Epoch,
        test_utils::{MithrilFixture, MithrilFixtureBuilder},
    };

    use crate::{entities::AggregatorEpochSettings, services::FakeEpochServiceBuilder};

    use super::*;

    fn build_signable_builder_service(
        epoch: Epoch,
        fixture: &MithrilFixture,
        next_fixture: &MithrilFixture,
    ) -> AggregatorSignableSeedBuilder {
        let epoch_service = Arc::new(RwLock::new(
            FakeEpochServiceBuilder {
                current_epoch_settings: AggregatorEpochSettings {
                    protocol_parameters: fixture.protocol_parameters(),
                    ..AggregatorEpochSettings::dummy()
                },
                next_epoch_settings: AggregatorEpochSettings {
                    protocol_parameters: next_fixture.protocol_parameters(),
                    ..AggregatorEpochSettings::dummy()
                },
                signer_registration_epoch_settings: AggregatorEpochSettings {
                    protocol_parameters: next_fixture.protocol_parameters(),
                    ..AggregatorEpochSettings::dummy()
                },
                current_signers_with_stake: fixture.signers_with_stake(),
                next_signers_with_stake: next_fixture.signers_with_stake(),
                ..FakeEpochServiceBuilder::dummy(epoch)
            }
            .build(),
        ));

        AggregatorSignableSeedBuilder::new(epoch_service)
    }

    #[tokio::test]
    async fn test_compute_next_aggregate_verification_key_protocol_message_value() {
        let epoch = Epoch(5);
        let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
        let next_fixture = MithrilFixtureBuilder::default().with_signers(4).build();
        let signable_seed_builder = build_signable_builder_service(epoch, &fixture, &next_fixture);
        let expected_next_aggregate_verification_key = next_fixture.compute_and_encode_avk();

        let next_aggregate_verification_key = signable_seed_builder
            .compute_next_aggregate_verification_key()
            .await
            .unwrap();

        assert_eq!(
            next_aggregate_verification_key,
            expected_next_aggregate_verification_key
        );
    }

    #[tokio::test]
    async fn test_compute_next_protocol_parameters_protocol_message_value() {
        let epoch = Epoch(5);
        let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
        let next_fixture = MithrilFixtureBuilder::default().with_signers(4).build();
        let signable_seed_builder = build_signable_builder_service(epoch, &fixture, &next_fixture);
        let expected_next_protocol_parameters = next_fixture.protocol_parameters().compute_hash();

        let next_protocol_parameters = signable_seed_builder
            .compute_next_protocol_parameters()
            .await
            .unwrap();

        assert_eq!(next_protocol_parameters, expected_next_protocol_parameters);
    }

    #[tokio::test]
    async fn test_compute_current_epoch_protocol_message_value() {
        let epoch = Epoch(5);
        let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
        let next_fixture = MithrilFixtureBuilder::default().with_signers(4).build();
        let signable_seed_builder = build_signable_builder_service(epoch, &fixture, &next_fixture);
        let expected_current_epoch = epoch.to_string();

        let current_epoch = signable_seed_builder.compute_current_epoch().await.unwrap();

        assert_eq!(current_epoch, expected_current_epoch);
    }
}