@@ -38,6 +38,8 @@ use snarkvm_ledger_query::QueryTrait;
38
38
use std:: str:: FromStr ;
39
39
use wasm_bindgen:: JsValue ;
40
40
41
+ const MAX_QUERY_ATTEMPTS : usize = 3 ;
42
+
41
43
/// A snapshot-based query object used to pin the block height, state root, and state paths to a single ledger view during online execution.
42
44
#[ derive( Clone , Debug , Deserialize , Eq , PartialEq , Serialize ) ]
43
45
pub struct SnapshotQuery {
@@ -146,7 +148,6 @@ impl SnapshotQuery {
146
148
node_url : & str ,
147
149
commitments : & [ FieldNative ] ,
148
150
) -> Result < ( u32 , Vec < ( FieldNative , StatePathNative ) > ) > {
149
- let max_attempts = 3 ;
150
151
let mut attempts = 0 ;
151
152
152
153
let ( latest_height, statepaths) = loop {
@@ -158,9 +159,9 @@ impl SnapshotQuery {
158
159
Err ( e) => {
159
160
attempts += 1 ;
160
161
log ( & format ! (
161
- "Failed to fetch latest block height and statepaths, attempt {attempts}/{max_attempts }..."
162
+ "Failed to fetch latest block height and statepaths, attempt {attempts}/{MAX_QUERY_ATTEMPTS }..."
162
163
) ) ;
163
- if attempts >= max_attempts {
164
+ if attempts > MAX_QUERY_ATTEMPTS {
164
165
bail ! ( "Failed to fetch latest block height and state root: {e}" ) ;
165
166
}
166
167
}
@@ -178,14 +179,16 @@ impl SnapshotQuery {
178
179
/// Attempt to fetch the latest block height and stateroot concurrently.
179
180
pub async fn snapshot_stateroot ( node_url : & str ) -> Result < ( u32 , <CurrentNetwork as Network >:: StateRoot ) > {
180
181
let mut attempts = 0 ;
181
- let max_attempts = 5 ;
182
182
183
183
loop {
184
184
match futures:: try_join!( latest_block_height( node_url) , latest_stateroot( node_url) , ) {
185
185
Ok ( ( height, state_root) ) => return Ok ( ( height, state_root) ) ,
186
186
Err ( e) => {
187
187
attempts += 1 ;
188
- if attempts >= max_attempts {
188
+ log ( & format ! (
189
+ "Failed to fetch latest block height and stateroot, attempt {attempts}/{MAX_QUERY_ATTEMPTS}..."
190
+ ) ) ;
191
+ if attempts > MAX_QUERY_ATTEMPTS {
189
192
bail ! ( "Failed to fetch latest block height and state root: {}" , e) ;
190
193
}
191
194
}
0 commit comments