use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ChainRoot(pub [u8; 32]);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CommitmentId(pub [u8; 32]);
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Scope(pub String);
pub const BASIS_POINTS: u32 = 10_000;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum TesseraEvent {
CommitmentMade {
by: ChainRoot,
commitment: CommitmentId,
scope: Scope,
cadence_ms: u64,
duration_ms: Option<u64>,
at_ms: u64,
},
Heartbeat {
by: ChainRoot,
commitment: CommitmentId,
at_ms: u64,
},
CommitmentFulfilled {
by: ChainRoot,
commitment: CommitmentId,
at_ms: u64,
},
CleanHandoff {
from: ChainRoot,
to: ChainRoot,
commitment: CommitmentId,
at_ms: u64,
},
Vouch {
voucher: ChainRoot,
newcomer: ChainRoot,
fraction_bp: u16,
at_ms: u64,
},
GovernanceParticipation { by: ChainRoot, at_ms: u64 },
Pardon {
by: ChainRoot,
target: ChainRoot,
weight: u64,
at_ms: u64,
},
Censure {
by: ChainRoot,
target: ChainRoot,
weight: u64,
at_ms: u64,
},
}
impl TesseraEvent {
pub fn at_ms(&self) -> u64 {
match self {
TesseraEvent::CommitmentMade { at_ms, .. }
| TesseraEvent::Heartbeat { at_ms, .. }
| TesseraEvent::CommitmentFulfilled { at_ms, .. }
| TesseraEvent::CleanHandoff { at_ms, .. }
| TesseraEvent::Vouch { at_ms, .. }
| TesseraEvent::GovernanceParticipation { at_ms, .. }
| TesseraEvent::Pardon { at_ms, .. }
| TesseraEvent::Censure { at_ms, .. } => *at_ms,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn at_ms_reads_every_variant() {
let root = ChainRoot([1; 32]);
let cid = CommitmentId([2; 32]);
let events = [
TesseraEvent::CommitmentMade {
by: root,
commitment: cid,
scope: Scope("a/b".into()),
cadence_ms: 100,
duration_ms: None,
at_ms: 1,
},
TesseraEvent::Heartbeat {
by: root,
commitment: cid,
at_ms: 2,
},
TesseraEvent::CommitmentFulfilled {
by: root,
commitment: cid,
at_ms: 3,
},
TesseraEvent::CleanHandoff {
from: root,
to: ChainRoot([9; 32]),
commitment: cid,
at_ms: 4,
},
TesseraEvent::Vouch {
voucher: root,
newcomer: ChainRoot([9; 32]),
fraction_bp: 5_000,
at_ms: 5,
},
TesseraEvent::GovernanceParticipation { by: root, at_ms: 6 },
TesseraEvent::Pardon {
by: ChainRoot([50; 32]),
target: root,
weight: 10,
at_ms: 7,
},
TesseraEvent::Censure {
by: ChainRoot([50; 32]),
target: root,
weight: 10,
at_ms: 8,
},
];
let stamps: Vec<u64> = events.iter().map(TesseraEvent::at_ms).collect();
assert_eq!(stamps, vec![1, 2, 3, 4, 5, 6, 7, 8]);
}
}