@@ -12,11 +12,12 @@ use dashcore_rpc::{
1212} ;
1313use dpp:: dashcore:: ProTxHash ;
1414use dpp:: prelude:: CoreBlockHeight ;
15- use drive_proof_verifier:: error:: ContextProviderError ;
1615use std:: time:: Duration ;
1716use std:: { fmt:: Debug , sync:: Mutex } ;
1817use zeroize:: Zeroizing ;
1918
19+ use super :: DashCoreError ;
20+
2021/// Core RPC client that can be used to retrieve quorum keys from core.
2122///
2223/// TODO: This is a temporary implementation, effective until we integrate SPV.
@@ -28,13 +29,6 @@ pub struct LowLevelDashCoreClient {
2829 core_port : u16 ,
2930}
3031
31- /// Client still warming up
32- pub const CORE_RPC_ERROR_IN_WARMUP : i32 = -28 ;
33- /// Dash is not connected
34- pub const CORE_RPC_CLIENT_NOT_CONNECTED : i32 = -9 ;
35- /// Still downloading initial blocks
36- pub const CORE_RPC_CLIENT_IN_INITIAL_DOWNLOAD : i32 = -10 ;
37-
3832macro_rules! retry {
3933 ( $action: expr) => { {
4034 /// Maximum number of retry attempts
@@ -60,30 +54,18 @@ macro_rules! retry {
6054 break ;
6155 }
6256 Err ( e) => {
63- match e {
64- dashcore_rpc:: Error :: JsonRpc (
65- // Retry on transport connection error
66- dashcore_rpc:: jsonrpc:: error:: Error :: Transport ( _)
67- | dashcore_rpc:: jsonrpc:: error:: Error :: Rpc (
68- // Retry on Core RPC "not ready" errors
69- dashcore_rpc:: jsonrpc:: error:: RpcError {
70- code:
71- CORE_RPC_ERROR_IN_WARMUP
72- | CORE_RPC_CLIENT_NOT_CONNECTED
73- | CORE_RPC_CLIENT_IN_INITIAL_DOWNLOAD ,
74- ..
75- } ,
76- ) ,
77- ) => {
78- if i == MAX_RETRIES - 1 {
79- final_result =
80- Some ( Err ( ContextProviderError :: Generic ( e. to_string( ) ) ) ) ;
81- }
82- let delay = fibonacci( i + 2 ) * FIB_MULTIPLIER ;
83- std:: thread:: sleep( Duration :: from_millis( delay * BASE_TIME_MS ) ) ;
57+ use rs_dapi_client:: CanRetry ;
58+
59+ let err: DashCoreError = e. into( ) ;
60+ if err. can_retry( ) {
61+ if i == MAX_RETRIES - 1 {
62+ final_result = Some ( Err ( err) ) ;
8463 }
85- _ => return Err ( ContextProviderError :: Generic ( e. to_string( ) ) ) ,
86- } ;
64+ let delay = fibonacci( i + 2 ) * FIB_MULTIPLIER ;
65+ std:: thread:: sleep( Duration :: from_millis( delay * BASE_TIME_MS ) ) ;
66+ } else {
67+ return Err ( err) ;
68+ }
8769 }
8870 }
8971 }
@@ -133,8 +115,7 @@ impl LowLevelDashCoreClient {
133115 let core = Client :: new (
134116 & addr,
135117 Auth :: UserPass ( core_user. to_string ( ) , core_password. to_string ( ) ) ,
136- )
137- . map_err ( Error :: CoreClientError ) ?;
118+ ) ?;
138119
139120 Ok ( Self {
140121 core : Mutex :: new ( core) ,
@@ -162,7 +143,7 @@ impl LowLevelDashCoreClient {
162143 pub fn list_unspent (
163144 & self ,
164145 minimum_sum_satoshi : Option < u64 > ,
165- ) -> Result < Vec < dashcore_rpc:: json:: ListUnspentResultEntry > , ContextProviderError > {
146+ ) -> Result < Vec < dashcore_rpc:: json:: ListUnspentResultEntry > , DashCoreError > {
166147 let options = json:: ListUnspentQueryOptions {
167148 minimum_sum_amount : minimum_sum_satoshi. map ( Amount :: from_sat) ,
168149 ..Default :: default ( )
@@ -176,7 +157,7 @@ impl LowLevelDashCoreClient {
176157 /// Return address to which change of transaction can be sent.
177158 #[ allow( dead_code) ]
178159 #[ deprecated( note = "This function is marked as unused." ) ]
179- pub fn get_balance ( & self ) -> Result < Amount , ContextProviderError > {
160+ pub fn get_balance ( & self ) -> Result < Amount , DashCoreError > {
180161 let core = self . core . lock ( ) . expect ( "Core lock poisoned" ) ;
181162 retry ! ( core. get_balance( None , None ) )
182163 }
@@ -186,9 +167,9 @@ impl LowLevelDashCoreClient {
186167 & self ,
187168 quorum_type : u32 ,
188169 quorum_hash : [ u8 ; 32 ] ,
189- ) -> Result < [ u8 ; 48 ] , ContextProviderError > {
170+ ) -> Result < [ u8 ; 48 ] , DashCoreError > {
190171 let quorum_hash = QuorumHash :: from_slice ( & quorum_hash)
191- . map_err ( |e| ContextProviderError :: InvalidQuorum ( e . to_string ( ) ) ) ?;
172+ . map_err ( |e| DashCoreError :: InvalidQuorum ( format ! ( "invalid quorum hash: {}" , e ) ) ) ?;
192173
193174 let core = self . core . lock ( ) . expect ( "Core lock poisoned" ) ;
194175
@@ -199,29 +180,29 @@ impl LowLevelDashCoreClient {
199180 // Extract the quorum public key and attempt to convert it
200181 let key = quorum_info. quorum_public_key ;
201182 let pubkey = <Vec < u8 > as TryInto < [ u8 ; 48 ] > >:: try_into ( key) . map_err ( |_| {
202- ContextProviderError :: InvalidQuorum (
203- "quorum public key is not 48 bytes long" . to_string ( ) ,
204- )
183+ DashCoreError :: InvalidQuorum ( "quorum public key is not 48 bytes long" . to_string ( ) )
205184 } ) ?;
206185
207186 Ok ( pubkey)
208187 }
209188
210189 /// Retrieve platform activation height from core.
211- pub fn get_platform_activation_height ( & self ) -> Result < CoreBlockHeight , ContextProviderError > {
190+ pub fn get_platform_activation_height ( & self ) -> Result < CoreBlockHeight , DashCoreError > {
212191 let core = self . core . lock ( ) . expect ( "Core lock poisoned" ) ;
213192
214193 let blockchain_info = retry ! ( core. get_blockchain_info( ) ) ?;
215194
216- let fork_info = blockchain_info. softforks . get ( "mn_rr" ) . ok_or (
217- ContextProviderError :: ActivationForkError ( "no fork info for mn_rr" . to_string ( ) ) ,
218- ) ?;
219-
220- fork_info
221- . height
222- . ok_or ( ContextProviderError :: ActivationForkError (
223- "unknown fork height" . to_string ( ) ,
224- ) )
195+ let fork_info =
196+ blockchain_info
197+ . softforks
198+ . get ( "mn_rr" )
199+ . ok_or ( DashCoreError :: ActivationForkError (
200+ "no fork info for mn_rr" . to_string ( ) ,
201+ ) ) ?;
202+
203+ fork_info. height . ok_or ( DashCoreError :: ActivationForkError (
204+ "unknown fork height" . to_string ( ) ,
205+ ) )
225206 }
226207
227208 /// Require list of validators from Core.
@@ -232,7 +213,7 @@ impl LowLevelDashCoreClient {
232213 & self ,
233214 height : Option < u32 > ,
234215 protx_type : Option < ProTxListType > ,
235- ) -> Result < Vec < ProTxHash > , ContextProviderError > {
216+ ) -> Result < Vec < ProTxHash > , DashCoreError > {
236217 let core = self . core . lock ( ) . expect ( "Core lock poisoned" ) ;
237218
238219 let pro_tx_list = retry ! ( core. get_protx_list( protx_type. clone( ) , Some ( false ) , height) ) ?;
0 commit comments