Skip to content

Commit

Permalink
test(integration): refactor RLP test to use OriginFor. Cleanup commen…
Browse files Browse the repository at this point in the history
…ts and unused imports
  • Loading branch information
peterwht committed Jan 28, 2025
1 parent 4c3f21c commit 0144497
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions integration-tests/public/rlp/e2e_tests.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use super::rlp::*;
use ink_e2e::{
ContractsRegistry,
Keypair,
};
use ink_sandbox::{api::prelude::*, AccountId32, DefaultSandbox, Sandbox};
use ink_sandbox::{api::prelude::*, DefaultSandbox, Sandbox};
use pallet_revive::ExecReturnValue;
use ink::H160;
use ink::primitives::{AccountId, DepositLimit};
use ink_e2e::subxt::tx::Signer;
use ink::primitives::{DepositLimit};
use ink_sandbox::frame_system::pallet_prelude::OriginFor;

const STORAGE_DEPOSIT_LIMIT: DepositLimit<u128> = DepositLimit::Unchecked;

#[test]
fn call_rlp_encoded_message() {
let built_contracts = ::ink_e2e::build_root_and_contract_dependencies();
let contracts = ContractsRegistry::new(built_contracts);

let mut sandbox = ink_e2e::DefaultSandbox::default();
let mut sandbox = DefaultSandbox::default();
let caller = ink_e2e::alice();
let origin = DefaultSandbox::convert_account_to_origin(DefaultSandbox::default_actor());

sandbox
.mint_into(
Expand All @@ -25,10 +26,8 @@ fn call_rlp_encoded_message() {
)
.unwrap_or_else(|_| panic!("Failed to mint tokens"));

let origin = DefaultSandbox::convert_account_to_origin(AccountId32::from(caller.public_key().0));
sandbox.map_account(origin.clone()).expect("unable to map");

// given
let constructor = RlpRef::new(false);
let params = constructor
.endowment(0u32.into())
Expand All @@ -37,18 +36,16 @@ fn call_rlp_encoded_message() {
.params();
let exec_input = params.exec_input();

// TODO: could potentially simplify if helpers are exposed.

let code = contracts.load_code("rlp");
let contract_addr = <DefaultSandbox as ink_sandbox::api::revive_api::ContractAPI>
let contract_addr = <DefaultSandbox as ContractAPI>
::deploy_contract(
&mut sandbox,
code,
0,
ink::scale::Encode::encode(&exec_input),
// salt
None,
origin,
origin.clone(),
<DefaultSandbox as Sandbox>::default_gas_limit(),
STORAGE_DEPOSIT_LIMIT
)
Expand All @@ -57,21 +54,21 @@ fn call_rlp_encoded_message() {

let mut contract = ContractSandbox {
sandbox,
contract_addr: contract_addr,
contract_addr,
};

// set value
contract.call("set_value", true, caller.clone());
contract.call("set_value", true, origin.clone());

// get value
let value: bool =
contract.call_with_return_value("get_value", Vec::<u8>::new(), caller);
contract.call_with_return_value("get_value", Vec::<u8>::new(), origin);

assert!(value, "value should have been set to true");
}

struct ContractSandbox {
sandbox: ink_e2e::DefaultSandbox,
sandbox: DefaultSandbox,
contract_addr: H160,
}

Expand All @@ -80,17 +77,17 @@ impl ContractSandbox {
&mut self,
message: &str,
args: Args,
caller: Keypair,
origin: OriginFor<<DefaultSandbox as Sandbox>::Runtime>,
) -> Ret
where
Args: ink::rlp::Encodable,
Ret: ink::rlp::Decodable,
{
let result = self.call(message, args, caller);
let result = self.call(message, args, origin);
ink::rlp::Decodable::decode(&mut &result[..]).expect("decode failed")
}

fn call<Args>(&mut self, message: &str, args: Args, caller: Keypair) -> Vec<u8>
fn call<Args>(&mut self, message: &str, args: Args, origin: OriginFor<<DefaultSandbox as Sandbox>::Runtime>) -> Vec<u8>
where
Args: ink::rlp::Encodable,
{
Expand All @@ -99,16 +96,15 @@ impl ContractSandbox {
ink::rlp::Encodable::encode(&args, &mut args_buf);
data.append(&mut args_buf);

let result = self.call_raw(data, caller);
let result = self.call_raw(data, origin);
assert!(!result.did_revert(), "'{message}' failed {:?}", result);
result.data
}


fn call_raw(&mut self, data: Vec<u8>, caller: Keypair) -> ExecReturnValue {
let origin = DefaultSandbox::convert_account_to_origin(AccountId32::from(caller.public_key().0));
fn call_raw(&mut self, data: Vec<u8>, origin: OriginFor<<DefaultSandbox as Sandbox>::Runtime>) -> ExecReturnValue {
let result =
<DefaultSandbox as ink_sandbox::api::revive_api::ContractAPI>
<DefaultSandbox as ContractAPI>
::call_contract(
&mut self.sandbox,
self.contract_addr.clone(),
Expand Down

0 comments on commit 0144497

Please sign in to comment.