Skip to main content

dig_clvm/consensus/
context.rs

1//! Validation context — L2 chain state passed into validation.
2
3use std::collections::{HashMap, HashSet};
4
5use chia_consensus::flags::MEMPOOL_MODE;
6use chia_consensus::spendbundle_validation::get_flags_for_height_and_constants;
7use chia_protocol::Bytes32;
8use chia_sdk_coinset::CoinRecord;
9use dig_constants::NetworkConstants;
10
11/// L2 chain state for validation.
12///
13/// `coin_records` should contain only the coins being spent in this bundle,
14/// not the full UTXO set. The caller loads these from their database and
15/// passes them in. dig-clvm never touches storage directly.
16pub struct ValidationContext {
17    /// Current L2 block height.
18    pub height: u32,
19    /// Current block timestamp (seconds since epoch).
20    pub timestamp: u64,
21    /// DIG network constants (from dig-constants crate).
22    pub constants: NetworkConstants,
23    /// Coins being spent in this bundle (coin_id -> CoinRecord).
24    /// Only the coins relevant to this validation — NOT the full UTXO set.
25    pub coin_records: HashMap<Bytes32, CoinRecord>,
26    /// Coins created by earlier bundles in the same block (ephemeral).
27    pub ephemeral_coins: HashSet<Bytes32>,
28}
29
30impl ValidationContext {
31    /// Execution flags for running a spend bundle at this context's height.
32    ///
33    /// Combines the height-activated hard-fork flags, mempool-mode strictness,
34    /// and the caller's `extra` flags.
35    ///
36    /// This derivation must be done by the caller: `chia-consensus` 0.26 took a
37    /// `height` argument and computed
38    /// `get_flags_for_height_and_constants(height, constants) | flags | MEMPOOL_MODE`
39    /// internally, but 0.36 dropped both the argument and the derivation. Passing
40    /// the caller's flags alone would silently execute spends under pre-hard-fork
41    /// rules and outside mempool mode — a consensus divergence that still compiles.
42    pub fn spend_flags(&self, extra: u32) -> u32 {
43        get_flags_for_height_and_constants(self.height, self.constants.consensus())
44            | extra
45            | MEMPOOL_MODE
46    }
47}