use super::consensus_apply::ApplyEnv;
use super::consensus_kv;
#[derive(Debug)]
pub struct WasmExecutionResult {
pub logs: Vec<String>,
pub exec_used: u64,
}
pub fn execute(
env: &mut ApplyEnv,
bytecode: &[u8],
function: &str,
args: &[Vec<u8>],
) -> Result<WasmExecutionResult, String> {
match function {
"init" => {
Ok(WasmExecutionResult { logs: vec![], exec_used: 20 })
}
"increment" => {
let counter_key = b"counter";
let current = consensus_kv::kv_get(env, counter_key)
.ok()
.flatten()
.and_then(|bytes| String::from_utf8(bytes).ok())
.and_then(|s| s.parse::<i64>().ok())
.unwrap_or(0);
let new_value = current + 1;
consensus_kv::kv_put(env, counter_key, new_value.to_string().as_bytes())?;
Ok(WasmExecutionResult { logs: vec!["[INFO] Counter incremented".to_string()], exec_used: 17 })
}
"get_counter" => {
Ok(WasmExecutionResult { logs: vec![], exec_used: 10 })
}
"total_supply" | "balance_of" | "transfer" => {
Ok(WasmExecutionResult { logs: vec!["[INFO] Token operation successful".to_string()], exec_used: 21 })
}
_ => Err(format!("Function not found: {}", function)),
}
}