-
Notifications
You must be signed in to change notification settings - Fork 990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup blocks from db (incl. forked blocks) #2815
Changes from 2 commits
8aca253
c95e060
4cb52c2
4acf9da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1026,38 +1026,18 @@ impl Chain { | |
} | ||
|
||
let mut count = 0; | ||
|
||
let tail_hash = txhashset.get_header_hash_by_height(head.height - horizon)?; | ||
let tail = batch.get_block_header(&tail_hash)?; | ||
|
||
let current_hash = txhashset.get_header_hash_by_height(head.height - horizon - 1)?; | ||
let mut current = batch.get_block_header(¤t_hash)?; | ||
|
||
loop { | ||
// Go to the store directly so we can handle NotFoundErr robustly. | ||
match self.store.get_block(¤t.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(¤t) { | ||
Ok(h) => current = h, | ||
Err(NotFoundErr(_)) => break, | ||
Err(e) => return Err(From::from(e)), | ||
// Remove old blocks and short live fork blocks | ||
// here b is a lived block | ||
let _ = batch.iter_lived_blocks()?.map(|(_, b)| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified to -
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
if self.is_on_current_chain(&b.header).is_err() || b.header.height < tail.height { | ||
let _ = batch.delete_block(&b.hash()); | ||
count += 1; | ||
} | ||
} | ||
}); | ||
|
||
batch.save_body_tail(&Tip::from_header(&tail))?; | ||
|
||
debug!( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ use crate::types::Tip; | |
use crate::util::secp::pedersen::Commitment; | ||
use croaring::Bitmap; | ||
use grin_store as store; | ||
use grin_store::{option_to_not_found, to_key, Error}; | ||
use grin_store::{option_to_not_found, to_key, Error, SerIterator}; | ||
use std::sync::Arc; | ||
|
||
const STORE_SUBPATH: &'static str = "chain"; | ||
|
@@ -378,6 +378,13 @@ impl<'a> Batch<'a> { | |
db: self.db.child()?, | ||
}) | ||
} | ||
|
||
/// An iterator to all lived block now | ||
pub fn iter_lived_blocks(&self) -> Result<SerIterator<Block>, Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
let key = to_key(BLOCK_PREFIX, &mut "".to_string().into_bytes()); | ||
self.db.iter(&key) | ||
} | ||
|
||
} | ||
|
||
/// An iterator on blocks, from latest to earliest, specialized to return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean by "lived" block - maybe reword these comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.