dusk_node/chain.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
mod acceptor;
mod consensus;
mod fallback;
mod fsm;
mod genesis;
mod header_validation;
mod metrics;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Result;
use async_trait::async_trait;
use dusk_consensus::config::is_emergency_block;
use dusk_consensus::errors::ConsensusError;
use dusk_core::signatures::bls::PublicKey as BlsPublicKey;
pub use header_validation::verify_att;
use node_data::events::Event;
use node_data::ledger::{to_str, BlockWithLabel, Label};
use node_data::message::payload::RatificationResult;
use node_data::message::{AsyncQueue, Payload, Topics};
use tokio::sync::mpsc::Sender;
use tokio::sync::RwLock;
use tokio::time::{sleep_until, Instant};
use tracing::{debug, error, info, warn};
use self::acceptor::Acceptor;
use self::fsm::SimpleFSM;
use crate::database::rocksdb::MD_HASH_KEY;
use crate::database::{Ledger, Metadata};
use crate::{database, vm, LongLivedService, Message, Network};
const TOPICS: &[u8] = &[
Topics::Block as u8,
Topics::Candidate as u8,
Topics::Validation as u8,
Topics::Ratification as u8,
Topics::Quorum as u8,
Topics::ValidationQuorum as u8,
];
const HEARTBEAT_SEC: Duration = Duration::from_secs(3);
pub struct ChainSrv<N: Network, DB: database::DB, VM: vm::VMExecution> {
/// Inbound wire messages queue
inbound: AsyncQueue<Message>,
keys_path: String,
acceptor: Option<Arc<RwLock<Acceptor<N, DB, VM>>>>,
max_consensus_queue_size: usize,
/// Sender channel for sending out RUES events
event_sender: Sender<Event>,
genesis_timestamp: u64,
dusk_key: BlsPublicKey,
}
#[async_trait]
impl<N: Network, DB: database::DB, VM: vm::VMExecution>
LongLivedService<N, DB, VM> for ChainSrv<N, DB, VM>
{
async fn initialize(
&mut self,
network: Arc<RwLock<N>>,
db: Arc<RwLock<DB>>,
vm: Arc<RwLock<VM>>,
) -> anyhow::Result<()> {
let tip = Self::load_tip(
db.read().await.deref(),
vm.read().await.deref(),
self.genesis_timestamp,
)
.await?;
let state_hash = tip.inner().header().state_hash;
let provisioners_list = vm.read().await.get_provisioners(state_hash)?;
// Initialize Acceptor
let acc = Acceptor::init_consensus(
&self.keys_path,
tip,
provisioners_list,
db,
network,
vm,
self.max_consensus_queue_size,
self.event_sender.clone(),
self.dusk_key,
)
.await?;
self.acceptor = Some(Arc::new(RwLock::new(acc)));
Ok(())
}
async fn execute(
&mut self,
network: Arc<RwLock<N>>,
_db: Arc<RwLock<DB>>,
_vm: Arc<RwLock<VM>>,
) -> anyhow::Result<usize> {
// Register routes
LongLivedService::<N, DB, VM>::add_routes(
self,
TOPICS,
self.inbound.clone(),
&network,
)
.await?;
let acc = self.acceptor.as_mut().expect("initialize is called");
acc.write().await.spawn_task().await;
// Start-up FSM instance
let mut fsm = SimpleFSM::new(acc.clone(), network.clone()).await;
let outbound_chan = acc.read().await.get_outbound_chan().await;
let result_chan = acc.read().await.get_result_chan().await;
let mut heartbeat = Instant::now().checked_add(HEARTBEAT_SEC).unwrap();
// Message loop for Chain context
loop {
tokio::select! {
biased;
// Receives results from the upper layer
recv = result_chan.recv() => {
match recv? {
Err(ConsensusError::Canceled(round)) => {
debug!(event = "consensus canceled", round);
}
Err(err) => {
// Internal consensus execution has terminated with an error
error!(event = "failed_consensus", ?err);
fsm.on_failed_consensus().await;
}
_ => {}
}
},
// Handles any inbound wire.
recv = self.inbound.recv() => {
let msg = recv?;
match msg.payload {
Payload::Candidate(_)
| Payload::Validation(_)
| Payload::Ratification(_)
| Payload::ValidationQuorum(_) => {
self.reroute_acceptor(msg).await;
}
Payload::Quorum(ref q) => {
fsm.on_quorum(q, msg.metadata.as_ref()).await;
self.reroute_acceptor(msg).await;
}
Payload::Block(blk) => {
info!(
event = "New block",
src = "Block msg",
height = blk.header().height,
iter = blk.header().iteration,
hash = to_str(&blk.header().hash),
metadata = ?msg.metadata,
);
// Handle a block that originates from a network peer.
// By disabling block broadcast, a block may be received
// from a peer only after explicit request (on demand).
match fsm.on_block_event(*blk, msg.metadata.clone()).await {
Ok(res) => {
if let Some(accepted_blk) = res {
// Repropagate Emergency Blocks
// We already know it's valid because we accepted it
if is_emergency_block(accepted_blk.header().iteration){
// We build a new `msg` to avoid cloning `blk` when
// passing it to `on_block_event`.
// We copy the metadata to keep the original ray_id.
let mut eb_msg = Message::from(accepted_blk);
eb_msg.metadata = msg.metadata;
if let Err(e) = network.read().await.broadcast(&eb_msg).await {
warn!("Unable to re-broadcast Emergency Block: {e}");
}
}
}
}
Err(err) => {
error!(event = "fsm::on_event failed", src = "wire", err = ?err);
}
}
}
_ => {
warn!("invalid inbound message");
},
}
},
// Re-routes messages originated from Consensus (upper) layer to the network layer.
recv = outbound_chan.recv() => {
let msg = recv?;
// Handle quorum messages from Consensus layer.
// If the associated candidate block already exists,
// the winner block will be compiled and redirected to the Acceptor.
if let Payload::Quorum(quorum) = &msg.payload {
if let RatificationResult::Success(_) = quorum.att.result {
fsm.on_success_quorum(quorum, msg.metadata.clone()).await;
}
}
if let Payload::GetResource(res) = &msg.payload {
if let Err(e) = network.read().await.flood_request(res.get_inv(), None, 16).await {
warn!("Unable to re-route message {e}");
}
} else if let Err(e) = network.read().await.broadcast(&msg).await {
warn!("Unable to broadcast message {e}");
}
},
// Handles heartbeat event
_ = sleep_until(heartbeat) => {
if let Err(err) = fsm.on_heartbeat_event().await {
error!(event = "heartbeat_failed", ?err);
}
heartbeat = Instant::now().checked_add(HEARTBEAT_SEC).unwrap();
},
}
}
}
/// Returns service name.
fn name(&self) -> &'static str {
"chain"
}
}
impl<N: Network, DB: database::DB, VM: vm::VMExecution> ChainSrv<N, DB, VM> {
pub fn new(
keys_path: String,
max_inbound_size: usize,
event_sender: Sender<Event>,
genesis_timestamp: u64,
dusk_key: BlsPublicKey,
) -> Self {
info!(
"ChainSrv::new with keys_path: {}, max_inbound_size: {}",
keys_path, max_inbound_size
);
Self {
inbound: AsyncQueue::bounded(max_inbound_size, "chain_inbound"),
keys_path,
acceptor: None,
max_consensus_queue_size: max_inbound_size,
event_sender,
genesis_timestamp,
dusk_key,
}
}
/// Load both the chain tip and last finalized block from persisted ledger.
///
/// Panics
///
/// If register entry is read but block is not found.
async fn load_tip(
db: &DB,
vm: &VM,
genesis_timestamp: u64,
) -> Result<BlockWithLabel> {
let stored_block = db.view(|t| {
anyhow::Ok(t.op_read(MD_HASH_KEY)?.and_then(|tip_hash| {
t.block(&tip_hash[..])
.expect("block to be found if metadata is set")
}))
})?;
let block = match stored_block {
Some(blk) => {
let (_, label) = db
.view(|t| t.block_label_by_height(blk.header().height))?
.unwrap();
BlockWithLabel::new_with_label(blk, label)
}
None => {
// Lack of register record means the loaded database is
// either malformed or empty.
let state = vm.get_state_root()?;
let genesis_blk =
genesis::generate_block(state, genesis_timestamp);
db.update(|t| {
// Persist genesis block
t.store_block(
genesis_blk.header(),
&[],
&[],
Label::Final(0),
)
})?;
BlockWithLabel::new_with_label(genesis_blk, Label::Final(0))
}
};
let block_header = block.inner().header();
tracing::info!(
event = "Ledger block loaded",
height = block_header.height,
hash = hex::encode(block_header.hash),
state_root = hex::encode(block_header.state_hash),
label = ?block.label()
);
Ok(block)
}
pub async fn revert_last_final(&self) -> anyhow::Result<()> {
self.acceptor
.as_ref()
.expect("Chain to be initialized")
.read()
.await
.try_revert(acceptor::RevertTarget::LastFinalizedState)
.await
}
async fn reroute_acceptor(&self, msg: Message) {
debug!(
event = "Consensus message received",
topic = ?msg.topic(),
info = ?msg.header,
metadata = ?msg.metadata,
);
// Re-route message to the Consensus
let acc = self.acceptor.as_ref().expect("initialize is called");
if let Err(e) = acc.read().await.reroute_msg(msg).await {
warn!("Could not reroute msg to Consensus: {}", e);
}
}
}