add method to init the hard fork struct at a certain height.

This commit is contained in:
Boog900 2023-09-05 19:49:01 +01:00
parent a56d8ea87f
commit 367ea1a837
No known key found for this signature in database
GPG key ID: 5401367FB7302004
2 changed files with 32 additions and 11 deletions

View file

@ -7,14 +7,13 @@ use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use tower::balance::p2c::Balance; use tower::balance::p2c::Balance;
use tower::discover::Change; use tower::discover::Change;
use tower::util::{BoxService}; use tower::util::BoxService;
use tracing::level_filters::LevelFilter; use tracing::level_filters::LevelFilter;
use monero_consensus::hardforks::{HardForkConfig, HardForks}; use monero_consensus::hardforks::{HardForkConfig, HardForks};
use monero_consensus::rpc::Rpc; use monero_consensus::rpc::Rpc;
struct RpcDiscoverer(Vec<String>, u64); struct RpcDiscoverer(Vec<String>, u64);
impl Stream for RpcDiscoverer { 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_buffer = tower::buffer::Buffer::new(BoxService::new(rpc_balance), 3);
let rpc = tower::retry::Retry::new(Attempts(3), rpc_buffer); 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 .await
.unwrap(); .unwrap();
} }

View file

@ -230,6 +230,27 @@ impl HardForks {
panic!("Database sent incorrect response") 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<D: Database + Clone>(
config: HardForkConfig,
chain_height: u64,
mut database: D,
) -> Result<Self, Error>
where
D::Future: Send + 'static,
{
let block_heights = if chain_height > config.window { let block_heights = if chain_height > config.window {
chain_height - config.window..chain_height chain_height - config.window..chain_height
} else { } else {
@ -257,8 +278,6 @@ impl HardForks {
last_height: chain_height - 1, last_height: chain_height - 1,
}; };
hfs.resync(&mut database).await?;
hfs.check_set_new_hf(); hfs.check_set_new_hf();
tracing::info!("HardFork state: {:?}", hfs); tracing::info!("HardFork state: {:?}", hfs);
@ -285,7 +304,7 @@ impl HardForks {
loop { loop {
while chain_height > self.last_height + 1 { while chain_height > self.last_height + 1 {
self.get_and_account_new_block(self.last_height + 1, &mut database) self.get_and_account_new_block(self.last_height + 1, &mut database)
.await; .await?;
} }
let DatabaseResponse::ChainHeight(c_h) = database let DatabaseResponse::ChainHeight(c_h) = database
@ -309,13 +328,16 @@ impl HardForks {
} }
} }
async fn get_and_account_new_block<D: Database>(&mut self, height: u64, mut database: D) { async fn get_and_account_new_block<D: Database>(
let header = get_block_header(&mut database, height) &mut self,
.await height: u64,
.expect("Error retrieving block we should have in database"); 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) 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 { pub fn check_block_version_vote(&self, version: &HardFork, vote: &HardFork) -> bool {