Skip to content

Commit

Permalink
refactor: set data_diras global
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Oct 25, 2023
1 parent 375977b commit b93adac
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
17 changes: 13 additions & 4 deletions spec/src/versionbits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
mod convert;

use crate::consensus::Consensus;
use ckb_logger::error;
use ckb_types::{
core::{EpochExt, EpochNumber, HeaderView, Ratio, TransactionView, Version, DATA_DIR},
core::{EpochExt, EpochNumber, HeaderView, Ratio, TransactionView, Version},
global::DATA_DIR,
packed::{Byte32, CellbaseWitnessReader},
prelude::*,
};
use std::fmt;
use std::sync::Arc;
use std::{collections::HashMap, path::PathBuf};
use ckb_logger::error;

/// What bits to set in version for versionbits blocks
pub const VERSIONBITS_TOP_BITS: Version = 0x00000000;
Expand Down Expand Up @@ -137,12 +138,17 @@ pub struct Deployment {
pub threshold: Ratio,
}

/// Signal state cache
///
/// Persistent signal state cache, mmap-based cacache
#[derive(Clone, Debug)]
pub struct Cache {
path: PathBuf,
}

impl Cache {
/// Reads the entire contents of a cache file synchronously into a bytes vector,
/// looking the data up by key.
pub fn get(&self, key: &Byte32) -> Option<ThresholdState> {
match cacache::read_sync(&self.path, Self::encode_key(key)) {
Ok(bytes) => Some(Self::decode_value(bytes)),
Expand All @@ -154,8 +160,11 @@ impl Cache {
}
}

/// Writes data to the cache synchronously
pub fn insert(&self, key: &Byte32, value: ThresholdState) {
if let Err(e) = cacache::write_sync(&self.path, Self::encode_key(&key), Self::encode_value(value)) {
if let Err(e) =
cacache::write_sync(&self.path, Self::encode_key(key), Self::encode_value(value))
{
error!("cacache write_sync failed {:?}", e);
}
}
Expand Down Expand Up @@ -184,7 +193,7 @@ impl VersionbitsCache {
/// Construct new VersionbitsCache instance from deployments
pub fn new<'a>(deployments: impl Iterator<Item = &'a DeploymentPos>) -> Self {
let default_dir = PathBuf::new();
let data_dir = DATA_DIR.get().unwrap_or_else(|| &default_dir);
let data_dir = DATA_DIR.get().unwrap_or(&default_dir);
let caches: HashMap<_, _> = deployments
.map(|pos| {
(
Expand Down
2 changes: 1 addition & 1 deletion util/app-config/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! we must put nested config struct in the tail to make it serializable,
//! details <https://docs.rs/toml/0.5.0/toml/ser/index.html>
use ckb_types::core::DATA_DIR;
use ckb_types::global::DATA_DIR;
use path_clean::PathClean;
use std::fs;
use std::path::{Path, PathBuf};
Expand Down
4 changes: 0 additions & 4 deletions util/types/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ pub use advanced_builders::{BlockBuilder, HeaderBuilder, TransactionBuilder};
pub use blockchain::DepType;
pub use extras::{BlockExt, EpochExt, EpochNumberWithFraction, TransactionInfo};
pub use fee_rate::FeeRate;
use once_cell::sync::OnceCell;
pub use reward::{BlockEconomicState, BlockIssuance, BlockReward, MinerReward};
use std::path::PathBuf;
pub use transaction_meta::{TransactionMeta, TransactionMetaBuilder};
pub use tx_pool::TransactionWithStatus;
pub use views::{
Expand All @@ -61,5 +59,3 @@ pub type Cycle = u64;

/// Version number.
pub type Version = u32;

pub static DATA_DIR: OnceCell<PathBuf> = OnceCell::new();
7 changes: 7 additions & 0 deletions util/types/src/global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Global data, initialized in the launch phase.
use once_cell::sync::OnceCell;
use std::path::PathBuf;

/// ckb data directory path, located under root/data, initialized during the launch phase
pub static DATA_DIR: OnceCell<PathBuf> = OnceCell::new();
1 change: 1 addition & 0 deletions util/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use molecule::{self, error};
pub use numext_fixed_uint::{u256, U128, U256};

pub mod core;
pub mod global;

pub mod constants;
mod conversion;
Expand Down

0 comments on commit b93adac

Please sign in to comment.