@@ -100,7 +100,7 @@ pub trait Rpc {
100
100
101
101
#[ method( name = "getspaceowner" ) ]
102
102
async fn get_space_owner ( & self , space_hash : & str )
103
- -> Result < Option < OutPoint > , ErrorObjectOwned > ;
103
+ -> Result < Option < OutPoint > , ErrorObjectOwned > ;
104
104
105
105
#[ method( name = "getspaceout" ) ]
106
106
async fn get_spaceout ( & self , outpoint : OutPoint ) -> Result < Option < SpaceOut > , ErrorObjectOwned > ;
@@ -111,8 +111,8 @@ pub trait Rpc {
111
111
#[ method( name = "getrollout" ) ]
112
112
async fn get_rollout ( & self , target : usize ) -> Result < Vec < ( u32 , SpaceKey ) > , ErrorObjectOwned > ;
113
113
114
- #[ method( name = "getblockmeta " ) ]
115
- async fn get_block_meta (
114
+ #[ method( name = "getblock " ) ]
115
+ async fn get_block (
116
116
& self ,
117
117
block_hash : BlockHash ,
118
118
) -> Result < Option < BlockMeta > , ErrorObjectOwned > ;
@@ -156,11 +156,11 @@ pub trait Rpc {
156
156
157
157
#[ method( name = "walletlistspaces" ) ]
158
158
async fn wallet_list_spaces ( & self , wallet : & str )
159
- -> Result < Vec < FullSpaceOut > , ErrorObjectOwned > ;
159
+ -> Result < Vec < FullSpaceOut > , ErrorObjectOwned > ;
160
160
161
161
#[ method( name = "walletlistunspent" ) ]
162
162
async fn wallet_list_unspent ( & self , wallet : & str )
163
- -> Result < Vec < LocalOutput > , ErrorObjectOwned > ;
163
+ -> Result < Vec < LocalOutput > , ErrorObjectOwned > ;
164
164
165
165
#[ method( name = "walletlistauctionoutputs" ) ]
166
166
async fn wallet_list_auction_outputs (
@@ -654,13 +654,13 @@ impl RpcServer for RpcServerImpl {
654
654
Ok ( rollouts)
655
655
}
656
656
657
- async fn get_block_meta (
657
+ async fn get_block (
658
658
& self ,
659
659
block_hash : BlockHash ,
660
660
) -> Result < Option < BlockMeta > , ErrorObjectOwned > {
661
661
let data = self
662
662
. store
663
- . get_block_meta ( block_hash)
663
+ . get_block ( block_hash)
664
664
. await
665
665
. map_err ( |error| ErrorObjectOwned :: owned ( -1 , error. to_string ( ) , None :: < String > ) ) ?;
666
666
@@ -796,7 +796,44 @@ impl AsyncChainState {
796
796
Self { sender }
797
797
}
798
798
799
+ async fn get_indexed_block (
800
+ index : & mut Option < LiveSnapshot > ,
801
+ block_hash : & BlockHash ,
802
+ client : & reqwest:: Client ,
803
+ rpc : & BitcoinRpc ,
804
+ chain_state : & mut LiveSnapshot ,
805
+ ) -> Result < Option < BlockMeta > , anyhow:: Error > {
806
+ let index = index. as_mut ( )
807
+ . ok_or_else ( || anyhow ! ( "block index must be enabled" ) ) ?;
808
+ let hash = BaseHash :: from_slice ( block_hash. as_ref ( ) ) ;
809
+ let block: Option < BlockMeta > = index. get ( hash)
810
+ . context ( "Could not fetch block from index" ) ?;
811
+
812
+ if let Some ( block_set) = block {
813
+ return Ok ( Some ( block_set) ) ;
814
+ }
815
+
816
+ let info: serde_json:: Value = rpc. send_json ( client, & rpc.
817
+ get_block_header ( block_hash) ) . await
818
+ . map_err ( |e| anyhow ! ( "Could not retrieve block ({})" , e) ) ?;
819
+
820
+ let height = info. get ( "height" ) . and_then ( |t| t. as_u64 ( ) )
821
+ . ok_or_else ( || anyhow ! ( "Could not retrieve block height" ) ) ?;
822
+
823
+ let tip = chain_state. tip . read ( ) . expect ( "read meta" ) . clone ( ) ;
824
+ if height > tip. height as u64 {
825
+ return Err ( anyhow ! (
826
+ "Spaces is syncing at height {}, requested block height {}" ,
827
+ tip. height,
828
+ height
829
+ ) ) ;
830
+ }
831
+ Err ( anyhow ! ( "Could not retrieve block" ) )
832
+ }
833
+
799
834
pub async fn handle_command (
835
+ client : & reqwest:: Client ,
836
+ rpc : & BitcoinRpc ,
800
837
chain_state : & mut LiveSnapshot ,
801
838
block_index : & mut Option < LiveSnapshot > ,
802
839
cmd : ChainStateCommand ,
@@ -822,19 +859,12 @@ impl AsyncChainState {
822
859
. context ( "could not fetch spaceout" ) ;
823
860
let _ = resp. send ( result) ;
824
861
}
825
- ChainStateCommand :: GetBlockMeta { block_hash, resp } => match block_index {
826
- None => {
827
- let _ = resp. send ( Err ( anyhow ! ( "block index must be enabled" ) ) ) ;
828
- }
829
- Some ( index) => {
830
- let hash = BaseHash :: from_slice ( block_hash. as_ref ( ) ) ;
831
- let _ = resp. send (
832
- index
833
- . get ( hash)
834
- . context ( "Could not fetch blockdata from index" ) ,
835
- ) ;
836
- }
837
- } ,
862
+ ChainStateCommand :: GetBlockMeta { block_hash, resp } => {
863
+ let res = Self :: get_indexed_block (
864
+ block_index, & block_hash, client, rpc, chain_state
865
+ ) . await ;
866
+ let _ = resp. send ( res) ;
867
+ }
838
868
ChainStateCommand :: EstimateBid { target, resp } => {
839
869
let estimate = chain_state. estimate_bid ( target) ;
840
870
_ = resp. send ( estimate) ;
@@ -847,6 +877,8 @@ impl AsyncChainState {
847
877
}
848
878
849
879
pub async fn handler (
880
+ client : & reqwest:: Client ,
881
+ rpc : BitcoinRpc ,
850
882
mut chain_state : LiveSnapshot ,
851
883
mut block_index : Option < LiveSnapshot > ,
852
884
mut rx : mpsc:: Receiver < ChainStateCommand > ,
@@ -858,7 +890,7 @@ impl AsyncChainState {
858
890
break ;
859
891
}
860
892
Some ( cmd) = rx. recv( ) => {
861
- Self :: handle_command( & mut chain_state, & mut block_index, cmd) . await ;
893
+ Self :: handle_command( client , & rpc , & mut chain_state, & mut block_index, cmd) . await ;
862
894
}
863
895
}
864
896
}
@@ -912,7 +944,7 @@ impl AsyncChainState {
912
944
resp_rx. await ?
913
945
}
914
946
915
- pub async fn get_block_meta ( & self , block_hash : BlockHash ) -> anyhow:: Result < Option < BlockMeta > > {
947
+ pub async fn get_block ( & self , block_hash : BlockHash ) -> anyhow:: Result < Option < BlockMeta > > {
916
948
let ( resp, resp_rx) = oneshot:: channel ( ) ;
917
949
self . sender
918
950
. send ( ChainStateCommand :: GetBlockMeta { block_hash, resp } )
0 commit comments