1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! The Tanssi AuRa consensus algorithm for orchestrator chain and container chain collators.    
18
//!
19
//! It calculates based on the orchestrator-state dictated authorities
20
//! It is identical to AuraWorker and AuraConsensus, except for the fact that we re-implement
21
//! the ParachainConsensus trait to access the orchestrator-dicated authorities, and further
22
//! it implements the TanssiWorker to TanssiOnSlot trait. This trait is
23
use {
24
    crate::{AuthorityId, Pair},
25
    sp_runtime::traits::Block as BlockT,
26
    tp_traits::SlotFrequency,
27
};
28

            
29
#[async_trait::async_trait]
30
pub trait RetrieveAuthoritiesFromOrchestrator<Block: BlockT, ExtraArgs, A>: Send + Sync {
31
    /// Create the inherent data providers at the given `parent` block using the given `extra_args`.
32
    async fn retrieve_authorities_from_orchestrator(
33
        &self,
34
        parent: <Block as BlockT>::Hash,
35
        extra_args: ExtraArgs,
36
    ) -> Result<A, Box<dyn std::error::Error + Send + Sync>>;
37
}
38

            
39
#[async_trait::async_trait]
40
impl<F, Block, ExtraArgs, Fut, A> RetrieveAuthoritiesFromOrchestrator<Block, ExtraArgs, A> for F
41
where
42
    Block: BlockT,
43
    F: Fn(Block::Hash, ExtraArgs) -> Fut + Sync + Send,
44
    Fut: std::future::Future<Output = Result<A, Box<dyn std::error::Error + Send + Sync>>>
45
        + Send
46
        + 'static,
47
    ExtraArgs: Send + 'static,
48
{
49
    async fn retrieve_authorities_from_orchestrator(
50
        &self,
51
        parent: <Block as BlockT>::Hash,
52
        extra_args: ExtraArgs,
53
    ) -> Result<A, Box<dyn std::error::Error + Send + Sync>> {
54
        (*self)(parent, extra_args).await
55
    }
56
}
57

            
58
pub struct OrchestratorAuraWorkerAuxData<P>
59
where
60
    P: Pair + Send + Sync + 'static,
61
{
62
    pub authorities: Vec<AuthorityId<P>>,
63
    pub slot_freq: Option<SlotFrequency>,
64
}