From 7c12425360c49aa5072d1335b911939b167152f5 Mon Sep 17 00:00:00 2001 From: mattstam Date: Wed, 18 Sep 2024 12:48:45 -0700 Subject: [PATCH] modify_cpu_cycles --- crates/sdk/src/network/auth.rs | 18 ++++++++++++ crates/sdk/src/network/client.rs | 29 +++++++++++++++++-- crates/sdk/src/proto/network.rs | 48 ++++++++++++++++++++++++++++++-- 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/crates/sdk/src/network/auth.rs b/crates/sdk/src/network/auth.rs index 45354a66ba..7cb2c2ef7d 100644 --- a/crates/sdk/src/network/auth.rs +++ b/crates/sdk/src/network/auth.rs @@ -34,6 +34,12 @@ sol! { string description; } + struct ModifyCpuCycles { + uint64 nonce; + string proof_id; + uint64 cycles; + } + struct FulfillProof { uint64 nonce; string proof_id; @@ -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> { + 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> { diff --git a/crates/sdk/src/network/client.rs b/crates/sdk/src/network/client.rs index 13b18f1e45..f9bdd94ccd 100644 --- a/crates/sdk/src/network/client.rs +++ b/crates/sdk/src/network/client.rs @@ -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}; @@ -219,9 +221,31 @@ 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 { + 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, cycles: u64) -> Result { + pub async fn fulfill_proof(&self, proof_id: &str) -> Result { let nonce = self.get_nonce().await?; let signature = self.auth.sign_fulfill_proof_message(nonce, proof_id).await?; let res = self @@ -229,7 +253,6 @@ impl NetworkClient { signature, nonce, proof_id: proof_id.to_string(), - cycles, })) .await?; diff --git a/crates/sdk/src/proto/network.rs b/crates/sdk/src/proto/network.rs index a12853961a..d2f2e118e2 100644 --- a/crates/sdk/src/proto/network.rs +++ b/crates/sdk/src/proto/network.rs @@ -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, + /// 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)] @@ -131,9 +154,6 @@ pub struct FulfillProofRequest { /// The proof identifier. #[prost(string, tag = "3")] pub proof_id: ::prost::alloc::string::String, - /// The number of cycles used. - #[prost(uint64, tag = "4")] - pub cycles: u64, } /// The response for fulfilling a proof, empty on success. #[derive(serde::Serialize, serde::Deserialize)] @@ -526,6 +546,11 @@ pub trait NetworkService { ctx: twirp::Context, req: UnclaimProofRequest, ) -> Result; + async fn modify_cpu_cycles( + &self, + ctx: twirp::Context, + req: ModifyCpuCyclesRequest, + ) -> Result; async fn fulfill_proof( &self, ctx: twirp::Context, @@ -586,6 +611,12 @@ where api.unclaim_proof(ctx, req).await }, ) + .route( + "/ModifyCpuCycles", + |api: std::sync::Arc, ctx: twirp::Context, req: ModifyCpuCyclesRequest| async move { + api.modify_cpu_cycles(ctx, req).await + }, + ) .route( "/FulfillProof", |api: std::sync::Arc, ctx: twirp::Context, req: FulfillProofRequest| async move { @@ -642,6 +673,10 @@ pub trait NetworkServiceClient: Send + Sync + std::fmt::Debug { &self, req: UnclaimProofRequest, ) -> Result; + async fn modify_cpu_cycles( + &self, + req: ModifyCpuCyclesRequest, + ) -> Result; async fn fulfill_proof( &self, req: FulfillProofRequest, @@ -695,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 { + let url = self.base_url.join("network.NetworkService/ModifyCpuCycles")?; + self.request(url, req).await + } async fn fulfill_proof( &self, req: FulfillProofRequest,