From 172c5e840b2a00535bcedf8f48432cffbcc4d71d Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Sat, 10 Jun 2017 11:31:05 -0700 Subject: [PATCH] TransactionPool uses non-dummy chain trait Introduced new non-dummy trait for the blockchain as seen from the pool that just produces a UTXO. Made to pool parametric on that trait to get rid of the Box wrapper and still allow the test implementation. --- pool/src/blockchain.rs | 10 +++++++--- pool/src/lib.rs | 9 ++++++--- pool/src/pool.rs | 26 +++++++++++++++++--------- pool/src/types.rs | 11 +++++++++-- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/pool/src/blockchain.rs b/pool/src/blockchain.rs index 9bda83ac6..499848d43 100644 --- a/pool/src/blockchain.rs +++ b/pool/src/blockchain.rs @@ -18,6 +18,8 @@ use secp::pedersen::Commitment; use std::sync::RwLock; +use types::BlockChain; + /// A DummyUtxoSet for mocking up the chain pub struct DummyUtxoSet { outputs : HashMap @@ -84,10 +86,13 @@ impl DummyChainImpl { } } -impl DummyChain for DummyChainImpl { +impl BlockChain for DummyChainImpl { fn get_unspent(&self, commitment: &Commitment) -> Option { self.utxo.read().unwrap().get_output(commitment).cloned() } +} + +impl DummyChain for DummyChainImpl { fn update_utxo_set(&mut self, new_utxo: DummyUtxoSet) { self.utxo = RwLock::new(new_utxo); } @@ -96,8 +101,7 @@ impl DummyChain for DummyChainImpl { } } -pub trait DummyChain { - fn get_unspent(&self, commitment: &Commitment) -> Option; +pub trait DummyChain: BlockChain { fn update_utxo_set(&mut self, new_utxo: DummyUtxoSet); fn apply_block(&self, b: &block::Block); } diff --git a/pool/src/lib.rs b/pool/src/lib.rs index 8d496ff6e..634b0fa4f 100644 --- a/pool/src/lib.rs +++ b/pool/src/lib.rs @@ -22,9 +22,9 @@ #![warn(missing_docs)] pub mod graph; -pub mod types; -pub mod blockchain; -pub mod pool; +mod types; +mod blockchain; +mod pool; extern crate time; extern crate rand; @@ -33,3 +33,6 @@ extern crate log; extern crate grin_core as core; extern crate secp256k1zkp as secp; + +pub use pool::TransactionPool; +pub use types::{BlockChain, PoolError}; diff --git a/pool/src/pool.rs b/pool/src/pool.rs index bcf82312b..841d49929 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -14,7 +14,7 @@ //! Top-level Pool type, methods, and tests -use types::{Pool, Orphans, Parent, PoolError, TxSource, TransactionGraphContainer}; +use types::{Pool, BlockChain, Orphans, Parent, PoolError, TxSource, TransactionGraphContainer}; pub use graph; use core::core::transaction; @@ -31,7 +31,7 @@ use std::collections::HashMap; /// The pool itself. /// The transactions HashMap holds ownership of all transactions in the pool, /// keyed by their transaction hash. -struct TransactionPool { +pub struct TransactionPool { pub transactions: HashMap>, pub pool : Pool, @@ -39,11 +39,19 @@ struct TransactionPool { // blockchain is a DummyChain, for now, which mimics what the future // chain will offer to the pool - blockchain: Arc>, + blockchain: Arc, } +impl TransactionPool where T: BlockChain { + fn new(chain: Arc) -> TransactionPool { + TransactionPool{ + transactions: HashMap::new(), + pool: Pool::empty(), + orphans: Orphans::empty(), + blockchain: chain, + } + } -impl TransactionPool { /// Searches for an output, designated by its commitment, from the current /// best UTXO view, presented by taking the best blockchain UTXO set (as /// determined by the blockchain component) and rectifying pool spent and @@ -491,7 +499,7 @@ mod tests { // To mirror how this construction is intended to be used, the pool // is placed inside a RwLock. - let pool = RwLock::new(test_setup(&Arc::new(Box::new(dummy_chain)))); + let pool = RwLock::new(test_setup(&Arc::new(dummy_chain))); // Take the write lock and add a pool entry { @@ -546,7 +554,7 @@ mod tests { dummy_chain.update_utxo_set(new_utxo); - let pool = RwLock::new(test_setup(&Arc::new(Box::new(dummy_chain)))); + let pool = RwLock::new(test_setup(&Arc::new(dummy_chain))); { let mut write_pool = pool.write().unwrap(); assert_eq!(write_pool.total_size(), 0); @@ -634,7 +642,7 @@ mod tests { dummy_chain.update_utxo_set(new_utxo); - let chain_ref = Arc::new(Box::new(dummy_chain) as Box); + let chain_ref = Arc::new(dummy_chain); let pool = RwLock::new(test_setup(&chain_ref)); @@ -774,7 +782,7 @@ mod tests { dummy_chain.update_utxo_set(new_utxo); - let chain_ref = Arc::new(Box::new(dummy_chain) as Box); + let chain_ref = Arc::new(dummy_chain); let pool = RwLock::new(test_setup(&chain_ref)); @@ -836,7 +844,7 @@ mod tests { } - fn test_setup(dummy_chain: &Arc>) -> TransactionPool { + fn test_setup(dummy_chain: &Arc) -> TransactionPool { TransactionPool{ transactions: HashMap::new(), pool: Pool::empty(), diff --git a/pool/src/types.rs b/pool/src/types.rs index e8dbf0d31..a496c93ad 100644 --- a/pool/src/types.rs +++ b/pool/src/types.rs @@ -34,8 +34,6 @@ use core::core::transaction; use core::core::block; use core::core::hash; - - /// Placeholder: the data representing where we heard about a tx from. /// /// Used to make decisions based on transaction acceptance priority from @@ -84,6 +82,15 @@ pub enum PoolError { OrphanTransaction, } +/// Interface that the pool requires from a blockchain implementation. +pub trait BlockChain { + /// Get an unspent output by its commitment. Will return None if the output + /// is spent or if it doesn't exist. The blockchain is expected to produce + /// a result with its current view of the most worked chain, ignoring + /// orphans, etc. + fn get_unspent(&self, output_ref: &Commitment) -> Option; +} + /// Pool contains the elements of the graph that are connected, in full, to /// the blockchain. /// Reservations of outputs by orphan transactions (not fully connected) are