no need to pass sync_head around in ctx ()

This commit is contained in:
Antioch Peverell 2018-09-29 16:19:19 +01:00 committed by GitHub
parent 985bced99d
commit 73ddd1d01d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 20 deletions

View file

@ -348,23 +348,21 @@ impl Chain {
&self,
headers: &Vec<BlockHeader>,
opts: Options,
) -> Result<Tip, Error> {
) -> Result<(), Error> {
let mut batch = self.store.batch()?;
let mut ctx = self.new_ctx(opts, &mut batch)?;
let res = pipe::sync_block_headers(headers, &mut ctx, &mut batch)?;
pipe::sync_block_headers(headers, &mut ctx, &mut batch)?;
batch.commit()?;
Ok(res)
Ok(())
}
fn new_ctx(&self, opts: Options, batch: &mut Batch) -> Result<pipe::BlockContext, Error> {
let head = batch.head()?;
let header_head = batch.get_header_head()?;
let sync_head = batch.get_sync_head()?;
Ok(pipe::BlockContext {
opts,
head,
header_head,
sync_head,
pow_verifier: self.pow_verifier,
block_hashes_cache: self.block_hashes_cache.clone(),
txhashset: self.txhashset.clone(),

View file

@ -47,8 +47,6 @@ pub struct BlockContext {
pub head: Tip,
/// The header head
pub header_head: Tip,
/// The sync head
pub sync_head: Tip,
/// The POW verification function
pub pow_verifier: fn(&BlockHeader, u8) -> Result<(), pow::Error>,
/// MMR sum tree states
@ -206,7 +204,7 @@ pub fn sync_block_headers(
headers: &Vec<BlockHeader>,
ctx: &mut BlockContext,
batch: &mut store::Batch,
) -> Result<Tip, Error> {
) -> Result<(), Error> {
if let Some(header) = headers.first() {
debug!(
LOGGER,
@ -217,8 +215,6 @@ pub fn sync_block_headers(
);
}
let mut sync_tip = batch.get_sync_head()?;
for header in headers {
handle_block_header(header, ctx, batch)?;
@ -227,10 +223,9 @@ pub fn sync_block_headers(
// and become the "most work" chain.
// header_head and sync_head will diverge in this situation until we switch to
// a single "most work" chain.
sync_tip = update_sync_head(header, ctx, batch)?;
update_sync_head(header, batch)?;
}
Ok(sync_tip)
Ok(())
}
fn handle_block_header(
@ -660,18 +655,13 @@ fn block_has_more_work(header: &BlockHeader, tip: &Tip) -> bool {
}
/// Update the sync head so we can keep syncing from where we left off.
fn update_sync_head(
bh: &BlockHeader,
ctx: &mut BlockContext,
batch: &mut store::Batch,
) -> Result<Tip, Error> {
fn update_sync_head(bh: &BlockHeader, batch: &mut store::Batch) -> Result<(), Error> {
let tip = Tip::from_block(bh);
batch
.save_sync_head(&tip)
.map_err(|e| ErrorKind::StoreErr(e, "pipe save sync head".to_owned()))?;
ctx.sync_head = tip.clone();
debug!(LOGGER, "sync head {} @ {}", bh.hash(), bh.height);
Ok(tip)
Ok(())
}
fn update_header_head(