Skip to content

Commit

Permalink
feat: add costs report in sdk tx response (#1620)
Browse files Browse the repository at this point in the history
* feat: add costs report in sdk tx response

* chore: update dependencies

* chore: update dependencies
  • Loading branch information
hugocaillard authored Dec 17, 2024
1 parent 682daf5 commit 6b68e00
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 55 deletions.
12 changes: 4 additions & 8 deletions components/clarinet-sdk-wasm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use std::{panic, path::PathBuf};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsValue;

use crate::utils::costs::SerializableCostsReport;
use crate::utils::events::serialize_event;

#[wasm_bindgen]
Expand Down Expand Up @@ -217,6 +216,7 @@ pub struct TxArgs {
pub struct TransactionRes {
pub result: String,
pub events: String,
pub costs: String,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -248,6 +248,7 @@ pub fn execution_result_to_transaction_res(execution: &ExecutionResult) -> Trans
TransactionRes {
result,
events: json!(events_as_strings).to_string(),
costs: json!(execution.cost).to_string(),
}
}

Expand Down Expand Up @@ -1076,13 +1077,8 @@ impl SDK {

let coverage = session.collect_lcov_content(&asts, &contract_paths);

let mut costs_reports = Vec::new();
costs_reports.append(&mut self.costs_reports);
let costs_reports: Vec<SerializableCostsReport> = costs_reports
.iter()
.map(SerializableCostsReport::from_vm_costs_report)
.collect();
let costs = serde_json::to_string(&costs_reports).map_err(|e| e.to_string())?;
let costs = serde_json::to_string(&self.costs_reports).map_err(|e| e.to_string())?;
self.costs_reports.clear();

Ok(SessionReport { coverage, costs })
}
Expand Down
44 changes: 0 additions & 44 deletions components/clarinet-sdk-wasm/src/utils/costs.rs

This file was deleted.

1 change: 0 additions & 1 deletion components/clarinet-sdk-wasm/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod costs;
pub mod events;
2 changes: 2 additions & 0 deletions components/clarinet-sdk/browser/src/sdkProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type ParsedTransactionResult,
type Execute,
type TransferSTX,
parseCosts,
} from "../../common/src/sdkProxyHelpers.js";

/** @deprecated use `simnet.execute(command)` instead */
Expand Down Expand Up @@ -48,6 +49,7 @@ function parseTxResponse(response: TransactionRes): ParsedTransactionResult {
return {
result: Cl.deserialize(response.result),
events: parseEvents(response.events),
costs: parseCosts(response.costs),
};
}

Expand Down
42 changes: 42 additions & 0 deletions components/clarinet-sdk/common/src/sdkProxyHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@ export type ClarityEvent = {
data: { raw_value?: string; value?: ClarityValue; [key: string]: any };
};

export type ExecutionCost = {
writeLength: number;
writeCount: number;
readLength: number;
readCount: number;
runtime: number;
};

export type ClarityCosts = {
total: ExecutionCost;
limit: ExecutionCost;
memory: number;
memory_limit: number;
};

export type ParsedTransactionResult = {
result: ClarityValue;
events: ClarityEvent[];
costs: ClarityCosts | null;
};

export type CallFn = (
Expand Down Expand Up @@ -113,6 +129,32 @@ export function parseEvents(events: string): ClarityEvent[] {
}
}

export function parseCosts(costs: string): ClarityCosts | null {
try {
let { memory, memory_limit, total, limit } = JSON.parse(costs);
return {
memory: memory,
memory_limit: memory_limit,
total: {
writeLength: total.write_length,
writeCount: total.write_count,
readLength: total.read_length,
readCount: total.read_count,
runtime: total.runtime,
},
limit: {
writeLength: limit.write_length,
writeCount: limit.write_count,
readLength: limit.read_length,
readCount: limit.read_count,
runtime: limit.runtime,
},
};
} catch (_e) {
return null;
}
}

export type MineBlock = (txs: Array<Tx>) => ParsedTransactionResult[];
export type Execute = (snippet: string) => ParsedTransactionResult;
export type GetDataVar = (contract: string, dataVar: string) => ClarityValue;
Expand Down
2 changes: 2 additions & 0 deletions components/clarinet-sdk/node/src/sdkProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type ParsedTransactionResult,
type Execute,
type TransferSTX,
parseCosts,
} from "../../common/src/sdkProxyHelpers.js";

/** @deprecated use `simnet.execute(command)` instead */
Expand Down Expand Up @@ -48,6 +49,7 @@ function parseTxResponse(response: TransactionRes): ParsedTransactionResult {
return {
result: Cl.deserialize(response.result),
events: parseEvents(response.events),
costs: parseCosts(response.costs),
};
}

Expand Down
29 changes: 28 additions & 1 deletion components/clarinet-sdk/node/tests/simnet-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ function deleteExistingDeploymentPlan() {

beforeEach(async () => {
deleteExistingDeploymentPlan();
simnet = await initSimnet("tests/fixtures/Clarinet.toml");
simnet = await initSimnet("tests/fixtures/Clarinet.toml", false, {
trackCosts: true,
trackCoverage: false,
});
});

afterEach(() => {
Expand Down Expand Up @@ -146,6 +149,30 @@ describe("simnet can call contracts function", () => {
expect(printEvent.data.value).toStrictEqual(Cl.stringAscii("call increment"));
});

it("reports costs", () => {
const res = simnet.callPublicFn("counter", "increment", [], address1);

expect(res).toHaveProperty("costs");
expect(res.costs).toStrictEqual({
memory: 417,
memory_limit: 100000000,
total: {
writeLength: 44,
writeCount: 3,
readLength: 1466,
readCount: 8,
runtime: 15630,
},
limit: {
writeLength: 15000000,
writeCount: 15000,
readLength: 100000000,
readCount: 15000,
runtime: 5000000000,
},
});
});

it("can call public functions with arguments", () => {
const res = simnet.callPublicFn("counter", "add", [Cl.uint(2)], address1);

Expand Down
2 changes: 1 addition & 1 deletion components/clarity-repl/src/repl/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ lazy_static! {
};
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct CostsReport {
pub test_name: String,
pub contract_id: String,
Expand Down

0 comments on commit 6b68e00

Please sign in to comment.