use chia_protocol::Bytes32;
use dig_chainsource_interface::CoinRecord;
pub const MIN_CONFIRMATION_DEPTH: u32 = 6;
#[allow(
dead_code,
reason = "a const assertion is evaluated at compile time, never read"
)]
const DEPTH_ALSO_REJECTS_A_FUTURE_HEIGHT: () = assert!(
MIN_CONFIRMATION_DEPTH > 1,
"MIN_CONFIRMATION_DEPTH must exceed 1, or a confirmation past the chain's peak becomes evidence"
);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingMint {
launcher_id: Bytes32,
did_coin_id: Bytes32,
source_coin_id: Bytes32,
pushed_at_height: u32,
}
impl PendingMint {
pub(super) fn new(
launcher_id: Bytes32,
did_coin_id: Bytes32,
source_coin_id: Bytes32,
pushed_at_height: u32,
) -> Self {
Self {
launcher_id,
did_coin_id,
source_coin_id,
pushed_at_height,
}
}
pub fn source_coin_id(&self) -> Bytes32 {
self.source_coin_id
}
pub fn pushed_at_height(&self) -> u32 {
self.pushed_at_height
}
pub fn launcher_id(&self) -> Bytes32 {
self.launcher_id
}
pub fn did_coin_id(&self) -> Bytes32 {
self.did_coin_id
}
pub fn pending_did_string(&self) -> String {
dig_did::did_string_from_launcher_id(self.launcher_id)
}
#[cfg(test)]
pub(crate) fn every_field(&self) -> (Bytes32, Bytes32, Bytes32, u32) {
let Self {
launcher_id,
did_coin_id,
source_coin_id,
pushed_at_height,
} = self;
(
*launcher_id,
*did_coin_id,
*source_coin_id,
*pushed_at_height,
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MintedDid {
did: String,
launcher_id: Bytes32,
coin_id: Bytes32,
confirmed_height: u32,
}
impl MintedDid {
pub(super) fn from_confirmed(
pending: &PendingMint,
record: &CoinRecord,
peak_height: u32,
) -> Option<Self> {
if record.coin.coin_id() != pending.did_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 {
did: dig_did::did_string_from_launcher_id(pending.launcher_id()),
launcher_id: pending.launcher_id(),
coin_id: pending.did_coin_id(),
confirmed_height,
})
}
pub(super) fn reverified(
launcher_id: Bytes32,
coin_id: Bytes32,
journalled_height: u32,
record: &CoinRecord,
peak_height: u32,
) -> Option<Self> {
Self::from_confirmed(
&PendingMint::new(launcher_id, coin_id, coin_id, journalled_height),
record,
peak_height,
)
}
pub fn did(&self) -> &str {
&self.did
}
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
}
#[cfg(test)]
pub(crate) fn every_field(&self) -> (&str, Bytes32, Bytes32, u32) {
let Self {
did,
launcher_id,
coin_id,
confirmed_height,
} = self;
(did, *launcher_id, *coin_id, *confirmed_height)
}
}
#[cfg(test)]
mod tests {
use super::*;
use chia_protocol::Coin;
fn coin() -> Coin {
Coin::new(Bytes32::new([1; 32]), Bytes32::new([2; 32]), 1)
}
const PUSHED_AT: u32 = 4_200_000;
const PEAK: u32 = PUSHED_AT + MIN_CONFIRMATION_DEPTH;
fn pending_for(coin: &Coin) -> PendingMint {
PendingMint::new(
Bytes32::new([9; 32]),
coin.coin_id(),
Bytes32::new([4; 32]),
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_record_of_the_expected_coin_yields_evidence() {
let coin = coin();
let pending = pending_for(&coin);
let minted = MintedDid::from_confirmed(&pending, &record(coin, Some(PUSHED_AT)), PEAK)
.expect("a buried confirmation of the expected coin is evidence");
assert_eq!(minted.confirmed_height(), PUSHED_AT);
assert_eq!(minted.launcher_id(), pending.launcher_id());
assert_eq!(
minted.did(),
dig_did::did_string_from_launcher_id(pending.launcher_id())
);
}
#[test]
fn an_unconfirmed_record_of_the_expected_coin_is_not_evidence() {
let coin = coin();
let pending = pending_for(&coin);
assert!(MintedDid::from_confirmed(&pending, &record(coin, None), PEAK).is_none());
}
#[test]
fn a_confirmed_record_of_another_coin_is_not_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.did_coin_id());
assert!(
MintedDid::from_confirmed(&pending, &record(other, Some(PUSHED_AT)), PEAK).is_none()
);
}
#[test]
fn a_confirmation_at_genesis_is_not_evidence() {
let coin = coin();
let pending = PendingMint::new(
Bytes32::new([9; 32]),
coin.coin_id(),
Bytes32::new([4; 32]),
0,
);
let peak = MIN_CONFIRMATION_DEPTH - 1;
assert!(
MintedDid::from_confirmed(&pending, &record(coin, Some(0)), peak).is_none(),
"no coin is created in block 0"
);
assert!(MintedDid::from_confirmed(&pending, &record(coin, Some(1)), peak + 1).is_some());
}
#[test]
fn a_confirmation_past_the_peak_is_not_evidence() {
let coin = coin();
let pending = pending_for(&coin);
for claimed in [PEAK + 1, u32::MAX] {
assert!(
MintedDid::from_confirmed(&pending, &record(coin, Some(claimed)), PEAK).is_none(),
"a confirmation at {claimed} is past the peak {PEAK}"
);
}
}
#[test]
fn a_confirmation_predating_the_push_is_not_evidence() {
let coin = coin();
let pending = pending_for(&coin);
assert!(
MintedDid::from_confirmed(&pending, &record(coin, Some(PUSHED_AT - 1)), PEAK).is_none()
);
}
#[test]
fn the_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!(
MintedDid::from_confirmed(&pending, &record(coin, Some(PUSHED_AT)), one_short)
.is_none(),
"one block short of {MIN_CONFIRMATION_DEPTH} deep is still reversible"
);
assert!(
MintedDid::from_confirmed(&pending, &record(coin, Some(PUSHED_AT)), at_bound).is_some(),
"exactly {MIN_CONFIRMATION_DEPTH} deep is evidence"
);
}
}