@@ -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 crossbeam_channel:: Receiver ;
88use rayon:: prelude:: * ;
@@ -19,7 +19,7 @@ use crate::{
1919 merkle:: Proof ,
2020 metrics:: { self , Histogram } ,
2121 signals:: Signal ,
22- status:: ScriptHashStatus ,
22+ status:: { OutPointStatus , ScriptHashStatus } ,
2323 tracker:: Tracker ,
2424 types:: ScriptHash ,
2525} ;
@@ -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 ) ]
@@ -176,7 +177,25 @@ impl Rpc {
176177 }
177178 } )
178179 . collect :: < Result < Vec < Value > > > ( )
179- . context ( "failed to update status" ) ?;
180+ . context ( "failed to update scripthash status" ) ?;
181+
182+ notifications. extend (
183+ client
184+ . outpoints
185+ . par_iter_mut ( )
186+ . filter_map ( |( outpoint, status) | -> Option < Result < Value > > {
187+ match self . tracker . update_outpoint_status ( status, & self . daemon ) {
188+ Ok ( true ) => Some ( Ok ( notification (
189+ "blockchain.outpoint.subscribe" ,
190+ & [ json ! ( [ outpoint. txid, outpoint. vout] ) , json ! ( status) ] ,
191+ ) ) ) ,
192+ Ok ( false ) => None , // outpoint status is the same
193+ Err ( e) => Some ( Err ( e) ) ,
194+ }
195+ } )
196+ . collect :: < Result < Vec < Value > > > ( )
197+ . context ( "failed to update scripthash status" ) ?,
198+ ) ;
180199
181200 if let Some ( old_tip) = client. tip {
182201 let new_tip = self . tracker . chain ( ) . tip ( ) ;
@@ -304,6 +323,28 @@ impl Rpc {
304323 Ok ( json ! ( result) )
305324 }
306325
326+ fn outpoint_subscribe ( & self , client : & mut Client , ( txid, vout) : ( Txid , u32 ) ) -> Result < Value > {
327+ let outpoint = OutPoint :: new ( txid, vout) ;
328+ Ok ( match client. outpoints . entry ( outpoint) {
329+ Entry :: Occupied ( e) => json ! ( e. get( ) ) ,
330+ Entry :: Vacant ( e) => {
331+ let outpoint = OutPoint :: new ( txid, vout) ;
332+ let mut status = OutPointStatus :: new ( outpoint) ;
333+ self . tracker
334+ . update_outpoint_status ( & mut status, & self . daemon ) ?;
335+ json ! ( e. insert( status) )
336+ }
337+ } )
338+ }
339+
340+ fn outpoint_unsubscribe (
341+ & self ,
342+ client : & mut Client ,
343+ ( txid, vout) : ( Txid , u32 ) ,
344+ ) -> Result < Value > {
345+ Ok ( json ! ( client. outpoints. remove( & OutPoint :: new( txid, vout) ) ) )
346+ }
347+
307348 fn new_status ( & self , scripthash : ScriptHash ) -> Result < ScriptHashStatus > {
308349 let mut status = ScriptHashStatus :: new ( scripthash) ;
309350 self . tracker
@@ -438,6 +479,8 @@ impl Rpc {
438479 Call :: HeadersSubscribe => self . headers_subscribe ( client) ,
439480 Call :: MempoolFeeHistogram => self . get_fee_histogram ( ) ,
440481 Call :: PeersSubscribe => Ok ( json ! ( [ ] ) ) ,
482+ Call :: OutPointSubscribe ( args) => self . outpoint_subscribe ( client, args) ,
483+ Call :: OutPointUnsubscribe ( args) => self . outpoint_unsubscribe ( client, args) ,
441484 Call :: Ping => Ok ( Value :: Null ) ,
442485 Call :: RelayFee => self . relayfee ( ) ,
443486 Call :: ScriptHashGetBalance ( args) => self . scripthash_get_balance ( client, args) ,
@@ -471,19 +514,21 @@ enum Call {
471514 Banner ,
472515 BlockHeader ( ( usize , ) ) ,
473516 BlockHeaders ( ( usize , usize ) ) ,
474- TransactionBroadcast ( ( String , ) ) ,
475517 Donation ,
476518 EstimateFee ( ( u16 , ) ) ,
477519 Features ,
478520 HeadersSubscribe ,
479521 MempoolFeeHistogram ,
522+ OutPointSubscribe ( ( Txid , u32 ) ) , // TODO: support spk_hint
523+ OutPointUnsubscribe ( ( Txid , u32 ) ) ,
480524 PeersSubscribe ,
481525 Ping ,
482526 RelayFee ,
483527 ScriptHashGetBalance ( ( ScriptHash , ) ) ,
484528 ScriptHashGetHistory ( ( ScriptHash , ) ) ,
485529 ScriptHashListUnspent ( ( ScriptHash , ) ) ,
486530 ScriptHashSubscribe ( ( ScriptHash , ) ) ,
531+ TransactionBroadcast ( ( String , ) ) ,
487532 TransactionGet ( TxGetArgs ) ,
488533 TransactionGetMerkle ( ( Txid , usize ) ) ,
489534 Version ( ( String , Version ) ) ,
@@ -501,6 +546,8 @@ impl Call {
501546 "blockchain.scripthash.get_history" => Call :: ScriptHashGetHistory ( convert ( params) ?) ,
502547 "blockchain.scripthash.listunspent" => Call :: ScriptHashListUnspent ( convert ( params) ?) ,
503548 "blockchain.scripthash.subscribe" => Call :: ScriptHashSubscribe ( convert ( params) ?) ,
549+ "blockchain.outpoint.subscribe" => Call :: OutPointSubscribe ( convert ( params) ?) ,
550+ "blockchain.outpoint.unsubscribe" => Call :: OutPointUnsubscribe ( convert ( params) ?) ,
504551 "blockchain.transaction.broadcast" => Call :: TransactionBroadcast ( convert ( params) ?) ,
505552 "blockchain.transaction.get" => Call :: TransactionGet ( convert ( params) ?) ,
506553 "blockchain.transaction.get_merkle" => Call :: TransactionGetMerkle ( convert ( params) ?) ,
0 commit comments