tendermint_testgen/
helpers.rs1use std::io::{self, Read};
4
5use serde::de::DeserializeOwned;
6use simple_error::*;
7use tendermint::{chain, public_key, signature::Signature, vote, Time};
8
9#[macro_export]
11macro_rules! set_option {
12 ($name:ident, $t:ty) => {
13 pub fn $name(mut self, $name: $t) -> Self {
14 self.$name = Some($name.clone());
15 self
16 }
17 };
18 ($name:ident, $t:ty, $val:expr) => {
19 pub fn $name(mut self, $name: $t) -> Self {
20 self.$name = $val;
21 self
22 }
23 };
24}
25
26pub fn parse_as<T: DeserializeOwned>(input: &str) -> Result<T, SimpleError> {
28 match serde_json::from_str(input) {
29 Ok(res) => Ok(res),
30 Err(e) => Err(SimpleError::new(e.to_string())),
31 }
32}
33
34pub fn read_stdin() -> Result<String, SimpleError> {
35 let mut buffer = String::new();
36 match io::stdin().read_to_string(&mut buffer) {
37 Ok(_) => Ok(buffer),
38 Err(e) => Err(SimpleError::new(e.to_string())),
39 }
40}
41
42pub fn get_vote_sign_bytes(chain_id: chain::Id, vote: &vote::Vote) -> Vec<u8> {
43 let signed_vote = vote::SignedVote::from_vote(vote.clone(), chain_id).unwrap();
44
45 signed_vote.sign_bytes()
46}
47
48pub fn verify_signature(pubkey: &public_key::Ed25519, msg: &[u8], signature: &Signature) -> bool {
49 let verifier = ed25519_consensus::VerificationKey::try_from(pubkey.as_bytes()).unwrap();
50 let sig = ed25519_consensus::Signature::try_from(signature.as_bytes()).unwrap();
51 verifier.verify(&sig, msg).is_ok()
52}
53
54pub fn get_time(abs: u64) -> Result<Time, SimpleError> {
55 let res =
56 Time::from_unix_timestamp(abs as i64, 0).map_err(|e| SimpleError::new(e.to_string()))?;
57
58 Ok(res)
59}