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
//! `zebra_scan::service::ScanService` request types.

use std::collections::HashSet;

use crate::BoxError;

/// The maximum number of keys that may be included in a request to the scan service
const MAX_REQUEST_KEYS: usize = 1000;

#[derive(Debug)]
/// Request types for `zebra_scan::service::ScanService`
pub enum Request {
    /// Requests general info about the scanner
    Info,

    /// Submits viewing keys with their optional birth-heights for scanning.
    RegisterKeys(Vec<(String, Option<u32>)>),

    /// Deletes viewing keys and their results from the database.
    DeleteKeys(Vec<String>),

    /// Accept keys and return transaction data
    Results(Vec<String>),

    /// Accept keys and return a channel receiver for transaction data
    SubscribeResults(HashSet<String>),

    /// Clear the results for a set of viewing keys
    ClearResults(Vec<String>),
}

impl Request {
    /// Check that the request data is valid for the request variant
    pub fn check(&self) -> Result<(), BoxError> {
        self.check_num_keys()?;

        Ok(())
    }

    /// Checks that requests which include keys have a valid number of keys.
    fn check_num_keys(&self) -> Result<(), BoxError> {
        match self {
            Request::DeleteKeys(keys) | Request::ClearResults(keys)
                if keys.is_empty() || keys.len() > MAX_REQUEST_KEYS =>
            {
                Err(format!("request must include between 1 and {MAX_REQUEST_KEYS} keys").into())
            }

            _ => Ok(()),
        }
    }
}

#[test]
fn test_check_num_keys() {
    let fake_keys: Vec<_> = std::iter::repeat(String::new())
        .take(MAX_REQUEST_KEYS + 1)
        .collect();

    let bad_requests = [
        Request::DeleteKeys(vec![]),
        Request::DeleteKeys(fake_keys.clone()),
        Request::ClearResults(vec![]),
        Request::ClearResults(fake_keys),
    ];

    let valid_requests = [
        Request::DeleteKeys(vec![String::new()]),
        Request::ClearResults(vec![String::new()]),
    ];

    for request in bad_requests {
        let error = request.check().expect_err("check should return an error");

        assert_eq!(
            format!("request must include between 1 and {MAX_REQUEST_KEYS} keys"),
            error.to_string(),
            "check_num_keys should return an error because there are too many keys"
        );
    }

    for request in valid_requests {
        request.check().expect("check should return Ok(())");
    }
}