From 9fb3b71864a5cdf080c81df642e3ec5769f30042 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 30 Aug 2018 01:40:18 +0000 Subject: [PATCH] Implement get_network, get_chain_txo, get_spendable_outpoint in ChainWatchInterface to Router check on channel_announcement Needed for BOLT 7 --- src/chain/chaininterface.rs | 55 ++++++++++++++++++++++++++++++++++++- src/ln/router.rs | 11 ++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/chain/chaininterface.rs b/src/chain/chaininterface.rs index bda99b2d8..533b007fe 100644 --- a/src/chain/chaininterface.rs +++ b/src/chain/chaininterface.rs @@ -1,11 +1,26 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::Script; +use bitcoin::blockdata::constants::genesis_block; use bitcoin::util::hash::Sha256dHash; +use bitcoin::network::constants::Network; +use bitcoin::network::serialize::BitcoinHash; use util::logger::Logger; use std::sync::{Mutex,Weak,MutexGuard,Arc}; use std::sync::atomic::{AtomicUsize, Ordering}; +/// Used to give chain error details upstream +pub enum ChainError { + /// Chain isn't supported + NotSupported, + /// Chain isn't the one watched + NotWatched, + /// Tx isn't there + UnknownTx, + /// Tx isn't confirmed + UnconfirmedTx, +} + /// An interface to request notification of certain scripts as they appear the /// chain. /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're @@ -24,6 +39,15 @@ pub trait ChainWatchInterface: Sync + Send { fn register_listener(&self, listener: Weak); //TODO: unregister + + /// Gets the chain currently watched + fn get_network(&self) -> Network; + + /// Gets the script and value in satoshis for a given txid and outpoint index + fn get_chain_txo(&self, genesis_hash: Sha256dHash, txid: Sha256dHash, output_index: u16) -> Result<(Script, u64), ChainError>; + + /// Gets if outpoint is among UTXO + fn get_spendable_outpoint(&self, genesis_hash: Sha256dHash, txid: Sha256dHash, output_index: u16) -> Result; } /// An interface to send a transaction to the Bitcoin network. @@ -69,12 +93,21 @@ pub trait FeeEstimator: Sync + Send { /// Utility to capture some common parts of ChainWatchInterface implementors. /// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful. pub struct ChainWatchInterfaceUtil { + network: Network, watched: Mutex<(Vec