mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-20 19:11:08 +03:00
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.
This commit is contained in:
parent
384554fc46
commit
172c5e840b
4 changed files with 39 additions and 17 deletions
|
@ -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<Commitment, transaction::Output>
|
||||
|
@ -84,10 +86,13 @@ impl DummyChainImpl {
|
|||
}
|
||||
}
|
||||
|
||||
impl DummyChain for DummyChainImpl {
|
||||
impl BlockChain for DummyChainImpl {
|
||||
fn get_unspent(&self, commitment: &Commitment) -> Option<transaction::Output> {
|
||||
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<transaction::Output>;
|
||||
pub trait DummyChain: BlockChain {
|
||||
fn update_utxo_set(&mut self, new_utxo: DummyUtxoSet);
|
||||
fn apply_block(&self, b: &block::Block);
|
||||
}
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -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<T> {
|
||||
pub transactions: HashMap<hash::Hash, Box<transaction::Transaction>>,
|
||||
|
||||
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<Box<DummyChain>>,
|
||||
blockchain: Arc<T>,
|
||||
}
|
||||
|
||||
impl<T> TransactionPool<T> where T: BlockChain {
|
||||
fn new(chain: Arc<T>) -> TransactionPool<T> {
|
||||
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<DummyChain>);
|
||||
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<DummyChain>);
|
||||
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<Box<DummyChain>>) -> TransactionPool {
|
||||
fn test_setup(dummy_chain: &Arc<DummyChainImpl>) -> TransactionPool<DummyChainImpl> {
|
||||
TransactionPool{
|
||||
transactions: HashMap::new(),
|
||||
pool: Pool::empty(),
|
||||
|
|
|
@ -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<transaction::Output>;
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
|
Loading…
Reference in a new issue