Skip to content

Commit

Permalink
node: store and load last_iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
fed-franz committed Sep 6, 2024
1 parent af9b367 commit 69c2165
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
40 changes: 38 additions & 2 deletions node/src/chain/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -211,6 +211,42 @@ impl<DB: database::DB> dusk_consensus::commons::Database for CandidateDB<DB> {
}
}
}
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.
Expand Down
1 change: 1 addition & 0 deletions node/src/database/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 69c2165

Please sign in to comment.