@@ -2,7 +2,7 @@ use anyhow::{bail, Context, Result};
22use bitcoin:: {
33 consensus:: { deserialize, serialize} ,
44 hashes:: hex:: { FromHex , ToHex } ,
5- BlockHash , Txid ,
5+ BlockHash , OutPoint , Txid ,
66} ;
77use rayon:: prelude:: * ;
88use serde_derive:: Deserialize ;
@@ -17,7 +17,7 @@ use crate::{
1717 daemon:: { self , extract_bitcoind_error, Daemon } ,
1818 merkle:: Proof ,
1919 metrics:: Histogram ,
20- status:: ScriptHashStatus ,
20+ status:: { OutPointStatus , ScriptHashStatus } ,
2121 tracker:: Tracker ,
2222 types:: ScriptHash ,
2323} ;
@@ -32,6 +32,7 @@ const UNKNOWN_FEE: isize = -1; // (allowed by Electrum protocol)
3232pub struct Client {
3333 tip : Option < BlockHash > ,
3434 scripthashes : HashMap < ScriptHash , ScriptHashStatus > ,
35+ outpoints : HashMap < OutPoint , OutPointStatus > ,
3536}
3637
3738#[ derive( Deserialize ) ]
@@ -158,7 +159,25 @@ impl Rpc {
158159 }
159160 } )
160161 . collect :: < Result < Vec < Value > > > ( )
161- . context ( "failed to update status" ) ?;
162+ . context ( "failed to update scripthash status" ) ?;
163+
164+ notifications. extend (
165+ client
166+ . outpoints
167+ . par_iter_mut ( )
168+ . filter_map ( |( outpoint, status) | -> Option < Result < Value > > {
169+ match self . tracker . update_outpoint_status ( status, & self . daemon ) {
170+ Ok ( true ) => Some ( Ok ( notification (
171+ "blockchain.outpoint.subscribe" ,
172+ & [ json ! ( [ outpoint. txid, outpoint. vout] ) , json ! ( status) ] ,
173+ ) ) ) ,
174+ Ok ( false ) => None , // outpoint status is the same
175+ Err ( e) => Some ( Err ( e) ) ,
176+ }
177+ } )
178+ . collect :: < Result < Vec < Value > > > ( )
179+ . context ( "failed to update scripthash status" ) ?,
180+ ) ;
162181
163182 if let Some ( old_tip) = client. tip {
164183 let new_tip = self . tracker . chain ( ) . tip ( ) ;
@@ -271,6 +290,28 @@ impl Rpc {
271290 Ok ( json ! ( result) )
272291 }
273292
293+ fn outpoint_subscribe ( & self , client : & mut Client , ( txid, vout) : ( Txid , u32 ) ) -> Result < Value > {
294+ let outpoint = OutPoint :: new ( txid, vout) ;
295+ Ok ( match client. outpoints . entry ( outpoint) {
296+ Entry :: Occupied ( e) => json ! ( e. get( ) ) ,
297+ Entry :: Vacant ( e) => {
298+ let outpoint = OutPoint :: new ( txid, vout) ;
299+ let mut status = OutPointStatus :: new ( outpoint) ;
300+ self . tracker
301+ . update_outpoint_status ( & mut status, & self . daemon ) ?;
302+ json ! ( e. insert( status) )
303+ }
304+ } )
305+ }
306+
307+ fn outpoint_unsubscribe (
308+ & self ,
309+ client : & mut Client ,
310+ ( txid, vout) : ( Txid , u32 ) ,
311+ ) -> Result < Value > {
312+ Ok ( json ! ( client. outpoints. remove( & OutPoint :: new( txid, vout) ) ) )
313+ }
314+
274315 fn new_status ( & self , scripthash : ScriptHash ) -> Result < ScriptHashStatus > {
275316 let mut status = ScriptHashStatus :: new ( scripthash) ;
276317 self . tracker
@@ -405,6 +446,8 @@ impl Rpc {
405446 Call :: HeadersSubscribe => self . headers_subscribe ( client) ,
406447 Call :: MempoolFeeHistogram => self . get_fee_histogram ( ) ,
407448 Call :: PeersSubscribe => Ok ( json ! ( [ ] ) ) ,
449+ Call :: OutPointSubscribe ( args) => self . outpoint_subscribe ( client, args) ,
450+ Call :: OutPointUnsubscribe ( args) => self . outpoint_unsubscribe ( client, args) ,
408451 Call :: Ping => Ok ( Value :: Null ) ,
409452 Call :: RelayFee => self . relayfee ( ) ,
410453 Call :: ScriptHashGetBalance ( args) => self . scripthash_get_balance ( client, args) ,
@@ -437,18 +480,20 @@ enum Call {
437480 Banner ,
438481 BlockHeader ( ( usize , ) ) ,
439482 BlockHeaders ( ( usize , usize ) ) ,
440- TransactionBroadcast ( ( String , ) ) ,
441483 Donation ,
442484 EstimateFee ( ( u16 , ) ) ,
443485 Features ,
444486 HeadersSubscribe ,
445487 MempoolFeeHistogram ,
488+ OutPointSubscribe ( ( Txid , u32 ) ) , // TODO: support spk_hint
489+ OutPointUnsubscribe ( ( Txid , u32 ) ) ,
446490 PeersSubscribe ,
447491 Ping ,
448492 RelayFee ,
449493 ScriptHashGetBalance ( ( ScriptHash , ) ) ,
450494 ScriptHashGetHistory ( ( ScriptHash , ) ) ,
451495 ScriptHashSubscribe ( ( ScriptHash , ) ) ,
496+ TransactionBroadcast ( ( String , ) ) ,
452497 TransactionGet ( TxGetArgs ) ,
453498 TransactionGetMerkle ( ( Txid , usize ) ) ,
454499 Version ( ( String , Version ) ) ,
@@ -465,6 +510,8 @@ impl Call {
465510 "blockchain.scripthash.get_balance" => Call :: ScriptHashGetBalance ( convert ( params) ?) ,
466511 "blockchain.scripthash.get_history" => Call :: ScriptHashGetHistory ( convert ( params) ?) ,
467512 "blockchain.scripthash.subscribe" => Call :: ScriptHashSubscribe ( convert ( params) ?) ,
513+ "blockchain.outpoint.subscribe" => Call :: OutPointSubscribe ( convert ( params) ?) ,
514+ "blockchain.outpoint.unsubscribe" => Call :: OutPointUnsubscribe ( convert ( params) ?) ,
468515 "blockchain.transaction.broadcast" => Call :: TransactionBroadcast ( convert ( params) ?) ,
469516 "blockchain.transaction.get" => Call :: TransactionGet ( convert ( params) ?) ,
470517 "blockchain.transaction.get_merkle" => Call :: TransactionGetMerkle ( convert ( params) ?) ,
0 commit comments