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
use std::sync::Arc;

use anyhow::{anyhow, Result};
use tokio::sync::{mpsc, oneshot};

use crate::{
    core::{
        common::logger::get_logger,
        consensus::{roothash, verifier::Verifier},
        dispatcher::PreInitState,
        host::{self, Host as _},
        identity::Identity,
        protocol::Protocol,
    },
    crypto::signature::{secp256k1, Signer},
};
use rand::rngs::OsRng;

use super::{init, notifier, registration, App, Environment};

/// Size of the processor command queue.
const CMDQ_BACKLOG: usize = 32;

/// Command sent to the processor task.
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub(super) enum Command {
    /// Process a notification of a new runtime block.
    ProcessRuntimeBlock(roothash::AnnotatedBlock),
    /// Retrieve the latest known round.
    GetLatestRound(oneshot::Sender<u64>),
    /// Notification that initial registration has been completed.
    InitialRegistrationCompleted,
}

/// Processor state.
pub(super) struct State<A: App> {
    pub(super) identity: Arc<Identity>,
    pub(super) host: Arc<Protocol>,
    pub(super) consensus_verifier: Arc<dyn Verifier>,
    pub(super) signer: Arc<dyn Signer>,
    pub(super) app: Arc<A>,
}

struct Tasks<A: App> {
    registration: registration::Task<A>,
    notifier: notifier::Task<A>,
}

/// Processor.
pub(super) struct Processor<A: App> {
    state: Arc<State<A>>,
    env: Environment<A>,
    tasks: Tasks<A>,
    cmdq: mpsc::Receiver<Command>,
    logger: slog::Logger,

    latest_round: u64,
}

impl<A> Processor<A>
where
    A: App,
{
    /// Create and start a new processor.
    pub(super) fn start(app: A, state: &PreInitState<'_>) -> mpsc::Sender<Command> {
        // Create the command channel.
        let (tx, rx) = mpsc::channel(CMDQ_BACKLOG);

        // Provision keys. Currently we provision a random key for signing transactions to avoid
        // using the RAK directly as the RAK is an Ed25519 key which cannot easily be used for EVM
        // calls due to the limitations of the current implementation.
        let signer = secp256k1::MemorySigner::random(&mut OsRng).unwrap();

        // Prepare state.
        let state = Arc::new(State {
            identity: state.identity.clone(),
            host: state.protocol.clone(),
            consensus_verifier: state.consensus_verifier.clone(),
            signer: Arc::new(signer),
            app: Arc::new(app),
        });

        // Prepare application environment.
        let env = Environment::new(state.clone(), tx.downgrade());

        // Create the processor and start it.
        let processor = Self {
            tasks: Tasks {
                registration: registration::Task::new(state.clone(), env.clone()),
                notifier: notifier::Task::new(state.clone(), env.clone()),
            },
            state,
            env,
            cmdq: rx,
            logger: get_logger("modules/rofl/app"),
            latest_round: 0,
        };
        tokio::spawn(processor.run());

        tx
    }

    /// Run the processor.
    async fn run(mut self) {
        slog::info!(self.logger, "starting processor";
            "app_id" => A::id(),
        );

        // Register for notifications.
        if let Err(err) = self
            .state
            .host
            .register_notify(host::RegisterNotifyOpts {
                runtime_block: true,
                runtime_event: vec![],
            })
            .await
        {
            slog::error!(self.logger, "failed to register for notifications";
                "err" => ?err,
            );
        }

        // Start the tasks.
        self.tasks.registration.start();
        self.tasks.notifier.start();

        slog::info!(self.logger, "entering processor loop");
        while let Some(cmd) = self.cmdq.recv().await {
            if let Err(err) = self.process(cmd).await {
                slog::error!(self.logger, "failed to process command";
                    "err" => ?err,
                );
            }
        }

        slog::info!(self.logger, "processor stopped");
    }

    /// Process a command.
    async fn process(&mut self, cmd: Command) -> Result<()> {
        match cmd {
            Command::ProcessRuntimeBlock(blk) => self.cmd_process_runtime_block(blk).await,
            Command::GetLatestRound(ch) => self.cmd_get_latest_round(ch).await,
            Command::InitialRegistrationCompleted => {
                self.cmd_initial_registration_completed().await
            }
        }
    }

    async fn cmd_process_runtime_block(&mut self, blk: roothash::AnnotatedBlock) -> Result<()> {
        // Update latest known round.
        if blk.block.header.round <= self.latest_round {
            return Err(anyhow!("round seems to have moved backwards"));
        }
        self.latest_round = blk.block.header.round;

        // Notify registration task.
        self.tasks.registration.refresh();
        // Notify notifier task.
        let _ = self
            .tasks
            .notifier
            .notify(notifier::Notify::RuntimeBlock(self.latest_round))
            .await;

        Ok(())
    }

    async fn cmd_get_latest_round(&self, ch: oneshot::Sender<u64>) -> Result<()> {
        let _ = ch.send(self.latest_round);
        Ok(())
    }

    async fn cmd_initial_registration_completed(&self) -> Result<()> {
        slog::info!(self.logger, "initial registration completed");

        // Start application after first registration.
        slog::info!(self.logger, "starting application");
        tokio::spawn(self.state.app.clone().run(self.env.clone()));

        // Perform post-registration initialization.
        slog::info!(
            self.logger,
            "performing additional post-registration initialization"
        );
        init::post_registration_init();

        // Notify notifier task.
        self.tasks
            .notifier
            .notify(notifier::Notify::InitialRegistrationCompleted)
            .await?;

        Ok(())
    }
}