mirror of
https://github.com/mimblewimble/grin.git
synced 2025-04-30 22:31:15 +03:00
no need to pass sync_head around in ctx (#1608)
This commit is contained in:
parent
985bced99d
commit
73ddd1d01d
2 changed files with 8 additions and 20 deletions
|
@ -348,23 +348,21 @@ impl Chain {
|
||||||
&self,
|
&self,
|
||||||
headers: &Vec<BlockHeader>,
|
headers: &Vec<BlockHeader>,
|
||||||
opts: Options,
|
opts: Options,
|
||||||
) -> Result<Tip, Error> {
|
) -> Result<(), Error> {
|
||||||
let mut batch = self.store.batch()?;
|
let mut batch = self.store.batch()?;
|
||||||
let mut ctx = self.new_ctx(opts, &mut 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()?;
|
batch.commit()?;
|
||||||
Ok(res)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_ctx(&self, opts: Options, batch: &mut Batch) -> Result<pipe::BlockContext, Error> {
|
fn new_ctx(&self, opts: Options, batch: &mut Batch) -> Result<pipe::BlockContext, Error> {
|
||||||
let head = batch.head()?;
|
let head = batch.head()?;
|
||||||
let header_head = batch.get_header_head()?;
|
let header_head = batch.get_header_head()?;
|
||||||
let sync_head = batch.get_sync_head()?;
|
|
||||||
Ok(pipe::BlockContext {
|
Ok(pipe::BlockContext {
|
||||||
opts,
|
opts,
|
||||||
head,
|
head,
|
||||||
header_head,
|
header_head,
|
||||||
sync_head,
|
|
||||||
pow_verifier: self.pow_verifier,
|
pow_verifier: self.pow_verifier,
|
||||||
block_hashes_cache: self.block_hashes_cache.clone(),
|
block_hashes_cache: self.block_hashes_cache.clone(),
|
||||||
txhashset: self.txhashset.clone(),
|
txhashset: self.txhashset.clone(),
|
||||||
|
|
|
@ -47,8 +47,6 @@ pub struct BlockContext {
|
||||||
pub head: Tip,
|
pub head: Tip,
|
||||||
/// The header head
|
/// The header head
|
||||||
pub header_head: Tip,
|
pub header_head: Tip,
|
||||||
/// The sync head
|
|
||||||
pub sync_head: Tip,
|
|
||||||
/// The POW verification function
|
/// The POW verification function
|
||||||
pub pow_verifier: fn(&BlockHeader, u8) -> Result<(), pow::Error>,
|
pub pow_verifier: fn(&BlockHeader, u8) -> Result<(), pow::Error>,
|
||||||
/// MMR sum tree states
|
/// MMR sum tree states
|
||||||
|
@ -206,7 +204,7 @@ pub fn sync_block_headers(
|
||||||
headers: &Vec<BlockHeader>,
|
headers: &Vec<BlockHeader>,
|
||||||
ctx: &mut BlockContext,
|
ctx: &mut BlockContext,
|
||||||
batch: &mut store::Batch,
|
batch: &mut store::Batch,
|
||||||
) -> Result<Tip, Error> {
|
) -> Result<(), Error> {
|
||||||
if let Some(header) = headers.first() {
|
if let Some(header) = headers.first() {
|
||||||
debug!(
|
debug!(
|
||||||
LOGGER,
|
LOGGER,
|
||||||
|
@ -217,8 +215,6 @@ pub fn sync_block_headers(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut sync_tip = batch.get_sync_head()?;
|
|
||||||
|
|
||||||
for header in headers {
|
for header in headers {
|
||||||
handle_block_header(header, ctx, batch)?;
|
handle_block_header(header, ctx, batch)?;
|
||||||
|
|
||||||
|
@ -227,10 +223,9 @@ pub fn sync_block_headers(
|
||||||
// and become the "most work" chain.
|
// and become the "most work" chain.
|
||||||
// header_head and sync_head will diverge in this situation until we switch to
|
// header_head and sync_head will diverge in this situation until we switch to
|
||||||
// a single "most work" chain.
|
// a single "most work" chain.
|
||||||
sync_tip = update_sync_head(header, ctx, batch)?;
|
update_sync_head(header, batch)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
Ok(sync_tip)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_block_header(
|
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.
|
/// Update the sync head so we can keep syncing from where we left off.
|
||||||
fn update_sync_head(
|
fn update_sync_head(bh: &BlockHeader, batch: &mut store::Batch) -> Result<(), Error> {
|
||||||
bh: &BlockHeader,
|
|
||||||
ctx: &mut BlockContext,
|
|
||||||
batch: &mut store::Batch,
|
|
||||||
) -> Result<Tip, Error> {
|
|
||||||
let tip = Tip::from_block(bh);
|
let tip = Tip::from_block(bh);
|
||||||
batch
|
batch
|
||||||
.save_sync_head(&tip)
|
.save_sync_head(&tip)
|
||||||
.map_err(|e| ErrorKind::StoreErr(e, "pipe save sync head".to_owned()))?;
|
.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);
|
debug!(LOGGER, "sync head {} @ {}", bh.hash(), bh.height);
|
||||||
Ok(tip)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_header_head(
|
fn update_header_head(
|
||||||
|
|
Loading…
Add table
Reference in a new issue