Skip to content

Commit

Permalink
feat(sdk): send cpu cycles in network (#1521)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam authored Sep 18, 2024
1 parent 7457d8b commit 23c97e9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
18 changes: 18 additions & 0 deletions crates/sdk/src/network/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ sol! {
string description;
}

struct ModifyCpuCycles {
uint64 nonce;
string proof_id;
uint64 cycles;
}

struct FulfillProof {
uint64 nonce;
string proof_id;
Expand Down Expand Up @@ -116,6 +122,18 @@ impl NetworkAuth {
self.sign_message(type_struct).await
}

/// Signs a message to modify the CPU cycles for a proof. The proof must have been previously
/// claimed by the signer first.
pub async fn sign_modify_cpu_cycles_message(
&self,
nonce: u64,
proof_id: &str,
cycles: u64,
) -> Result<Vec<u8>> {
let type_struct = ModifyCpuCycles { nonce, proof_id: proof_id.to_string(), cycles };
self.sign_message(type_struct).await
}

/// Signs a message to fulfill a proof. The proof must have been previously claimed by the
/// signer first.
pub async fn sign_fulfill_proof_message(&self, nonce: u64, proof_id: &str) -> Result<Vec<u8>> {
Expand Down
26 changes: 25 additions & 1 deletion crates/sdk/src/network/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::{env, time::Duration};

use crate::{
network::auth::NetworkAuth,
proto::network::{UnclaimProofRequest, UnclaimReason},
proto::network::{
ModifyCpuCyclesRequest, ModifyCpuCyclesResponse, UnclaimProofRequest, UnclaimReason,
},
};
use anyhow::{Context, Ok, Result};
use futures::{future::join_all, Future};
Expand Down Expand Up @@ -219,6 +221,28 @@ impl NetworkClient {
Ok(())
}

/// Modifies the CPU cycles for a proof. May be called by the claimer after the proof has been
/// claimed. Returns an error if the proof is not in a PROOF_CLAIMED state or if the caller is
/// not the claimer.
pub async fn modify_cpu_cycles(
&self,
proof_id: &str,
cycles: u64,
) -> Result<ModifyCpuCyclesResponse> {
let nonce = self.get_nonce().await?;
let signature = self.auth.sign_modify_cpu_cycles_message(nonce, proof_id, cycles).await?;
let res = self
.with_error_handling(self.rpc.modify_cpu_cycles(ModifyCpuCyclesRequest {
signature,
nonce,
proof_id: proof_id.to_string(),
cycles,
}))
.await?;

Ok(res)
}

/// Fulfill a proof. Should only be called after the proof has been uploaded. Returns an error
/// if the proof is not in a PROOF_CLAIMED state or if the caller is not the claimer.
pub async fn fulfill_proof(&self, proof_id: &str) -> Result<FulfillProofResponse> {
Expand Down
45 changes: 45 additions & 0 deletions crates/sdk/src/proto/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ pub struct UnclaimProofRequest {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct UnclaimProofResponse {}
/// The request to update a proof's CPU cycle count.
#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ModifyCpuCyclesRequest {
/// The signature of the message.
#[prost(bytes = "vec", tag = "1")]
pub signature: ::prost::alloc::vec::Vec<u8>,
/// The nonce for the account.
#[prost(uint64, tag = "2")]
pub nonce: u64,
/// The proof identifier.
#[prost(string, tag = "3")]
pub proof_id: ::prost::alloc::string::String,
/// The number of CPU cycles for this proof.
#[prost(uint64, tag = "4")]
pub cycles: u64,
}
/// The response for updating a proof's CPU cycle count, empty on success.
#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ModifyCpuCyclesResponse {}
/// The request to fulfill a proof. MUST be called after the proof has been uploaded and MUST be called
/// when the proof is in a PROOF_CLAIMED state.
#[derive(serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -523,6 +546,11 @@ pub trait NetworkService {
ctx: twirp::Context,
req: UnclaimProofRequest,
) -> Result<UnclaimProofResponse, twirp::TwirpErrorResponse>;
async fn modify_cpu_cycles(
&self,
ctx: twirp::Context,
req: ModifyCpuCyclesRequest,
) -> Result<ModifyCpuCyclesResponse, twirp::TwirpErrorResponse>;
async fn fulfill_proof(
&self,
ctx: twirp::Context,
Expand Down Expand Up @@ -583,6 +611,12 @@ where
api.unclaim_proof(ctx, req).await
},
)
.route(
"/ModifyCpuCycles",
|api: std::sync::Arc<T>, ctx: twirp::Context, req: ModifyCpuCyclesRequest| async move {
api.modify_cpu_cycles(ctx, req).await
},
)
.route(
"/FulfillProof",
|api: std::sync::Arc<T>, ctx: twirp::Context, req: FulfillProofRequest| async move {
Expand Down Expand Up @@ -639,6 +673,10 @@ pub trait NetworkServiceClient: Send + Sync + std::fmt::Debug {
&self,
req: UnclaimProofRequest,
) -> Result<UnclaimProofResponse, twirp::ClientError>;
async fn modify_cpu_cycles(
&self,
req: ModifyCpuCyclesRequest,
) -> Result<ModifyCpuCyclesResponse, twirp::ClientError>;
async fn fulfill_proof(
&self,
req: FulfillProofRequest,
Expand Down Expand Up @@ -692,6 +730,13 @@ impl NetworkServiceClient for twirp::client::Client {
let url = self.base_url.join("network.NetworkService/UnclaimProof")?;
self.request(url, req).await
}
async fn modify_cpu_cycles(
&self,
req: ModifyCpuCyclesRequest,
) -> Result<ModifyCpuCyclesResponse, twirp::ClientError> {
let url = self.base_url.join("network.NetworkService/ModifyCpuCycles")?;
self.request(url, req).await
}
async fn fulfill_proof(
&self,
req: FulfillProofRequest,
Expand Down

0 comments on commit 23c97e9

Please sign in to comment.