From fbe4f7a442f4e969bf22dfca5d41482c80206c84 Mon Sep 17 00:00:00 2001 From: Tamas Blummer Date: Tue, 13 Mar 2018 18:51:33 +0100 Subject: [PATCH] Split out BroadcastInterface, ChainWatchInterface monitors re-enter from called listeners --- Cargo.toml | 1 - src/chain/bitcoincorerpcchain.rs | 12 +++-- src/chain/chaininterface.rs | 91 +++++++++++++++++++++++--------- src/chain/mod.rs | 1 - src/chain/rustbitcoinchain.rs | 66 ----------------------- src/lib.rs | 1 - src/ln/channelmanager.rs | 4 +- src/ln/channelmonitor.rs | 14 ++--- src/util/test_utils.rs | 3 -- 9 files changed, 84 insertions(+), 109 deletions(-) delete mode 100644 src/chain/rustbitcoinchain.rs 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