From 367ea1a8378f829886ba2505b1163d05bd93d41f Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 5 Sep 2023 19:49:01 +0100 Subject: [PATCH] add method to init the hard fork struct at a certain height. --- consensus/src/bin/scan_chain.rs | 5 ++--- consensus/src/hardforks.rs | 38 ++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/consensus/src/bin/scan_chain.rs b/consensus/src/bin/scan_chain.rs index d8bfcdbd..8d71db7d 100644 --- a/consensus/src/bin/scan_chain.rs +++ b/consensus/src/bin/scan_chain.rs @@ -7,14 +7,13 @@ use std::pin::Pin; use std::task::{Context, Poll}; use tower::balance::p2c::Balance; use tower::discover::Change; -use tower::util::{BoxService}; +use tower::util::BoxService; use tracing::level_filters::LevelFilter; use monero_consensus::hardforks::{HardForkConfig, HardForks}; use monero_consensus::rpc::Rpc; - struct RpcDiscoverer(Vec, u64); impl Stream for RpcDiscoverer { @@ -80,7 +79,7 @@ async fn main() { let rpc_buffer = tower::buffer::Buffer::new(BoxService::new(rpc_balance), 3); let rpc = tower::retry::Retry::new(Attempts(3), rpc_buffer); - let _hfs = HardForks::init(HardForkConfig::default(), rpc.clone()) + let _hfs = HardForks::init_at_chain_height(HardForkConfig::default(), 1009827, rpc.clone()) .await .unwrap(); } diff --git a/consensus/src/hardforks.rs b/consensus/src/hardforks.rs index 2873e4c9..6d1abfb9 100644 --- a/consensus/src/hardforks.rs +++ b/consensus/src/hardforks.rs @@ -230,6 +230,27 @@ impl HardForks { panic!("Database sent incorrect response") }; + let mut hfs = + HardForks::init_at_chain_height(config, chain_height, database.clone()).await?; + + // This is only needed if the database moves independently of the HardFork class aka if we are checking a node instead of keeping state ourself. + hfs.resync(&mut database).await?; + + hfs.check_set_new_hf(); + + tracing::info!("HardFork state: {:?}", hfs); + + Ok(hfs) + } + + pub async fn init_at_chain_height( + config: HardForkConfig, + chain_height: u64, + mut database: D, + ) -> Result + where + D::Future: Send + 'static, + { let block_heights = if chain_height > config.window { chain_height - config.window..chain_height } else { @@ -257,8 +278,6 @@ impl HardForks { last_height: chain_height - 1, }; - hfs.resync(&mut database).await?; - hfs.check_set_new_hf(); tracing::info!("HardFork state: {:?}", hfs); @@ -285,7 +304,7 @@ impl HardForks { loop { while chain_height > self.last_height + 1 { self.get_and_account_new_block(self.last_height + 1, &mut database) - .await; + .await?; } let DatabaseResponse::ChainHeight(c_h) = database @@ -309,13 +328,16 @@ impl HardForks { } } - async fn get_and_account_new_block(&mut self, height: u64, mut database: D) { - let header = get_block_header(&mut database, height) - .await - .expect("Error retrieving block we should have in database"); + async fn get_and_account_new_block( + &mut self, + height: u64, + mut database: D, + ) -> Result<(), Error> { + let header = get_block_header(&mut database, height).await?; self.new_block(HardFork::from_vote(&header.minor_version), height, database) - .await + .await; + Ok(()) } pub fn check_block_version_vote(&self, version: &HardFork, vote: &HardFork) -> bool {