Skip to main content

dig_constants/
lib.rs

1//! DIG Network Constants
2//!
3//! Defines network parameters for the DIG L2 blockchain. This crate exists
4//! separately so that any DIG crate can import network constants without
5//! pulling in the full CLVM engine or other heavy dependencies.
6//!
7//! The core type is [`NetworkConstants`], which wraps `chia-consensus`'s
8//! `ConsensusConstants` with DIG-specific values (genesis challenge,
9//! AGG_SIG additional data, cost limits, etc.).
10//!
11//! # Usage
12//!
13//! ```rust,ignore
14//! use dig_constants::DIG_MAINNET;
15//!
16//! let genesis = DIG_MAINNET.genesis_challenge();
17//! let consensus = DIG_MAINNET.consensus();
18//! ```
19
20use chia_consensus::consensus_constants::ConsensusConstants;
21use chia_protocol::Bytes32;
22use hex_literal::hex;
23
24/// DIG network constants.
25///
26/// Wraps `chia-consensus::ConsensusConstants` with accessors for the fields
27/// that DIG validators and wallet code commonly need. The underlying
28/// `ConsensusConstants` is available via [`consensus()`](Self::consensus)
29/// for direct use with `chia-consensus` functions like `run_spendbundle()`.
30#[derive(Debug, Clone)]
31pub struct NetworkConstants {
32    inner: ConsensusConstants,
33}
34
35impl NetworkConstants {
36    /// The underlying `chia-consensus` constants, for passing directly to
37    /// `run_spendbundle()`, `validate_clvm_and_signature()`, etc.
38    pub fn consensus(&self) -> &ConsensusConstants {
39        &self.inner
40    }
41
42    /// DIG genesis challenge.
43    pub fn genesis_challenge(&self) -> Bytes32 {
44        self.inner.genesis_challenge
45    }
46
47    /// AGG_SIG_ME additional data (== genesis_challenge on Chia L1).
48    pub fn agg_sig_me_additional_data(&self) -> Bytes32 {
49        self.inner.agg_sig_me_additional_data
50    }
51
52    /// Maximum CLVM cost per block.
53    pub fn max_block_cost_clvm(&self) -> u64 {
54        self.inner.max_block_cost_clvm
55    }
56
57    /// Cost per byte of generator program.
58    pub fn cost_per_byte(&self) -> u64 {
59        self.inner.cost_per_byte
60    }
61
62    /// Maximum coin amount (u64::MAX).
63    pub fn max_coin_amount(&self) -> u64 {
64        self.inner.max_coin_amount
65    }
66}
67
68// =============================================================================
69// AGG_SIG additional data derivation
70//
71// On Chia L1, each AGG_SIG_* variant's additional_data is:
72//   sha256(genesis_challenge || opcode_byte)
73// except AGG_SIG_ME which uses genesis_challenge directly.
74//
75// See: condition_tools.py:58-71
76//   https://github.com/Chia-Network/chia-blockchain/blob/main/chia/consensus/condition_tools.py#L58
77// =============================================================================
78
79// ---------------------------------------------------------------------------
80// DIG Mainnet
81//
82// The genesis challenge is the 32-byte consensus anchor for the DIG L2 network.
83// It doubles as the gossip `network_id` gate: `dig-gossip` REJECTS an all-zero
84// network_id, so this value MUST be non-zero for the node's gossip pool / DHT /
85// PEX to start.
86//
87// DIG_MAINNET L2 genesis = the Chia mainnet header hash @ height 9,021,277
88//   (0af981...1abf), pinned 2026-07-17 — anchors the DIG L2 genesis to a real,
89//   verifiable Chia block (captured via coinset.org get_blockchain_state).
90//
91//   DIG_MAINNET_GENESIS_CHALLENGE
92//     = 0af981862a4df51f51ec59c312315d959931d917c375730b89b9e2b0854d1abf
93//
94// This is the PRE-LAUNCH canonical DIG mainnet genesis. Per CLAUDE.md §3.7 the
95// ecosystem is pre-release with no live users, so this value is revisable at
96// true mainnet launch — re-anchor to the launch-time Chia header hash and
97// recompute every derived value below if it is ever changed.
98//
99// All `agg_sig_*_additional_data` values are derived from this genesis as
100// `sha256(genesis_challenge || opcode_byte)` (AGG_SIG_ME = genesis directly),
101// so they were all recomputed for this genesis.
102// ---------------------------------------------------------------------------
103
104/// Canonical DIG mainnet genesis challenge.
105///
106/// The Chia mainnet header hash at block height 9,021,277 (`0af981…1abf`),
107/// pinned 2026-07-17 — a real, verifiable, fixed 32-byte value anchoring the
108/// DIG L2 genesis to a real Chia block. This is the pre-launch canonical value;
109/// per §3.7 it is revisable at true mainnet launch. All
110/// `agg_sig_*_additional_data` fields are derived from this.
111const DIG_MAINNET_GENESIS_CHALLENGE: [u8; 32] =
112    hex!("0af981862a4df51f51ec59c312315d959931d917c375730b89b9e2b0854d1abf");
113
114/// DIG mainnet constants.
115///
116/// Uses DIG's own genesis challenge and AGG_SIG domain separation.
117/// Proof-of-space and VDF fields are set to neutral values since DIG L2
118/// does not use Chia's proof-of-space consensus.
119pub const DIG_MAINNET: NetworkConstants = NetworkConstants {
120    inner: ConsensusConstants {
121        // -- DIG-specific values --
122        genesis_challenge: Bytes32::new(DIG_MAINNET_GENESIS_CHALLENGE),
123
124        // AGG_SIG additional data: derived from genesis_challenge.
125        // AGG_SIG_ME = genesis_challenge directly.
126        // Others = sha256(genesis_challenge || opcode_byte).
127        // Derivation: condition_tools.py:58-71
128        //   https://github.com/Chia-Network/chia-blockchain/blob/main/chia/consensus/condition_tools.py#L58
129        // Opcode bytes: AGG_SIG_PARENT=43, PUZZLE=44, AMOUNT=45,
130        //   PUZZLE_AMOUNT=46, PARENT_AMOUNT=47, PARENT_PUZZLE=48
131        // NOTE: Recompute ALL values when genesis_challenge is finalized.
132        agg_sig_me_additional_data: Bytes32::new(DIG_MAINNET_GENESIS_CHALLENGE),
133        agg_sig_parent_additional_data: Bytes32::new(hex!(
134            "196d63b6dfbd4440656f9c1eadc686cacfaae771c565762a8cd6e51c892a0077"
135        )),
136        agg_sig_puzzle_additional_data: Bytes32::new(hex!(
137            "9ca719659b5e2355a91ff330c8612cb58c74f1063eaff99e507602d450b1f71f"
138        )),
139        agg_sig_amount_additional_data: Bytes32::new(hex!(
140            "d13767da4a8bd9520dbd9e039e68b3eb4b16fdcbb7e7755b5064840eaeb553ce"
141        )),
142        agg_sig_puzzle_amount_additional_data: Bytes32::new(hex!(
143            "73eea3473bd0daa28793d4bcd218ade462b634b53af97f9a01a91f3059ac75df"
144        )),
145        agg_sig_parent_amount_additional_data: Bytes32::new(hex!(
146            "eb7302224e77c0f269d0c8b105d4cc786775ae012ed2db49751c33c244c3f647"
147        )),
148        agg_sig_parent_puzzle_additional_data: Bytes32::new(hex!(
149            "ccac5983685257d50ee7b439bbb502128ddb262813dde4e4a11ac6cdfc66fa8e"
150        )),
151
152        // DIG L2 cost limits
153        max_block_cost_clvm: 11_000_000_000, // per-spend limit, same as Chia L1
154        cost_per_byte: 12_000,
155        max_coin_amount: u64::MAX,
156
157        // Block generator limits
158        max_generator_size: 1_000_000,
159        max_generator_ref_list_size: 512,
160
161        // Hard fork heights — set to 0 to always use latest consensus rules.
162        // DIG L2 starts with all features enabled from block 0.
163        hard_fork_height: 0,
164        hard_fork2_height: 0,
165
166        // Pre-farm puzzle hashes — not used by DIG L2, set to zero.
167        genesis_pre_farm_pool_puzzle_hash: Bytes32::new([0u8; 32]),
168        genesis_pre_farm_farmer_puzzle_hash: Bytes32::new([0u8; 32]),
169
170        // -- Proof-of-space / VDF fields (not used by DIG L2) --
171        // These must be valid values since ConsensusConstants is passed to
172        // chia-consensus functions, but DIG does not use PoS consensus.
173        slot_blocks_target: 32,
174        min_blocks_per_challenge_block: 16,
175        max_sub_slot_blocks: 128,
176        num_sps_sub_slot: 64,
177        sub_slot_iters_starting: 1 << 27,
178        difficulty_constant_factor: 1 << 67,
179        difficulty_starting: 7,
180        difficulty_change_max_factor: 3,
181        sub_epoch_blocks: 384,
182        epoch_blocks: 4608,
183        significant_bits: 8,
184        discriminant_size_bits: 1024,
185        number_zero_bits_plot_filter_v1: 9,
186        number_zero_bits_plot_filter_v2: 9,
187        min_plot_size_v1: 32,
188        max_plot_size_v1: 50,
189        min_plot_size_v2: 28,
190        max_plot_size_v2: 32,
191        sub_slot_time_target: 600,
192        num_sp_intervals_extra: 3,
193        max_future_time2: 120,
194        number_of_timestamps: 11,
195        max_vdf_witness_size: 64,
196        mempool_block_buffer: 10,
197        weight_proof_threshold: 2,
198        blocks_cache_size: 4608 + (128 * 4),
199        weight_proof_recent_blocks: 1000,
200        max_block_count_per_requests: 32,
201        pool_sub_slot_iters: 37_600_000_000,
202        plot_filter_128_height: 0xffff_ffff,
203        plot_filter_64_height: 0xffff_ffff,
204        plot_filter_32_height: 0xffff_ffff,
205        plot_difficulty_initial: 2,
206        plot_difficulty_4_height: 0xffff_ffff,
207        plot_difficulty_5_height: 0xffff_ffff,
208        plot_difficulty_6_height: 0xffff_ffff,
209        plot_difficulty_7_height: 0xffff_ffff,
210        plot_difficulty_8_height: 0xffff_ffff,
211    },
212};
213
214// =============================================================================
215// NAT-traversal relay endpoint
216//
217// A DIG Node behind NAT cannot accept inbound dials, so it holds a constant
218// reservation with a publicly-reachable relay to stay discoverable. The
219// canonical public relay is `relay.dig.net`, serving the `RelayMessage`
220// WebSocket wire (RLY-001..RLY-007) on port 9450.
221//
222// This constant is the single source of truth for that endpoint so consumers
223// (`dig-node`, `dig-gossip`) don't each hardcode it. It MUST stay byte-identical
224// to `dig-node`'s `relay::DEFAULT_RELAY_URL` (the string a node actually dials
225// when `DIG_RELAY_URL` is unset) and to the `dig-relay` server's documented
226// client endpoint.
227//
228// Port 443: the live `relay.dig.net` NLB exposes its public TLS listener on the
229// standard HTTPS port 443 (the earlier :9450 listener is closed). Using 443 also
230// maximizes reachability from restrictive networks that only allow outbound 443.
231// =============================================================================
232
233/// Canonical DIG NAT-traversal relay endpoint.
234///
235/// This is the WebSocket URL a DIG Node dials by default to obtain a relay
236/// reservation (so NAT'd peers stay reachable). It is the value used unless an
237/// operator overrides it via the `DIG_RELAY_URL` environment variable (or
238/// disables the reservation with `DIG_RELAY_URL=off`).
239///
240/// Format: `wss://<host>:<port>` — the relay protocol (`RelayMessage`,
241/// RLY-001..RLY-007) is JSON over a secure WebSocket. Mainnet uses the canonical
242/// public deployment `relay.dig.net` on port 443 (the live NLB public TLS
243/// listener; the earlier :9450 listener is closed).
244///
245/// Kept byte-identical to `dig-node`'s `relay::DEFAULT_RELAY_URL` and the
246/// `dig-relay` server's documented client endpoint.
247pub const DIG_RELAY_URL: &str = "wss://relay.dig.net:443";
248
249// =============================================================================
250// DIG Node localhost endpoint
251//
252// A client connecting to a local DIG node (§5.3 client→node connection order)
253// resolves `dig.local` or `localhost` to reach the node via localhost TCP on
254// port 9778. This constant is the single source of truth for that port so
255// consumers (dig-node, dig-dns, dig-installer, SDK, CLI) don't each hardcode it.
256// =============================================================================
257
258/// The default localhost port a client uses to reach the local DIG node.
259///
260/// This is used to implement §5.3 client→node connection order: when a client
261/// needs to connect to a DIG node, it tries `dig.local` and `localhost` on this
262/// port before falling back to the public `rpc.dig.net` gateway. This constant
263/// ensures all consumers (dig-node, dig-dns, dig-installer, dig-sdk, digstore CLI)
264/// use an identical port, preventing port-mismatch bugs. It MUST stay byte-identical
265/// to `dig-node`'s documented localhost serve port and the installer's registered
266/// `dig.local` address.
267pub const DIG_NODE_PORT: u16 = 9778;
268
269// ---------------------------------------------------------------------------
270// DIG Testnet
271// ---------------------------------------------------------------------------
272
273/// Canonical DIG testnet genesis challenge.
274///
275/// Deterministically derived as `sha256(b"DIG_TESTNET:genesis:v1")` — distinct
276/// from mainnet so the two networks never share a `network_id`. Non-zero so the
277/// gossip network_id gate accepts it. Pre-launch canonical value (§3.7),
278/// revisable at true launch; all derived agg_sig data below follows it.
279///   = 088c18d6b7859d885dc2f03166e862c958f74b63b6353c3df71d103b9b806c3b
280const DIG_TESTNET_GENESIS_CHALLENGE: [u8; 32] =
281    hex!("088c18d6b7859d885dc2f03166e862c958f74b63b6353c3df71d103b9b806c3b");
282
283/// DIG testnet constants.
284///
285/// Same structure as mainnet but with a different genesis challenge.
286/// Useful for testing without risking mainnet state.
287pub const DIG_TESTNET: NetworkConstants = NetworkConstants {
288    inner: ConsensusConstants {
289        genesis_challenge: Bytes32::new(DIG_TESTNET_GENESIS_CHALLENGE),
290        // AGG_SIG_ME = genesis_challenge. Others = sha256(genesis || opcode_byte).
291        agg_sig_me_additional_data: Bytes32::new(DIG_TESTNET_GENESIS_CHALLENGE),
292        agg_sig_parent_additional_data: Bytes32::new(hex!(
293            "85b3963bdeb9848af970a9bbd1d36809ae41491ffd67aee7f27e8883936d495c"
294        )),
295        agg_sig_puzzle_additional_data: Bytes32::new(hex!(
296            "66aba1939e128e1465d58fde414325630e891747c1428d76ebce193cbe966301"
297        )),
298        agg_sig_amount_additional_data: Bytes32::new(hex!(
299            "eccab86920a6d982a68898b2dcb7c150383529fcd532fe84c693fb4592c38ae3"
300        )),
301        agg_sig_puzzle_amount_additional_data: Bytes32::new(hex!(
302            "eb088fad0d4caba66e29130fb07407e60a7545d035d19a188fef0855c874084e"
303        )),
304        agg_sig_parent_amount_additional_data: Bytes32::new(hex!(
305            "232aec0a351ba4936b04920e074aebcc621a458f6b1461c4b28c658552f2f35d"
306        )),
307        agg_sig_parent_puzzle_additional_data: Bytes32::new(hex!(
308            "96263ac395703ab9b3b0f0587e79185f4a9898574a28b4491015ddcf9d321873"
309        )),
310        // All other fields same as mainnet
311        max_block_cost_clvm: 11_000_000_000,
312        cost_per_byte: 12_000,
313        max_coin_amount: u64::MAX,
314        max_generator_size: 1_000_000,
315        max_generator_ref_list_size: 512,
316        hard_fork_height: 0,
317        hard_fork2_height: 0,
318        genesis_pre_farm_pool_puzzle_hash: Bytes32::new([0u8; 32]),
319        genesis_pre_farm_farmer_puzzle_hash: Bytes32::new([0u8; 32]),
320        slot_blocks_target: 32,
321        min_blocks_per_challenge_block: 16,
322        max_sub_slot_blocks: 128,
323        num_sps_sub_slot: 64,
324        sub_slot_iters_starting: 1 << 27,
325        difficulty_constant_factor: 1 << 67,
326        difficulty_starting: 7,
327        difficulty_change_max_factor: 3,
328        sub_epoch_blocks: 384,
329        epoch_blocks: 4608,
330        significant_bits: 8,
331        discriminant_size_bits: 1024,
332        number_zero_bits_plot_filter_v1: 9,
333        number_zero_bits_plot_filter_v2: 9,
334        min_plot_size_v1: 32,
335        max_plot_size_v1: 50,
336        min_plot_size_v2: 28,
337        max_plot_size_v2: 32,
338        sub_slot_time_target: 600,
339        num_sp_intervals_extra: 3,
340        max_future_time2: 120,
341        number_of_timestamps: 11,
342        max_vdf_witness_size: 64,
343        mempool_block_buffer: 10,
344        weight_proof_threshold: 2,
345        blocks_cache_size: 4608 + (128 * 4),
346        weight_proof_recent_blocks: 1000,
347        max_block_count_per_requests: 32,
348        pool_sub_slot_iters: 37_600_000_000,
349        plot_filter_128_height: 0xffff_ffff,
350        plot_filter_64_height: 0xffff_ffff,
351        plot_filter_32_height: 0xffff_ffff,
352        plot_difficulty_initial: 2,
353        plot_difficulty_4_height: 0xffff_ffff,
354        plot_difficulty_5_height: 0xffff_ffff,
355        plot_difficulty_6_height: 0xffff_ffff,
356        plot_difficulty_7_height: 0xffff_ffff,
357        plot_difficulty_8_height: 0xffff_ffff,
358    },
359};
360
361#[cfg(test)]
362mod tests {
363    use super::*;
364
365    /// The canonical relay endpoint must equal exactly what a DIG Node dials by
366    /// default. This pins the value byte-for-byte against `dig-node`'s
367    /// `relay::DEFAULT_RELAY_URL` (`wss://relay.dig.net:9450`) and the
368    /// `dig-relay` server's documented client endpoint. If either side ever
369    /// changes the scheme, host, or port, this guard fails so the shared
370    /// contract can't silently drift.
371    #[test]
372    fn dig_relay_url_is_canonical_endpoint() {
373        assert_eq!(DIG_RELAY_URL, "wss://relay.dig.net:443");
374    }
375
376    /// The relay endpoint is a secure-WebSocket URL pointing at the canonical
377    /// public host on the relay protocol port.
378    #[test]
379    fn dig_relay_url_is_well_formed() {
380        assert!(
381            DIG_RELAY_URL.starts_with("wss://"),
382            "relay must use secure WebSocket"
383        );
384        assert!(
385            DIG_RELAY_URL.contains("relay.dig.net"),
386            "relay must point at the canonical host"
387        );
388        assert!(
389            DIG_RELAY_URL.ends_with(":443"),
390            "relay must use the live NLB public TLS port 443"
391        );
392    }
393
394    /// The DIG node localhost port must equal the expected default.
395    ///
396    /// This guards against accidental mutations and ensures all consumers
397    /// (dig-node, dig-dns, dig-installer, dig-sdk, digstore) use a consistent
398    /// port when connecting to the local node on `dig.local` or `localhost`.
399    #[test]
400    fn dig_node_port_is_canonical() {
401        assert_eq!(DIG_NODE_PORT, 9778);
402    }
403
404    // -- Genesis challenge canonical-value guards --------------------------
405    //
406    // These pin the pre-launch canonical genesis challenges byte-for-byte AND
407    // prove they are reproducible from their documented preimages, so the
408    // values can never silently drift (a drift changes every derived signature
409    // domain + the gossip network_id — a cross-repo breaking event).
410
411    use sha2::{Digest, Sha256};
412
413    /// AGG_SIG opcode bytes, per §4.2 of `SPEC.md` (Chia L1 `condition_tools`).
414    const AGG_SIG_OPCODES: [u8; 6] = [43, 44, 45, 46, 47, 48];
415
416    fn sha256(bytes: &[u8]) -> [u8; 32] {
417        let mut hasher = Sha256::new();
418        hasher.update(bytes);
419        hasher.finalize().into()
420    }
421
422    /// The genesis MUST be non-zero: `dig-gossip` rejects an all-zero
423    /// `network_id`, so a zero genesis would stop the node's gossip pool / DHT /
424    /// PEX from ever starting. This is the connect-enabler invariant.
425    #[test]
426    fn genesis_challenges_are_non_zero() {
427        assert_ne!(DIG_MAINNET.genesis_challenge(), Bytes32::new([0u8; 32]));
428        assert_ne!(DIG_TESTNET.genesis_challenge(), Bytes32::new([0u8; 32]));
429    }
430
431    /// The mainnet genesis is pinned to the Chia mainnet header hash @ height
432    /// 9,021,277 (a real anchored value), and the testnet genesis is the
433    /// reproducible `sha256` of its documented preimage. These pin both values
434    /// byte-for-byte so neither can silently drift.
435    #[test]
436    fn genesis_challenges_are_the_pinned_values() {
437        assert_eq!(
438            DIG_MAINNET_GENESIS_CHALLENGE,
439            hex_literal::hex!("0af981862a4df51f51ec59c312315d959931d917c375730b89b9e2b0854d1abf"),
440        );
441        assert_eq!(
442            DIG_TESTNET_GENESIS_CHALLENGE,
443            sha256(b"DIG_TESTNET:genesis:v1"),
444        );
445    }
446
447    /// Mainnet and testnet MUST NOT share a genesis (no cross-network replay).
448    #[test]
449    fn mainnet_and_testnet_genesis_differ() {
450        assert_ne!(
451            DIG_MAINNET.genesis_challenge(),
452            DIG_TESTNET.genesis_challenge(),
453        );
454    }
455
456    /// Every baked-in AGG_SIG additional-data value MUST equal the §4.1 rule
457    /// applied to the network's genesis: AGG_SIG_ME == genesis, and each other
458    /// variant == `sha256(genesis || opcode_byte)`. This regenerates the values
459    /// independently and asserts the constants match — so a genesis change that
460    /// forgets to recompute a derived value is caught.
461    #[test]
462    fn agg_sig_additional_data_matches_derivation_rule() {
463        for net in [&DIG_MAINNET, &DIG_TESTNET] {
464            let genesis = net.genesis_challenge();
465            assert_eq!(net.agg_sig_me_additional_data(), genesis);
466
467            let c = net.consensus();
468            let derived: Vec<Bytes32> = AGG_SIG_OPCODES
469                .iter()
470                .map(|&op| {
471                    let mut preimage = genesis.as_ref().to_vec();
472                    preimage.push(op);
473                    Bytes32::new(sha256(&preimage))
474                })
475                .collect();
476            assert_eq!(c.agg_sig_parent_additional_data, derived[0]);
477            assert_eq!(c.agg_sig_puzzle_additional_data, derived[1]);
478            assert_eq!(c.agg_sig_amount_additional_data, derived[2]);
479            assert_eq!(c.agg_sig_puzzle_amount_additional_data, derived[3]);
480            assert_eq!(c.agg_sig_parent_amount_additional_data, derived[4]);
481            assert_eq!(c.agg_sig_parent_puzzle_additional_data, derived[5]);
482        }
483    }
484}