@@ -25,6 +25,7 @@ use protocol::{
25
25
constants:: ChainAnchor ,
26
26
hasher:: { BaseHash , SpaceKey } ,
27
27
prepare:: DataSource ,
28
+ validate:: TxChangeSet ,
28
29
FullSpaceOut , SpaceOut ,
29
30
} ;
30
31
use serde:: { Deserialize , Serialize } ;
@@ -71,6 +72,10 @@ pub enum ChainStateCommand {
71
72
hash : SpaceKey ,
72
73
resp : Responder < anyhow:: Result < Option < OutPoint > > > ,
73
74
} ,
75
+ GetTxMeta {
76
+ txid : Txid ,
77
+ resp : Responder < anyhow:: Result < Option < TxChangeSet > > > ,
78
+ } ,
74
79
GetBlockMeta {
75
80
block_hash : BlockHash ,
76
81
resp : Responder < anyhow:: Result < Option < BlockMeta > > > ,
@@ -111,9 +116,14 @@ pub trait Rpc {
111
116
#[ method( name = "getrollout" ) ]
112
117
async fn get_rollout ( & self , target : usize ) -> Result < Vec < ( u32 , SpaceKey ) > , ErrorObjectOwned > ;
113
118
114
- #[ method( name = "getblock" ) ]
115
- async fn get_block ( & self , block_hash : BlockHash )
116
- -> Result < Option < BlockMeta > , ErrorObjectOwned > ;
119
+ #[ method( name = "getblockmeta" ) ]
120
+ async fn get_block_meta (
121
+ & self ,
122
+ block_hash : BlockHash ,
123
+ ) -> Result < Option < BlockMeta > , ErrorObjectOwned > ;
124
+
125
+ #[ method( name = "gettxmeta" ) ]
126
+ async fn get_tx_meta ( & self , txid : Txid ) -> Result < Option < TxChangeSet > , ErrorObjectOwned > ;
117
127
118
128
#[ method( name = "walletload" ) ]
119
129
async fn wallet_load ( & self , name : & str ) -> Result < ( ) , ErrorObjectOwned > ;
@@ -652,19 +662,28 @@ impl RpcServer for RpcServerImpl {
652
662
Ok ( rollouts)
653
663
}
654
664
655
- async fn get_block (
665
+ async fn get_block_meta (
656
666
& self ,
657
667
block_hash : BlockHash ,
658
668
) -> Result < Option < BlockMeta > , ErrorObjectOwned > {
659
669
let data = self
660
670
. store
661
- . get_block ( block_hash)
671
+ . get_block_meta ( block_hash)
662
672
. await
663
673
. map_err ( |error| ErrorObjectOwned :: owned ( -1 , error. to_string ( ) , None :: < String > ) ) ?;
664
674
665
675
Ok ( data)
666
676
}
667
677
678
+ async fn get_tx_meta ( & self , txid : Txid ) -> Result < Option < TxChangeSet > , ErrorObjectOwned > {
679
+ let data = self
680
+ . store
681
+ . get_tx_meta ( txid)
682
+ . await
683
+ . map_err ( |error| ErrorObjectOwned :: owned ( -1 , error. to_string ( ) , None :: < String > ) ) ?;
684
+ Ok ( data)
685
+ }
686
+
668
687
async fn wallet_load ( & self , name : & str ) -> Result < ( ) , ErrorObjectOwned > {
669
688
self . wallet_manager
670
689
. load_wallet ( & self . client , name)
@@ -794,6 +813,31 @@ impl AsyncChainState {
794
813
Self { sender }
795
814
}
796
815
816
+ async fn get_indexed_tx (
817
+ index : & mut Option < LiveSnapshot > ,
818
+ txid : & Txid ,
819
+ client : & reqwest:: Client ,
820
+ rpc : & BitcoinRpc ,
821
+ chain_state : & mut LiveSnapshot ,
822
+ ) -> Result < Option < TxChangeSet > , anyhow:: Error > {
823
+ let info: serde_json:: Value = rpc
824
+ . send_json ( client, & rpc. get_raw_transaction ( & txid, true ) )
825
+ . await
826
+ . map_err ( |e| anyhow ! ( "Could not retrieve tx ({})" , e) ) ?;
827
+
828
+ let block_hash = BlockHash :: from_str (
829
+ info. get ( "blockhash" )
830
+ . and_then ( |t| t. as_str ( ) )
831
+ . ok_or_else ( || anyhow ! ( "Could not retrieve block hash" ) ) ?,
832
+ ) ?;
833
+ let block = Self :: get_indexed_block ( index, & block_hash, client, rpc, chain_state) . await ?;
834
+
835
+ if let Some ( block) = block {
836
+ return Ok ( block. tx_meta . into_iter ( ) . find ( |tx| & tx. txid == txid) ) ;
837
+ }
838
+ Ok ( None )
839
+ }
840
+
797
841
async fn get_indexed_block (
798
842
index : & mut Option < LiveSnapshot > ,
799
843
block_hash : & BlockHash ,
@@ -868,6 +912,10 @@ impl AsyncChainState {
868
912
. await ;
869
913
let _ = resp. send ( res) ;
870
914
}
915
+ ChainStateCommand :: GetTxMeta { txid, resp } => {
916
+ let res = Self :: get_indexed_tx ( block_index, & txid, client, rpc, chain_state) . await ;
917
+ let _ = resp. send ( res) ;
918
+ }
871
919
ChainStateCommand :: EstimateBid { target, resp } => {
872
920
let estimate = chain_state. estimate_bid ( target) ;
873
921
_ = resp. send ( estimate) ;
@@ -947,13 +995,21 @@ impl AsyncChainState {
947
995
resp_rx. await ?
948
996
}
949
997
950
- pub async fn get_block ( & self , block_hash : BlockHash ) -> anyhow:: Result < Option < BlockMeta > > {
998
+ pub async fn get_block_meta ( & self , block_hash : BlockHash ) -> anyhow:: Result < Option < BlockMeta > > {
951
999
let ( resp, resp_rx) = oneshot:: channel ( ) ;
952
1000
self . sender
953
1001
. send ( ChainStateCommand :: GetBlockMeta { block_hash, resp } )
954
1002
. await ?;
955
1003
resp_rx. await ?
956
1004
}
1005
+
1006
+ pub async fn get_tx_meta ( & self , txid : Txid ) -> anyhow:: Result < Option < TxChangeSet > > {
1007
+ let ( resp, resp_rx) = oneshot:: channel ( ) ;
1008
+ self . sender
1009
+ . send ( ChainStateCommand :: GetTxMeta { txid, resp } )
1010
+ . await ?;
1011
+ resp_rx. await ?
1012
+ }
957
1013
}
958
1014
959
1015
fn space_hash_from_string ( space_hash : & str ) -> Result < SpaceKey , ErrorObjectOwned > {
0 commit comments