Skip to content

Commit

Permalink
Bench cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Oct 18, 2024
1 parent c0640f2 commit 68f98fc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
26 changes: 21 additions & 5 deletions packages/account_sdk/cmd/bench.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use account_sdk::abigen::controller::{Call, OutsideExecution};
use account_sdk::account::outside_execution::{OutsideExecutionAccount, OutsideExecutionCaller};
use account_sdk::account::session::hash::Policy;
use account_sdk::artifacts::{Version, CONTROLLERS};
use account_sdk::controller::Controller;
use account_sdk::factory::ControllerFactory;
Expand Down Expand Up @@ -28,8 +29,7 @@ const DURATION_SECS: u64 = 30 * 60;

#[tokio::main]
async fn main() {
let rpc_url = Url::parse("http://localhost:8001/x/starknet/sepolia").unwrap();
// let rpc_url = Url::parse("https://api.cartridge.gg/x/starknet/sepolia").unwrap();
let rpc_url = Url::parse("https://api.cartridge.gg/x/starknet/sepolia").unwrap();
let provider = CartridgeJsonRpcProvider::new(rpc_url.clone());

let chain_id = felt!("0x534e5f5345504f4c4941"); // Hex for "SN_SEPOLIA"
Expand All @@ -50,7 +50,9 @@ async fn main() {

let address = factory.address(salt);

let controller = Controller::new(
println!("Controller address: {:#x}", address);

let mut controller = Controller::new(
"app_id".to_string(),
username,
CONTROLLERS[&Version::LATEST].hash,
Expand Down Expand Up @@ -90,18 +92,31 @@ async fn main() {
let duration = Duration::from_secs(DURATION_SECS);
let total_transactions = TPS * duration.as_secs() as usize;

let _ = controller
.create_session(
vec![Policy::new(contract_address, selector!("flip"))],
u32::MAX as u64,
)
.await
.unwrap();

let controller = Arc::new(controller);
let interval = Duration::from_secs_f64(1.0 / TPS as f64);
let nonce_channel = SigningKey::from_random().secret_scalar();
let nonce_counter = Arc::new(std::sync::atomic::AtomicU64::new(0));

let mut handles = vec![];

for i in 0..total_transactions {
let controller = Arc::clone(&controller);
let nonce_counter = Arc::clone(&nonce_counter);
let handle = tokio::spawn(async move {
let x = rand::thread_rng().gen_range(0..=100);
let y = rand::thread_rng().gen_range(0..=100);
let nonce_increment = nonce_counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let nonce = (nonce_channel, Felt::from(nonce_increment));

match flip(&controller, contract_address.into(), x, y).await {
match flip(&controller, contract_address.into(), x, y, nonce).await {
Ok(_) => {
println!("Routine {}: Successfully executed flip function", i);
}
Expand All @@ -125,6 +140,7 @@ async fn flip(
contract_address: ContractAddress,
x: u64,
y: u64,
nonce: (Felt, Felt),
) -> Result<ExecuteFromOutsideResponse, ExecuteFromOutsideError> {
let flip = Call {
to: contract_address,
Expand All @@ -139,7 +155,7 @@ async fn flip(
execute_after: 0,
execute_before: u32::MAX as u64,
calls: vec![flip],
nonce: (Felt::ONE, Felt::ZERO),
nonce,
};

let flip_signed = session_account
Expand Down
2 changes: 0 additions & 2 deletions packages/account_sdk/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub struct Controller {
factory: ControllerFactory,
pub storage: Storage,
nonce: Felt,
pub(crate) execute_from_outside_nonce: (Felt, Felt),
}

impl Controller {
Expand Down Expand Up @@ -80,7 +79,6 @@ impl Controller {
factory,
storage: Storage::default(),
nonce: Felt::ZERO,
execute_from_outside_nonce: (SigningKey::from_random().secret_scalar(), Felt::ZERO),
};

let contract = Box::new(abigen::controller::Controller::new(
Expand Down
4 changes: 3 additions & 1 deletion packages/account_sdk/src/execute_from_outside.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use starknet::{
accounts::ConnectedAccount,
core::types::{Call, InvokeTransactionResult},
signers::SigningKey,
};
use starknet_crypto::Felt;

use crate::{
abigen::controller::OutsideExecution,
Expand Down Expand Up @@ -47,7 +49,7 @@ impl Controller {
execute_after: 0,
execute_before: now + 600,
calls: calls.into_iter().map(|call| call.into()).collect(),
nonce: self.execute_from_outside_nonce,
nonce: (SigningKey::from_random().secret_scalar(), Felt::ZERO),
};

self.execute_from_outside_raw(outside_execution).await
Expand Down
6 changes: 0 additions & 6 deletions packages/account_sdk/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ impl CartridgeProvider for CartridgeJsonRpcProvider {
},
};

println!(
"JSON-RPC request: {}",
serde_json::to_string_pretty(&request).unwrap()
);

let client = Client::new();
let response = client
.post(self.rpc_url.as_str())
Expand All @@ -92,7 +87,6 @@ impl CartridgeProvider for CartridgeJsonRpcProvider {
.await?;

let json_response: Value = response.json().await?;
println!("json_rpc_response: {:?}", json_response);
let json_rpc_response: JsonRpcResponse<ExecuteFromOutsideResponse> =
serde_json::from_value(json_response)?;

Expand Down

0 comments on commit 68f98fc

Please sign in to comment.