From 384554fc4604635e9d2fd63a63c49fc35782b7a8 Mon Sep 17 00:00:00 2001 From: MoaningMyrtle Date: Fri, 9 Jun 2017 19:33:33 -0700 Subject: [PATCH] Change to pool->blockchain interface to avoid get_best_utxo_set (#60) * Update DummyChain and DummyChainImpl to get_unspent * Update pool to use get_unspent --- pool/src/blockchain.rs | 6 +++--- pool/src/pool.rs | 25 +++++++++---------------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/pool/src/blockchain.rs b/pool/src/blockchain.rs index 5a96adac8..9bda83ac6 100644 --- a/pool/src/blockchain.rs +++ b/pool/src/blockchain.rs @@ -85,8 +85,8 @@ impl DummyChainImpl { } impl DummyChain for DummyChainImpl { - fn get_best_utxo_set(&self) -> DummyUtxoSet { - self.utxo.read().unwrap().clone() + fn get_unspent(&self, commitment: &Commitment) -> Option { + self.utxo.read().unwrap().get_output(commitment).cloned() } fn update_utxo_set(&mut self, new_utxo: DummyUtxoSet) { self.utxo = RwLock::new(new_utxo); @@ -97,7 +97,7 @@ impl DummyChain for DummyChainImpl { } pub trait DummyChain { - fn get_best_utxo_set(&self) -> DummyUtxoSet; + fn get_unspent(&self, commitment: &Commitment) -> Option; fn update_utxo_set(&mut self, new_utxo: DummyUtxoSet); fn apply_block(&self, b: &block::Block); } diff --git a/pool/src/pool.rs b/pool/src/pool.rs index 3ba8e5065..bcf82312b 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -66,7 +66,7 @@ impl TransactionPool { // unspent set, represented by blockchain unspents - pool spents, for an // output designated by output_commitment. fn search_blockchain_unspents(&self, output_commitment: &Commitment) -> Option { - self.blockchain.get_best_utxo_set().get_output(output_commitment). + self.blockchain.get_unspent(output_commitment). map(|o| match self.pool.get_blockchain_spent(output_commitment) { Some(x) => Parent::AlreadySpent{other_tx: x.destination_hash().unwrap()}, None => Parent::BlockTransaction, @@ -218,7 +218,7 @@ impl TransactionPool { // Checking against current blockchain unspent outputs // We want outputs even if they're spent by pool txs, so we ignore // consumed_blockchain_outputs - if self.blockchain.get_best_utxo_set().get_output(&output.commitment()).is_some() { + if self.blockchain.get_unspent(&output.commitment()).is_some() { return Err(PoolError::DuplicateOutput{ other_tx: None, in_chain: true, @@ -326,9 +326,6 @@ impl TransactionPool { /// returning them to the pool in the event of a reorg that invalidates /// this block. pub fn reconcile_block(&mut self, block: &block::Block) -> Result>, PoolError> { - // Prepare the new blockchain-only UTXO view for this process - let updated_blockchain_utxo = self.blockchain.get_best_utxo_set(); - // If this pool has been kept in sync correctly, serializing all // updates, then the inputs must consume only members of the blockchain // utxo set. @@ -378,12 +375,10 @@ impl TransactionPool { println!("Conflicting txs: {:?}", conflicting_txs); for txh in conflicting_txs { - self.mark_transaction(&updated_blockchain_utxo, - txh, &mut marked_transactions); + self.mark_transaction(txh, &mut marked_transactions); } } - let freed_txs = self.sweep_transactions(marked_transactions, - &updated_blockchain_utxo); + let freed_txs = self.sweep_transactions(marked_transactions); self.reconcile_orphans(); @@ -400,8 +395,7 @@ impl TransactionPool { /// /// Marked transactions are added to the mutable marked_txs HashMap which /// is supplied by the calling function. - fn mark_transaction(&self, updated_utxo: &DummyUtxoSet, - conflicting_tx: hash::Hash, + fn mark_transaction(&self, conflicting_tx: hash::Hash, marked_txs: &mut HashMap) { marked_txs.insert(conflicting_tx, ()); @@ -411,9 +405,8 @@ impl TransactionPool { for output in &tx_ref.unwrap().outputs { match self.pool.get_internal_spent_output(&output.commitment()) { Some(x) => { - if updated_utxo.get_output(&x.output_commitment()).is_none() { - self.mark_transaction(updated_utxo, - x.destination_hash().unwrap(), marked_txs); + if self.blockchain.get_unspent(&x.output_commitment()).is_none() { + self.mark_transaction(x.destination_hash().unwrap(), marked_txs); } }, None => {}, @@ -432,8 +425,8 @@ impl TransactionPool { /// TODO: There's some iteration overlap between this and the mark step. /// Additional bookkeeping in the mark step could optimize that away. fn sweep_transactions(&mut self, - marked_transactions: HashMap, - updated_utxo: &DummyUtxoSet)->Vec> { + marked_transactions: HashMap,) + ->Vec> { println!("marked_txs: {:?}", marked_transactions); let mut removed_txs = Vec::new();