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
// Copyright (c) 2021, Facebook, Inc. and its affiliates
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use config::WorkerId;
use fastcrypto::Hash;
use store::Store;
use tokio::{sync::watch, task::JoinHandle};
use types::{
    error::DagError,
    metered_channel::{Receiver, Sender},
    Batch, BatchDigest, ReconfigureNotification, WorkerPrimaryMessage,
};

#[cfg(test)]
#[path = "tests/processor_tests.rs"]
pub mod processor_tests;

/// Hashes and stores batches, it then outputs the batch's digest.
pub struct Processor;

impl Processor {
    #[must_use]
    pub fn spawn(
        // Our worker's id.
        id: WorkerId,
        // The persistent storage.
        store: Store<BatchDigest, Batch>,
        // Receive reconfiguration signals.
        mut rx_reconfigure: watch::Receiver<ReconfigureNotification>,
        // Input channel to receive batches.
        mut rx_batch: Receiver<Batch>,
        // Output channel to send out batches' digests.
        tx_digest: Sender<WorkerPrimaryMessage>,
        // Whether we are processing our own batches or the batches of other nodes.
        own_digest: bool,
    ) -> JoinHandle<()> {
        tokio::spawn(async move {
            loop {
                tokio::select! {
                    Some(batch) = rx_batch.recv() => {
                        // Hash the batch.
                        let digest = batch.digest();

                        // Store the batch.
                        store.write(digest, batch).await;

                        // Deliver the batch's digest.
                        let message = match own_digest {
                            true => WorkerPrimaryMessage::OurBatch(digest, id),
                            false => WorkerPrimaryMessage::OthersBatch(digest, id),
                        };
                        if tx_digest
                            .send(message)
                            .await
                            .is_err() {
                            tracing::debug!("{}", DagError::ShuttingDown);
                        };
                    },

                    // Trigger reconfigure.
                    result = rx_reconfigure.changed() => {
                        result.expect("Committee channel dropped");
                        let message = rx_reconfigure.borrow().clone();
                        if let ReconfigureNotification::Shutdown = message {
                            return;
                        }
                    }
                }
            }
        })
    }
}