Skip to content

Commit

Permalink
dbs rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
YeahNotSewerSide committed May 4, 2024
1 parent df6d865 commit d42ce34
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/blockchaintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
use error_stack::{Report, ResultExt};
use primitive_types::U256;
use sled::Db;
use std::fs;

pub struct BlockChainTree {
pub main_chain: chain::MainChain,
Expand Down Expand Up @@ -314,7 +315,7 @@ impl BlockChainTree {
}

pub async fn emmit_new_main_block(
&self,
&mut self,
pow: [u8; 32],
founder: [u8; 33],
transactions: &[Hash],
Expand Down Expand Up @@ -355,6 +356,7 @@ impl BlockChainTree {
default_info,
merkle_tree_root,
});
self.rotate_dbs().await?;

summarize_block
} else {
Expand Down Expand Up @@ -407,4 +409,59 @@ impl BlockChainTree {

Ok(())
}

pub async fn rotate_dbs(&mut self) -> Result<(), Report<BlockChainTreeError>> {
self.flush().await?;

let path_summary = Path::new(AMMOUNT_SUMMARY);
let path_summary_old = Path::new(OLD_AMMOUNT_SUMMARY);
let path_gas = Path::new(GAS_SUMMARY);
let path_gas_old = Path::new(OLD_GAS_SUMMARY);

fs::remove_dir_all(path_summary_old)
.change_context(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::MoveSummaryDB,
))
.attach_printable("failed to remove previous summary database")?;

fs::create_dir(path_summary_old)
.change_context(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::MoveSummaryDB,
))
.attach_printable("failed to create previous summary database folder")?;

fs::remove_dir_all(path_gas_old)
.change_context(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::MoveSummaryDB,
))
.attach_printable("failed to remove previous gas database")?;

fs::create_dir(path_gas_old)
.change_context(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::MoveSummaryDB,
))
.attach_printable("failed to remove previous gas database folder")?;

tools::copy_dir_all(path_summary, path_summary_old)
.change_context(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::MoveSummaryDB,
))
.attach_printable("failed to copy summary database")?;

tools::copy_dir_all(path_gas, path_gas_old)
.change_context(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::MoveSummaryDB,
))
.attach_printable("failed to copy gas database")?;

self.old_summary_db = sled::open(path_summary_old)
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::Init))
.attach_printable("failed to open old summary db")?;

self.old_gas_db = sled::open(path_gas_old)
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::Init))
.attach_printable("failed to open old gas db")?;

Ok(())
}
}

0 comments on commit d42ce34

Please sign in to comment.