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
use legacy_aleph_bft::{default_config, Config, LocalIO, Terminator};
use log::debug;
use network_clique::SpawnHandleT;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block, Header};

mod network;
mod traits;

pub use network::NetworkData;

use super::common::{sanity_check_round_delays, unit_creation_delay_fn, MAX_ROUNDS};
pub use crate::phron_primitives::{BlockHash, BlockNumber, LEGACY_FINALITY_VERSION as VERSION};
use crate::{
    abft::NetworkWrapper,
    data_io::{
        legacy::{PhronData, OrderedDataInterpreter},
        SubstrateChainInfoProvider,
    },
    network::data::Network,
    oneshot,
    party::{
        backup::ABFTBackup,
        manager::{Task, TaskCommon},
    },
    Keychain, LegacyNetworkData, NodeIndex, SessionId, UnitCreationDelay,
};

pub fn run_member<B, C, ADN>(
    subtask_common: TaskCommon,
    multikeychain: Keychain,
    config: Config,
    network: NetworkWrapper<LegacyNetworkData, ADN>,
    data_provider: impl legacy_aleph_bft::DataProvider<PhronData> + Send + 'static,
    ordered_data_interpreter: OrderedDataInterpreter<SubstrateChainInfoProvider<B, C>>,
    backup: ABFTBackup,
) -> Task
where
    B: Block<Hash = BlockHash>,
    B::Header: Header<Number = BlockNumber>,
    C: HeaderBackend<B> + Send + 'static,
    ADN: Network<LegacyNetworkData> + 'static,
{
    // Remove this check once we implement one on the PhronBFT side (A0-2583).
    // Checks that the total time of a session is at least 7 days.
    sanity_check_round_delays(
        config.max_round,
        config.delay_config.unit_creation_delay.clone(),
    );
    let TaskCommon {
        spawn_handle,
        session_id,
    } = subtask_common;
    let (stop, exit) = oneshot::channel();
    let member_terminator = Terminator::create_root(exit, "member");
    let local_io = LocalIO::new(data_provider, ordered_data_interpreter, backup.0, backup.1);

    let task = {
        let spawn_handle = spawn_handle.clone();
        async move {
            debug!(target: "phron-party", "Running the member task for {:?}", session_id);
            legacy_aleph_bft::run_session(
                config,
                local_io,
                network,
                multikeychain,
                spawn_handle,
                member_terminator,
            )
            .await;
            debug!(target: "phron-party", "Member task stopped for {:?}", session_id);
        }
    };

    let handle = spawn_handle.spawn_essential("phron/consensus_session_member", task);
    Task::new(handle, stop)
}

pub fn create_phron_config(
    n_members: usize,
    node_id: NodeIndex,
    session_id: SessionId,
    unit_creation_delay: UnitCreationDelay,
) -> Config {
    let mut config = default_config(n_members.into(), node_id.into(), session_id.0 as u64);
    config.delay_config.unit_creation_delay = unit_creation_delay_fn(unit_creation_delay);
    config.max_round = MAX_ROUNDS;
    config
}