Skip to content

Commit de99340

Browse files
Move retries to a constant variable
1 parent 160ffd1 commit de99340

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

wasm/src/programs/snapshot_query.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use snarkvm_ledger_query::QueryTrait;
3838
use std::str::FromStr;
3939
use wasm_bindgen::JsValue;
4040

41+
const MAX_QUERY_ATTEMPTS: usize = 3;
42+
4143
/// A snapshot-based query object used to pin the block height, state root, and state paths to a single ledger view during online execution.
4244
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
4345
pub struct SnapshotQuery {
@@ -146,7 +148,6 @@ impl SnapshotQuery {
146148
node_url: &str,
147149
commitments: &[FieldNative],
148150
) -> Result<(u32, Vec<(FieldNative, StatePathNative)>)> {
149-
let max_attempts = 3;
150151
let mut attempts = 0;
151152

152153
let (latest_height, statepaths) = loop {
@@ -158,9 +159,9 @@ impl SnapshotQuery {
158159
Err(e) => {
159160
attempts += 1;
160161
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}..."
162163
));
163-
if attempts >= max_attempts {
164+
if attempts > MAX_QUERY_ATTEMPTS {
164165
bail!("Failed to fetch latest block height and state root: {e}");
165166
}
166167
}
@@ -178,14 +179,16 @@ impl SnapshotQuery {
178179
/// Attempt to fetch the latest block height and stateroot concurrently.
179180
pub async fn snapshot_stateroot(node_url: &str) -> Result<(u32, <CurrentNetwork as Network>::StateRoot)> {
180181
let mut attempts = 0;
181-
let max_attempts = 5;
182182

183183
loop {
184184
match futures::try_join!(latest_block_height(node_url), latest_stateroot(node_url),) {
185185
Ok((height, state_root)) => return Ok((height, state_root)),
186186
Err(e) => {
187187
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 {
189192
bail!("Failed to fetch latest block height and state root: {}", e);
190193
}
191194
}

0 commit comments

Comments
 (0)