From 4d2cbe6596462e609a528182e57a965ca4d5aa8d Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Mon, 1 Oct 2018 16:03:06 +0100 Subject: [PATCH] fix: init_sync_head was not behaving as expected (#1624) Fix #1612 --- chain/src/chain.rs | 9 ++++----- chain/src/store.rs | 15 ++++----------- servers/src/grin/sync.rs | 2 +- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 2f8d148e7..deb893f83 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -975,10 +975,10 @@ impl Chain { .map_err(|e| ErrorKind::StoreErr(e, "chain block exists".to_owned()).into()) } - /// reset sync_head to header_head - pub fn init_sync_head(&self, header_head: &Tip) -> Result<(), Error> { + /// Reset sync_head to the provided head. + pub fn reset_sync_head(&self, head: &Tip) -> Result<(), Error> { let batch = self.store.batch()?; - batch.init_sync_head(header_head)?; + batch.save_sync_head(head)?; batch.commit()?; Ok(()) } @@ -1074,14 +1074,13 @@ fn setup_head( Ok(()) })?; - head = tip; info!(LOGGER, "chain: init: saved genesis: {:?}", genesis.hash()); } Err(e) => return Err(ErrorKind::StoreErr(e, "chain init load head".to_owned()))?, }; // Initialize header_head and sync_head as necessary for chain init. - batch.init_sync_head(&head)?; + batch.reset_head()?; batch.commit()?; Ok(()) diff --git a/chain/src/store.rs b/chain/src/store.rs index db5fc907c..e7c380be7 100644 --- a/chain/src/store.rs +++ b/chain/src/store.rs @@ -206,21 +206,14 @@ impl<'a> Batch<'a> { self.db.put_ser(&vec![SYNC_HEAD_PREFIX], t) } - pub fn init_sync_head(&self, t: &Tip) -> Result<(), Error> { - let header_tip = match self.store.get_header_head() { - Ok(hh) => hh, - Err(store::Error::NotFoundErr(_)) => { - self.save_header_head(t)?; - t.clone() - } - Err(e) => return Err(e), - }; - self.save_sync_head(&header_tip) + pub fn reset_sync_head(&self) -> Result<(), Error> { + let head = self.get_header_head()?; + self.save_sync_head(&head) } // Reset both header_head and sync_head to the current head of the body chain pub fn reset_head(&self) -> Result<(), Error> { - let tip = self.store.head()?; + let tip = self.head()?; self.save_header_head(&tip)?; self.save_sync_head(&tip) } diff --git a/servers/src/grin/sync.rs b/servers/src/grin/sync.rs index 5bfe2a5d7..b5de1450d 100644 --- a/servers/src/grin/sync.rs +++ b/servers/src/grin/sync.rs @@ -213,7 +213,7 @@ fn do_header_sync( header_head.hash(), header_head.height, ); - chain.init_sync_head(&header_head).unwrap(); + chain.reset_sync_head(&header_head).unwrap(); history_locators.clear(); true }