Skip to content

Commit

Permalink
Tests : proving client tests (#85)
Browse files Browse the repository at this point in the history
* feat : added test boilerplate

* feat : updated sharp prover client and added certificate feature

* feat : updated sharp prover client and added certificate feature

* feat : added certificates for testing

* feat : added tests for proving client and fixed fact calculation logic

* fix : linter fixes

* feat : revamp code

* feat : test fix

* refactor : resolved comments #1

* refactor : removed redundant code

* fix : lint

* refactor : updated code as per comments (0xevolve)

* markdown lint fix

* feat : resolved comment
  • Loading branch information
ocdbytes authored Aug 22, 2024
1 parent fa59cdf commit 7926588
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 49 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ SQS_JOB_VERIFICATION_QUEUE_URL=
# S3
AWS_S3_BUCKET_NAME=
AWS_S3_BUCKET_REGION=

# Sharp Services
SHARP_CUSTOMER_ID=
SHARP_USER_CRT=
SHARP_USER_KEY=
SHARP_SERVER_CRT=
SHARP_PROOF_LAYOUT=
9 changes: 9 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ PROVER_SERVICE="sharp"
SETTLEMENT_LAYER="ethereum"
DATA_STORAGE="s3"
MONGODB_CONNECTION_STRING="mongodb://localhost:27017"

# Sharp Services
SHARP_CUSTOMER_ID="sharp_consumer_id"
SHARP_URL="http://127.0.0.1:5000"
# [IMP!!!] These are test certificates (they don't work)
SHARP_USER_CRT="LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR4ekNDQXErZ0F3SUJBZ0lVTjBSK0xpb1MzL2ZadUZsK291RjZNNFk2RnRZd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2N6RUxNQWtHQTFVRUJoTUNTVTR4RXpBUkJnTlZCQWdNQ2xOdmJXVXRVM1JoZEdVeElUQWZCZ05WQkFvTQpHRWx1ZEdWeWJtVjBJRmRwWkdkcGRITWdVSFI1SUV4MFpERU5NQXNHQTFVRUF3d0V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hcWhnQ20KTjFUV2FxL0xJMVBLSkxPK085NFlMa2FsNVpyOTJCOXk4Q0VKVUVuSTA1R1N1MmJUOFM2a0ZBMEpadEszTW9SbwpqRWZWV1lQVHR5TFR4amNvRndCcDlHaXZYSDdSdHBxMDlmSmFhU1pNekxmNGlyNHpBdXprbExBNWZvampPNXlKCllnYlVaQUU2aS81N1NFWjR3VmxTCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K"
SHARP_PROOF_LAYOUT="small"
4 changes: 4 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
- name: Install nextest
uses: taiki-e/install-action@nextest

- name: Getting neccesary files for testing
run: |
wget -P ./crates/prover-services/sharp-service/tests/artifacts https://madara-orchestrator-sharp-pie.s3.amazonaws.com/238996-SN.zip
- name: Clean workspace
run: |
cargo clean --doc
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
*.code-workspace
.vscode

lcov.info
lcov.info

**/*-SN.zip
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## Added

- Tests for prover client.
- Added Rust Cache for Coverage Test CI.
- support for fetching PIE file from storage client in proving job.
- added coveralls support
Expand Down Expand Up @@ -39,4 +40,5 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## Fixed

- Get Fact Info logic.
- Fixed state update worker logic as per the new implementation.
5 changes: 5 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dotenvy = "0.15.7"
futures = "0.3.30"
mongodb = { version = "2.8.1" }
omniqueue = { version = "0.2.0" }
reqwest = { version = "0.11.24" }
reqwest = { version = "0.11.24", features = ["rustls-tls", "native-tls"] }
rstest = "0.18.2"
serde = { version = "1.0.197" }
serde_json = "1.0.114"
Expand All @@ -48,7 +48,7 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18" }
url = { version = "2.5.0", features = ["serde"] }
uuid = { version = "1.7.0", features = ["v4", "serde"] }
httpmock = { version = "0.7.0" }
httpmock = { version = "0.7.0", features = ["remote"] }
num-bigint = { version = "0.4.4" }
arc-swap = { version = "1.7.1" }
num-traits = "0.2"
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ The tentative flow of the orchestrator looks like this but this is subject to
change as we learn more about external systems and the constraints involved.

![orchestrator_da_sequencer_diagram](./docs/orchestrator_da_sequencer_diagram.png)

## Testing

- Files needed for tests can be fetched through s3 :

```shell
wget -P ./crates/prover-services/sharp-service/tests/artifacts https://madara-orchestrator-sharp-pie.s3.amazonaws.com/238996-SN.zip
```

- To run all the tests :

```shell
cargo llvm-cov nextest --release --lcov --output-path lcov.info --test-threads=1
```
2 changes: 2 additions & 0 deletions crates/prover-services/gps-fact-checker/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ pub enum FactCheckerError {
TreeStructureEndOffsetInvalid(usize, usize),
#[error("Tree structure: root offset {0} does not match the output length {1}")]
TreeStructureRootOffsetInvalid(usize, usize),
#[error("Program output doesn't match the segment size.")]
InvalidSegment,
}
42 changes: 17 additions & 25 deletions crates/prover-services/gps-fact-checker/src/fact_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::runners::cairo_pie::CairoPie;
use cairo_vm::Felt252;
use starknet::core::types::FieldElement;
use utils::ensure;

use super::error::FactCheckerError;
use super::fact_node::generate_merkle_root;
Expand Down Expand Up @@ -45,33 +44,26 @@ pub fn get_program_output(cairo_pie: &CairoPie) -> Result<Vec<Felt252>, FactChec
.get(&BuiltinName::output)
.ok_or(FactCheckerError::OutputBuiltinNoSegmentInfo)?;

let segment_start = cairo_pie
.memory
.0
.iter()
.enumerate()
.find_map(|(ptr, ((index, _), _))| if *index == segment_info.index as usize { Some(ptr) } else { None })
.ok_or(FactCheckerError::OutputSegmentNotFound)?;

let mut output = Vec::with_capacity(segment_info.size);
let mut expected_offset = 0;

#[allow(clippy::explicit_counter_loop)]
for i in segment_start..segment_start + segment_info.size {
let ((_, offset), value) = cairo_pie.memory.0.get(i).ok_or(FactCheckerError::OutputSegmentInvalidRange)?;

ensure!(
*offset == expected_offset,
FactCheckerError::OutputSegmentInconsistentOffset(*offset, expected_offset)
);
match value {
MaybeRelocatable::Int(felt) => output.push(*felt),
MaybeRelocatable::RelocatableValue(_) => {
return Err(FactCheckerError::OutputSegmentUnexpectedRelocatable(*offset));
let mut output = vec![Felt252::from(0); segment_info.size];
let mut insertion_count = 0;
let cairo_program_memory = &cairo_pie.memory.0;

for ((index, offset), value) in cairo_program_memory.iter() {
if *index == segment_info.index as usize {
match value {
MaybeRelocatable::Int(felt) => {
output[*offset] = *felt;
insertion_count += 1;
}
MaybeRelocatable::RelocatableValue(_) => {
return Err(FactCheckerError::OutputSegmentUnexpectedRelocatable(*offset));
}
}
}
}

expected_offset += 1;
if insertion_count != segment_info.size {
return Err(FactCheckerError::InvalidSegment);
}

Ok(output)
Expand Down
7 changes: 7 additions & 0 deletions crates/prover-services/sharp-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ edition.workspace = true
[dependencies]
alloy.workspace = true
async-trait.workspace = true
base64 = "0.22.1"
cairo-vm.workspace = true
dotenvy.workspace = true
gps-fact-checker.workspace = true
hex.workspace = true
httpmock.workspace = true
lazy_static.workspace = true
prover-client-interface.workspace = true
reqwest.workspace = true
rstest.workspace = true
serde.workspace = true
serde_json.workspace = true
snos.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
url.workspace = true
utils.workspace = true
uuid.workspace = true

[dev-dependencies]
tokio.workspace = true
httpmock.workspace = true
97 changes: 84 additions & 13 deletions crates/prover-services/sharp-service/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use serde_json::json;
use snos::sharp::{CairoJobResponse, CairoStatusResponse};
use base64::engine::general_purpose;
use base64::Engine;
use reqwest::{Certificate, ClientBuilder, Identity};
use std::clone::Clone;
use url::Url;
use utils::env_utils::get_env_var_or_panic;
use uuid::Uuid;

use crate::error::SharpError;
use crate::types::{SharpAddJobResponse, SharpGetStatusResponse};

/// SHARP endpoint for Sepolia testnet
pub const DEFAULT_SHARP_URL: &str = "https://testnet.provingservice.io";
pub const DEFAULT_SHARP_URL: &str = "https://sepolia-recursive.public-testnet.provingservice.io/v1/gateway";

/// SHARP API async wrapper
pub struct SharpClient {
Expand All @@ -15,25 +19,85 @@ pub struct SharpClient {
}

impl SharpClient {
/// We need to set up the client with the provided certificates.
/// We need to have three secrets :
/// - base64(SHARP_USER_CRT)
/// - base64(SHARP_USER_KEY)
/// - base64(SHARP_SERVER_CRT)
///
/// You can run this command in terminal to convert a file output into base64
/// and then copy it and paste it into .env file :
///
/// `cat <file_name> | base64`
pub fn new(url: Url) -> Self {
Self { base_url: url, client: reqwest::Client::new() }
// Getting the cert files from the .env and then decoding it from base64
let cert = general_purpose::STANDARD.decode(get_env_var_or_panic("SHARP_USER_CRT")).unwrap();
let key = general_purpose::STANDARD.decode(get_env_var_or_panic("SHARP_USER_KEY")).unwrap();
let server_cert = general_purpose::STANDARD.decode(get_env_var_or_panic("SHARP_SERVER_CRT")).unwrap();

// Adding Customer ID to the url
let mut url_mut = url.clone();
let customer_id = get_env_var_or_panic("SHARP_CUSTOMER_ID");
url_mut.query_pairs_mut().append_pair("customer_id", customer_id.as_str());

Self {
base_url: url_mut,
client: ClientBuilder::new()
.identity(Identity::from_pkcs8_pem(&cert, &key).unwrap())
.add_root_certificate(Certificate::from_pem(server_cert.as_slice()).unwrap())
.build()
.unwrap(),
}
}

pub async fn add_job(&self, encoded_pie: &str) -> Result<CairoJobResponse, SharpError> {
let data = json!({ "action": "add_job", "request": { "cairo_pie": encoded_pie } });
let url = self.base_url.join("add_job").unwrap();
let res = self.client.post(url).json(&data).send().await.map_err(SharpError::AddJobFailure)?;
pub async fn add_job(&self, encoded_pie: &str) -> Result<(SharpAddJobResponse, Uuid), SharpError> {
let mut base_url = self.base_url.clone();

base_url.path_segments_mut().map_err(|_| SharpError::PathSegmentMutFailOnUrl)?.push("add_job");

let cairo_key = Uuid::new_v4();
let cairo_key_string = cairo_key.to_string();
let proof_layout = get_env_var_or_panic("SHARP_PROOF_LAYOUT");

// Params for sending the PIE file to the prover
// for temporary reference you can check this doc :
// https://docs.google.com/document/d/1-9ggQoYmjqAtLBGNNR2Z5eLreBmlckGYjbVl0khtpU0
let params = vec![
("cairo_job_key", cairo_key_string.as_str()),
("offchain_proof", "true"),
("proof_layout", proof_layout.as_str()),
];

// Adding params to the URL
add_params_to_url(&mut base_url, params);

let res =
self.client.post(base_url).body(encoded_pie.to_string()).send().await.map_err(SharpError::AddJobFailure)?;

match res.status() {
reqwest::StatusCode::OK => res.json().await.map_err(SharpError::AddJobFailure),
reqwest::StatusCode::OK => {
let result: SharpAddJobResponse = res.json().await.map_err(SharpError::AddJobFailure)?;
Ok((result, cairo_key))
}
code => Err(SharpError::SharpService(code)),
}
}

pub async fn get_job_status(&self, job_key: &Uuid) -> Result<CairoStatusResponse, SharpError> {
let data = json!({ "action": "get_status", "request": { "cairo_job_key": job_key } });
let url = self.base_url.join("get_status").unwrap();
let res = self.client.post(url).json(&data).send().await.map_err(SharpError::GetJobStatusFailure)?;
pub async fn get_job_status(&self, job_key: &Uuid) -> Result<SharpGetStatusResponse, SharpError> {
let mut base_url = self.base_url.clone();

base_url.path_segments_mut().map_err(|_| SharpError::PathSegmentMutFailOnUrl)?.push("get_status");
let cairo_key_string = job_key.to_string();

// Params for getting the prover job status
// for temporary reference you can check this doc :
// https://docs.google.com/document/d/1-9ggQoYmjqAtLBGNNR2Z5eLreBmlckGYjbVl0khtpU0
let params = vec![("cairo_job_key", cairo_key_string.as_str())];

// Adding params to the url
add_params_to_url(&mut base_url, params);

let res = self.client.post(base_url).send().await.map_err(SharpError::GetJobStatusFailure)?;

match res.status() {
reqwest::StatusCode::OK => res.json().await.map_err(SharpError::GetJobStatusFailure),
Expand All @@ -42,6 +106,13 @@ impl SharpClient {
}
}

fn add_params_to_url(url: &mut Url, params: Vec<(&str, &str)>) {
let mut pairs = url.query_pairs_mut();
for (key, value) in params {
pairs.append_pair(key, value);
}
}

impl Default for SharpClient {
fn default() -> Self {
Self::new(DEFAULT_SHARP_URL.parse().unwrap())
Expand Down
2 changes: 1 addition & 1 deletion crates/prover-services/sharp-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Default for SharpConfig {
fn default() -> Self {
Self {
service_url: DEFAULT_SHARP_URL.parse().unwrap(),
rpc_node_url: "https://sepolia.drpc.org".parse().unwrap(),
rpc_node_url: "https://ethereum-sepolia-rpc.publicnode.com".parse().unwrap(),
verifier_address: "0x07ec0D28e50322Eb0C159B9090ecF3aeA8346DFe".parse().unwrap(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/prover-services/sharp-service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum SharpError {
TaskIdSplit,
#[error("Failed to encode PIE")]
PieEncode(#[source] snos::error::SnOsError),
#[error("Failed to get url as path segment mut. URL is cannot-be-a-base.")]
PathSegmentMutFailOnUrl,
}

impl From<SharpError> for ProverClientError {
Expand Down
Loading

0 comments on commit 7926588

Please sign in to comment.