1
1
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
2
2
use bitcoin:: blockdata:: transaction:: Transaction ;
3
3
use bitcoin:: blockdata:: script:: Script ;
4
+ use bitcoin:: blockdata:: constants:: genesis_block;
4
5
use bitcoin:: util:: hash:: Sha256dHash ;
6
+ use bitcoin:: network:: constants:: Network ;
7
+ use bitcoin:: network:: serialize:: BitcoinHash ;
5
8
use util:: logger:: Logger ;
6
9
use std:: sync:: { Mutex , Weak , MutexGuard , Arc } ;
7
10
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
8
11
12
+ /// Used to give chain error details upstream
13
+ pub enum ChainError {
14
+ /// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
15
+ NotSupported ,
16
+ /// Chain isn't the one watched
17
+ NotWatched ,
18
+ /// Tx doesn't exist or is unconfirmed
19
+ UnknownTx ,
20
+ }
21
+
9
22
/// An interface to request notification of certain scripts as they appear the
10
23
/// chain.
11
24
/// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
@@ -24,6 +37,12 @@ pub trait ChainWatchInterface: Sync + Send {
24
37
25
38
fn register_listener ( & self , listener : Weak < ChainListener > ) ;
26
39
//TODO: unregister
40
+
41
+ /// Gets the script and value in satoshis for a given unspent transaction output given a
42
+ /// short_channel_id (aka unspent_tx_output_identier). For BTC/tBTC channels the top three
43
+ /// bytes are the block height, the next 3 the transaction index within the block, and the
44
+ /// final two the output within the transaction.
45
+ fn get_chain_utxo ( & self , genesis_hash : Sha256dHash , unspent_tx_output_identifier : u64 ) -> Result < ( Script , u64 ) , ChainError > ;
27
46
}
28
47
29
48
/// An interface to send a transaction to the Bitcoin network.
@@ -69,6 +88,7 @@ pub trait FeeEstimator: Sync + Send {
69
88
/// Utility to capture some common parts of ChainWatchInterface implementors.
70
89
/// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful.
71
90
pub struct ChainWatchInterfaceUtil {
91
+ network : Network ,
72
92
watched : Mutex < ( Vec < Script > , Vec < ( Sha256dHash , u32 ) > , bool ) > , //TODO: Something clever to optimize this
73
93
listeners : Mutex < Vec < Weak < ChainListener > > > ,
74
94
reentered : AtomicUsize ,
@@ -99,11 +119,19 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
99
119
let mut vec = self . listeners . lock ( ) . unwrap ( ) ;
100
120
vec. push ( listener) ;
101
121
}
122
+
123
+ fn get_chain_utxo ( & self , genesis_hash : Sha256dHash , _unspent_tx_output_identifier : u64 ) -> Result < ( Script , u64 ) , ChainError > {
124
+ if genesis_hash != genesis_block ( self . network ) . header . bitcoin_hash ( ) {
125
+ return Err ( ChainError :: NotWatched ) ;
126
+ }
127
+ Err ( ChainError :: NotSupported )
128
+ }
102
129
}
103
130
104
131
impl ChainWatchInterfaceUtil {
105
- pub fn new ( logger : Arc < Logger > ) -> ChainWatchInterfaceUtil {
132
+ pub fn new ( network : Network , logger : Arc < Logger > ) -> ChainWatchInterfaceUtil {
106
133
ChainWatchInterfaceUtil {
134
+ network : network,
107
135
watched : Mutex :: new ( ( Vec :: new ( ) , Vec :: new ( ) , false ) ) ,
108
136
listeners : Mutex :: new ( Vec :: new ( ) ) ,
109
137
reentered : AtomicUsize :: new ( 1 ) ,
0 commit comments