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
// This is free and unencumbered software released into the public domain.

use crate::{prelude::Bytes, StdioConfig, StdioError, StdioSystem, System};
use blake3::Hasher;
use protoflow_core::{Block, BlockResult, BlockRuntime, InputPort, OutputPort, Port, PortError};
use protoflow_derive::Block;
use simple_mermaid::mermaid;

/// Computes the cryptographic hash of a byte stream, while optionally
/// passing it through.
///
/// # Block Diagram
#[doc = mermaid!("../../doc/hash/hash.mmd")]
///
/// # Sequence Diagram
#[doc = mermaid!("../../doc/hash/hash.seq.mmd" framed)]
///
/// # Examples
///
/// ## Using the block in a system
///
/// ```rust
/// # use protoflow_blocks::*;
/// # fn main() {
/// System::build(|s| {
///     let stdin = s.read_stdin();
///     let hasher = s.hash_blake3();
///     let hex_encoder = s.encode_hex();
///     let stdout = s.write_stdout();
///     s.connect(&stdin.output, &hasher.input);
///     s.connect(&hasher.hash, &hex_encoder.input);
///     s.connect(&hex_encoder.output, &stdout.input);
/// });
/// # }
/// ```
///
/// ## Running the block via the CLI
///
/// ```console
/// $ protoflow execute Hash algorithm=blake3
/// ```
///
#[derive(Block, Clone)]
pub struct Hash {
    /// The input byte stream.
    #[input]
    pub input: InputPort<Bytes>,

    /// The (optional) output target for the stream being passed through.
    #[output]
    pub output: OutputPort<Bytes>,

    /// The output port for the computed hash.
    #[output]
    pub hash: OutputPort<Bytes>,

    /// A configuration parameter for which algorithm to use.
    #[parameter]
    pub algorithm: HashAlgorithm,

    /// The internal state for computing the hash.
    #[state]
    hasher: Hasher,
}

/// The cryptographic hash algorithm to use.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum HashAlgorithm {
    #[default]
    BLAKE3,
}

impl Hash {
    pub fn new(
        input: InputPort<Bytes>,
        output: OutputPort<Bytes>,
        hash: OutputPort<Bytes>,
    ) -> Self {
        Self::with_params(input, output, hash, HashAlgorithm::default())
    }

    pub fn with_params(
        input: InputPort<Bytes>,
        output: OutputPort<Bytes>,
        hash: OutputPort<Bytes>,
        algorithm: HashAlgorithm,
    ) -> Self {
        Self {
            input,
            output,
            hash,
            algorithm,
            hasher: Hasher::new(),
        }
    }
}

impl Block for Hash {
    fn execute(&mut self, runtime: &dyn BlockRuntime) -> BlockResult {
        while let Some(message) = self.input.recv()? {
            self.hasher.update(&message);

            if self.output.is_connected() {
                self.output.send(&message)?;
            } else {
                drop(message);
            }
        }
        self.output.close()?;

        runtime.wait_for(&self.hash)?;

        let hash = Bytes::from(self.hasher.finalize().as_bytes().to_vec());
        match self.hash.send(&hash) {
            Ok(()) => {}
            Err(PortError::Closed | PortError::Disconnected) => {
                // TODO: log the error
            }
            Err(e) => return Err(e)?,
        };

        Ok(())
    }
}

#[cfg(feature = "std")]
impl StdioSystem for Hash {
    fn build_system(_config: StdioConfig) -> Result<System, StdioError> {
        use crate::{HashBlocks, IoBlocks, SysBlocks, SystemBuilding};

        // TODO: parse the algorithm parameter

        Ok(System::build(|s| {
            let stdin = s.read_stdin();
            let hasher = s.hash_blake3();
            let hex_encoder = s.encode_hex();
            let stdout = s.write_stdout();
            s.connect(&stdin.output, &hasher.input);
            s.connect(&hasher.hash, &hex_encoder.input);
            s.connect(&hex_encoder.output, &stdout.input);
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::Hash;
    use crate::{System, SystemBuilding};

    #[test]
    fn instantiate_block() {
        // Check that the block is constructible:
        let _ = System::build(|s| {
            let _ = s.block(Hash::new(s.input(), s.output(), s.output()));
        });
    }
}