diff --git a/concordium-std/src/prims.rs b/concordium-std/src/prims.rs index efb88c9f..2fdd333c 100644 --- a/concordium-std/src/prims.rs +++ b/concordium-std/src/prims.rs @@ -205,7 +205,7 @@ extern "C" { pub fn state_entry_resize(entry: u64, new_size: u32) -> u32; // Getter for the init context. - /// Address of the sender, 32 bytes + /// Address of the sender, AccountAddress. pub fn get_init_origin(start: *mut u8); // Getters for the receive context @@ -215,7 +215,7 @@ extern "C" { pub fn get_receive_self_address(start: *mut u8); /// Self-balance of the contract, returns the amount pub fn get_receive_self_balance() -> u64; - /// Immediate sender of the message (either contract or account). + /// Immediate sender of the message, Address (either contract or account). pub fn get_receive_sender(start: *mut u8); /// Owner of the contract, AccountAddress. pub fn get_receive_owner(start: *mut u8); @@ -305,10 +305,19 @@ extern "C" { #[cfg(all(feature = "wasm-test", target_arch = "wasm32"))] pub(crate) fn set_parameter(i: u32, start: *const u8, length: u32); - /// Sets the address of the sender, 32 bytes + /// Sets the address of the sender, AccountAddress #[cfg(all(feature = "wasm-test", target_arch = "wasm32"))] pub(crate) fn set_init_origin(start: *const u8); + /// Set the invoker of the top-level transaction, AccountAddress. + #[cfg(all(feature = "wasm-test", target_arch = "wasm32"))] + pub(crate) fn set_receive_invoker(start: *const u8); + + /// Set the immediate sender of the message, Address (either contract or account). + #[cfg(all(feature = "wasm-test", target_arch = "wasm32"))] + pub(crate) fn set_receive_sender(start: *const u8); + + /// Gets event number `i` in the smart contract state. Returns `-1` if `i` /// is an invalid index. Otherwise returns bytes written. #[cfg(all(feature = "wasm-test", target_arch = "wasm32"))] diff --git a/concordium-std/src/test_env.rs b/concordium-std/src/test_env.rs index 8954ea4b..6e80145d 100644 --- a/concordium-std/src/test_env.rs +++ b/concordium-std/src/test_env.rs @@ -1,6 +1,4 @@ -use concordium_contracts_common::{ - AccountAddress, Amount, ContractAddress, Serial, SlotTime, -}; +use concordium_contracts_common::{AccountAddress, Amount, ContractAddress, Serial, SlotTime, Address}; use crate::{prims, to_bytes}; @@ -14,13 +12,13 @@ impl TestEnv { self } - /// Sets the current balance of this smart contract + /// Sets the current balance of this smart contract. pub fn set_receive_balance(self, balance: Amount) -> Self { unsafe { prims::set_receive_self_balance(balance.micro_ccd) }; self } - /// Sets the address of this smart contract + /// Sets the address of this smart contract. pub fn set_receive_self_address(self, address: ContractAddress) -> Self { unsafe { prims::set_receive_self_address(to_bytes(&address).as_ptr()) }; self @@ -52,10 +50,25 @@ impl TestEnv { } } + /// Set the address of the sender. pub fn set_init_origin(self, address: AccountAddress) -> Self { unsafe { prims::set_init_origin(address.0.as_ptr()) }; self } + + /// Set the invoker of the top-level transaction. + pub fn set_receive_invoker(self, address: AccountAddress) -> Self { + unsafe { prims::set_receive_invoker(address.0.as_ptr()) }; + self + } + + /// Immediate sender of the message (either contract or account). + pub fn set_receive_sender(self, address: Address) -> Self { + let mut buf = Vec::new(); + address.serial(&mut buf).unwrap(); + unsafe { prims::set_receive_sender(buf.as_ptr()) }; + self + } } #[cfg(feature = "internal-wasm-test")] @@ -67,7 +80,9 @@ mod wasm_test { use super::*; use crate::*; + // ----- Helper Functions ----- // Helper functions to get host data, as a real smart contract would + fn extern_host() -> ExternHost<()> { let state_api = ExternStateApi::open(); let state_builder = StateBuilder::open(state_api); @@ -77,6 +92,20 @@ mod wasm_test { } } + fn receive_context() -> ReceiveContext { ExternContext::default() } + + fn init_context() -> InitContext { ExternContext::default() } + + fn account_address() -> AccountAddress { + // 3kBx2h5Y2veb4hZgAJWPrr8RyQESKm5TjzF3ti1QQ4VSYLwK1G + AccountAddress([ + 105, 117, 36, 6, 204, 147, 159, 201, 12, 166, 167, 59, 87, 206, 225, 9, 150, 53, 71, + 249, 66, 0, 109, 33, 145, 68, 146, 79, 132, 133, 251, 13, + ]) + } + + // ----- Tests ----- + #[concordium_test] fn set_get_slot_time() { let extern_chain_meta = ExternChainMeta {}; @@ -96,10 +125,9 @@ mod wasm_test { #[concordium_test] fn set_get_receive_self_address() { - let receive_context: ReceiveContext = ExternContext::default(); let original = ContractAddress::new(5040, 12); TestEnv.set_receive_self_address(original); - let stored = receive_context.self_address(); + let stored = receive_context().self_address(); claim_eq!(original, stored); } @@ -190,17 +218,30 @@ mod wasm_test { #[concordium_test] fn set_get_init_origin() { - let init_context: InitContext = ExternContext::default(); - // 3kBx2h5Y2veb4hZgAJWPrr8RyQESKm5TjzF3ti1QQ4VSYLwK1G - let address = AccountAddress([ - 105, 117, 36, 6, 204, 147, 159, 201, 12, 166, 167, 59, 87, 206, 225, 9, 150, 53, 71, - 249, 66, 0, 109, 33, 145, 68, 146, 79, 132, 133, 251, 13, - ]); + let address = account_address(); TestEnv.set_init_origin(address); + let stored = init_context().init_origin(); + claim_eq!(stored, address); + } - let stored = init_context.init_origin(); - let expected = address; + #[concordium_test] + fn set_get_receive_invoker() { + let address = account_address(); + TestEnv.set_receive_invoker(address); + let stored = receive_context().invoker(); + claim_eq!(stored, address); + } - claim_eq!(stored, expected); + #[concordium_test] + fn set_get_receive_sender() { + let address = Address::Account(account_address()); + TestEnv.set_receive_sender(address); + let stored = receive_context().sender(); + claim_eq!(stored, address); + + let address = Address::Contract(ContractAddress::new(3, 7)); + TestEnv.set_receive_sender(address); + let stored = receive_context().sender(); + claim_eq!(stored, address); } }