tendermint_testgen/
helpers.rs

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
//! Helper functions

use std::io::{self, Read};

use serde::de::DeserializeOwned;
use simple_error::*;
use tendermint::{chain, public_key, signature::Signature, vote, Time};

/// A macro that generates a complete setter method from a one-liner with necessary information
#[macro_export]
macro_rules! set_option {
    ($name:ident, $t:ty) => {
        pub fn $name(mut self, $name: $t) -> Self {
            self.$name = Some($name.clone());
            self
        }
    };
    ($name:ident, $t:ty, $val:expr) => {
        pub fn $name(mut self, $name: $t) -> Self {
            self.$name = $val;
            self
        }
    };
}

/// Tries to parse a string as the given type; otherwise returns the input wrapped in SimpleError
pub fn parse_as<T: DeserializeOwned>(input: &str) -> Result<T, SimpleError> {
    match serde_json::from_str(input) {
        Ok(res) => Ok(res),
        Err(e) => Err(SimpleError::new(e.to_string())),
    }
}

pub fn read_stdin() -> Result<String, SimpleError> {
    let mut buffer = String::new();
    match io::stdin().read_to_string(&mut buffer) {
        Ok(_) => Ok(buffer),
        Err(e) => Err(SimpleError::new(e.to_string())),
    }
}

pub fn get_vote_sign_bytes(chain_id: chain::Id, vote: &vote::Vote) -> Vec<u8> {
    let signed_vote = vote::SignedVote::from_vote(vote.clone(), chain_id).unwrap();

    signed_vote.sign_bytes()
}

pub fn verify_signature(pubkey: &public_key::Ed25519, msg: &[u8], signature: &Signature) -> bool {
    let verifier = ed25519_consensus::VerificationKey::try_from(pubkey.as_bytes()).unwrap();
    let sig = ed25519_consensus::Signature::try_from(signature.as_bytes()).unwrap();
    verifier.verify(&sig, msg).is_ok()
}

pub fn get_time(abs: u64) -> Result<Time, SimpleError> {
    let res =
        Time::from_unix_timestamp(abs as i64, 0).map_err(|e| SimpleError::new(e.to_string()))?;

    Ok(res)
}