Skip to content

Commit

Permalink
:fix: try to fix issue mimblewimble#2585.
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Tang <[email protected]>
  • Loading branch information
miketang84 committed May 6, 2019
1 parent 42b0812 commit f1ea2c7
Showing 1 changed file with 74 additions and 19 deletions.
93 changes: 74 additions & 19 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,32 +1033,87 @@ impl Chain {
let current_hash = txhashset.get_header_hash_by_height(head.height - horizon - 1)?;
let mut current = batch.get_block_header(&current_hash)?;

loop {
// Go to the store directly so we can handle NotFoundErr robustly.
match self.store.get_block(&current.hash()) {
Ok(b) => {
batch.delete_block(&b.hash())?;
count += 1;
let mut fork_long_enough = true;
match self.store.get_block(&current.hash()) {
Ok(_b) => {
// do nothing
}
Err(NotFoundErr(_)) => {
fork_long_enough = false;
}
Err(e) => {
return Err(
ErrorKind::StoreErr(e, "retrieving block to compact".to_owned()).into(),
);
}
}

// if fork is long enough
if fork_long_enough {
loop {
// Go to the store directly so we can handle NotFoundErr robustly.
match self.store.get_block(&current.hash()) {
Ok(b) => {
batch.delete_block(&b.hash())?;
count += 1;
}
Err(NotFoundErr(_)) => {
break;
}
Err(e) => {
return Err(
ErrorKind::StoreErr(e, "retrieving block to compact".to_owned()).into(),
);
}
}
Err(NotFoundErr(_)) => {
if current.height <= 1 {
break;
}
Err(e) => {
return Err(
ErrorKind::StoreErr(e, "retrieving block to compact".to_owned()).into(),
);
match batch.get_previous_header(&current) {
Ok(h) => current = h,
Err(NotFoundErr(_)) => break,
Err(e) => return Err(From::from(e)),
}
}
if current.height <= 1 {
break;
}
match batch.get_previous_header(&current) {
Ok(h) => current = h,
Err(NotFoundErr(_)) => break,
Err(e) => return Err(From::from(e)),
batch.save_body_tail(&Tip::from_header(&tail))?;
}
else {
// clear all, then this blockchain folk will disappear?
// clear from head? or use another horizon value?
let current_hash = txhashset.get_header_hash_by_height(head.height)?;
let mut current = batch.get_block_header(&current_hash)?;

loop {
// Go to the store directly so we can handle NotFoundErr robustly.
match self.store.get_block(&current.hash()) {
Ok(b) => {
batch.delete_block(&b.hash())?;
count += 1;
}
Err(NotFoundErr(_)) => {
break;
}
Err(e) => {
return Err(
ErrorKind::StoreErr(e, "retrieving block to compact".to_owned()).into(),
);
}
}

if current.height <= 1 {
break;
}

match batch.get_previous_header(&current) {
Ok(h) => current = h,
Err(NotFoundErr(_)) => break,
Err(e) => return Err(From::from(e)),
}
}

//TODO: Then need to clear these range flags
//batch.save_body_tail(&Tip::from_header(&tail))?;
}
batch.save_body_tail(&Tip::from_header(&tail))?;

debug!(
"remove_historical_blocks: removed {} blocks. tail height: {}",
Expand Down

0 comments on commit f1ea2c7

Please sign in to comment.