pub type HexString = String;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SubmissionEvent {
Submitted { tx: Vec<u8>, tx_hash: HexString },
InBlock {
tx: Vec<u8>,
block_hash: HexString,
block_height: u64,
tx_hash: HexString,
},
Finalized {
tx: Vec<u8>,
block_hash: HexString,
block_height: u64,
tx_hash: HexString,
},
}
impl SubmissionEvent {
#[must_use]
pub fn stage(&self) -> SubmissionStage {
match self {
Self::Submitted { .. } => SubmissionStage::Submitted,
Self::InBlock { .. } => SubmissionStage::InBlock,
Self::Finalized { .. } => SubmissionStage::Finalized,
}
}
#[must_use]
pub fn tx_hash(&self) -> &str {
match self {
Self::Submitted { tx_hash, .. }
| Self::InBlock { tx_hash, .. }
| Self::Finalized { tx_hash, .. } => tx_hash,
}
}
#[must_use]
pub fn tx_bytes(&self) -> &[u8] {
match self {
Self::Submitted { tx, .. } | Self::InBlock { tx, .. } | Self::Finalized { tx, .. } => {
tx
}
}
}
#[must_use]
pub fn block(&self) -> Option<(&str, u64)> {
match self {
Self::Submitted { .. } => None,
Self::InBlock {
block_hash,
block_height,
..
}
| Self::Finalized {
block_hash,
block_height,
..
} => Some((block_hash, *block_height)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SubmissionStage {
Submitted,
InBlock,
Finalized,
}
impl SubmissionStage {
#[must_use]
pub fn as_tag(&self) -> &'static str {
match self {
Self::Submitted => "Submitted",
Self::InBlock => "InBlock",
Self::Finalized => "Finalized",
}
}
}
impl std::fmt::Display for SubmissionStage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_tag())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_tx() -> Vec<u8> {
vec![0xde, 0xad, 0xbe, 0xef]
}
#[test]
fn submitted_event_exposes_tag_hash_and_bytes() {
let event = SubmissionEvent::Submitted {
tx: sample_tx(),
tx_hash: "0xabc".into(),
};
assert_eq!(event.stage(), SubmissionStage::Submitted);
assert_eq!(event.tx_hash(), "0xabc");
assert_eq!(event.tx_bytes(), sample_tx().as_slice());
assert_eq!(event.block(), None);
}
#[test]
fn in_block_event_exposes_block_height_and_hash() {
let event = SubmissionEvent::InBlock {
tx: sample_tx(),
block_hash: "0xblk".into(),
block_height: 7,
tx_hash: "0xabc".into(),
};
assert_eq!(event.stage(), SubmissionStage::InBlock);
assert_eq!(event.block(), Some(("0xblk", 7)));
}
#[test]
fn finalized_event_exposes_block_height_and_hash() {
let event = SubmissionEvent::Finalized {
tx: sample_tx(),
block_hash: "0xfin".into(),
block_height: 8,
tx_hash: "0xabc".into(),
};
assert_eq!(event.stage(), SubmissionStage::Finalized);
assert_eq!(event.block(), Some(("0xfin", 8)));
}
#[test]
fn submission_stage_ordering_matches_tx_lifecycle() {
assert!(SubmissionStage::Submitted < SubmissionStage::InBlock);
assert!(SubmissionStage::InBlock < SubmissionStage::Finalized);
}
#[test]
fn submission_stage_tags_are_stable_strings() {
assert_eq!(SubmissionStage::Submitted.as_tag(), "Submitted");
assert_eq!(SubmissionStage::InBlock.as_tag(), "InBlock");
assert_eq!(SubmissionStage::Finalized.as_tag(), "Finalized");
assert_eq!(format!("{}", SubmissionStage::Finalized), "Finalized");
}
}