Skip to content
Permalink
Browse files

blockchain: Add applied blocks to storage

Instead of Blockchain::apply_block which only adds reference
chain, make public method apply_and_store_block which also
adds the block into storage.

The loading methods still use apply_block internally.
  • Loading branch information...
mzabaluev committed Aug 13, 2019
1 parent 8b24443 commit c9a83b30a3bd77ab1ac6766718aebe73fa4bef30
@@ -388,11 +388,7 @@ impl Blockchain {
}
}

/// TODO: document this function
///
/// apply the block on the blockchain from a post checked header
///
pub fn apply_block(
fn apply_block(
&mut self,
post_checked_header: PostCheckedHeader,
block: &Block,
@@ -429,6 +425,23 @@ impl Blockchain {
})
}

/// Apply the block on the blockchain from a post checked header
/// and add it to the storage.
pub fn apply_and_store_block(
&mut self,
post_checked_header: PostCheckedHeader,
block: Block,
) -> impl Future<Item = Ref, Error = Error> {
let mut storage = self.storage.clone();
self.apply_block(post_checked_header, &block)
.and_then(move |block_ref| {
storage
.put_block(block)
.map_err(|e| e.into())
.and_then(move |()| Ok(block_ref))
})
}

/// Apply the given block0 in the blockchain (updating the RefCache and the other objects)
///
/// This function returns the created block0 branch. Having it will
@@ -71,7 +71,7 @@ pub fn process_leadership_block(
))
}
})
.and_then(move |post_checked| end_blockchain.apply_block(post_checked, &block))
.and_then(move |post_checked| end_blockchain.apply_and_store_block(post_checked, block))
}

pub fn process_block_announcement(
@@ -145,7 +145,9 @@ pub fn process_network_block(
PreCheckedHeader::HeaderWithCache { header, parent_ref } => {
let post_check_and_apply = blockchain
.post_check_header(header, parent_ref)
.and_then(move |post_checked| end_blockchain.apply_block(post_checked, &block))
.and_then(move |post_checked| {
end_blockchain.apply_and_store_block(post_checked, block)
})
.map(move |_| {
// TODO: advance branch?
debug!(logger, "block successfully applied");
@@ -135,7 +135,7 @@ fn handle_block(
})
.and_then(move |post_checked| {
end_blockchain
.apply_block(post_checked, &block)
.apply_and_store_block(post_checked, block)
.map_err(Error::ApplyBlockFailed)
})
}

0 comments on commit c9a83b3

Please sign in to comment.
You can’t perform that action at this time.