diff --git a/node/src/chain.rs b/node/src/chain.rs index e9ca1fb447..8157a04069 100644 --- a/node/src/chain.rs +++ b/node/src/chain.rs @@ -26,6 +26,7 @@ pub use header_validation::verify_att; use node_data::ledger::{to_str, BlockWithLabel, Label}; use node_data::message::AsyncQueue; use node_data::message::{Payload, Topics}; +use std::ops::Deref; use std::sync::Arc; use std::time::Duration; use tokio::sync::RwLock; @@ -61,7 +62,9 @@ impl db: Arc>, vm: Arc>, ) -> anyhow::Result<()> { - let tip = Self::load_tip(db.clone(), vm.clone()).await?; + let tip = + Self::load_tip(db.read().await.deref(), vm.read().await.deref()) + .await?; let state_hash = tip.inner().header().state_hash; let provisioners_list = vm.read().await.get_provisioners(state_hash)?; @@ -72,8 +75,8 @@ impl tip, provisioners_list, db, - network.clone(), - vm.clone(), + network, + vm, ) .await?; @@ -246,12 +249,9 @@ impl ChainSrv { /// Panics /// /// If register entry is read but block is not found. - async fn load_tip( - db: Arc>, - vm: Arc>, - ) -> Result { - let stored_block = db.read().await.update(|t| { - Ok(t.op_read(MD_HASH_KEY)?.and_then(|tip_hash| { + async fn load_tip(db: &DB, vm: &VM) -> Result { + let stored_block = db.view(|t| { + anyhow::Ok(t.op_read(MD_HASH_KEY)?.and_then(|tip_hash| { t.fetch_block(&tip_hash[..]) .expect("block to be found if metadata is set") })) @@ -260,8 +260,6 @@ impl ChainSrv { let block = match stored_block { Some(blk) => { let (_, label) = db - .read() - .await .view(|t| { t.fetch_block_label_by_height(blk.header().height) })? @@ -272,9 +270,9 @@ impl ChainSrv { None => { // Lack of register record means the loaded database is // either malformed or empty. - let state = vm.read().await.get_state_root()?; + let state = vm.get_state_root()?; let genesis_blk = genesis::generate_state(state); - db.write().await.update(|t| { + db.update(|t| { // Persist genesis block t.store_block( genesis_blk.header(),