Skip to content

Commit 2a6182b

Browse files
committed
fix: hashset have 2 folders including txhashset and header
1 parent eb4adef commit 2a6182b

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

chain/src/chain.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Chain {
183183

184184
// open the txhashset, creating a new one if necessary
185185
let mut txhashset =
186-
txhashset::TxHashSet::open(db_root.clone(), store.clone(), None, None)?;
186+
txhashset::TxHashSet::open(db_root.clone(), store.clone(), None)?;
187187

188188
setup_head(&genesis, &store, &mut txhashset)?;
189189
Chain::log_heads(&store)?;
@@ -846,8 +846,8 @@ impl Chain {
846846
}
847847

848848
/// Clean the temporary sandbox folder
849-
pub fn clean_txhashset_sandbox(&self) {
850-
txhashset::clean_txhashset_folder(&env::temp_dir());
849+
pub fn clean_hashset_sandbox(&self) {
850+
txhashset::clean_hashset_folder(&env::temp_dir());
851851
}
852852

853853
/// Writes a reading view on a txhashset state that's been provided to us.
@@ -871,20 +871,17 @@ impl Chain {
871871

872872
let header = self.get_block_header(&h)?;
873873

874-
// Write txhashset to sandbox
875-
txhashset::clean_txhashset_folder(&env::temp_dir());
874+
// Write txhashset to sandbox (in the os temporary directory)
875+
txhashset::clean_hashset_folder(&env::temp_dir());
876876
txhashset::zip_write(env::temp_dir(), txhashset_data.try_clone()?, &header)?;
877877

878878
let mut txhashset = txhashset::TxHashSet::open(
879-
self.db_root.clone(),
879+
env::temp_dir()
880+
.to_str()
881+
.expect("invalid temp folder")
882+
.to_owned(),
880883
self.store.clone(),
881884
Some(&header),
882-
Some(
883-
env::temp_dir()
884-
.to_str()
885-
.expect("invalid temp folder")
886-
.to_owned(),
887-
),
888885
)?;
889886

890887
// The txhashset.zip contains the output, rangeproof and kernel MMRs.
@@ -937,21 +934,21 @@ impl Chain {
937934

938935
debug!("txhashset_write: finished committing the batch (head etc.)");
939936

940-
// sandbox full validation ok, go to overwrite txhashset on db root
937+
// Sandbox full validation ok, go to overwrite txhashset on db root
941938
{
942-
// Before overwriting, drop file handles in underlying txhashset
939+
// Before overwriting, drop file handlers in underlying txhashset
943940
{
944941
let mut txhashset_ref = self.txhashset.write();
945942
txhashset_ref.release_backend_files();
946943
}
947944

948-
txhashset::txhashset_replace(env::temp_dir(), PathBuf::from(self.db_root.clone()))?;
945+
txhashset.release_backend_files();
946+
txhashset::hashset_replace(env::temp_dir(), PathBuf::from(self.db_root.clone()))?;
949947

950948
txhashset = txhashset::TxHashSet::open(
951949
self.db_root.clone(),
952950
self.store.clone(),
953951
Some(&header),
954-
None,
955952
)?;
956953
}
957954

chain/src/txhashset/txhashset.rs

+31-23
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@ impl TxHashSet {
114114
root_dir: String,
115115
commit_index: Arc<ChainStore>,
116116
header: Option<&BlockHeader>,
117-
sandbox_dir: Option<String>,
118117
) -> Result<TxHashSet, Error> {
119-
let txhashset_rootdir = if let Some(sandbox) = sandbox_dir {
120-
sandbox.clone()
121-
} else {
122-
root_dir.clone()
123-
};
124118
Ok(TxHashSet {
125119
header_pmmr_h: PMMRHandle::new(
126120
&root_dir,
@@ -137,21 +131,21 @@ impl TxHashSet {
137131
None,
138132
)?,
139133
output_pmmr_h: PMMRHandle::new(
140-
&txhashset_rootdir,
134+
&root_dir,
141135
TXHASHSET_SUBDIR,
142136
OUTPUT_SUBDIR,
143137
true,
144138
header,
145139
)?,
146140
rproof_pmmr_h: PMMRHandle::new(
147-
&txhashset_rootdir,
141+
&root_dir,
148142
TXHASHSET_SUBDIR,
149143
RANGE_PROOF_SUBDIR,
150144
true,
151145
header,
152146
)?,
153147
kernel_pmmr_h: PMMRHandle::new(
154-
&txhashset_rootdir,
148+
&root_dir,
155149
TXHASHSET_SUBDIR,
156150
KERNEL_SUBDIR,
157151
false,
@@ -1468,35 +1462,49 @@ pub fn zip_write(
14681462
check_and_remove_files(&txhashset_path, header)
14691463
}
14701464

1471-
/// Rename a folder to another
1472-
pub fn txhashset_replace(from: PathBuf, to: PathBuf) -> Result<(), Error> {
1473-
debug!("txhashset_replace: move from {:?} to {:?}", from, to);
1465+
/// Overwrite 2 hashset folders ('txhashset' & 'header') in "to" folder with "from" folder
1466+
pub fn hashset_replace(from: PathBuf, to: PathBuf) -> Result<(), Error> {
1467+
debug!("hashset_replace: move from {:?} to {:?}", from, to);
14741468

14751469
// clean the 'to' folder firstly
1476-
clean_txhashset_folder(&to);
1470+
clean_hashset_folder(&to);
14771471

14781472
// rename the 'from' folder as the 'to' folder
1479-
let txhashset_from_path = from.clone().join(TXHASHSET_SUBDIR);
1480-
let txhashset_to_path = to.clone().join(TXHASHSET_SUBDIR);
1481-
if let Err(e) = fs::rename(txhashset_from_path, txhashset_to_path) {
1482-
error!("txhashset_replace fail. err: {}", e);
1483-
Err(ErrorKind::TxHashSetErr(format!("txhashset replacing fail")).into())
1484-
} else {
1485-
Ok(())
1473+
if let Err(e) = fs::rename(from.clone().join(TXHASHSET_SUBDIR), to.clone().join(TXHASHSET_SUBDIR)) {
1474+
error!("hashset_replace fail on {}. err: {}", TXHASHSET_SUBDIR, e);
1475+
return Err(ErrorKind::TxHashSetErr(format!("txhashset replacing fail")).into());
14861476
}
1477+
1478+
// rename the 'from' folder as the 'to' folder
1479+
if let Err(e) = fs::rename(from.clone().join(HEADERHASHSET_SUBDIR), to.clone().join(HEADERHASHSET_SUBDIR)) {
1480+
error!("hashset_replace fail on {}. err: {}", HEADERHASHSET_SUBDIR, e);
1481+
return Err(ErrorKind::TxHashSetErr(format!("txhashset replacing fail")).into());
1482+
}
1483+
1484+
return Ok(());
14871485
}
14881486

1489-
/// Clean the txhashset folder
1490-
pub fn clean_txhashset_folder(root_dir: &PathBuf) {
1487+
/// Clean the hashset folder (txhashset and header hashset)
1488+
pub fn clean_hashset_folder(root_dir: &PathBuf) {
14911489
let txhashset_path = root_dir.clone().join(TXHASHSET_SUBDIR);
14921490
if txhashset_path.exists() {
14931491
if let Err(e) = fs::remove_dir_all(txhashset_path.clone()) {
14941492
warn!(
1495-
"clean_txhashset_folder: fail on {:?}. err: {}",
1493+
"clean_hashset_folder: fail on {:?}. err: {}",
14961494
txhashset_path, e
14971495
);
14981496
}
14991497
}
1498+
1499+
let header_hashset_path = root_dir.clone().join(HEADERHASHSET_SUBDIR);
1500+
if header_hashset_path.exists() {
1501+
if let Err(e) = fs::remove_dir_all(header_hashset_path.clone()) {
1502+
warn!(
1503+
"clean_hashset_folder: fail on {:?}. err: {}",
1504+
header_hashset_path, e
1505+
);
1506+
}
1507+
}
15001508
}
15011509

15021510
fn expected_file(path: &Path) -> bool {

chain/tests/test_txhashset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn test_unexpected_zip() {
4444
let db_env = Arc::new(store::new_env(db_root.clone()));
4545
let chain_store = ChainStore::new(db_env).unwrap();
4646
let store = Arc::new(chain_store);
47-
txhashset::TxHashSet::open(db_root.clone(), store.clone(), None, None).unwrap();
47+
txhashset::TxHashSet::open(db_root.clone(), store.clone(), None).unwrap();
4848
// First check if everything works out of the box
4949
assert!(txhashset::zip_read(db_root.clone(), &BlockHeader::default(), Some(rand)).is_ok());
5050
let zip_path = Path::new(&db_root).join(format!("txhashset_snapshot_{}.zip", rand));

servers/src/common/adapters.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl p2p::ChainAdapter for NetToChainAdapter {
359359
.chain()
360360
.txhashset_write(h, txhashset_data, self.sync_state.as_ref())
361361
{
362-
self.chain().clean_txhashset_sandbox();
362+
self.chain().clean_hashset_sandbox();
363363
error!("Failed to save txhashset archive: {}", e);
364364
let is_good_data = !e.is_bad_data();
365365
self.sync_state.set_sync_error(types::Error::Chain(e));

0 commit comments

Comments
 (0)