From: Tamas Blummer Date: Tue, 13 Mar 2018 17:51:33 +0000 (+0100) Subject: Split out BroadcastInterface, ChainWatchInterface monitors re-enter from called listeners X-Git-Tag: v0.0.12~421^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=fbe4f7a442f4e969bf22dfca5d41482c80206c84;p=rust-lightning Split out BroadcastInterface, ChainWatchInterface monitors re-enter from called listeners --- diff --git a/Cargo.toml b/Cargo.toml index 822484298..b7c6e5cf5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ non_bitcoin_chain_hash_routing = [] [dependencies] bitcoin = "0.11" -bitcoin-chain = { git = "https://github.com/rust-bitcoin/rust-bitcoin-chain", branch = "master" } rust-crypto = "0.2" rand = "0.4" secp256k1 = "0.8" diff --git a/src/chain/bitcoincorerpcchain.rs b/src/chain/bitcoincorerpcchain.rs index f51b1d735..5c14b5da0 100644 --- a/src/chain/bitcoincorerpcchain.rs +++ b/src/chain/bitcoincorerpcchain.rs @@ -2,7 +2,7 @@ use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::Script; use bitcoin::util::hash::Sha256dHash; -use chain::chaininterface::{ChainWatchInterface,ChainWatchInterfaceUtil,ChainListener}; +use chain::chaininterface::{ChainWatchInterface,ChainWatchInterfaceUtil,ChainListener, BroadcasterInterface}; use std::sync::Weak; @@ -23,15 +23,17 @@ impl ChainWatchInterface for BitcoinCoreRPCClientChain { self.util.watch_all_txn() } - fn broadcast_transaction(&self, _tx: &Transaction) { - unimplemented!() - } - fn register_listener(&self, listener: Weak) { self.util.register_listener(listener) } } +impl BroadcasterInterface for BitcoinCoreRPCClientChain { + fn broadcast_transaction(&self, _tx: &Transaction) { + unimplemented!() + } +} + impl BitcoinCoreRPCClientChain { pub fn new() -> BitcoinCoreRPCClientChain { BitcoinCoreRPCClientChain { diff --git a/src/chain/chaininterface.rs b/src/chain/chaininterface.rs index ad298ce94..f456afda6 100644 --- a/src/chain/chaininterface.rs +++ b/src/chain/chaininterface.rs @@ -1,9 +1,9 @@ -use bitcoin::blockdata::block::BlockHeader; +use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::Script; use bitcoin::util::hash::Sha256dHash; - -use std::sync::{Weak,Mutex}; +use std::sync::{Mutex,Weak,MutexGuard}; +use std::sync::atomic::{AtomicUsize, Ordering}; /// An interface to request notification of certain scripts as they appear the /// chain. @@ -21,13 +21,18 @@ pub trait ChainWatchInterface: Sync + Send { /// Indicates that a listener needs to see all transactions. fn watch_all_txn(&self); - /// Sends a transaction out to (hopefully) be mined - fn broadcast_transaction(&self, tx: &Transaction); - fn register_listener(&self, listener: Weak); //TODO: unregister } +/// An interface to send a transaction to connected Bitcoin peers. +/// This is for final settlement. An error might indicate that no peers can be reached or +/// that peers rejected the transaction. +pub trait BroadcasterInterface: Sync + Send { + /// Sends a transaction out to (hopefully) be mined + fn broadcast_transaction(&self, tx: &Transaction); +} + /// A trait indicating a desire to listen for events from the chain pub trait ChainListener: Sync + Send { /// Notifies a listener that a block was connected. @@ -54,7 +59,7 @@ pub enum ConfirmationTarget { /// called from inside the library in response to ChainListener events, P2P events, or timer /// events). pub trait FeeEstimator: Sync + Send { - fn get_est_sat_per_vbyte(&self, ConfirmationTarget) -> u64; + fn get_est_sat_per_vbyte(&self, confirmation_target: ConfirmationTarget) -> u64; } /// Utility to capture some common parts of ChainWatchInterface implementors. @@ -62,59 +67,97 @@ pub trait FeeEstimator: Sync + Send { pub struct ChainWatchInterfaceUtil { watched: Mutex<(Vec