use chia_protocol::Bytes32;
use dig_chainsource_interface::CoinRecord;
use super::evidence::MIN_CONFIRMATION_DEPTH;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingStoreLaunch {
launcher_id: Bytes32,
store_coin_id: Bytes32,
did_coin_id: Bytes32,
committed_root: [u8; 32],
pushed_at_height: u32,
}
impl PendingStoreLaunch {
pub(crate) fn new(
launcher_id: Bytes32,
store_coin_id: Bytes32,
did_coin_id: Bytes32,
committed_root: [u8; 32],
pushed_at_height: u32,
) -> Self {
Self {
launcher_id,
store_coin_id,
did_coin_id,
committed_root,
pushed_at_height,
}
}
pub fn launcher_id(&self) -> Bytes32 {
self.launcher_id
}
pub fn store_coin_id(&self) -> Bytes32 {
self.store_coin_id
}
pub fn did_coin_id(&self) -> Bytes32 {
self.did_coin_id
}
pub fn committed_root(&self) -> [u8; 32] {
self.committed_root
}
pub fn pushed_at_height(&self) -> u32 {
self.pushed_at_height
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfirmedStore {
launcher_id: Bytes32,
coin_id: Bytes32,
confirmed_height: u32,
did_coin_id: Bytes32,
committed_root: [u8; 32],
}
impl ConfirmedStore {
pub(crate) fn from_confirmed(
pending: &PendingStoreLaunch,
record: &CoinRecord,
peak_height: u32,
) -> Option<Self> {
if record.coin.coin_id() != pending.store_coin_id() {
return None;
}
let confirmed_height = record.confirmed_height?;
if confirmed_height == 0 || confirmed_height < pending.pushed_at_height() {
return None;
}
if peak_height
.saturating_sub(confirmed_height)
.saturating_add(1)
< MIN_CONFIRMATION_DEPTH
{
return None;
}
Some(Self {
launcher_id: pending.launcher_id(),
coin_id: pending.store_coin_id(),
confirmed_height,
did_coin_id: pending.did_coin_id(),
committed_root: pending.committed_root(),
})
}
pub fn launcher_id(&self) -> Bytes32 {
self.launcher_id
}
pub fn coin_id(&self) -> Bytes32 {
self.coin_id
}
pub fn confirmed_height(&self) -> u32 {
self.confirmed_height
}
pub fn did_coin_id(&self) -> Bytes32 {
self.did_coin_id
}
pub fn committed_root(&self) -> [u8; 32] {
self.committed_root
}
#[cfg(test)]
pub(crate) fn every_field(&self) -> (Bytes32, Bytes32, u32, Bytes32, [u8; 32]) {
let Self {
launcher_id,
coin_id,
confirmed_height,
did_coin_id,
committed_root,
} = self;
(
*launcher_id,
*coin_id,
*confirmed_height,
*did_coin_id,
*committed_root,
)
}
}
impl PendingStoreLaunch {
#[cfg(test)]
pub(crate) fn every_field(&self) -> (Bytes32, Bytes32, Bytes32, [u8; 32], u32) {
let Self {
launcher_id,
store_coin_id,
did_coin_id,
committed_root,
pushed_at_height,
} = self;
(
*launcher_id,
*store_coin_id,
*did_coin_id,
*committed_root,
*pushed_at_height,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use chia_protocol::Coin;
const ROOT: [u8; 32] = [0xAB; 32];
const PUSHED_AT: u32 = 4_200_000;
const PEAK: u32 = PUSHED_AT + MIN_CONFIRMATION_DEPTH;
fn coin() -> Coin {
Coin::new(Bytes32::new([1; 32]), Bytes32::new([2; 32]), 1)
}
fn pending_for(coin: &Coin) -> PendingStoreLaunch {
PendingStoreLaunch::new(
Bytes32::new([9; 32]),
coin.coin_id(),
Bytes32::new([4; 32]),
ROOT,
PUSHED_AT,
)
}
fn record(coin: Coin, confirmed_height: Option<u32>) -> CoinRecord {
CoinRecord {
coin,
confirmed_height,
spent_height: None,
timestamp: None,
coinbase: false,
}
}
#[test]
fn a_confirmed_store_record_of_the_expected_coin_yields_evidence() {
let coin = coin();
let pending = pending_for(&coin);
let store = ConfirmedStore::from_confirmed(&pending, &record(coin, Some(PUSHED_AT)), PEAK)
.expect("a buried confirmation of the expected coin is evidence");
assert_eq!(store.confirmed_height(), PUSHED_AT);
assert_eq!(store.launcher_id(), pending.launcher_id());
assert_eq!(store.coin_id(), pending.store_coin_id());
assert_eq!(
store.did_coin_id(),
pending.did_coin_id(),
"the DID coin the launch spent is carried through, or the store loses its owner"
);
assert_ne!(
store.did_coin_id(),
store.coin_id(),
"the fixture must distinguish the two coins, or a transposed field would pass"
);
assert_eq!(
store.committed_root(),
ROOT,
"the root comes from the bundle this crate built, never from the record"
);
}
#[test]
fn an_unconfirmed_store_record_is_not_evidence() {
let coin = coin();
let pending = pending_for(&coin);
assert!(ConfirmedStore::from_confirmed(&pending, &record(coin, None), PEAK).is_none());
}
#[test]
fn a_confirmed_record_of_another_coin_is_not_store_evidence() {
let pending = pending_for(&coin());
let other = Coin::new(Bytes32::new([7; 32]), Bytes32::new([8; 32]), 1);
assert_ne!(other.coin_id(), pending.store_coin_id());
assert!(
ConfirmedStore::from_confirmed(&pending, &record(other, Some(PUSHED_AT)), PEAK)
.is_none()
);
}
#[test]
fn a_store_confirmation_at_genesis_is_not_evidence() {
let coin = coin();
let pending = PendingStoreLaunch::new(
Bytes32::new([9; 32]),
coin.coin_id(),
Bytes32::new([4; 32]),
ROOT,
0,
);
let peak = MIN_CONFIRMATION_DEPTH - 1;
assert!(
ConfirmedStore::from_confirmed(&pending, &record(coin, Some(0)), peak).is_none(),
"no coin is created in block 0"
);
assert!(
ConfirmedStore::from_confirmed(&pending, &record(coin, Some(1)), peak + 1).is_some()
);
}
#[test]
fn a_store_confirmation_predating_the_push_is_not_evidence() {
let coin = coin();
let pending = pending_for(&coin);
assert!(
ConfirmedStore::from_confirmed(&pending, &record(coin, Some(PUSHED_AT - 1)), PEAK)
.is_none()
);
}
#[test]
fn a_store_confirmation_past_the_peak_is_not_evidence() {
let coin = coin();
let pending = pending_for(&coin);
for claimed in [PEAK + 1, u32::MAX] {
assert!(
ConfirmedStore::from_confirmed(&pending, &record(coin, Some(claimed)), PEAK)
.is_none(),
"a confirmation at {claimed} is past the peak {PEAK}"
);
}
}
#[test]
fn the_store_confirmation_depth_bound_holds_from_both_sides() {
let coin = coin();
let pending = pending_for(&coin);
let at_bound = PUSHED_AT + MIN_CONFIRMATION_DEPTH - 1;
let one_short = at_bound - 1;
assert!(
ConfirmedStore::from_confirmed(&pending, &record(coin, Some(PUSHED_AT)), one_short)
.is_none(),
"one block short of {MIN_CONFIRMATION_DEPTH} deep is still reversible"
);
assert!(
ConfirmedStore::from_confirmed(&pending, &record(coin, Some(PUSHED_AT)), at_bound)
.is_some(),
"exactly {MIN_CONFIRMATION_DEPTH} deep is evidence"
);
}
}