From 4c910ce330beae3c0b988c2a1937fd115b6dc4e6 Mon Sep 17 00:00:00 2001 From: Damir Vodenicarevic Date: Wed, 28 Jun 2023 13:35:02 +0200 Subject: [PATCH] simplify interface --- src/as_execution/abi.rs | 91 ++++++++++++--------- src/as_execution/common.rs | 2 +- src/tests/mod.rs | 75 ++++------------- src/types.rs | 134 ++++++------------------------- src/wasmv1_execution/abi/abis.rs | 19 +++-- 5 files changed, 99 insertions(+), 222 deletions(-) diff --git a/src/as_execution/abi.rs b/src/as_execution/abi.rs index 06cbae7b..43a11ec7 100644 --- a/src/as_execution/abi.rs +++ b/src/as_execution/abi.rs @@ -37,6 +37,14 @@ pub(crate) fn get_env(ctx: &FunctionEnvMut) -> ABIResult { } } +/// Get the address at the top of the callstack +fn get_current_address(env: &ASEnv) -> ABIResult { + match env.get_interface().get_call_stack()?.last().cloned() { + None => abi_bail!("No address on the call stack. This should never happen."), + Some(addr) => Ok(addr), + } +} + /// Get the coins that have been made available for a specific purpose for the current call. #[named] pub(crate) fn assembly_script_get_call_coins(mut ctx: FunctionEnvMut) -> ABIResult { @@ -57,6 +65,7 @@ pub(crate) fn assembly_script_transfer_coins( if raw_amount.is_negative() { abi_bail!("Negative raw amount."); } + let from_address = get_current_address(&env)?; let memory = get_memory!(env); let to_address = &read_string(memory, &ctx, to_address)?; // Do not remove this. It could be used for gas_calibration in future. @@ -66,7 +75,7 @@ pub(crate) fn assembly_script_transfer_coins( // } Ok(env .get_interface() - .transfer_coins(to_address, raw_amount as u64)?) + .transfer_coins(&from_address, to_address, raw_amount as u64)?) } /// Transfer an amount from the specified address to a target address. @@ -94,14 +103,15 @@ pub(crate) fn assembly_script_transfer_coins_for( // } Ok(env .get_interface() - .transfer_coins_for(from_address, to_address, raw_amount as u64)?) + .transfer_coins(from_address, to_address, raw_amount as u64)?) } #[named] pub(crate) fn assembly_script_get_balance(mut ctx: FunctionEnvMut) -> ABIResult { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; - Ok(env.get_interface().get_balance()? as i64) + let cur_address = get_current_address(&env)?; + Ok(env.get_interface().get_balance(&cur_address)? as i64) } #[named] @@ -118,7 +128,7 @@ pub(crate) fn assembly_script_get_balance_for( // let fname = format!("massa.{}:0", function_name!()); // param_size_update(&env, &mut ctx, &fname, address.len(), true); // } - Ok(env.get_interface().get_balance_for(address)? as i64) + Ok(env.get_interface().get_balance(address)? as i64) } /// Raw call that have the right type signature to be able to be call a module @@ -169,20 +179,8 @@ pub(crate) fn assembly_script_get_remaining_gas(mut ctx: FunctionEnvMut) /// given interface, an operation number limit and a webassembly module /// /// An utility print function to write on stdout directly from AssemblyScript: -#[named] -pub(crate) fn assembly_script_print(mut ctx: FunctionEnvMut, arg: i32) -> ABIResult<()> { - let env = get_env(&ctx)?; - sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; - let memory = get_memory!(env); - let message = read_string(memory, &ctx, arg)?; - - // Do not remove this. It could be used for gas_calibration in future. - // if cfg!(feature = "gas_calibration") { - // let fname = format!("massa.{}:0", function_name!()); - // param_size_update(&env, &mut ctx, &fname, message.len(), true); - // } - - env.get_interface().print(&message)?; +pub(crate) fn assembly_script_print(mut _ctx: FunctionEnvMut, _arg: i32) -> ABIResult<()> { + // DEPRECATED Ok(()) } @@ -191,7 +189,7 @@ pub(crate) fn assembly_script_print(mut ctx: FunctionEnvMut, arg: i32) -> pub(crate) fn assembly_script_get_op_keys(mut ctx: FunctionEnvMut) -> ABIResult { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; - match env.get_interface().get_op_keys() { + match env.get_interface().get_op_keys(&[]) { Err(err) => abi_bail!(err), Ok(keys) => { let fmt_keys = @@ -219,7 +217,7 @@ pub(crate) fn assembly_script_has_op_key( // param_size_update(&env, &mut ctx, &fname, key_bytes.len(), true); // } - match env.get_interface().has_op_key(&key_bytes) { + match env.get_interface().op_entry_exists(&key_bytes) { Err(err) => abi_bail!(err), Ok(b) => { // https://doc.rust-lang.org/reference/types/boolean.html @@ -246,7 +244,7 @@ pub(crate) fn assembly_script_get_op_data( // let fname = format!("massa.{}:0", function_name!()); // param_size_update(&env, &mut ctx, &fname, key_bytes.len(), true); // } - let data = env.get_interface().get_op_data(&key_bytes)?; + let data = env.get_interface().get_op_value(&key_bytes)?; let ptr = pointer_from_bytearray(&env, &mut ctx, &data)?.offset() as i32; Ok(ptr) } @@ -296,6 +294,7 @@ pub(crate) fn assembly_script_get_keys( ) -> ABIResult { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + let cur_address = get_current_address(&env)?; let memory = get_memory!(env); let prefix = read_buffer(memory, &ctx, prefix)?; let prefix_opt = if !prefix.is_empty() { @@ -303,7 +302,9 @@ pub(crate) fn assembly_script_get_keys( } else { None }; - let keys = env.get_interface().get_keys(prefix_opt)?; + let keys = env + .get_interface() + .get_ds_keys(&cur_address, prefix_opt.unwrap_or_default())?; let fmt_keys = ser_bytearray_vec(&keys, keys.len(), settings::max_datastore_entry_count())?; let ptr = pointer_from_bytearray(&env, &mut ctx, &fmt_keys)?.offset(); Ok(ptr as i32) @@ -326,7 +327,9 @@ pub(crate) fn assembly_script_get_keys_for( } else { None }; - let keys = env.get_interface().get_keys_for(&address, prefix_opt)?; + let keys = env + .get_interface() + .get_ds_keys(&address, prefix_opt.unwrap_or_default())?; let fmt_keys = ser_bytearray_vec(&keys, keys.len(), settings::max_datastore_entry_count())?; let ptr = pointer_from_bytearray(&env, &mut ctx, &fmt_keys)?.offset(); Ok(ptr as i32) @@ -341,6 +344,7 @@ pub(crate) fn assembly_script_set_data( ) -> ABIResult<()> { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + let cur_address = get_current_address(&env)?; let memory = get_memory!(env); let key = read_buffer(memory, &ctx, key)?; let value = read_buffer(memory, &ctx, value)?; @@ -363,7 +367,8 @@ pub(crate) fn assembly_script_set_data( // ); // } - env.get_interface().raw_set_data(&key, &value)?; + env.get_interface() + .set_ds_value(&cur_address, &key, &value)?; Ok(()) } @@ -376,6 +381,7 @@ pub(crate) fn assembly_script_append_data( ) -> ABIResult<()> { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + let cur_address = get_current_address(&env)?; let memory = get_memory!(env); let key = read_buffer(memory, &ctx, key)?; let value = read_buffer(memory, &ctx, value)?; @@ -386,7 +392,8 @@ pub(crate) fn assembly_script_append_data( // let fname = format!("massa.{}:1", function_name!()); // param_size_update(&env, &mut ctx, &fname, value.len(), true); // } - env.get_interface().raw_append_data(&key, &value)?; + env.get_interface() + .append_ds_value(&cur_address, &key, &value)?; Ok(()) } @@ -395,6 +402,7 @@ pub(crate) fn assembly_script_append_data( pub(crate) fn assembly_script_get_data(mut ctx: FunctionEnvMut, key: i32) -> ABIResult { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + let cur_address = get_current_address(&env)?; let memory = get_memory!(env); let key = read_buffer(memory, &ctx, key)?; // Do not remove this. It could be used for gas_calibration in future. @@ -402,7 +410,7 @@ pub(crate) fn assembly_script_get_data(mut ctx: FunctionEnvMut, key: i32) // let fname = format!("massa.{}:0", function_name!()); // param_size_update(&env, &mut ctx, &fname, key.len(), true); // } - let data = env.get_interface().raw_get_data(&key)?; + let data = env.get_interface().get_ds_value(&cur_address, &key)?; Ok(pointer_from_bytearray(&env, &mut ctx, &data)?.offset() as i32) } @@ -411,6 +419,7 @@ pub(crate) fn assembly_script_get_data(mut ctx: FunctionEnvMut, key: i32) pub(crate) fn assembly_script_has_data(mut ctx: FunctionEnvMut, key: i32) -> ABIResult { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + let cur_address = get_current_address(&env)?; let memory = get_memory!(env); let key = read_buffer(memory, &ctx, key)?; // Do not remove this. It could be used for gas_calibration in future. @@ -418,7 +427,7 @@ pub(crate) fn assembly_script_has_data(mut ctx: FunctionEnvMut, key: i32) // let fname = format!("massa.{}:0", function_name!()); // param_size_update(&env, &mut ctx, &fname, key.len(), true); // } - Ok(env.get_interface().has_data(&key)? as i32) + Ok(env.get_interface().ds_entry_exists(&cur_address, &key)? as i32) } /// deletes a key-indexed data entry in the datastore of the current address, fails if the entry is absent @@ -429,6 +438,7 @@ pub(crate) fn assembly_script_delete_data( ) -> ABIResult<()> { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + let cur_address = get_current_address(&env)?; let memory = get_memory!(env); let key = read_buffer(memory, &ctx, key)?; // Do not remove this. It could be used for gas_calibration in future. @@ -436,7 +446,7 @@ pub(crate) fn assembly_script_delete_data( // let fname = format!("massa.{}:0", function_name!()); // param_size_update(&env, &mut ctx, &fname, key.len(), true); // } - env.get_interface().raw_delete_data(&key)?; + env.get_interface().delete_ds_entry(&cur_address, &key)?; Ok(()) } @@ -464,8 +474,7 @@ pub(crate) fn assembly_script_set_data_for( // let fname = format!("massa.{}:2", function_name!()); // param_size_update(&env, &mut ctx, &fname, value.len(), true); // } - env.get_interface() - .raw_set_data_for(&address, &key, &value)?; + env.get_interface().set_ds_value(&address, &key, &value)?; Ok(()) } @@ -493,7 +502,7 @@ pub(crate) fn assembly_script_append_data_for( // param_size_update(&env, &mut ctx, &fname, value.len(), true); // } env.get_interface() - .raw_append_data_for(&address, &key, &value)?; + .append_ds_value(&address, &key, &value)?; Ok(()) } @@ -517,7 +526,7 @@ pub(crate) fn assembly_script_get_data_for( // param_size_update(&env, &mut ctx, &fname, key.len(), true); // } - let data = env.get_interface().raw_get_data_for(&address, &key)?; + let data = env.get_interface().get_ds_value(&address, &key)?; Ok(pointer_from_bytearray(&env, &mut ctx, &data)?.offset() as i32) } @@ -540,7 +549,7 @@ pub(crate) fn assembly_script_delete_data_for( // let fname = format!("massa.{}:1", function_name!()); // param_size_update(&env, &mut ctx, &fname, key.len(), true); // } - env.get_interface().raw_delete_data_for(&address, &key)?; + env.get_interface().delete_ds_entry(&address, &key)?; Ok(()) } @@ -562,7 +571,7 @@ pub(crate) fn assembly_script_has_data_for( // let fname = format!("massa.{}:1", function_name!()); // param_size_update(&env, &mut ctx, &fname, key.len(), true); // } - Ok(env.get_interface().has_data_for(&address, &key)? as i32) + Ok(env.get_interface().ds_entry_exists(&address, &key)? as i32) } #[named] @@ -792,8 +801,7 @@ pub(crate) fn assembly_script_set_bytecode_for( // let fname = format!("massa.{}:1", function_name!()); // param_size_update(&env, &mut ctx, &fname, bytecode_raw.len(), true); // } - env.get_interface() - .raw_set_bytecode_for(&address, &bytecode_raw)?; + env.get_interface().set_bytecode(&address, &bytecode_raw)?; Ok(()) } @@ -805,6 +813,7 @@ pub(crate) fn assembly_script_set_bytecode( ) -> ABIResult<()> { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + let cur_address = get_current_address(&env)?; let memory = get_memory!(env); let bytecode_raw = read_buffer(memory, &ctx, bytecode)?; // Do not remove this. It could be used for gas_calibration in future. @@ -812,7 +821,8 @@ pub(crate) fn assembly_script_set_bytecode( // let fname = format!("massa.{}:0", function_name!()); // param_size_update(&env, &mut ctx, &fname, bytecode_raw.len(), true); // } - env.get_interface().raw_set_bytecode(&bytecode_raw)?; + env.get_interface() + .set_bytecode(&cur_address, &bytecode_raw)?; Ok(()) } @@ -821,7 +831,8 @@ pub(crate) fn assembly_script_set_bytecode( pub(crate) fn assembly_script_get_bytecode(mut ctx: FunctionEnvMut) -> ABIResult { let env = get_env(&ctx)?; sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; - let data = env.get_interface().raw_get_bytecode()?; + let cur_address = get_current_address(&env)?; + let data = env.get_interface().get_bytecode(&cur_address)?; Ok(pointer_from_bytearray(&env, &mut ctx, &data)?.offset() as i32) } @@ -835,7 +846,7 @@ pub(crate) fn assembly_script_get_bytecode_for( sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; let memory = get_memory!(env); let address = read_string(memory, &ctx, address)?; - let data = env.get_interface().raw_get_bytecode_for(&address)?; + let data = env.get_interface().get_bytecode(&address)?; Ok(pointer_from_bytearray(&env, &mut ctx, &data)?.offset() as i32) } @@ -878,7 +889,7 @@ pub(crate) fn assembly_script_local_call( let memory = get_memory!(env); let address = &read_string(memory, &ctx, address)?; - let bytecode = env.get_interface().raw_get_bytecode_for(address)?; + let bytecode = env.get_interface().get_bytecode(address)?; let function = &read_string(memory, &ctx, function)?; let param = &read_buffer(memory, &ctx, param)?; diff --git a/src/as_execution/common.rs b/src/as_execution/common.rs index e861c1de..5324c870 100644 --- a/src/as_execution/common.rs +++ b/src/as_execution/common.rs @@ -92,7 +92,7 @@ pub(crate) fn function_exists( ) -> ABIResult { let env = get_env(ctx)?; let interface = env.get_interface(); - let bytecode = interface.raw_get_bytecode_for(address)?; + let bytecode = interface.get_bytecode(address)?; let remaining_gas = if cfg!(feature = "gas_calibration") { u64::MAX diff --git a/src/tests/mod.rs b/src/tests/mod.rs index a38ef93c..48c6880a 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -5,6 +5,7 @@ use crate::{Compiler, GasCosts, RuntimeModule}; use anyhow::{bail, Result}; use parking_lot::Mutex; use sha2::{Digest, Sha256}; +use std::collections::BTreeSet; use std::sync::Arc; use std::{collections::BTreeMap, str::FromStr}; @@ -32,15 +33,7 @@ impl Interface for TestInterface { Ok(()) } - fn get_balance(&self) -> Result { - Ok(1) - } - - fn transfer_coins(&self, _to_address: &str, _raw_amount: u64) -> Result<()> { - Ok(()) - } - - fn transfer_coins_for( + fn transfer_coins( &self, _from_address: &str, _to_address: &str, @@ -80,11 +73,7 @@ impl Interface for TestInterface { Ok(vec![]) } - fn has_data(&self, _key: &[u8]) -> Result { - Ok(false) - } - - fn has_data_for(&self, _address: &str, _key: &[u8]) -> Result { + fn ds_entry_exists(&self, _address: &str, _key: &[u8]) -> Result { Ok(false) } @@ -92,33 +81,19 @@ impl Interface for TestInterface { unimplemented!() } - fn raw_append_data(&self, _key: &[u8], _value: &[u8]) -> Result<()> { - Ok(()) - } - - fn raw_append_data_for(&self, _address: &str, _key: &[u8], _value: &[u8]) -> Result<()> { - Ok(()) - } - - fn raw_delete_data(&self, _key: &[u8]) -> Result<()> { + fn append_ds_value(&self, _address: &str, _key: &[u8], _value: &[u8]) -> Result<()> { Ok(()) } - fn raw_delete_data_for(&self, _address: &str, _key: &[u8]) -> Result<()> { + fn delete_ds_entry(&self, _address: &str, _key: &[u8]) -> Result<()> { Ok(()) } - fn raw_get_data_for(&self, _address: &str, _key: &[u8]) -> Result> { + fn get_ds_value(&self, _address: &str, _key: &[u8]) -> Result> { Ok(vec![]) } - fn raw_set_data(&self, _key: &[u8], value: &[u8]) -> Result<()> { - let mut bytes = self.0.lock().clone(); - bytes.insert(String::from_str("print").unwrap(), value.to_vec()); - Ok(()) - } - - fn raw_set_data_for(&self, _address: &str, _key: &[u8], value: &[u8]) -> Result<()> { + fn set_ds_value(&self, _address: &str, _key: &[u8], value: &[u8]) -> Result<()> { let mut bytes = self.0.lock().clone(); bytes.insert(String::from_str("print").unwrap(), value.to_vec()); Ok(()) @@ -132,37 +107,15 @@ impl Interface for TestInterface { Ok(0) } - fn get_balance_for(&self, _address: &str) -> Result { + fn get_balance(&self, _address: &str) -> Result { Ok(1) } - fn raw_set_bytecode_for(&self, address: &str, bytecode: &[u8]) -> Result<()> { + fn set_bytecode(&self, address: &str, bytecode: &[u8]) -> Result<()> { self.0.lock().insert(address.to_string(), bytecode.to_vec()); Ok(()) } - fn raw_set_bytecode(&self, bytecode: &[u8]) -> Result<()> { - let address = String::from("get_string"); - self.0.lock().insert(address, bytecode.to_vec()); - Ok(()) - } - - fn print(&self, message: &str) -> Result<()> { - println!("{}", message); - self.0 - .lock() - .insert("print".into(), message.as_bytes().to_vec()); - Ok(()) - } - - fn raw_get_data(&self, _: &[u8]) -> Result> { - let bytes = self.0.lock().clone(); - match bytes.get(&"print".to_string()) { - Some(bytes) => Ok(bytes.clone()), - _ => Ok(vec![]), - } - } - fn get_call_coins(&self) -> Result { Ok(0) } @@ -188,15 +141,17 @@ impl Interface for TestInterface { Ok(()) } - fn get_op_keys(&self) -> Result>> { + fn get_op_keys(&self, _prefix: &[u8]) -> Result>> { Ok(vec![ vec![0, 1, 2, 3, 4, 5, 6, 11], vec![127, 128], vec![254, 255], - ]) + ] + .into_iter() + .collect()) } - fn has_op_key(&self, key: &[u8]) -> Result { + fn op_entry_exists(&self, key: &[u8]) -> Result { let ds: BTreeMap, Vec> = BTreeMap::from([ (vec![0, 1, 2, 3, 4, 5, 6, 11], vec![65]), (vec![127, 128], vec![66, 67]), @@ -206,7 +161,7 @@ impl Interface for TestInterface { Ok(ds.contains_key(key)) } - fn get_op_data(&self, key: &[u8]) -> Result> { + fn get_op_value(&self, key: &[u8]) -> Result> { let ds: BTreeMap, Vec> = BTreeMap::from([ (vec![0, 1, 2, 3, 4, 5, 6, 11], vec![65]), (vec![127, 128], vec![66, 67]), diff --git a/src/types.rs b/src/types.rs index 26c58b93..a342847c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,5 +1,4 @@ use anyhow::{anyhow, bail, Result}; -use serde::{de::DeserializeOwned, Serialize}; use std::{ collections::{BTreeSet, HashMap}, path::PathBuf, @@ -149,30 +148,14 @@ pub trait Interface: Send + Sync + InterfaceClone { unimplemented!("finish_call") } - /// Get the SCE ledger balance for the current address. - /// Defaults to zero if the address is not found. - fn get_balance(&self) -> Result { - unimplemented!("get_balance") - } - /// Get the SCE ledger balance for an address. /// Defaults to zero if the address is not found. - fn get_balance_for(&self, address: &str) -> Result { + fn get_balance(&self, address: &str) -> Result { unimplemented!("get_balance_for") } - /// Transfer an amount from the address on the current call stack to a target address. - fn transfer_coins(&self, to_address: &str, raw_amount: u64) -> Result<()> { - unimplemented!("transfer_coins") - } - /// Transfer an amount from the specified address to a target address. - fn transfer_coins_for( - &self, - from_address: &str, - to_address: &str, - raw_amount: u64, - ) -> Result<()> { + fn transfer_coins(&self, from_address: &str, to_address: &str, raw_amount: u64) -> Result<()> { unimplemented!("transfer_coins_for") } @@ -183,13 +166,8 @@ pub trait Interface: Send + Sync + InterfaceClone { /// Sets the executable bytecode at a target address. /// The target address must exist and the current context must have access rights. - fn raw_set_bytecode_for(&self, address: &str, bytecode: &[u8]) -> Result<()> { - unimplemented!("raw_set_bytecode_for") - } - - /// Sets the executable bytecode at a current address. - fn raw_set_bytecode(&self, bytecode: &[u8]) -> Result<()> { - unimplemented!("raw_set_bytecode") + fn set_bytecode(&self, address: &str, bytecode: &[u8]) -> Result<()> { + unimplemented!("set_bytecode") } /// Requires a new address that contains the sent &[u8] @@ -197,99 +175,55 @@ pub trait Interface: Send + Sync + InterfaceClone { unimplemented!("create_module") } - /// Print function for examples - fn print(&self, message: &str) -> Result<()> { - unimplemented!("print") - } - /// Return datastore keys /// Will only return keys with a given prefix if provided in args - fn get_keys(&self, prefix: Option<&[u8]>) -> Result>> { - unimplemented!("get_op_keys") - } - - /// Return datastore keys - /// Will only return keys with a given prefix if provided in args - fn get_keys_for(&self, address: &str, prefix: Option<&[u8]>) -> Result>> { - unimplemented!("get_op_keys_for") - } - - /// Return the datastore value of the corresponding key - fn raw_get_data(&self, key: &[u8]) -> Result> { - unimplemented!("raw_get_data") - } - - /// Set the datastore value for the corresponding key - fn raw_set_data(&self, key: &[u8], value: &[u8]) -> Result<()> { - unimplemented!("raw_set_data") - } - - /// Append a value to the current datastore value for the corresponding key - fn raw_append_data(&self, key: &[u8], value: &[u8]) -> Result<()> { - unimplemented!("raw_append_data") - } - - /// Delete a datastore entry - fn raw_delete_data(&self, key: &[u8]) -> Result<()> { - unimplemented!("raw_delete_data") + fn get_ds_keys(&self, address: &str, prefix: &[u8]) -> Result>> { + unimplemented!("get_ds_keys") } /// Requires the data at the address - fn raw_get_data_for(&self, address: &str, key: &[u8]) -> Result> { - unimplemented!("raw_get_data_for") + fn get_ds_value(&self, address: &str, key: &[u8]) -> Result> { + unimplemented!("get_ds_value") } /// Set the datastore value for the corresponding key of the given address - fn raw_set_data_for(&self, address: &str, key: &[u8], value: &[u8]) -> Result<()> { - unimplemented!("raw_set_data_for") + fn set_ds_value(&self, address: &str, key: &[u8], value: &[u8]) -> Result<()> { + unimplemented!("set_ds_value") } /// Append a value to the current datastore value for the corresponding key and the given address - fn raw_append_data_for(&self, address: &str, key: &[u8], value: &[u8]) -> Result<()> { - unimplemented!("raw_append_data_for") + fn append_ds_value(&self, address: &str, key: &[u8], value: &[u8]) -> Result<()> { + unimplemented!("append_ds_value") } /// Delete a datastore entry at of the given address - fn raw_delete_data_for(&self, address: &str, key: &[u8]) -> Result<()> { - unimplemented!("raw_delete_data_for") - } - - /// Requires to replace the data in the current address - /// - /// Note: - /// The execution lib will always use the current context address for the update - fn has_data(&self, key: &[u8]) -> Result { - unimplemented!("has_data") + fn delete_ds_entry(&self, address: &str, key: &[u8]) -> Result<()> { + unimplemented!("delete_ds_entry") } /// Check if a datastore entry exists - fn has_data_for(&self, address: &str, key: &[u8]) -> Result { - unimplemented!("has_data_for") - } - - /// Returns bytecode of the current address - fn raw_get_bytecode(&self) -> Result> { - unimplemented!("raw_get_bytecode") + fn ds_entry_exists(&self, address: &str, key: &[u8]) -> Result { + unimplemented!("ds_entry_exists") } /// Returns bytecode of the target address - fn raw_get_bytecode_for(&self, address: &str) -> Result> { - unimplemented!("raw_get_bytecode_for") + fn get_bytecode(&self, address: &str) -> Result> { + unimplemented!("get_bytecode") } /// Return operation datastore keys - fn get_op_keys(&self) -> Result>> { + fn get_op_keys(&self, prefix: &[u8]) -> Result>> { unimplemented!("get_op_keys") } /// Check if key is in operation datastore - fn has_op_key(&self, key: &[u8]) -> Result { - unimplemented!("has_op_data") + fn op_entry_exists(&self, key: &[u8]) -> Result { + unimplemented!("op_entry_exists") } /// Return operation datastore data for a given key - fn get_op_data(&self, key: &[u8]) -> Result> { - unimplemented!("get_op_data") + fn get_op_value(&self, key: &[u8]) -> Result> { + unimplemented!("get_op_value") } /// Check whether or not the caller has write access in the current context @@ -405,25 +339,3 @@ pub trait Interface: Send + Sync + InterfaceClone { unimplemented!("hash_sha256") } } - -impl dyn Interface { - pub fn get_data(&self, key: &[u8]) -> Result { - Ok(serde_json::from_str::(std::str::from_utf8( - &self.raw_get_data(key)?, - )?)?) - } - - pub fn set_data(&self, key: &[u8], value: &T) -> Result<()> { - self.raw_set_data(key, serde_json::to_string::(value)?.as_bytes()) - } - - pub fn get_data_for(&self, address: &str, key: &[u8]) -> Result { - Ok(serde_json::from_str::(std::str::from_utf8( - &self.raw_get_data_for(address, key)?, - )?)?) - } - - pub fn set_data_for(&self, address: &str, key: &[u8], value: &T) -> Result<()> { - self.raw_set_data_for(address, key, serde_json::to_string::(value)?.as_bytes()) - } -} diff --git a/src/wasmv1_execution/abi/abis.rs b/src/wasmv1_execution/abi/abis.rs index f46ca27f..8abddfe9 100644 --- a/src/wasmv1_execution/abi/abis.rs +++ b/src/wasmv1_execution/abi/abis.rs @@ -14,7 +14,7 @@ pub fn register_abis(store: &mut impl AsStoreMut, shared_abi_env: ABIEnv) -> Imp "abi_call" => Function::new_typed_with_env(store, &fn_env, abi_call), "abi_local_call" => Function::new_typed_with_env(store, &fn_env, abi_local_call), "abi_create_sc" => Function::new_typed_with_env(store, &fn_env, abi_create_sc), - "abi_transfer_coins" => Function::new_typed_with_env(store, &fn_env, abi_transfer_coins), + //"abi_transfer_coins" => Function::new_typed_with_env(store, &fn_env, abi_transfer_coins), "abi_generate_event" => Function::new_typed_with_env(store, &fn_env, abi_generate_event), "abi_function_exists" => Function::new_typed_with_env(store, &fn_env, abi_function_exists), }, @@ -128,6 +128,7 @@ pub(crate) fn abi_create_sc( } /// Function designed to abort execution. +/* pub fn abi_transfer_coins( store_env: FunctionEnvMut, arg_offset: i32, @@ -162,6 +163,7 @@ pub fn abi_transfer_coins( }, ) } +*/ pub fn abi_generate_event( store_env: FunctionEnvMut, @@ -235,15 +237,12 @@ fn helper_get_bytecode( handler: &mut super::handler::ABIHandler, address: String, ) -> Result, WasmV1Error> { - let bytecode = handler - .interface - .raw_get_bytecode_for(&address) - .map_err(|err| { - WasmV1Error::RuntimeError(format!( - "Could not get bytecode for address: {}: {}", - address, err - )) - })?; + let bytecode = handler.interface.get_bytecode(&address).map_err(|err| { + WasmV1Error::RuntimeError(format!( + "Could not get bytecode for address: {}: {}", + address, err + )) + })?; Ok(bytecode) }