Transaction broadcast (#209)
* Add transaction broadcast to all known peers once they have been accepted by our own transaction pool. * Some debug log Fixes #200
This commit is contained in:
+1
-1
@@ -37,4 +37,4 @@ extern crate grin_keychain as keychain;
|
||||
extern crate secp256k1zkp as secp;
|
||||
|
||||
pub use pool::TransactionPool;
|
||||
pub use types::{BlockChain, TxSource, PoolError, PoolConfig};
|
||||
pub use types::{BlockChain, PoolAdapter, TxSource, PoolError, PoolConfig};
|
||||
|
||||
+5
-1
@@ -43,6 +43,7 @@ pub struct TransactionPool<T> {
|
||||
// blockchain is a DummyChain, for now, which mimics what the future
|
||||
// chain will offer to the pool
|
||||
blockchain: Arc<T>,
|
||||
adapter: Arc<PoolAdapter>,
|
||||
}
|
||||
|
||||
impl<T> TransactionPool<T>
|
||||
@@ -50,13 +51,14 @@ where
|
||||
T: BlockChain,
|
||||
{
|
||||
/// Create a new transaction pool
|
||||
pub fn new(config: PoolConfig, chain: Arc<T>) -> TransactionPool<T> {
|
||||
pub fn new(config: PoolConfig, chain: Arc<T>, adapter: Arc<PoolAdapter>) -> TransactionPool<T> {
|
||||
TransactionPool {
|
||||
config: config,
|
||||
transactions: HashMap::new(),
|
||||
pool: Pool::empty(),
|
||||
orphans: Orphans::empty(),
|
||||
blockchain: chain,
|
||||
adapter: adapter,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,6 +243,7 @@ where
|
||||
);
|
||||
|
||||
self.reconcile_orphans().unwrap();
|
||||
self.adapter.tx_accepted(&tx);
|
||||
self.transactions.insert(tx_hash, Box::new(tx));
|
||||
Ok(())
|
||||
|
||||
@@ -1196,6 +1199,7 @@ mod tests {
|
||||
pool: Pool::empty(),
|
||||
orphans: Orphans::empty(),
|
||||
blockchain: dummy_chain.clone(),
|
||||
adapter: Arc::new(NoopAdapter{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -163,6 +163,21 @@ pub trait BlockChain {
|
||||
fn head_header(&self) -> Result<block::BlockHeader, PoolError>;
|
||||
}
|
||||
|
||||
/// Bridge between the transaction pool and the rest of the system. Handles
|
||||
/// downstream processing of valid transactions by the rest of the system, most
|
||||
/// importantly the broadcasting of transactions to our peers.
|
||||
pub trait PoolAdapter: Send + Sync {
|
||||
/// The transaction pool has accepted this transactions as valid and added
|
||||
/// it to its internal cache.
|
||||
fn tx_accepted(&self, tx: &transaction::Transaction);
|
||||
}
|
||||
|
||||
/// Dummy adapter used as a placeholder for real implementations
|
||||
pub struct NoopAdapter {}
|
||||
impl PoolAdapter for NoopAdapter {
|
||||
fn tx_accepted(&self, _: &transaction::Transaction) {}
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
Reference in New Issue
Block a user