Skip to content

Commit

Permalink
Merge pull request #174 from cspr-rad/fix-submit-batch
Browse files Browse the repository at this point in the history
Fix submit batch json
  • Loading branch information
marijanp authored Jul 29, 2024
2 parents d53e3a4 + 884a254 commit 9519070
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions kairos-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ reqwest = { version = "0.12", features = ["json"] }
casper-event-toolkit = { git = "https://github.com/koxu1996/casper-event-toolkit.git", version = "0.1.3" }
thiserror = "1.0"
chrono = "0.4.38"
risc0-zkvm = { version="=1.0.3", default-features=false }
backoff = { version = "0.4", features = ["tokio", "futures"]}

[dev-dependencies]
proptest = "1"
Expand Down
13 changes: 8 additions & 5 deletions kairos-server/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod trie;
use std::collections::HashSet;
use std::{sync::Arc, thread};

use kairos_circuit_logic::ProofOutputs;
use risc0_zkvm::Receipt;
use tokio::{
sync::{mpsc, RwLock},
task,
Expand Down Expand Up @@ -94,17 +96,18 @@ impl BatchStateManager {

if res.status().is_success() {
tracing::info!("Proving server returned success");
let proof_serialized = res.bytes().await.unwrap_or_else(|e| {
tracing::error!("Could not read response from proving server: {}", e);
panic!("Could not read response from proving server: {}", e);
});
let (_proof_outputs, receipt): (ProofOutputs, Receipt) =
res.json().await.unwrap_or_else(|e| {
tracing::error!("Could not parse response from proving server: {}", e);
panic!("Could not parse response from proving server: {}", e);
});

if let Some(secret_key) = secret_key.as_ref() {
submit_proof_to_contract(
secret_key,
contract_hash,
casper_rpc.clone(),
proof_serialized.to_vec(),
serde_json::to_vec(&receipt).expect("Could not serialize receipt"),
)
.await
} else {
Expand Down
59 changes: 57 additions & 2 deletions kairos-server/src/state/submit_batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use casper_client::types::{DeployBuilder, ExecutableDeployItem, TimeDiff, Timestamp};
use casper_client_types::{bytesrepr::Bytes, runtime_args, ContractHash, RuntimeArgs, SecretKey};
use std::time::Instant;

use anyhow::anyhow;
use backoff::{future::retry, ExponentialBackoff};
use casper_client::{
types::{DeployBuilder, ExecutableDeployItem, TimeDiff, Timestamp},
Error, JsonRpcId,
};
use casper_client_types::{
bytesrepr::Bytes, runtime_args, ContractHash, ExecutionResult, RuntimeArgs, SecretKey,
};
use rand::random;
use reqwest::Url;

Expand Down Expand Up @@ -27,6 +36,8 @@ pub async fn submit_proof_to_contract(
.build()
.expect("could not build deploy");

let deploy_hash = *deploy.id();

let r = casper_client::put_deploy(
casper_client::JsonRpcId::Number(random()),
casper_rpc.as_str(),
Expand All @@ -36,5 +47,49 @@ pub async fn submit_proof_to_contract(
.await
.expect("could not put deploy");

let start = Instant::now();
let timed_out = start.elapsed().as_secs() > 60;

retry(ExponentialBackoff::default(), || async {
let response = casper_client::get_deploy(
JsonRpcId::Number(1),
casper_rpc.as_str(),
casper_client::Verbosity::Low,
deploy_hash,
false,
)
.await
.map_err(|err| {
let elapsed = start.elapsed().as_secs();
tracing::info!("Running for {elapsed}s, Error: {err:?}");
err
})
.map_err(|err| match &err {
e if timed_out => backoff::Error::permanent(anyhow!("Timeout on error: {e:?}")),
Error::ResponseIsHttpError { .. } | Error::FailedToGetResponse { .. } => {
backoff::Error::transient(anyhow!(err))
}
_ => backoff::Error::permanent(anyhow!(err)),
})
.expect("could not get deploy");

match response.result.execution_results.first() {
Some(result) => match &result.result {
ExecutionResult::Failure { error_message, .. } => {
Err(backoff::Error::permanent(anyhow!(error_message.clone())))
}
ExecutionResult::Success { .. } => Ok(()),
},
None if timed_out => Err(backoff::Error::permanent(anyhow!(
"Timeout on error: No execution results"
))),
None => Err(backoff::Error::transient(anyhow!(
"No execution results there yet"
))),
}
})
.await
.expect("could not get deploy");

tracing::info!("Deploy successful: {:?}", r);
}

0 comments on commit 9519070

Please sign in to comment.