Skip to content

Commit

Permalink
[pallet-revive] Add pallet to AH westend (#5502)
Browse files Browse the repository at this point in the history
Add pallet-revive to Westend runtime, and configure the runtime to
accept Ethereum signed transaction
  • Loading branch information
pgherveou authored Oct 23, 2024
1 parent b4732ad commit ed23182
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 7 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pallet-timestamp = { workspace = true }
pallet-transaction-payment = { workspace = true }
pallet-transaction-payment-rpc-runtime-api = { workspace = true }
pallet-uniques = { workspace = true }
pallet-revive = { workspace = true }
pallet-utility = { workspace = true }
sp-api = { workspace = true }
sp-block-builder = { workspace = true }
Expand Down Expand Up @@ -129,6 +130,7 @@ runtime-benchmarks = [
"pallet-nft-fractionalization/runtime-benchmarks",
"pallet-nfts/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-revive/runtime-benchmarks",
"pallet-state-trie-migration/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-transaction-payment/runtime-benchmarks",
Expand Down Expand Up @@ -169,6 +171,7 @@ try-runtime = [
"pallet-nft-fractionalization/try-runtime",
"pallet-nfts/try-runtime",
"pallet-proxy/try-runtime",
"pallet-revive/try-runtime",
"pallet-session/try-runtime",
"pallet-state-trie-migration/try-runtime",
"pallet-timestamp/try-runtime",
Expand Down Expand Up @@ -221,6 +224,7 @@ std = [
"pallet-nfts-runtime-api/std",
"pallet-nfts/std",
"pallet-proxy/std",
"pallet-revive/std",
"pallet-session/std",
"pallet-state-trie-migration/std",
"pallet-timestamp/std",
Expand Down
183 changes: 180 additions & 3 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use frame_support::{
fungible, fungibles,
tokens::{imbalance::ResolveAssetTo, nonfungibles_v2::Inspect},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, InstanceFilter,
TransformOrigin,
Nothing, TransformOrigin,
},
weights::{ConstantMultiplier, Weight, WeightToFee as _},
BoundedVec, PalletId,
Expand All @@ -58,13 +58,14 @@ use frame_system::{
};
use pallet_asset_conversion_tx_payment::SwapAssetAdapter;
use pallet_nfts::{DestroyWitness, PalletFeatures};
use pallet_revive::evm::runtime::EthExtra;
use parachains_common::{
impls::DealWithFees, message_queue::*, AccountId, AssetIdForTrustBackedAssets, AuraId, Balance,
BlockNumber, CollectionId, Hash, Header, ItemId, Nonce, Signature, AVERAGE_ON_INITIALIZE_RATIO,
NORMAL_DISPATCH_RATIO,
};
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{AccountIdConversion, BlakeTwo256, Block as BlockT, Saturating, Verify},
Expand Down Expand Up @@ -934,6 +935,52 @@ impl pallet_xcm_bridge_hub_router::Config<ToRococoXcmRouterInstance> for Runtime
type FeeAsset = xcm_config::bridging::XcmBridgeHubRouterFeeAssetId;
}

parameter_types! {
pub const DepositPerItem: Balance = deposit(1, 0);
pub const DepositPerByte: Balance = deposit(0, 1);
pub CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(30);
}

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
<Runtime as frame_system::Config>::Hash,
>;

impl pallet_revive::Config for Runtime {
type Time = Timestamp;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type CallFilter = Nothing;
type DepositPerItem = DepositPerItem;
type DepositPerByte = DepositPerByte;
type WeightPrice = pallet_transaction_payment::Pallet<Self>;
type WeightInfo = pallet_revive::weights::SubstrateWeight<Self>;
type ChainExtension = ();
type AddressMapper = pallet_revive::DefaultAddressMapper;
type RuntimeMemory = ConstU32<{ 128 * 1024 * 1024 }>;
type PVFMemory = ConstU32<{ 512 * 1024 * 1024 }>;
type UnsafeUnstableInterface = ConstBool<false>;
type UploadOrigin = EnsureSigned<Self::AccountId>;
type InstantiateOrigin = EnsureSigned<Self::AccountId>;
type RuntimeHoldReason = RuntimeHoldReason;
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type Debug = ();
type Xcm = pallet_xcm::Pallet<Self>;
type ChainId = ConstU64<420_420_421>;
}

impl TryFrom<RuntimeCall> for pallet_revive::Call<Runtime> {
type Error = ();

fn try_from(value: RuntimeCall) -> Result<Self, Self::Error> {
match value {
RuntimeCall::Revive(call) => Ok(call),
_ => Err(()),
}
}
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime
Expand Down Expand Up @@ -982,6 +1029,7 @@ construct_runtime!(
AssetsFreezer: pallet_assets_freezer::<Instance1> = 57,
ForeignAssetsFreezer: pallet_assets_freezer::<Instance2> = 58,
PoolAssetsFreezer: pallet_assets_freezer::<Instance3> = 59,
Revive: pallet_revive = 60,

StateTrieMigration: pallet_state_trie_migration = 70,

Expand Down Expand Up @@ -1012,9 +1060,34 @@ pub type TxExtension = (
cumulus_primitives_storage_weight_reclaim::StorageWeightReclaim<Runtime>,
frame_metadata_hash_extension::CheckMetadataHash<Runtime>,
);

/// Default extensions applied to Ethereum transactions.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct EthExtraImpl;

impl EthExtra for EthExtraImpl {
type Config = Runtime;
type Extension = TxExtension;

fn get_eth_extension(nonce: u32, tip: Balance) -> Self::Extension {
(
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckMortality::from(generic::Era::Immortal),
frame_system::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
pallet_asset_conversion_tx_payment::ChargeAssetTxPayment::<Runtime>::from(tip, None),
cumulus_primitives_storage_weight_reclaim::StorageWeightReclaim::<Runtime>::new(),
frame_metadata_hash_extension::CheckMetadataHash::<Runtime>::new(false),
)
}
}

/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
pallet_revive::evm::runtime::UncheckedExtrinsic<Address, Signature, EthExtraImpl>;

/// Migrations to apply on runtime upgrade.
pub type Migrations = (
Expand Down Expand Up @@ -1249,6 +1322,7 @@ mod benches {
[cumulus_pallet_xcmp_queue, XcmpQueue]
[pallet_xcm_bridge_hub_router, ToRococo]
[pallet_asset_conversion_ops, AssetConversionMigration]
[pallet_revive, Revive]
// XCM
[pallet_xcm, PalletXcmExtrinsicsBenchmark::<Runtime>]
// NOTE: Make sure you point to the individual modules below.
Expand Down Expand Up @@ -1994,6 +2068,109 @@ impl_runtime_apis! {
PolkadotXcm::is_trusted_teleporter(asset, location)
}
}

impl pallet_revive::ReviveApi<Block, AccountId, Balance, BlockNumber, EventRecord> for Runtime
{
fn eth_transact(
from: H160,
dest: Option<H160>,
value: Balance,
input: Vec<u8>,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
) -> pallet_revive::EthContractResult<Balance>
{
use pallet_revive::AddressMapper;
let blockweights = <Runtime as frame_system::Config>::BlockWeights::get();
let origin = <Runtime as pallet_revive::Config>::AddressMapper::to_account_id(&from);

let encoded_size = |pallet_call| {
let call = RuntimeCall::Revive(pallet_call);
let uxt: UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic::new_bare(call).into();
uxt.encoded_size() as u32
};

Revive::bare_eth_transact(
origin,
dest,
value,
input,
gas_limit.unwrap_or(blockweights.max_block),
storage_deposit_limit.unwrap_or(u128::MAX),
encoded_size,
pallet_revive::DebugInfo::UnsafeDebug,
pallet_revive::CollectEvents::UnsafeCollect,
)
}

fn call(
origin: AccountId,
dest: H160,
value: Balance,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
input_data: Vec<u8>,
) -> pallet_revive::ContractResult<pallet_revive::ExecReturnValue, Balance, EventRecord> {
let blockweights= <Runtime as frame_system::Config>::BlockWeights::get();
Revive::bare_call(
RuntimeOrigin::signed(origin),
dest,
value,
gas_limit.unwrap_or(blockweights.max_block),
storage_deposit_limit.unwrap_or(u128::MAX),
input_data,
pallet_revive::DebugInfo::UnsafeDebug,
pallet_revive::CollectEvents::UnsafeCollect,
)
}

fn instantiate(
origin: AccountId,
value: Balance,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
code: pallet_revive::Code,
data: Vec<u8>,
salt: Option<[u8; 32]>,
) -> pallet_revive::ContractResult<pallet_revive::InstantiateReturnValue, Balance, EventRecord>
{
let blockweights= <Runtime as frame_system::Config>::BlockWeights::get();
Revive::bare_instantiate(
RuntimeOrigin::signed(origin),
value,
gas_limit.unwrap_or(blockweights.max_block),
storage_deposit_limit.unwrap_or(u128::MAX),
code,
data,
salt,
pallet_revive::DebugInfo::UnsafeDebug,
pallet_revive::CollectEvents::UnsafeCollect,
)
}

fn upload_code(
origin: AccountId,
code: Vec<u8>,
storage_deposit_limit: Option<Balance>,
) -> pallet_revive::CodeUploadResult<Balance>
{
Revive::bare_upload_code(
RuntimeOrigin::signed(origin),
code,
storage_deposit_limit.unwrap_or(u128::MAX),
)
}

fn get_storage(
address: H160,
key: [u8; 32],
) -> pallet_revive::GetStorageResult {
Revive::get_storage(
address,
key
)
}
}
}

cumulus_pallet_parachain_system::register_validate_block! {
Expand Down
7 changes: 7 additions & 0 deletions prdoc/pr_5502.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: '[pallet-revive] Add pallet to AH westend'
doc:
- audience: Runtime Dev
description: 'Add pallet-revive to Westend runtime, and configure the runtime to accept Ethereum signed transaction'
crates:
- name: asset-hub-westend-runtime
bump: major

0 comments on commit ed23182

Please sign in to comment.