diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 61e7391d9..e707068c4 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -636,6 +636,7 @@ impl Chain { Ok(b) => { count += 1; batch.delete_block(&b.hash())?; + //TODO: Validation seems to fail as block markers are being deleted? batch.delete_block_marker(&b.hash())?; batch.delete_block_input_bitmap(&b.hash())?; } diff --git a/chain/src/store.rs b/chain/src/store.rs index fa590c9bf..818db8f78 100644 --- a/chain/src/store.rs +++ b/chain/src/store.rs @@ -272,7 +272,7 @@ impl<'a> Batch<'a> { } /// get block - fn get_block(&self, h: &Hash) -> Result { + pub fn get_block(&self, h: &Hash) -> Result { option_to_not_found(self.db.get_ser(&to_key(BLOCK_PREFIX, &mut h.to_vec()))) } diff --git a/chain/tests/mine_simple_chain.rs b/chain/tests/mine_simple_chain.rs index 092facd62..13c49a84d 100644 --- a/chain/tests/mine_simple_chain.rs +++ b/chain/tests/mine_simple_chain.rs @@ -356,8 +356,12 @@ fn spend_in_fork_and_compact() { } chain.validate(false).unwrap(); - chain.compact().unwrap(); - chain.validate(false).unwrap(); + if let Err(e) = chain.compact() { + panic!("Error compacting chain: {:?}", e); + } + if let Err(e) = chain.validate(false) { + panic!("Validation error after compacting chain: {:?}", e); + } } fn prepare_block(kc: &K, prev: &BlockHeader, chain: &Chain, diff: u64) -> Block diff --git a/wallet/tests/common/mod.rs b/wallet/tests/common/mod.rs index 33671c9bc..eaa7b208e 100644 --- a/wallet/tests/common/mod.rs +++ b/wallet/tests/common/mod.rs @@ -28,7 +28,8 @@ use core::core::hash::Hashed; use core::core::{Output, OutputFeatures, OutputIdentifier, Transaction, TxKernel}; use core::{consensus, global, pow}; use keychain::ExtKeychain; -use wallet::file_wallet::{FileWallet, WalletConfig}; +use wallet::file_wallet::FileWallet; +use wallet::WalletConfig; use wallet::libwallet::internal::updater; use wallet::libwallet::types::{BlockFees, BlockIdentifier, MerkleProofWrapper, OutputStatus, WalletBackend}; @@ -78,40 +79,35 @@ where T: WalletBackend, K: keychain::Keychain, { - let ret_val = wallet.read_wallet(|wallet_data| { - let mut unspent_total = 0; - let mut unspent_but_locked_total = 0; - let mut unconfirmed_total = 0; - let mut locked_total = 0; - let keychain = wallet_data.keychain().clone(); - for out in wallet_data - .outputs() - .values() - .filter(|out| out.root_key_id == keychain.root_key_id()) - { - if out.status == OutputStatus::Unspent { - unspent_total += out.value; - if out.lock_height > height { - unspent_but_locked_total += out.value; - } - } - if out.status == OutputStatus::Unconfirmed && !out.is_coinbase { - unconfirmed_total += out.value; - } - if out.status == OutputStatus::Locked { - locked_total += out.value; + let mut unspent_total = 0; + let mut unspent_but_locked_total = 0; + let mut unconfirmed_total = 0; + let mut locked_total = 0; + let keychain = wallet.keychain().clone(); + for out in wallet.iter() + .filter(|out| out.root_key_id == keychain.root_key_id()) + { + if out.status == OutputStatus::Unspent { + unspent_total += out.value; + if out.lock_height > height { + unspent_but_locked_total += out.value; } } + if out.status == OutputStatus::Unconfirmed && !out.is_coinbase { + unconfirmed_total += out.value; + } + if out.status == OutputStatus::Locked { + locked_total += out.value; + } + } - Ok(( - unspent_total + unconfirmed_total, //total - unconfirmed_total, //amount_awaiting_confirmation - unspent_but_locked_total, // confirmed but locked - unspent_total - unspent_but_locked_total, // currently spendable - locked_total, // locked total - )) - }); - ret_val + Ok(( + unspent_total + unconfirmed_total, //total + unconfirmed_total, //amount_awaiting_confirmation + unspent_but_locked_total, // confirmed but locked + unspent_total - unspent_but_locked_total, // currently spendable + locked_total, // locked total + )) } /// Get an output from the chain locally and present it back as an API output @@ -183,16 +179,12 @@ where let output_id = OutputIdentifier::from_output(&coinbase_tx.0.clone()); let m_proof = chain.get_merkle_proof(&output_id, &chain.head_header().unwrap()); let block_id = Some(BlockIdentifier(chain.head_header().unwrap().hash())); - let _ = wallet.with_wallet(|wallet_data| { - if let Entry::Occupied(mut output) = wallet_data - .outputs() - .entry(fees.key_id.as_ref().unwrap().to_hex()) - { - let output = output.get_mut(); - output.block = block_id; - output.merkle_proof = Some(MerkleProofWrapper(m_proof.unwrap())); - } - }); + let mut output = wallet.get(&fees.key_id.unwrap()).unwrap(); + output.block = block_id; + output.merkle_proof = Some(MerkleProofWrapper(m_proof.unwrap())); + let mut batch = wallet.batch().unwrap(); + batch.save(output).unwrap(); + batch.commit().unwrap(); } /// adds many block rewards to a wallet, no transactions