diff --git a/consensus/src/commons.rs b/consensus/src/commons.rs index 558d5aa63e..36f06a5734 100644 --- a/consensus/src/commons.rs +++ b/consensus/src/commons.rs @@ -139,6 +139,8 @@ impl From for ConsensusError { #[async_trait::async_trait] pub trait Database: Send + Sync { fn store_candidate_block(&mut self, b: Block); + async fn get_last_iter(&self) -> (Hash, u8); + async fn store_last_iter(&mut self, data: (Hash, u8)); } #[derive(Clone)] diff --git a/consensus/src/consensus.rs b/consensus/src/consensus.rs index b264f336d2..9ee27b1a75 100644 --- a/consensus/src/consensus.rs +++ b/consensus/src/consensus.rs @@ -185,13 +185,13 @@ impl Consensus { ru.base_timeouts.clone(), ); - // TODO: load save iteration - let saved_iter = 0; + let (prev_block_hash, saved_iter) = + db.lock().await.get_last_iter().await; - // If starting from `saved_iter`, we regenerate all committees - // in case they are needed to process past-iteration messages in - // Emergency Mode - if saved_iter != 0 { + if ru.hash() == prev_block_hash { + // If starting from `saved_iter`, we regenerate all committees + // in case they are needed to process past-iteration messages in + // Emergency Mode while iter <= saved_iter { iter_ctx.on_begin(iter); iter_ctx.generate_committee( @@ -209,13 +209,13 @@ impl Consensus { provisioners.as_ref(), ru.seed(), ); + iter = saved_iter + 1; } - - iter = saved_iter + 1; } loop { Self::consensus_delay().await; + db.lock().await.store_last_iter((ru.hash(), iter)).await; iter_ctx.on_begin(iter);