Skip to content

Commit

Permalink
New type idiom for EthereumTxInput.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 29, 2023
1 parent 1317da5 commit 7ab559e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pallets/ethereum-checked/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use super::*;

use astar_primitives::ethereum_checked::MAX_ETHEREUM_TX_INPUT_SIZE;
use astar_primitives::ethereum_checked::EthereumTxInput;
use frame_benchmarking::v2::*;

#[benchmarks]
Expand All @@ -31,7 +31,7 @@ mod benchmarks {
let target =
H160::from_slice(&hex::decode("dfb975d018f03994a3b943808e3aa0964bd78463").unwrap());
// Calling `store(3)`
let input = BoundedVec::<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>::try_from(
let input = EthereumTxInput::try_from(
hex::decode("6057361d0000000000000000000000000000000000000000000000000000000000000003")
.unwrap(),
)
Expand Down
9 changes: 4 additions & 5 deletions pallets/ethereum-checked/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
use super::*;
use mock::*;

use astar_primitives::ethereum_checked::MAX_ETHEREUM_TX_INPUT_SIZE;
use astar_primitives::ethereum_checked::EthereumTxInput;
use ethereum::{ReceiptV3, TransactionV2 as Transaction};
use frame_support::{assert_noop, assert_ok, traits::ConstU32};
use frame_support::{assert_noop, assert_ok};
use sp_runtime::DispatchError;

fn bounded_input(data: &'static str) -> BoundedVec<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>> {
BoundedVec::<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>::try_from(hex::decode(data).unwrap())
.unwrap()
fn bounded_input(data: &'static str) -> EthereumTxInput {
EthereumTxInput::try_from(hex::decode(data).unwrap()).unwrap()
}

#[test]
Expand Down
17 changes: 6 additions & 11 deletions pallets/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{
ensure,
traits::{ConstU32, Currency},
BoundedVec,
};
use frame_support::{ensure, traits::Currency};
use pallet_contracts::{CollectEvents, DebugInfo, Determinism};
use pallet_evm::GasWeightMapping;
use parity_scale_codec::Decode;
Expand All @@ -51,7 +47,7 @@ use sp_std::{marker::PhantomData, prelude::*};

use astar_primitives::{
ethereum_checked::{
AccountMapping, CheckedEthereumTransact, CheckedEthereumTx, MAX_ETHEREUM_TX_INPUT_SIZE,
AccountMapping, CheckedEthereumTransact, CheckedEthereumTx, EthereumTxInput,
},
xvm::{CallError, CallErrorWithWeight, CallInfo, CallResult, Context, VmId, XvmCall},
Balance,
Expand Down Expand Up @@ -171,11 +167,10 @@ where
error: CallError::InvalidTarget,
used_weight: WeightInfoOf::<T>::evm_call_overheads(),
})?;
let bounded_input = BoundedVec::<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>::try_from(input)
.map_err(|_| CallErrorWithWeight {
error: CallError::InputTooLarge,
used_weight: WeightInfoOf::<T>::evm_call_overheads(),
})?;
let bounded_input = EthereumTxInput::try_from(input).map_err(|_| CallErrorWithWeight {
error: CallError::InputTooLarge,
used_weight: WeightInfoOf::<T>::evm_call_overheads(),
})?;

let value_u256 = U256::from(value);
// With overheads, less weight is available.
Expand Down
3 changes: 1 addition & 2 deletions pallets/xvm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ fn evm_call_works() {
gas_limit: U256::from(182000),
target: H160::repeat_byte(0xFF),
value: U256::from(value),
input: BoundedVec::<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>::try_from(input)
.unwrap(),
input: EthereumTxInput::try_from(input).unwrap(),
maybe_access_list: None,
},
);
Expand Down
4 changes: 3 additions & 1 deletion primitives/src/ethereum_checked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use sp_std::{prelude::*, result::Result};
/// Max Ethereum tx input size: 65_536 bytes
pub const MAX_ETHEREUM_TX_INPUT_SIZE: u32 = 2u32.pow(16);

pub type EthereumTxInput = BoundedVec<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>;

/// The checked Ethereum transaction. Only contracts `call` is support(no `create`).
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub struct CheckedEthereumTx {
Expand All @@ -45,7 +47,7 @@ pub struct CheckedEthereumTx {
/// Amount to transfer.
pub value: U256,
/// Input of a contract call.
pub input: BoundedVec<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>,
pub input: EthereumTxInput,
/// Optional access list, specified in EIP-2930.
pub maybe_access_list: Option<Vec<(H160, Vec<H256>)>>,
}
Expand Down

0 comments on commit 7ab559e

Please sign in to comment.