dig_network_block/lib.rs
1//! dig-network-block crate library entry point.
2//!
3//! This crate provides primitives for defining and hashing an L2 block structure.
4//!
5//! Current modules implemented:
6//! - `serde_hex`: Serde helpers to encode/decode byte arrays and vectors as 0x-prefixed hex.
7//! - `dig_l2_definition`: CAPITALIZED spec functions (hash domains, Merkle, roots, consensus emissions tuples).
8//! - `emission`, `body`, `header`, `block`: core L2 types each with `calculate_root()`.
9//!
10//! # Example
11//!
12//! The following example shows how to create a DIG L2 block, compute and print
13//! its root hash, serialize it to JSON, restore it back from JSON, compute the
14//! root again, print it, and assert that both roots are equal.
15//!
16//! ```rust
17//! use dig_network_block::block::{DigL2Block, BuildL2BlockArgs};
18//! use dig_network_block::emission_config::ConsensusEmissionConfig;
19//!
20//! // 1) Create a DIG block
21//! let version = 1u32;
22//! let network_id = [1u8; 32];
23//! let epoch = 42u64;
24//! let prev_block_root = [0u8; 32];
25//! let proposer_pubkey = [9u8; 48];
26//! let data = vec![1u8, 2, 3, 4, 5];
27//! let extra_emissions = vec![]; // none for this example
28//! let attesters: Vec<[u8; 48]> = vec![]; // no attesters
29//! let cfg = ConsensusEmissionConfig::new(12, 0); // zero attester share since there are no attesters
30//! let args = BuildL2BlockArgs {
31//! version,
32//! network_id,
33//! epoch,
34//! prev_block_root,
35//! proposer_pubkey,
36//! data,
37//! extra_emissions,
38//! attester_pubkeys: &attesters,
39//! cfg: &cfg,
40//! };
41//!
42//! let block = DigL2Block::build(&args).unwrap();
43//!
44//! // 2) Take its root hash and print it
45//! let root1 = block.calculate_root();
46//! println!("root1: {:?}", root1); // prints the 32-byte hash as a byte array
47//!
48//! // 3) Serialize the block and print the JSON
49//! let json = serde_json::to_string_pretty(&block).unwrap(); // requires serde_json
50//! println!("json: {}", json);
51//!
52//! // 4) Use the JSON to re-instantiate a new block from the original
53//! let block2: DigL2Block = serde_json::from_str(&json).unwrap();
54//!
55//! // 5) Take the root hash of the re-instantiated block, print it
56//! let root2 = block2.calculate_root();
57//! println!("root2: {:?}", root2);
58//!
59//! // 6) Assert the original hash equals the new one
60//! assert_eq!(root1, root2);
61//! ```
62
63pub mod block;
64pub mod body;
65pub mod dig_l2_definition;
66pub mod emission;
67pub mod emission_config;
68pub mod header;
69pub mod serde_hex;