Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions src/backend_task/contested_names/query_dpns_contested_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use dash_sdk::platform::FetchMany;
use dash_sdk::query_types::ContestedResource;
use dash_sdk::Sdk;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::{mpsc, OwnedSemaphorePermit, Semaphore};

impl AppContext {
Expand All @@ -24,22 +25,27 @@ impl AppContext {
let Some(contested_index) = document_type.find_contested_index() else {
return Err("No contested index on dpns domains".to_string());
};
const MAX_RETRIES: usize = 3;
let mut start_at_value = None;
loop {
let query = VotePollsByDocumentTypeQuery {
contract_id: data_contract.id(),
document_type_name: document_type.name().to_string(),
index_name: contested_index.name.clone(),
start_at_value,
start_at_value: start_at_value.clone(),
start_index_values: vec!["dash".into()], // hardcoded for dpns
end_index_values: vec![],
limit: Some(100),
order_ascending: true,
};

let (contested_resources) = ContestedResource::fetch_many(&sdk, query.clone())
.await
.map_err(|e| {
// Initialize retry counter
let mut retries = 0;

let contested_resources = match ContestedResource::fetch_many(&sdk, query.clone()).await
{
Ok(contested_resources) => contested_resources,
Err(e) => {
tracing::error!("Error fetching contested resources: {}", e);
if let dash_sdk::Error::Proof(
dash_sdk::ProofVerifierError::GroveDBProofVerificationError {
Expand All @@ -55,22 +61,22 @@ impl AppContext {
let encoded_query =
match bincode::encode_to_vec(&query, bincode::config::standard())
.map_err(|encode_err| {
tracing::error!("error encoding query: {}", encode_err);
format!("error encoding query: {}", encode_err)
tracing::error!("Error encoding query: {}", encode_err);
format!("Error encoding query: {}", encode_err)
}) {
Ok(encoded_query) => encoded_query,
Err(e) => return e,
Err(e) => return Err(e),
};

// Encode the path_query using bincode
let verification_path_query_bytes =
match bincode::encode_to_vec(&path_query, bincode::config::standard())
.map_err(|encode_err| {
tracing::error!("error encoding path_query: {}", encode_err);
format!("error encoding path_query: {}", encode_err)
tracing::error!("Error encoding path_query: {}", encode_err);
format!("Error encoding path_query: {}", encode_err)
}) {
Ok(encoded_path_query) => encoded_path_query,
Err(e) => return e,
Err(e) => return Err(e),
};

if let Err(e) = self
Expand All @@ -86,12 +92,30 @@ impl AppContext {
})
.map_err(|e| e.to_string())
{
return e;
return Err(e);
}
}
format!("Error fetching contested resources: {}", e)
})?;

if e.to_string().contains("try another server")
|| e.to_string().contains(
"contract not found when querying from value with contract info",
)
{
retries += 1;
if retries > MAX_RETRIES {
tracing::error!("Max retries reached for query: {}", e);
return Err(format!(
"Error fetching contested resources after retries: {}",
e
));
} else {
// Retry
continue;
}
} else {
return Err(format!("Error fetching contested resources: {}", e));
}
}
};
let contested_resources_len = contested_resources.0.len();

if contested_resources_len == 0 {
Expand Down Expand Up @@ -146,7 +170,7 @@ impl AppContext {
.expect("expected to send refresh");
}
Err(e) => {
tracing::error!("error querying dpns end times: {}", e);
tracing::error!("Error querying dpns end times: {}", e);
sender
.send(TaskResult::Error(e))
.await
Expand Down Expand Up @@ -184,7 +208,7 @@ impl AppContext {
}
Err(e) => {
tracing::error!(
"error querying dpns vote contenders for {}: {}",
"Error querying dpns vote contenders for {}: {}",
name,
e
);
Expand Down
50 changes: 41 additions & 9 deletions src/backend_task/contested_names/query_dpns_vote_contenders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,47 @@ impl AppContext {
result_type: ContestedDocumentVotePollDriveQueryResultType::DocumentsAndVoteTally,
};

let contenders =
ContenderWithSerializedDocument::fetch_many(&sdk, contenders_query.clone())
.await
.map_err(|e| {
// Define retries
const MAX_RETRIES: usize = 3;
let mut retries = 0;

loop {
match ContenderWithSerializedDocument::fetch_many(&sdk, contenders_query.clone()).await
{
Ok(contenders) => {
// If successful, proceed to insert/update contenders
return self
.db
.insert_or_update_contenders(name, &contenders, document_type, self)
.map_err(|e| e.to_string());
}
Err(e) => {
tracing::error!("Error fetching contested resources: {}", e);
format!("Error fetching contested resources: {}", e)
})?;
self.db
.insert_or_update_contenders(name, &contenders, document_type, self)
.map_err(|e| e.to_string())
let error_str = e.to_string();
if error_str.contains("try another server")
|| error_str.contains(
"contract not found when querying from value with contract info",
)
{
retries += 1;
if retries > MAX_RETRIES {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix off-by-one error in retries condition

The condition if retries > MAX_RETRIES may allow for one additional retry beyond the intended MAX_RETRIES. Since retries is incremented before the check and starts at 0, the condition should be if retries >= MAX_RETRIES to ensure the method retries exactly MAX_RETRIES times.

Apply this change:

- if retries > MAX_RETRIES {
+ if retries >= MAX_RETRIES {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if retries > MAX_RETRIES {
if retries >= MAX_RETRIES {

tracing::error!(
"Max retries reached for query_dpns_vote_contenders: {}",
e
);
return Err(format!(
"Error fetching contested resources after retries: {}",
e
));
} else {
continue;
}
} else {
// For other errors, return immediately
return Err(format!("Error fetching contested resources: {}", e));
}
}
}
}
}
}