pub use self::{
signed::{IntoMessage, Verified},
types::*,
};
use crate::crypto::{PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH};
mod signed;
mod types;
#[doc(hidden)]
pub const SIGNED_MESSAGE_MIN_SIZE: usize = PUBLIC_KEY_LENGTH + SIGNATURE_LENGTH + 8;
#[cfg(test)]
mod tests {
use chrono::Utc;
use exonum_crypto::{self as crypto, KeyPair};
use exonum_merkledb::BinaryValue;
use exonum_proto::ProtobufConvert;
use protobuf::Message;
use super::*;
use crate::{
helpers::{Height, Round, ValidatorId},
proto::schema::messages as proto,
};
#[test]
fn test_signed_message_min_size() {
let keys = KeyPair::random();
let msg = SignedMessage::new(vec![], keys.public_key(), keys.secret_key());
assert_eq!(SIGNED_MESSAGE_MIN_SIZE, msg.into_bytes().len())
}
#[test]
fn test_message_roundtrip() {
let keys = KeyPair::random();
let ts = Utc::now();
let msg = Verified::from_value(
Precommit::new(
ValidatorId(123),
Height(15),
Round(25),
crypto::hash(&[1, 2, 3]),
crypto::hash(&[3, 2, 1]),
ts,
),
keys.public_key(),
keys.secret_key(),
);
let bytes = msg.to_bytes();
let message =
SignedMessage::from_bytes(bytes.into()).expect("Cannot deserialize signed message");
let msg_roundtrip = message
.into_verified::<Precommit>()
.expect("Failed to check precommit");
assert_eq!(msg, msg_roundtrip);
}
#[test]
fn test_signed_message_unusual_protobuf() {
let keys = KeyPair::random();
let mut ex_msg = proto::CoreMessage::new();
let precommit_msg = Precommit::new(
ValidatorId(123),
Height(15),
Round(25),
crypto::hash(&[1, 2, 3]),
crypto::hash(&[3, 2, 1]),
Utc::now(),
);
ex_msg.set_precommit(precommit_msg.to_pb());
let mut payload = ex_msg.write_to_bytes().unwrap();
payload.append(&mut payload.clone());
let signed = SignedMessage::new(payload, keys.public_key(), keys.secret_key());
let bytes = signed.into_bytes();
let message =
SignedMessage::from_bytes(bytes.into()).expect("Cannot deserialize signed message");
let deserialized_precommit = message
.into_verified::<Precommit>()
.expect("Failed to check precommit");
assert_eq!(precommit_msg, *deserialized_precommit.payload())
}
#[test]
fn test_precommit_serde_correct() {
let keys = KeyPair::random();
let ts = Utc::now();
let precommit = Verified::from_value(
Precommit::new(
ValidatorId(123),
Height(15),
Round(25),
crypto::hash(&[1, 2, 3]),
crypto::hash(&[3, 2, 1]),
ts,
),
keys.public_key(),
keys.secret_key(),
);
let precommit_json = serde_json::to_string(&precommit).unwrap();
let precommit2: Verified<Precommit> = serde_json::from_str(&precommit_json).unwrap();
assert_eq!(precommit2, precommit);
}
}