Skip to content

Commit

Permalink
refactor: remove testnet extension
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Aug 7, 2024
1 parent a7fd45b commit d21aed5
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 205 deletions.
3 changes: 1 addition & 2 deletions runtime/devnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ hex = "0.4.3"
rand = "0.8.5"

[features]
default = ["std"]
default = ["std", "pop-runtime-extensions/pop-devnet"]
std = [
"codec/std",
"cumulus-pallet-aura-ext/std",
Expand Down Expand Up @@ -142,7 +142,6 @@ std = [
"polkadot-runtime-common/std",
"pop-primitives/std",
"pop-runtime-extensions/std",
"pop-runtime-extensions/pop-devnet",
"scale-info/std",
"sp-api/std",
"sp-io/std",
Expand Down
1 change: 1 addition & 0 deletions runtime/extensions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ homepage.workspace = true
repository.workspace = true
edition.workspace = true
publish = false
resolver = "2"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
2 changes: 0 additions & 2 deletions runtime/extensions/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#[cfg(feature = "pop-devnet")]
pub(crate) mod devnet;
#[cfg(feature = "pop-testnet")]
pub(crate) mod testnet;
4 changes: 4 additions & 0 deletions runtime/extensions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ use sp_core::crypto::UncheckedFrom;
use sp_runtime::{traits::Dispatchable, DispatchError};
use sp_std::vec::Vec;

#[cfg(all(feature = "pop-devnet", feature = "pop-testnet"))]
compile_error!(
"Feature \"pop-devnet\" and feature \"pop-testnet\" cannot be enabled at the same time."
);
// Conditionally use different implementations of `PopApiExtensionConfig` based on the active feature.
cfg_if::cfg_if! {
if #[cfg(feature = "pop-devnet")] {
Expand Down
3 changes: 1 addition & 2 deletions runtime/testnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ env_logger = "0.11.2"
hex = "0.4.3"

[features]
default = ["std"]
default = ["std", "pop-runtime-extensions/pop-testnet"]
std = [
"codec/std",
"cumulus-pallet-aura-ext/std",
Expand Down Expand Up @@ -139,7 +139,6 @@ std = [
"polkadot-runtime-common/std",
"pop-primitives/std",
"pop-runtime-extensions/std",
"pop-runtime-extensions/pop-testnet",
"scale-info/std",
"sp-api/std",
"sp-io/std",
Expand Down
6 changes: 3 additions & 3 deletions runtime/testnet/src/config/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
deposit, extensions, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall,
RuntimeEvent, RuntimeHoldReason, Timestamp,
deposit, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall, RuntimeEvent,
RuntimeHoldReason, Timestamp,
};
use frame_support::{
parameter_types,
Expand Down Expand Up @@ -63,7 +63,7 @@ impl pallet_contracts::Config for Runtime {
type CallStack = [pallet_contracts::Frame<Self>; 23];
type WeightPrice = pallet_transaction_payment::Pallet<Self>;
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
type ChainExtension = extensions::PopApiExtension;
type ChainExtension = pop_runtime_extensions::PopApiExtension;
type Schedule = Schedule;
type AddressGenerator = pallet_contracts::DefaultAddressGenerator;
// This node is geared towards development and testing of contracts.
Expand Down
112 changes: 112 additions & 0 deletions runtime/testnet/src/config/extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use crate::{Runtime, RuntimeCall};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::traits::OriginTrait;
use frame_support::{ensure, traits::Contains};
use frame_system::RawOrigin;
use pallet_contracts::chain_extension::{BufInBufOutState, Environment, Ext};
use pop_runtime_extensions::{
constants::{DECODING_FAILED_ERROR, LOG_TARGET, UNKNOWN_CALL_ERROR},
dispatch_call, CallDispatchHandler, PopApiExtensionConfig, StateReadHandler,
};
use sp_core::Get;
use sp_runtime::DispatchError;

/// A query of runtime state.
#[derive(Encode, Decode, Debug, MaxEncodedLen)]
pub enum RuntimeRead {}

/// A type to identify allowed calls to the Runtime from the API.
pub struct AllowedApiCalls;

impl Contains<RuntimeCall> for AllowedApiCalls {
/// Allowed runtime calls from the API.
fn contains(_: &RuntimeCall) -> bool {
false
}
}

impl Contains<RuntimeRead> for AllowedApiCalls {
/// Allowed state queries from the API.
fn contains(_: &RuntimeRead) -> bool {
false
}
}

/// Wrapper to enable versioning of runtime state reads.
#[derive(Decode, Debug)]
enum VersionedStateRead {
/// Version zero of state reads.
#[codec(index = 0)]
V0(RuntimeRead),
}

/// Wrapper to enable versioning of runtime calls.
#[derive(Decode, Debug)]
enum VersionedDispatch<T: PopApiExtensionConfig> {
/// Version zero of dispatch calls.
#[codec(index = 0)]
V0(T::RuntimeCall),
}

pub struct ContractExecutionContext;

impl CallDispatchHandler for ContractExecutionContext {
fn handle_params<T, E>(
env: &mut Environment<E, BufInBufOutState>,
params: Vec<u8>,
) -> Result<(), DispatchError>
where
E: Ext<T = T>,
T: PopApiExtensionConfig,
{
const LOG_PREFIX: &str = " dispatch |";

let call =
<VersionedDispatch<T>>::decode(&mut &params[..]).map_err(|_| DECODING_FAILED_ERROR)?;

// Contract is the origin by default.
let mut origin: T::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into();
match call {
VersionedDispatch::V0(call) => {
origin.add_filter(T::AllowedDispatchCalls::contains);
dispatch_call::<T, E>(env, call, origin, LOG_PREFIX)
},
}
}
}

impl StateReadHandler for ContractExecutionContext {
fn handle_params<T, E>(
env: &mut Environment<E, BufInBufOutState>,
params: Vec<u8>,
) -> Result<(), DispatchError>
where
E: Ext<T = T>,
T: PopApiExtensionConfig,
{
const LOG_PREFIX: &str = " read_state |";

let read =
<VersionedStateRead>::decode(&mut &params[..]).map_err(|_| DECODING_FAILED_ERROR)?;

// Charge weight for doing one storage read.
env.charge_weight(T::DbWeight::get().reads(1_u64))?;
let result = match read {
VersionedStateRead::V0(read) => {
ensure!(AllowedApiCalls::contains(&read), UNKNOWN_CALL_ERROR);
vec![0u8]
},
};
log::trace!(
target:LOG_TARGET,
"{} result: {:?}.", LOG_PREFIX, result
);
env.write(&result, false, None)
}
}

impl PopApiExtensionConfig for Runtime {
type StateReadHandler = ContractExecutionContext;
type CallDispatchHandler = ContractExecutionContext;
type AllowedDispatchCalls = AllowedApiCalls;
}
1 change: 1 addition & 0 deletions runtime/testnet/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod assets;
mod contracts;
mod extension;
mod proxy;
// Public due to integration tests crate.
pub mod xcm;
195 changes: 0 additions & 195 deletions runtime/testnet/src/extensions.rs

This file was deleted.

1 change: 0 additions & 1 deletion runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

mod config;
mod extensions;
mod weights;

use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
Expand Down

0 comments on commit d21aed5

Please sign in to comment.