Skip to content

Commit b93adac

Browse files
committed
refactor: set data_diras global
1 parent 375977b commit b93adac

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

spec/src/versionbits/mod.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
mod convert;
55

66
use crate::consensus::Consensus;
7+
use ckb_logger::error;
78
use ckb_types::{
8-
core::{EpochExt, EpochNumber, HeaderView, Ratio, TransactionView, Version, DATA_DIR},
9+
core::{EpochExt, EpochNumber, HeaderView, Ratio, TransactionView, Version},
10+
global::DATA_DIR,
911
packed::{Byte32, CellbaseWitnessReader},
1012
prelude::*,
1113
};
1214
use std::fmt;
1315
use std::sync::Arc;
1416
use std::{collections::HashMap, path::PathBuf};
15-
use ckb_logger::error;
1617

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

141+
/// Signal state cache
142+
///
143+
/// Persistent signal state cache, mmap-based cacache
140144
#[derive(Clone, Debug)]
141145
pub struct Cache {
142146
path: PathBuf,
143147
}
144148

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

163+
/// Writes data to the cache synchronously
157164
pub fn insert(&self, key: &Byte32, value: ThresholdState) {
158-
if let Err(e) = cacache::write_sync(&self.path, Self::encode_key(&key), Self::encode_value(value)) {
165+
if let Err(e) =
166+
cacache::write_sync(&self.path, Self::encode_key(key), Self::encode_value(value))
167+
{
159168
error!("cacache write_sync failed {:?}", e);
160169
}
161170
}
@@ -184,7 +193,7 @@ impl VersionbitsCache {
184193
/// Construct new VersionbitsCache instance from deployments
185194
pub fn new<'a>(deployments: impl Iterator<Item = &'a DeploymentPos>) -> Self {
186195
let default_dir = PathBuf::new();
187-
let data_dir = DATA_DIR.get().unwrap_or_else(|| &default_dir);
196+
let data_dir = DATA_DIR.get().unwrap_or(&default_dir);
188197
let caches: HashMap<_, _> = deployments
189198
.map(|pos| {
190199
(

util/app-config/src/app_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! we must put nested config struct in the tail to make it serializable,
55
//! details <https://docs.rs/toml/0.5.0/toml/ser/index.html>
66
7-
use ckb_types::core::DATA_DIR;
7+
use ckb_types::global::DATA_DIR;
88
use path_clean::PathClean;
99
use std::fs;
1010
use std::path::{Path, PathBuf};

util/types/src/core/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ pub use advanced_builders::{BlockBuilder, HeaderBuilder, TransactionBuilder};
3232
pub use blockchain::DepType;
3333
pub use extras::{BlockExt, EpochExt, EpochNumberWithFraction, TransactionInfo};
3434
pub use fee_rate::FeeRate;
35-
use once_cell::sync::OnceCell;
3635
pub use reward::{BlockEconomicState, BlockIssuance, BlockReward, MinerReward};
37-
use std::path::PathBuf;
3836
pub use transaction_meta::{TransactionMeta, TransactionMetaBuilder};
3937
pub use tx_pool::TransactionWithStatus;
4038
pub use views::{
@@ -61,5 +59,3 @@ pub type Cycle = u64;
6159

6260
/// Version number.
6361
pub type Version = u32;
64-
65-
pub static DATA_DIR: OnceCell<PathBuf> = OnceCell::new();

util/types/src/global.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Global data, initialized in the launch phase.
2+
3+
use once_cell::sync::OnceCell;
4+
use std::path::PathBuf;
5+
6+
/// ckb data directory path, located under root/data, initialized during the launch phase
7+
pub static DATA_DIR: OnceCell<PathBuf> = OnceCell::new();

util/types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub use molecule::{self, error};
1111
pub use numext_fixed_uint::{u256, U128, U256};
1212

1313
pub mod core;
14+
pub mod global;
1415

1516
pub mod constants;
1617
mod conversion;

0 commit comments

Comments
 (0)