Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage deposit limit param to XVM call #1018

Merged
merged 8 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chain-extensions/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ where
}
}
};
let call_result = XC::call(xvm_context, vm_id, source.clone(), to, input, value);
let call_result =
XC::call(xvm_context, vm_id, source.clone(), to, input, value, None);

let actual_weight = match call_result {
Ok(ref info) => info.used_weight,
Expand Down
4 changes: 2 additions & 2 deletions pallets/xvm/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod benchmarks {

#[block]
{
Pallet::<T>::call_without_execution(context, vm_id, source, target, input, value)
Pallet::<T>::call_without_execution(context, vm_id, source, target, input, value, None)
.unwrap();
}
}
Expand All @@ -65,7 +65,7 @@ mod benchmarks {

#[block]
{
Pallet::<T>::call_without_execution(context, vm_id, source, target, input, value)
Pallet::<T>::call_without_execution(context, vm_id, source, target, input, value, None)
.unwrap();
}
}
Expand Down
33 changes: 28 additions & 5 deletions pallets/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,18 @@ where
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
storage_deposit_limit: Option<Balance>,
) -> CallResult {
Pallet::<T>::do_call(context, vm_id, source, target, input, value, false)
Pallet::<T>::do_call(
context,
vm_id,
source,
target,
input,
value,
storage_deposit_limit,
false,
)
}
}

Expand All @@ -128,6 +138,7 @@ where
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
storage_deposit_limit: Option<Balance>,
skip_execution: bool,
) -> CallResult {
let overheads = match vm_id {
Expand Down Expand Up @@ -162,6 +173,7 @@ where
input,
value,
overheads,
storage_deposit_limit,
skip_execution,
),
};
Expand Down Expand Up @@ -267,12 +279,13 @@ where
input: Vec<u8>,
value: Balance,
overheads: Weight,
storage_deposit_limit: Option<Balance>,
skip_execution: bool,
) -> CallResult {
log::trace!(
target: "xvm::wasm_call",
"Calling WASM: {:?} {:?}, {:?}, {:?}, {:?}",
context, source, target, input, value,
"Calling WASM: {:?} {:?}, {:?}, {:?}, {:?}, {:?}",
context, source, target, input, value, storage_deposit_limit,
);

let dest = {
Expand All @@ -295,7 +308,7 @@ where
dest,
value,
weight_limit,
None,
storage_deposit_limit,
input,
DebugInfo::Skip,
CollectEvents::Skip,
Expand Down Expand Up @@ -327,7 +340,17 @@ where
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
storage_deposit_limit: Option<Balance>,
) -> CallResult {
Self::do_call(context, vm_id, source, target, input, value, true)
Self::do_call(
context,
vm_id,
source,
target,
input,
value,
storage_deposit_limit,
true,
)
}
}
26 changes: 19 additions & 7 deletions pallets/xvm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ fn calling_into_same_vm_is_not_allowed() {
ALICE,
evm_target,
input.clone(),
value
value,
None
),
CallFailure::error(SameVmCallDenied, evm_used_weight,),
);
Expand All @@ -61,7 +62,15 @@ fn calling_into_same_vm_is_not_allowed() {
let wasm_used_weight: Weight =
weights::SubstrateWeight::<TestRuntime>::wasm_call_overheads();
assert_noop!(
Xvm::call(wasm_context, wasm_vm_id, ALICE, wasm_target, input, value),
Xvm::call(
wasm_context,
wasm_vm_id,
ALICE,
wasm_target,
input,
value,
None
),
CallFailure::error(SameVmCallDenied, wasm_used_weight,),
);
});
Expand All @@ -86,13 +95,14 @@ fn evm_call_fails_if_target_not_h160() {
ALICE,
ALICE.encode(),
input.clone(),
value
value,
None
),
CallFailure::revert(InvalidTarget, used_weight,),
);

assert_noop!(
Xvm::call(context, vm_id, ALICE, vec![1, 2, 3], input, value),
Xvm::call(context, vm_id, ALICE, vec![1, 2, 3], input, value, None),
CallFailure::revert(InvalidTarget, used_weight,),
);
});
Expand All @@ -117,7 +127,8 @@ fn evm_call_fails_if_input_too_large() {
ALICE,
target.encode(),
vec![1; 65_537],
value
value,
None
),
CallFailure::revert(InputTooLarge, used_weight,),
);
Expand All @@ -142,7 +153,8 @@ fn evm_call_works() {
ALICE,
target.encode(),
input.clone(),
value
value,
None
));
let source = Decode::decode(
&mut hex::decode("f0bd9ffde7f9f4394d8cc1d86bf24d87e5d5a9a9")
Expand Down Expand Up @@ -177,7 +189,7 @@ fn wasm_call_fails_if_invalid_target() {
let used_weight: Weight = weights::SubstrateWeight::<TestRuntime>::wasm_call_overheads();

assert_noop!(
Xvm::call(context, vm_id, ALICE, target.encode(), input, value),
Xvm::call(context, vm_id, ALICE, target.encode(), input, value, None),
CallFailure::revert(InvalidTarget, used_weight,),
);
});
Expand Down
4 changes: 3 additions & 1 deletion precompiles/xvm/evm_sdk/XVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ interface XVM {
* @param to - call recipient
* @param input - SCALE-encoded call arguments
* @param value - value to transfer
* @param storage_deposit_limit - storage deposit limit, use 0 for unlimited.
* @return success - operation outcome
* @return data - output data if successful, error data on error
*/
function xvm_call(
uint8 vm_id,
bytes calldata to,
bytes calldata input,
uint256 value
uint256 value,
uint256 storage_deposit_limit
) external payable returns (bool success, bytes memory data);
}
20 changes: 18 additions & 2 deletions precompiles/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const EVM_ERROR_MSG_SELECTOR: [u8; 4] = [8, 195, 121, 160];
#[precompile_utils::generate_function_selector]
#[derive(Debug, PartialEq)]
pub enum Action {
XvmCall = "xvm_call(uint8,bytes,bytes,uint256)",
XvmCall = "xvm_call(uint8,bytes,bytes,uint256,uint256)",
}

/// A precompile that expose XVM related functions.
Expand Down Expand Up @@ -102,9 +102,25 @@ where
let call_to = input.read::<Bytes>()?.0;
let call_input = input.read::<Bytes>()?.0;
let value = input.read::<Balance>()?;
let storage_deposit_limit = {
let limit = input.read::<Balance>()?;
if limit == 0 {
None
} else {
Some(limit)
}
};
let from = R::AddressMapping::into_account_id(handle.context().caller);

let call_result = XC::call(xvm_context, vm_id, from, call_to, call_input, value);
let call_result = XC::call(
xvm_context,
vm_id,
from,
call_to,
call_input,
value,
storage_deposit_limit,
);

let used_weight = match &call_result {
Ok(s) => s.used_weight,
Expand Down
1 change: 1 addition & 0 deletions precompiles/xvm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ impl XvmCall<AccountId> for MockXvmWithArgsCheck {
target: Vec<u8>,
input: Vec<u8>,
_value: Balance,
_storage_deposit_limit: Option<Balance>,
) -> CallResult {
ensure!(
vm_id != VmId::Evm,
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/xvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,14 @@ pub trait XvmCall<AccountId> {
/// - `target`: Target contract address.
/// - `input`: call input data.
/// - `value`: value to transfer.
/// - `storage_deposit_limit`: storage deposit limit for wasm calls.
fn call(
context: Context,
vm_id: VmId,
source: AccountId,
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
storage_deposit_limit: Option<Balance>,
) -> CallResult;
}
Loading