Skip to content

Commit

Permalink
Add tests for XVM precompile gas limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Aug 10, 2023
1 parent 5907746 commit be43e09
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions precompiles/utils/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ impl<'p, P: PrecompileSet> PrecompilesTester<'p, P> {
self
}

pub fn with_gas_limit(mut self, gas_limit: u64) -> Self {
self.handle.gas_limit = gas_limit;
self
}

pub fn expect_cost(mut self, cost: u64) -> Self {
self.expected_cost = Some(cost);
self
Expand Down
1 change: 1 addition & 0 deletions precompiles/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
hex = { workspace = true }
log = { workspace = true }
num_enum = { workspace = true }
precompile-utils = { workspace = true }
Expand Down
27 changes: 26 additions & 1 deletion precompiles/xvm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
use sp_std::cell::RefCell;

use astar_primitives::xvm::{CallError::*, CallErrorWithWeight, CallInfo, CallResult};

Expand Down Expand Up @@ -236,10 +237,29 @@ impl pallet_evm::Config for Runtime {
type GasLimitPovSizeRatio = ConstU64<4>;
}

thread_local! {
static WEIGHT_LIMIT: RefCell<Weight> = RefCell::new(Weight::zero());
}

pub(crate) struct WeightLimitCalledWith;
impl WeightLimitCalledWith {
pub(crate) fn get() -> Weight {
WEIGHT_LIMIT.with(|gas_limit| *gas_limit.borrow())
}

pub(crate) fn set(value: Weight) {
WEIGHT_LIMIT.with(|gas_limit| *gas_limit.borrow_mut() = value)
}

pub(crate) fn reset() {
Self::set(Weight::zero());
}
}

struct MockXvmWithArgsCheck;
impl XvmCall<AccountId> for MockXvmWithArgsCheck {
fn call(
_context: Context,
context: Context,
vm_id: VmId,
_source: AccountId,
target: Vec<u8>,
Expand Down Expand Up @@ -267,6 +287,9 @@ impl XvmCall<AccountId> for MockXvmWithArgsCheck {
used_weight: Weight::zero()
}
);

WeightLimitCalledWith::set(context.weight_limit);

Ok(CallInfo {
output: vec![],
used_weight: Weight::zero(),
Expand Down Expand Up @@ -297,6 +320,8 @@ impl ExtBuilder {
.build_storage::<Runtime>()
.expect("Frame system builds valid default genesis config");

WeightLimitCalledWith::reset();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
Expand Down
51 changes: 51 additions & 0 deletions precompiles/xvm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,54 @@ fn correct_arguments_works() {
);
})
}

#[test]
fn weight_limit_is_min_of_remaining_and_user_limit() {
ExtBuilder::default().build().execute_with(|| {
// The caller didn't set a limit.
precompiles()
.prepare_test(
TestAccount::Alice,
PRECOMPILE_ADDRESS,
EvmDataWriter::new_with_selector(Action::XvmCall)
.write(0x1Fu8)
.write(Bytes(
hex::decode("0000000000000000000000000000000000000000")
.expect("invalid hex"),
))
.write(Bytes(b"".to_vec()))
.write(U256::one())
.build(),
)
.expect_no_logs()
.execute_some();
assert_eq!(
WeightLimitCalledWith::get(),
<Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(u64::MAX, true)
);

// The caller set a limit.
let gas_limit = 1_000;
precompiles()
.prepare_test(
TestAccount::Alice,
PRECOMPILE_ADDRESS,
EvmDataWriter::new_with_selector(Action::XvmCall)
.write(0x1Fu8)
.write(Bytes(
hex::decode("0000000000000000000000000000000000000000")
.expect("invalid hex"),
))
.write(Bytes(b"".to_vec()))
.write(U256::one())
.build(),
)
.with_gas_limit(gas_limit)
.expect_no_logs()
.execute_some();
assert_eq!(
WeightLimitCalledWith::get(),
<Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(gas_limit, true)
);
});
}

0 comments on commit be43e09

Please sign in to comment.