From 07df881a722f568dfa217fce0db879865f7b69bc Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Thu, 1 Mar 2018 03:08:35 +0000 Subject: [PATCH] Initialize sumtree if genesis isn't empty --- chain/src/chain.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 78fe83cd3..7d6a1e02e 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -151,16 +151,24 @@ impl Chain { ) -> Result { let chain_store = store::ChainKVStore::new(db_root.clone())?; + let store = Arc::new(chain_store); + let mut sumtrees = sumtree::SumTrees::open(db_root.clone(), store.clone())?; + // check if we have a head in store, otherwise the genesis block is it - let head = match chain_store.head() { + let head = match store.head() { Ok(tip) => tip, Err(NotFoundErr) => { let tip = Tip::new(genesis.hash()); - chain_store.save_block(&genesis)?; - chain_store.setup_height(&genesis.header, &tip)?; + store.save_block(&genesis)?; + store.setup_height(&genesis.header, &tip)?; + if genesis.kernels.len() > 0 { + sumtree::extending(&mut sumtrees, |extension| { + extension.apply_block(&genesis) + })?; + } // saving a new tip based on genesis - chain_store.save_head(&tip)?; + store.save_head(&tip)?; info!( LOGGER, "Saved genesis block: {:?}, nonce: {:?}, pow: {:?}", @@ -175,16 +183,9 @@ impl Chain { // Reset sync_head and header_head to head of current chain. // Make sure sync_head is available for later use when needed. - chain_store.reset_head()?; + store.reset_head()?; - info!( - LOGGER, - "Chain init: {:?}", - head, - ); - - let store = Arc::new(chain_store); - let sumtrees = sumtree::SumTrees::open(db_root.clone(), store.clone())?; + debug!(LOGGER, "Chain init: {:?}", head); Ok(Chain { db_root: db_root,