Skip to content

Commit

Permalink
add submit-job and checks for on-chain PDA before submission
Browse files Browse the repository at this point in the history
  • Loading branch information
ngundotra committed Dec 2, 2024
1 parent 9789ce7 commit 3a0e292
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
50 changes: 47 additions & 3 deletions src/api/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::anyhow;
use crossbeam_channel::{unbounded, Receiver};
use indicatif::{HumanDuration, ProgressBar, ProgressStyle};
use reqwest::Client;
use reqwest::{Client, Response};
use serde_json::json;
use solana_client::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use std::thread;
use std::time::{Duration, Instant};
Expand All @@ -11,6 +12,7 @@ use crate::api::models::{
ErrorResponse, JobResponse, JobStatus, JobVerificationResponse, RemoteStatusResponseWrapper,
VerifyResponse,
};
use crate::solana_program::get_program_pda;

// URL for the remote server
pub const REMOTE_SERVER_URL: &str = "https://verify.osec.io";
Expand Down Expand Up @@ -109,10 +111,52 @@ pub async fn send_job_to_remote(
.send()
.await?;

handle_submission_response(&client, response, program_id).await
}

pub async fn send_job_with_uploader_to_remote(
connection: &RpcClient,
program_id: &Pubkey,
uploader: &Pubkey,
) -> anyhow::Result<()> {
// Check that PDA exists before sending job
get_program_pda(connection, program_id, Some(uploader.to_string())).await?;

let client = Client::builder()
.timeout(Duration::from_secs(18000))
.build()?;

// Send the POST request
let response = client
.post(format!("{}/verify-with-signer", REMOTE_SERVER_URL))
.json(&json!({
"program_id": program_id.to_string(),
"signer": uploader.to_string(),
"repository": "",
"commit_hash": "",
}))
.send()
.await?;

handle_submission_response(&client, response, program_id).await
}

pub async fn handle_submission_response(
client: &Client,
response: Response,
program_id: &Pubkey,
) -> anyhow::Result<()> {
if response.status().is_success() {
let status_response: VerifyResponse = response.json().await?;
// First get the raw text to preserve it in case of parsing failure
let response_text = response.text().await?;
let status_response =
serde_json::from_str::<VerifyResponse>(&response_text).map_err(|e| {
eprintln!("Failed to parse response as VerifyResponse: {}", e);
eprintln!("Raw response: {}", response_text);
anyhow!("Failed to parse server response")
})?;
let request_id = status_response.request_id;
println!("Verification request sent. ✅");
println!("Verification request sent with request id: {}", request_id);
println!("Verification in progress... ⏳");
// Span new thread for polling the server for status
// Create a channel for communication between threads
Expand Down
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ mod solana;
pub use client::get_remote_job;
pub use client::get_remote_status;
pub use client::send_job_to_remote;
pub use client::send_job_with_uploader_to_remote;
pub use solana::get_last_deployed_slot;
24 changes: 23 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::anyhow;
use api::{get_remote_job, get_remote_status};
use api::{get_remote_job, get_remote_status, send_job_with_uploader_to_remote};
use cargo_lock::Lockfile;
use cargo_toml::Manifest;
use clap::{App, AppSettings, Arg, SubCommand};
Expand Down Expand Up @@ -233,6 +233,17 @@ async fn main() -> anyhow::Result<()> {
.long("job-id")
.required(true)
.takes_value(true)))
.subcommand(SubCommand::with_name("submit-job")
.about("Submit a verification job with with on-chain information")
.arg(Arg::with_name("program-id")
.long("program-id")
.required(true)
.takes_value(true))
.arg(Arg::with_name("uploader")
.long("uploader")
.required(true)
.takes_value(true)
.help("This is the address that uploaded verified build information for the program-id")))
)
.get_matches();

Expand Down Expand Up @@ -366,6 +377,17 @@ async fn main() -> anyhow::Result<()> {
let job_id = sub_m.value_of("job-id").unwrap();
get_remote_job(job_id).await
}
("submit-job", Some(sub_m)) => {
let program_id = sub_m.value_of("program-id").unwrap();
let uploader = sub_m.value_of("uploader").unwrap();

send_job_with_uploader_to_remote(
&connection,
&Pubkey::try_from(program_id)?,
&Pubkey::try_from(uploader)?,
)
.await
}
_ => unreachable!(),
},
// Handle other subcommands in a similar manner, for now let's panic
Expand Down
6 changes: 5 additions & 1 deletion src/solana_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ pub async fn get_program_pda(
.map_err(|err| anyhow!("Unable to parse build params: {}", err))?,
))
} else {
Err(anyhow!("PDA not found"))
Err(anyhow!(
"PDA not found for {:?} and uploader {:?}",
program_id,
signer_pubkey
))
}
}

Expand Down

0 comments on commit 3a0e292

Please sign in to comment.