|
1 | | -mod ouroboros; |
| 1 | +//! Acropolis Block KES Validator module for Caryatid |
| 2 | +//! Validate KES signatures in the block header |
| 3 | +
|
| 4 | +use acropolis_common::{ |
| 5 | + messages::{CardanoMessage, Message}, |
| 6 | + state_history::{StateHistory, StateHistoryStore}, |
| 7 | + BlockInfo, BlockStatus, |
| 8 | +}; |
| 9 | +use anyhow::Result; |
| 10 | +use caryatid_sdk::{module, Context, Module, Subscription}; |
| 11 | +use config::Config; |
| 12 | +use std::sync::Arc; |
| 13 | +use tokio::sync::Mutex; |
| 14 | +use tracing::{error, info, info_span, Instrument}; |
2 | 15 | mod state; |
| 16 | +use state::State; |
| 17 | + |
| 18 | +use crate::kes_validation_publisher::KesValidationPublisher; |
| 19 | +mod kes_validation_publisher; |
| 20 | +mod ouroboros; |
| 21 | + |
| 22 | +const DEFAULT_VALIDATION_KES_PUBLISHER_TOPIC: (&str, &str) = |
| 23 | + ("validation-kes-publisher-topic", "cardano.validation.kes"); |
| 24 | + |
| 25 | +const DEFAULT_BOOTSTRAPPED_SUBSCRIBE_TOPIC: (&str, &str) = ( |
| 26 | + "bootstrapped-subscribe-topic", |
| 27 | + "cardano.sequence.bootstrapped", |
| 28 | +); |
| 29 | +const DEFAULT_PROTOCOL_PARAMETERS_SUBSCRIBE_TOPIC: (&str, &str) = ( |
| 30 | + "protocol-parameters-subscribe-topic", |
| 31 | + "cardano.protocol.parameters", |
| 32 | +); |
| 33 | +const DEFAULT_BLOCKS_SUBSCRIBE_TOPIC: (&str, &str) = |
| 34 | + ("blocks-subscribe-topic", "cardano.block.proposed"); |
| 35 | + |
| 36 | +/// Block KES Validator module |
| 37 | +#[module( |
| 38 | + message_type(Message), |
| 39 | + name = "block-kes-validator", |
| 40 | + description = "Validate the KES signatures in the block header" |
| 41 | +)] |
| 42 | + |
| 43 | +pub struct BlockKesValidator; |
| 44 | + |
| 45 | +impl BlockKesValidator { |
| 46 | + #[allow(clippy::too_many_arguments)] |
| 47 | + async fn run( |
| 48 | + history: Arc<Mutex<StateHistory<State>>>, |
| 49 | + mut kes_validation_publisher: KesValidationPublisher, |
| 50 | + mut bootstrapped_subscription: Box<dyn Subscription<Message>>, |
| 51 | + mut blocks_subscription: Box<dyn Subscription<Message>>, |
| 52 | + mut protocol_parameters_subscription: Box<dyn Subscription<Message>>, |
| 53 | + ) -> Result<()> { |
| 54 | + let (_, bootstrapped_message) = bootstrapped_subscription.read().await?; |
| 55 | + let genesis = match bootstrapped_message.as_ref() { |
| 56 | + Message::Cardano((_, CardanoMessage::GenesisComplete(complete))) => { |
| 57 | + complete.values.clone() |
| 58 | + } |
| 59 | + _ => panic!("Unexpected message in genesis completion topic: {bootstrapped_message:?}"), |
| 60 | + }; |
| 61 | + |
| 62 | + // Consume initial protocol parameters |
| 63 | + let _ = protocol_parameters_subscription.read().await?; |
| 64 | + |
| 65 | + loop { |
| 66 | + // Get a mutable state |
| 67 | + let mut state = history.lock().await.get_or_init_with(State::new); |
| 68 | + let mut current_block: Option<BlockInfo> = None; |
| 69 | + |
| 70 | + let (_, message) = blocks_subscription.read().await?; |
| 71 | + match message.as_ref() { |
| 72 | + Message::Cardano((block_info, CardanoMessage::BlockAvailable(block_msg))) => { |
| 73 | + // handle rollback here |
| 74 | + if block_info.status == BlockStatus::RolledBack { |
| 75 | + state = history.lock().await.get_rolled_back_state(block_info.number); |
| 76 | + } |
| 77 | + current_block = Some(block_info.clone()); |
| 78 | + let is_new_epoch = block_info.new_epoch && block_info.epoch > 0; |
| 79 | + |
| 80 | + if is_new_epoch { |
| 81 | + // read epoch boundary messages |
| 82 | + let protocol_parameters_message_f = protocol_parameters_subscription.read(); |
| 83 | + |
| 84 | + let (_, protocol_parameters_msg) = protocol_parameters_message_f.await?; |
| 85 | + let span = info_span!( |
| 86 | + "block_kes_validator.handle_protocol_parameters", |
| 87 | + epoch = block_info.epoch |
| 88 | + ); |
| 89 | + span.in_scope(|| match protocol_parameters_msg.as_ref() { |
| 90 | + Message::Cardano((block_info, CardanoMessage::ProtocolParams(msg))) => { |
| 91 | + Self::check_sync(¤t_block, block_info); |
| 92 | + state.handle_protocol_parameters(msg); |
| 93 | + } |
| 94 | + _ => error!("Unexpected message type: {protocol_parameters_msg:?}"), |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + let span = |
| 99 | + info_span!("block_kes_validator.validate", block = block_info.number); |
| 100 | + async { |
| 101 | + let result = state |
| 102 | + .validate_block_kes(block_info, &block_msg.header, &genesis) |
| 103 | + .map_err(|e| *e); |
| 104 | + if let Err(e) = kes_validation_publisher |
| 105 | + .publish_kes_validation(block_info, result) |
| 106 | + .await |
| 107 | + { |
| 108 | + error!("Failed to publish KES validation: {e}") |
| 109 | + } |
| 110 | + } |
| 111 | + .instrument(span) |
| 112 | + .await; |
| 113 | + } |
| 114 | + _ => error!("Unexpected message type: {message:?}"), |
| 115 | + } |
| 116 | + |
| 117 | + // Commit the new state |
| 118 | + if let Some(block_info) = current_block { |
| 119 | + history.lock().await.commit(block_info.number, state); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + pub async fn init(&self, context: Arc<Context<Message>>, config: Arc<Config>) -> Result<()> { |
| 125 | + // Publish topics |
| 126 | + let validation_kes_publisher_topic = config |
| 127 | + .get_string(DEFAULT_VALIDATION_KES_PUBLISHER_TOPIC.0) |
| 128 | + .unwrap_or(DEFAULT_VALIDATION_KES_PUBLISHER_TOPIC.1.to_string()); |
| 129 | + info!("Creating validation KES publisher on '{validation_kes_publisher_topic}'"); |
| 130 | + |
| 131 | + // Subscribe topics |
| 132 | + let bootstrapped_subscribe_topic = config |
| 133 | + .get_string(DEFAULT_BOOTSTRAPPED_SUBSCRIBE_TOPIC.0) |
| 134 | + .unwrap_or(DEFAULT_BOOTSTRAPPED_SUBSCRIBE_TOPIC.1.to_string()); |
| 135 | + info!("Creating subscriber for bootstrapped on '{bootstrapped_subscribe_topic}'"); |
| 136 | + let protocol_parameters_subscribe_topic = config |
| 137 | + .get_string(DEFAULT_PROTOCOL_PARAMETERS_SUBSCRIBE_TOPIC.0) |
| 138 | + .unwrap_or(DEFAULT_PROTOCOL_PARAMETERS_SUBSCRIBE_TOPIC.1.to_string()); |
| 139 | + info!("Creating subscriber for protocol parameters on '{protocol_parameters_subscribe_topic}'"); |
| 140 | + |
| 141 | + let blocks_subscribe_topic = config |
| 142 | + .get_string(DEFAULT_BLOCKS_SUBSCRIBE_TOPIC.0) |
| 143 | + .unwrap_or(DEFAULT_BLOCKS_SUBSCRIBE_TOPIC.1.to_string()); |
| 144 | + info!("Creating blocks subscription on '{blocks_subscribe_topic}'"); |
| 145 | + |
| 146 | + // publishers |
| 147 | + let kes_validation_publisher = |
| 148 | + KesValidationPublisher::new(context.clone(), validation_kes_publisher_topic); |
| 149 | + |
| 150 | + // Subscribers |
| 151 | + let bootstrapped_subscription = context.subscribe(&bootstrapped_subscribe_topic).await?; |
| 152 | + let protocol_parameters_subscription = |
| 153 | + context.subscribe(&protocol_parameters_subscribe_topic).await?; |
| 154 | + let blocks_subscription = context.subscribe(&blocks_subscribe_topic).await?; |
| 155 | + |
| 156 | + // state history |
| 157 | + let history = Arc::new(Mutex::new(StateHistory::<State>::new( |
| 158 | + "block_kes_validator", |
| 159 | + StateHistoryStore::default_block_store(), |
| 160 | + ))); |
| 161 | + |
| 162 | + // Start run task |
| 163 | + context.run(async move { |
| 164 | + Self::run( |
| 165 | + history, |
| 166 | + kes_validation_publisher, |
| 167 | + bootstrapped_subscription, |
| 168 | + blocks_subscription, |
| 169 | + protocol_parameters_subscription, |
| 170 | + ) |
| 171 | + .await |
| 172 | + .unwrap_or_else(|e| error!("Failed: {e}")); |
| 173 | + }); |
| 174 | + |
| 175 | + Ok(()) |
| 176 | + } |
| 177 | + |
| 178 | + /// Check for synchronisation |
| 179 | + fn check_sync(expected: &Option<BlockInfo>, actual: &BlockInfo) { |
| 180 | + if let Some(ref block) = expected { |
| 181 | + if block.number != actual.number { |
| 182 | + error!( |
| 183 | + expected = block.number, |
| 184 | + actual = actual.number, |
| 185 | + "Messages out of sync" |
| 186 | + ); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments