Skip to content

Commit

Permalink
simplify kernel_pos index, need undo list per block to handle rewind/…
Browse files Browse the repository at this point in the history
…undo

this is conceptually similar to output_pos but we support duplicates rather than spending to remove
  • Loading branch information
antiochp committed Mar 4, 2020
1 parent 12dd0e5 commit 2585ad0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ impl Chain {

// Remove historical blocks from the db unless we are running in archive mode.
if !self.archive_mode {
self.remove_historical_blocks(&header_pmmr, &mut batch)?;
self.remove_historical_blocks(&header_pmmr, &batch)?;
}

// Compact the txhashset itself (rewriting the pruned backend files).
Expand Down Expand Up @@ -1154,7 +1154,7 @@ impl Chain {
/// Get the position of the kernel if it exists in the kernel_pos index.
/// The index is limited to 14 days of recent kernels.
pub fn get_kernel_pos(&self, excess: Commitment) -> Result<u64, Error> {
Ok(self.txhashset.read().get_kernel_pos(excess)?)
self.txhashset.read().get_kernel_pos(excess)
}

/// outputs by insertion index
Expand Down
11 changes: 11 additions & 0 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const KERNEL_POS_PREFIX: u8 = b'k';
const BLOCK_INPUT_BITMAP_PREFIX: u8 = b'B';
const BLOCK_SUMS_PREFIX: u8 = b'M';
const BLOCK_SPENT_PREFIX: u8 = b'S';
const BLOCK_KERNEL_UNDO_PREFIX: u8 = b'U';

/// All chain-related database operations
pub struct ChainStore {
Expand Down Expand Up @@ -208,6 +209,16 @@ impl<'a> Batch<'a> {
Ok(())
}

/// We maintain an "undo" index for each full block to allow the kernel_pos
/// to be easily reverted during rewind.
/// We allow duplicate kernels so we need to know what to revert the kernel_pos
/// index to if we "undo" a kernel when rewinding a block.
pub fn save_kernel_undo_index(&self, h: &Hash, pos: &Vec<CommitPos>) -> Result<(), Error> {
self.db
.put_ser(&to_key(BLOCK_KERNEL_UNDO_PREFIX, &mut h.to_vec())[..], pos)?;
Ok(())
}

/// Migrate a block stored in the db by serializing it using the provided protocol version.
/// Block may have been read using a previous protocol version but we do not actually care.
pub fn migrate_block(&self, b: &Block, version: ProtocolVersion) -> Result<(), Error> {
Expand Down
17 changes: 5 additions & 12 deletions chain/src/txhashset/txhashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,13 @@ impl TxHashSet {
Ok(self.commit_index.get_output_pos(&commit)?)
}

/// TODO - This needs to be a multi-step process.
/// TODO - Rename this.
/// First we need to look the entry up in the index.
/// This entry will be a vec of (pos, height).
/// The entry may be an empty vec.
/// We then need to filter this vec by height (discounting anything beyond current head)
/// and then actually look the kernels up, filtering out any that do not align with the index.
///
/// Get the position of the kernel if it exists in the kernel_pos index.
/// The index is limited to 14 days of recent kernels.
pub fn get_kernel_pos(&self, excess: Commitment) -> Result<Vec<(TxKernel, u64)>, Error> {
let pos = self.commit_index.get_kernel_pos_height(&excess);
// .map(|entry| entry)?;
Ok(pos)
pub fn get_kernel_pos(&self, excess: Commitment) -> Result<u64, Error> {
Ok(self
.commit_index
.get_kernel_pos_height(&excess)
.map(|x| x.1)?)
}

/// build a new merkle proof for the given position.
Expand Down

0 comments on commit 2585ad0

Please sign in to comment.