use super::{content::Content, event_hash::EventHash};
#[cfg(all(feature = "mock", any(feature = "testing", test)))]
use crate::{
gossip::Cause,
id::SecretId,
mock::{PeerId, Transaction},
observation::Observation,
};
use crate::{hash::Hash, serialise, NetworkEvent, PublicId, Vote};
use std::fmt::{self, Debug, Formatter};
#[serde(bound = "")]
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PackedEvent<T: NetworkEvent, P: PublicId> {
pub(super) content: Content<Vote<T, P>, EventHash, P>,
pub(super) signature: P::Signature,
}
impl<T: NetworkEvent, P: PublicId> Debug for PackedEvent<T, P> {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(
formatter,
"Event{{ {:?}, creator: {:?}, self_parent: {:?}, other_parent: {:?} }}",
self.content.cause,
self.content.creator,
self.content.self_parent(),
self.content.other_parent()
)
}
}
impl<T: NetworkEvent, P: PublicId> PackedEvent<T, P> {
pub(crate) fn compute_hash(&self) -> EventHash {
EventHash(Hash::from(serialise(&self.content).as_slice()))
}
}
#[cfg(all(feature = "mock", any(feature = "testing", test)))]
impl PackedEvent<Transaction, PeerId> {
pub fn new_requesting(creator: PeerId, recipient: PeerId, self_parent: EventHash) -> Self {
let content = Content {
creator,
cause: Cause::Requesting {
self_parent,
recipient,
},
};
Self::new(content)
}
pub fn new_request(creator: PeerId, self_parent: EventHash, other_parent: EventHash) -> Self {
let content = Content {
creator,
cause: Cause::Request {
self_parent,
other_parent,
},
};
Self::new(content)
}
pub fn new_response(creator: PeerId, self_parent: EventHash, other_parent: EventHash) -> Self {
let content = Content {
creator,
cause: Cause::Response {
self_parent,
other_parent,
},
};
Self::new(content)
}
pub fn new_observation(
creator: PeerId,
self_parent: EventHash,
observation: Observation<Transaction, PeerId>,
) -> Self {
let vote = Vote::new(&creator, observation);
let content = Content {
creator,
cause: Cause::Observation { self_parent, vote },
};
Self::new(content)
}
pub fn new_initial(creator: PeerId) -> Self {
let content = Content {
creator,
cause: Cause::Initial,
};
Self::new(content)
}
fn new(content: Content<Vote<Transaction, PeerId>, EventHash, PeerId>) -> Self {
let serialised_content = serialise(&content);
let signature = content.creator.sign_detached(&serialised_content);
PackedEvent { content, signature }
}
pub fn creator(&self) -> &PeerId {
&self.content.creator
}
pub fn self_parent(&self) -> Option<&EventHash> {
self.content.self_parent()
}
pub fn other_parent(&self) -> Option<&EventHash> {
self.content.other_parent()
}
}