|
| 1 | +//! Genesis block creation for V2 consensus layer |
| 2 | +//! |
| 3 | +//! This module handles creating the genesis block (height 0) by querying |
| 4 | +//! the execution layer for block #0 and wrapping it in a ConsensusBlock. |
| 5 | +//! |
| 6 | +//! The genesis block serves as the common foundation that all validator nodes |
| 7 | +//! share, ensuring consensus starts from the same state. |
| 8 | +
|
| 9 | +use crate::actors_v2::chain::ChainError; |
| 10 | +use crate::actors_v2::engine::EngineActor; |
| 11 | +use crate::block::SignedConsensusBlock; |
| 12 | +use crate::spec::ChainSpec; |
| 13 | +use actix::Addr; |
| 14 | +use lighthouse_wrapper::types::MainnetEthSpec; |
| 15 | +use tracing::{debug, info}; |
| 16 | + |
| 17 | +/// Create genesis block by querying execution layer for block #0 |
| 18 | +/// |
| 19 | +/// This function: |
| 20 | +/// 1. Queries the execution layer (Reth/Geth) for block #0 |
| 21 | +/// 2. Wraps the execution payload in a ConsensusBlock structure |
| 22 | +/// 3. Returns a genesis block ready for storage |
| 23 | +/// |
| 24 | +/// # Genesis Block Properties |
| 25 | +/// - Height: 0 |
| 26 | +/// - Slot: 0 |
| 27 | +/// - Parent hash: 0x0000...0000 (genesis has no parent) |
| 28 | +/// - Execution payload: Retrieved from execution layer block #0 |
| 29 | +/// - Signature: Empty (genesis is not signed by any authority) |
| 30 | +/// |
| 31 | +/// # Determinism |
| 32 | +/// All nodes using the same genesis.json will produce identical genesis blocks |
| 33 | +/// because the execution layer's block #0 is deterministically generated from |
| 34 | +/// the genesis.json configuration. |
| 35 | +/// |
| 36 | +/// # Arguments |
| 37 | +/// * `engine_actor` - Address of the EngineActor for querying execution layer |
| 38 | +/// * `chain_spec` - Chain specification (authorities, slot duration, etc.) |
| 39 | +/// |
| 40 | +/// # Returns |
| 41 | +/// - `Ok(SignedConsensusBlock)` - Genesis block ready for storage |
| 42 | +/// - `Err(ChainError)` - If execution layer query fails |
| 43 | +/// |
| 44 | +/// # Errors |
| 45 | +/// Returns `ChainError::Engine` if: |
| 46 | +/// - Cannot communicate with EngineActor |
| 47 | +/// - Execution layer doesn't have block #0 |
| 48 | +/// - Execution payload is invalid |
| 49 | +/// |
| 50 | +pub async fn create_genesis_block( |
| 51 | + engine_actor: &Addr<EngineActor>, |
| 52 | + chain_spec: ChainSpec, |
| 53 | +) -> Result<SignedConsensusBlock<MainnetEthSpec>, ChainError> { |
| 54 | + info!("Creating genesis block from execution layer"); |
| 55 | + |
| 56 | + // Query execution layer for block #0 |
| 57 | + // We use the GetPayloadByTag message which accepts "0x0" or "earliest" |
| 58 | + let get_genesis_msg = crate::actors_v2::engine::messages::EngineMessage::GetPayloadByTag { |
| 59 | + block_tag: "0x0".to_string(), // Query block #0 (genesis) |
| 60 | + correlation_id: Some(uuid::Uuid::new_v4()), |
| 61 | + }; |
| 62 | + |
| 63 | + debug!("Querying execution layer for block #0"); |
| 64 | + |
| 65 | + let execution_payload = match engine_actor.send(get_genesis_msg).await { |
| 66 | + Ok(Ok(crate::actors_v2::engine::messages::EngineResponse::PayloadByTag { payload })) => { |
| 67 | + // Extract the Capella payload |
| 68 | + match payload { |
| 69 | + lighthouse_wrapper::types::ExecutionPayload::Capella(capella_payload) => { |
| 70 | + info!( |
| 71 | + block_number = capella_payload.block_number, |
| 72 | + block_hash = %capella_payload.block_hash, |
| 73 | + "Retrieved execution layer block #0" |
| 74 | + ); |
| 75 | + capella_payload |
| 76 | + } |
| 77 | + _ => { |
| 78 | + return Err(ChainError::Engine( |
| 79 | + "Expected Capella execution payload for genesis".to_string(), |
| 80 | + )); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + Ok(Ok(_)) => { |
| 85 | + return Err(ChainError::Engine( |
| 86 | + "Unexpected response type from EngineActor".to_string(), |
| 87 | + )); |
| 88 | + } |
| 89 | + Ok(Err(e)) => { |
| 90 | + return Err(ChainError::Engine(format!( |
| 91 | + "Execution layer failed to provide block #0: {}", |
| 92 | + e |
| 93 | + ))); |
| 94 | + } |
| 95 | + Err(e) => { |
| 96 | + return Err(ChainError::NetworkError(format!( |
| 97 | + "Failed to communicate with EngineActor: {}", |
| 98 | + e |
| 99 | + ))); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + // Validate that we actually got block #0 |
| 104 | + if execution_payload.block_number != 0 { |
| 105 | + return Err(ChainError::InvalidBlock(format!( |
| 106 | + "Expected block #0 from execution layer, got block #{}", |
| 107 | + execution_payload.block_number |
| 108 | + ))); |
| 109 | + } |
| 110 | + |
| 111 | + // Wrap execution payload in a ConsensusBlock |
| 112 | + let genesis = SignedConsensusBlock::genesis(chain_spec, execution_payload); |
| 113 | + |
| 114 | + let genesis_hash = genesis.canonical_root(); |
| 115 | + let genesis_exec_hash = genesis.message.execution_payload.block_hash; |
| 116 | + |
| 117 | + info!( |
| 118 | + consensus_hash = %genesis_hash, |
| 119 | + execution_hash = %genesis_exec_hash, |
| 120 | + "Genesis block created successfully" |
| 121 | + ); |
| 122 | + |
| 123 | + Ok(genesis) |
| 124 | +} |
| 125 | + |
| 126 | +/// Check if genesis block exists in storage |
| 127 | +/// |
| 128 | +/// Helper function to determine if genesis has already been initialized. |
| 129 | +/// Used during ChainActor startup to decide whether to create genesis. |
| 130 | +/// |
| 131 | +/// # Arguments |
| 132 | +/// * `storage_actor` - Address of the StorageActor |
| 133 | +/// |
| 134 | +/// # Returns |
| 135 | +/// - `Ok(true)` - Genesis block exists in storage |
| 136 | +/// - `Ok(false)` - Genesis block does not exist |
| 137 | +/// - `Err(ChainError)` - Communication or query error |
| 138 | +/// |
| 139 | +pub async fn genesis_exists( |
| 140 | + storage_actor: &Addr<crate::actors_v2::storage::StorageActor>, |
| 141 | +) -> Result<bool, ChainError> { |
| 142 | + let get_genesis_msg = crate::actors_v2::storage::messages::GetBlockByHeightMessage { |
| 143 | + height: 0, |
| 144 | + correlation_id: Some(uuid::Uuid::new_v4()), |
| 145 | + }; |
| 146 | + |
| 147 | + match storage_actor.send(get_genesis_msg).await { |
| 148 | + Ok(Ok(Some(_))) => Ok(true), |
| 149 | + Ok(Ok(None)) => Ok(false), |
| 150 | + Ok(Err(e)) => Err(ChainError::Storage(format!( |
| 151 | + "Failed to query genesis from storage: {}", |
| 152 | + e |
| 153 | + ))), |
| 154 | + Err(e) => Err(ChainError::NetworkError(format!( |
| 155 | + "Failed to communicate with StorageActor: {}", |
| 156 | + e |
| 157 | + ))), |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +#[cfg(test)] |
| 162 | +mod tests { |
| 163 | + use super::*; |
| 164 | + use crate::aura::Authority; |
| 165 | + use lighthouse_wrapper::bls::Keypair; |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn test_genesis_has_zero_height() { |
| 169 | + use crate::block::ConsensusBlock; |
| 170 | + |
| 171 | + let block = ConsensusBlock::default(); |
| 172 | + let keypair = Keypair::random(); |
| 173 | + let authority = Authority { |
| 174 | + signer: keypair.clone(), |
| 175 | + index: 0, |
| 176 | + }; |
| 177 | + |
| 178 | + // Create a signed block with default values |
| 179 | + let signed_block = block.sign_block(&authority); |
| 180 | + |
| 181 | + // Default ConsensusBlock should have height 0 |
| 182 | + assert_eq!( |
| 183 | + signed_block.message.execution_payload.block_number, 0, |
| 184 | + "Default block should have height 0" |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn test_genesis_has_zero_parent_hash() { |
| 190 | + use crate::block::ConsensusBlock; |
| 191 | + use ethereum_types::H256; |
| 192 | + |
| 193 | + let block = ConsensusBlock::default(); |
| 194 | + let keypair = Keypair::random(); |
| 195 | + let authority = Authority { |
| 196 | + signer: keypair.clone(), |
| 197 | + index: 0, |
| 198 | + }; |
| 199 | + |
| 200 | + let signed_block = block.sign_block(&authority); |
| 201 | + |
| 202 | + // Genesis parent hash should be zero |
| 203 | + assert_eq!( |
| 204 | + signed_block.message.parent_hash, |
| 205 | + H256::zero(), |
| 206 | + "Genesis block should have zero parent hash" |
| 207 | + ); |
| 208 | + } |
| 209 | +} |
0 commit comments