From de57beccc979fcb98fa632ba37845086a51a8b65 Mon Sep 17 00:00:00 2001 From: Federico Franzoni <8609060+fed-franz@users.noreply.github.com> Date: Mon, 2 Sep 2024 15:48:13 +0200 Subject: [PATCH] node: store and load last_iteration --- node/src/chain/consensus.rs | 40 ++++++++++++++++++++++++++++++++++-- node/src/database/rocksdb.rs | 1 + 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/node/src/chain/consensus.rs b/node/src/chain/consensus.rs index d851447a4f..176a5570a1 100644 --- a/node/src/chain/consensus.rs +++ b/node/src/chain/consensus.rs @@ -14,7 +14,7 @@ use dusk_consensus::operations::{ }; use dusk_consensus::queue::MsgRegistry; use dusk_consensus::user::provisioners::ContextProvisioners; -use node_data::ledger::{Block, Fault, Header}; +use node_data::ledger::{Block, Fault, Hash, Header}; use node_data::message::AsyncQueue; use tokio::sync::{oneshot, Mutex, RwLock}; @@ -24,7 +24,7 @@ use tracing::{debug, info, trace, warn}; use crate::chain::header_validation::Validator; use crate::chain::metrics::AverageElapsedTime; use crate::database::rocksdb::{ - MD_AVG_PROPOSAL, MD_AVG_RATIFICATION, MD_AVG_VALIDATION, + MD_AVG_PROPOSAL, MD_AVG_RATIFICATION, MD_AVG_VALIDATION, MD_LAST_ITER, }; use metrics::gauge; use node_data::{ledger, Serializable, StepName}; @@ -211,6 +211,42 @@ impl dusk_consensus::commons::Database for CandidateDB { } } } + async fn get_last_iter(&self) -> (Hash, u8) { + let data = self + .db + .read() + .await + .view(|t| t.op_read(MD_LAST_ITER)) + .unwrap_or_else(|e| { + warn!("Cannot read last_iter from database {e:?}"); + None + }) + .filter(|v| v.len() == 33) + .unwrap_or_else(|| { + warn!("No last_iter saved, falling back to default"); + [0u8; 33].to_vec() + }); + + let mut hash = [0u8; 32]; + hash.copy_from_slice(&data[0..32]); + + let iter = data[32]; + + (hash, iter) + } + async fn store_last_iter(&mut self, (hash, iter): (Hash, u8)) { + let mut to_store = hash.to_vec(); + to_store.push(iter); + + if let Err(e) = self + .db + .read() + .await + .update(|t| t.op_write(MD_LAST_ITER, to_store)) + { + warn!("Cannot write last_iter to database {e:?}"); + } + } } /// Implements Executor trait to mock Contract Storage calls. diff --git a/node/src/database/rocksdb.rs b/node/src/database/rocksdb.rs index 08c7723444..e3b8b83e9d 100644 --- a/node/src/database/rocksdb.rs +++ b/node/src/database/rocksdb.rs @@ -53,6 +53,7 @@ pub const MD_STATE_ROOT_KEY: &[u8] = b"state_hash_key"; pub const MD_AVG_VALIDATION: &[u8] = b"avg_validation_time"; pub const MD_AVG_RATIFICATION: &[u8] = b"avg_ratification_time"; pub const MD_AVG_PROPOSAL: &[u8] = b"avg_proposal_time"; +pub const MD_LAST_ITER: &[u8] = b"consensus_last_iter"; #[derive(Clone)] pub struct Backend {