@@ -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 ;
@@ -18,7 +18,7 @@ use crate::{
1818 merkle:: Proof ,
1919 metrics:: Histogram ,
2020 signals:: Signal ,
21- status:: ScriptHashStatus ,
21+ status:: { OutPointStatus , ScriptHashStatus } ,
2222 tracker:: Tracker ,
2323 types:: ScriptHash ,
2424} ;
@@ -33,6 +33,7 @@ const UNKNOWN_FEE: isize = -1; // (allowed by Electrum protocol)
3333pub struct Client {
3434 tip : Option < BlockHash > ,
3535 scripthashes : HashMap < ScriptHash , ScriptHashStatus > ,
36+ outpoints : HashMap < OutPoint , OutPointStatus > ,
3637}
3738
3839#[ derive( Deserialize ) ]
@@ -168,7 +169,25 @@ impl Rpc {
168169 }
169170 } )
170171 . collect :: < Result < Vec < Value > > > ( )
171- . context ( "failed to update status" ) ?;
172+ . context ( "failed to update scripthash status" ) ?;
173+
174+ notifications. extend (
175+ client
176+ . outpoints
177+ . par_iter_mut ( )
178+ . filter_map ( |( outpoint, status) | -> Option < Result < Value > > {
179+ match self . tracker . update_outpoint_status ( status, & self . daemon ) {
180+ Ok ( true ) => Some ( Ok ( notification (
181+ "blockchain.outpoint.subscribe" ,
182+ & [ json ! ( [ outpoint. txid, outpoint. vout] ) , json ! ( status) ] ,
183+ ) ) ) ,
184+ Ok ( false ) => None , // outpoint status is the same
185+ Err ( e) => Some ( Err ( e) ) ,
186+ }
187+ } )
188+ . collect :: < Result < Vec < Value > > > ( )
189+ . context ( "failed to update scripthash status" ) ?,
190+ ) ;
172191
173192 if let Some ( old_tip) = client. tip {
174193 let new_tip = self . tracker . chain ( ) . tip ( ) ;
@@ -296,6 +315,28 @@ impl Rpc {
296315 Ok ( json ! ( result) )
297316 }
298317
318+ fn outpoint_subscribe ( & self , client : & mut Client , ( txid, vout) : ( Txid , u32 ) ) -> Result < Value > {
319+ let outpoint = OutPoint :: new ( txid, vout) ;
320+ Ok ( match client. outpoints . entry ( outpoint) {
321+ Entry :: Occupied ( e) => json ! ( e. get( ) ) ,
322+ Entry :: Vacant ( e) => {
323+ let outpoint = OutPoint :: new ( txid, vout) ;
324+ let mut status = OutPointStatus :: new ( outpoint) ;
325+ self . tracker
326+ . update_outpoint_status ( & mut status, & self . daemon ) ?;
327+ json ! ( e. insert( status) )
328+ }
329+ } )
330+ }
331+
332+ fn outpoint_unsubscribe (
333+ & self ,
334+ client : & mut Client ,
335+ ( txid, vout) : ( Txid , u32 ) ,
336+ ) -> Result < Value > {
337+ Ok ( json ! ( client. outpoints. remove( & OutPoint :: new( txid, vout) ) ) )
338+ }
339+
299340 fn new_status ( & self , scripthash : ScriptHash ) -> Result < ScriptHashStatus > {
300341 let mut status = ScriptHashStatus :: new ( scripthash) ;
301342 self . tracker
@@ -430,6 +471,8 @@ impl Rpc {
430471 Call :: HeadersSubscribe => self . headers_subscribe ( client) ,
431472 Call :: MempoolFeeHistogram => self . get_fee_histogram ( ) ,
432473 Call :: PeersSubscribe => Ok ( json ! ( [ ] ) ) ,
474+ Call :: OutPointSubscribe ( args) => self . outpoint_subscribe ( client, args) ,
475+ Call :: OutPointUnsubscribe ( args) => self . outpoint_unsubscribe ( client, args) ,
433476 Call :: Ping => Ok ( Value :: Null ) ,
434477 Call :: RelayFee => self . relayfee ( ) ,
435478 Call :: ScriptHashGetBalance ( args) => self . scripthash_get_balance ( client, args) ,
@@ -463,19 +506,21 @@ enum Call {
463506 Banner ,
464507 BlockHeader ( ( usize , ) ) ,
465508 BlockHeaders ( ( usize , usize ) ) ,
466- TransactionBroadcast ( ( String , ) ) ,
467509 Donation ,
468510 EstimateFee ( ( u16 , ) ) ,
469511 Features ,
470512 HeadersSubscribe ,
471513 MempoolFeeHistogram ,
514+ OutPointSubscribe ( ( Txid , u32 ) ) , // TODO: support spk_hint
515+ OutPointUnsubscribe ( ( Txid , u32 ) ) ,
472516 PeersSubscribe ,
473517 Ping ,
474518 RelayFee ,
475519 ScriptHashGetBalance ( ( ScriptHash , ) ) ,
476520 ScriptHashGetHistory ( ( ScriptHash , ) ) ,
477521 ScriptHashListUnspent ( ( ScriptHash , ) ) ,
478522 ScriptHashSubscribe ( ( ScriptHash , ) ) ,
523+ TransactionBroadcast ( ( String , ) ) ,
479524 TransactionGet ( TxGetArgs ) ,
480525 TransactionGetMerkle ( ( Txid , usize ) ) ,
481526 Version ( ( String , Version ) ) ,
@@ -493,6 +538,8 @@ impl Call {
493538 "blockchain.scripthash.get_history" => Call :: ScriptHashGetHistory ( convert ( params) ?) ,
494539 "blockchain.scripthash.listunspent" => Call :: ScriptHashListUnspent ( convert ( params) ?) ,
495540 "blockchain.scripthash.subscribe" => Call :: ScriptHashSubscribe ( convert ( params) ?) ,
541+ "blockchain.outpoint.subscribe" => Call :: OutPointSubscribe ( convert ( params) ?) ,
542+ "blockchain.outpoint.unsubscribe" => Call :: OutPointUnsubscribe ( convert ( params) ?) ,
496543 "blockchain.transaction.broadcast" => Call :: TransactionBroadcast ( convert ( params) ?) ,
497544 "blockchain.transaction.get" => Call :: TransactionGet ( convert ( params) ?) ,
498545 "blockchain.transaction.get_merkle" => Call :: TransactionGetMerkle ( convert ( params) ?) ,
0 commit comments