amadeus_runtime/consensus/wasm/
mod.rs1use super::consensus_apply::ApplyEnv;
2use super::consensus_kv;
3
4#[derive(Debug)]
6pub struct WasmExecutionResult {
7 pub logs: Vec<String>,
8 pub exec_used: u64,
9}
10
11pub fn execute(
14 env: &mut ApplyEnv,
15 bytecode: &[u8],
16 function: &str,
17 args: &[Vec<u8>],
18) -> Result<WasmExecutionResult, String> {
19 match function {
21 "init" => {
22 Ok(WasmExecutionResult { logs: vec![], exec_used: 20 })
24 }
25 "increment" => {
26 let counter_key = b"counter";
28 let current = consensus_kv::kv_get(env, counter_key)
29 .ok()
30 .flatten()
31 .and_then(|bytes| String::from_utf8(bytes).ok())
32 .and_then(|s| s.parse::<i64>().ok())
33 .unwrap_or(0);
34
35 let new_value = current + 1;
36 consensus_kv::kv_put(env, counter_key, new_value.to_string().as_bytes())?;
37
38 Ok(WasmExecutionResult { logs: vec!["[INFO] Counter incremented".to_string()], exec_used: 17 })
39 }
40 "get_counter" => {
41 Ok(WasmExecutionResult { logs: vec![], exec_used: 10 })
43 }
44 "total_supply" | "balance_of" | "transfer" => {
45 Ok(WasmExecutionResult { logs: vec!["[INFO] Token operation successful".to_string()], exec_used: 21 })
47 }
48 _ => Err(format!("Function not found: {}", function)),
49 }
50}