Skip to main content

chia_query/types/
chain_claim.rs

1//! What part of an answer is a claim about the CHAIN, and therefore has to be agreed on.
2//!
3//! A positive answer is not self-verifying. The coin-id binding — `SHA256(parent_coin_info ‖
4//! puzzle_hash ‖ amount)` — authenticates the coin's *identity*, and nothing else on the record:
5//! `created_height`, `spent_height` and `spent` are copied verbatim from whatever an anonymous
6//! peer sent, and those are the fields a consumer reads to decide whether money is settled
7//! (dig_ecosystem#2462).
8//!
9//! So corroboration compares the claim, not the struct. Two sources describing the same chain
10//! state must compare EQUAL even though they filled in different amounts of local colour: the peer
11//! protocol cannot supply `timestamp` or `coinbase` at all and leaves them zeroed, while the
12//! coinset API returns both. Comparing whole records would make every peer/coinset pair
13//! "disagree" and turn the corroboration into a permanent error.
14
15use crate::types::{CoinRecord, CoinSpend};
16
17/// The chain-state claim inside an answer, rendered as a value two sources can be compared on.
18///
19/// Implement this for anything that travels through a corroborated read. The implementation MUST
20/// include every field a consumer could read as evidence about the chain, and MUST exclude fields
21/// that merely vary by which tier answered — a claim that includes tier-local colour cannot be
22/// corroborated across tiers, and one that omits a height cannot catch the fabrication this trait
23/// exists to catch.
24pub trait ChainClaim {
25    /// The claim, canonically rendered. Equal strings mean two sources asserted the same thing
26    /// about the chain.
27    fn chain_claim(&self) -> String;
28}
29
30impl ChainClaim for CoinRecord {
31    /// The coin's identity plus the three fields that say where it sits on the chain.
32    ///
33    /// `timestamp` and `coinbase` are deliberately absent: the peer protocol carries neither, so
34    /// including them would make a peer answer and a coinset answer about the same coin
35    /// unconditionally unequal.
36    fn chain_claim(&self) -> String {
37        format!(
38            "coin({},{},{}) created={} spent={}",
39            self.coin.parent_coin_info,
40            self.coin.puzzle_hash,
41            self.coin.amount,
42            self.confirmed_block_index,
43            if self.spent {
44                self.spent_block_index.to_string()
45            } else {
46                "no".to_string()
47            },
48        )
49    }
50}
51
52impl ChainClaim for CoinSpend {
53    /// A spend is claimed by the coin it spent and the program bytes that spent it.
54    fn chain_claim(&self) -> String {
55        format!(
56            "spend({},{},{}) puzzle={} solution={}",
57            self.coin.parent_coin_info,
58            self.coin.puzzle_hash,
59            self.coin.amount,
60            self.puzzle_reveal,
61            self.solution,
62        )
63    }
64}