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

refactor: remove testnet extension #166

Closed
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: 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_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 Expand Up @@ -53,7 +57,7 @@
pub struct PopApiExtension;

/// Extract (version, function_id, pallet_index, call_index) from the payload bytes
fn extract_env<T, E: Ext>(env: &Environment<E, BufInBufOutState>) -> (u8, u8, u8, u8)

Check warning on line 60 in runtime/extensions/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

bound is defined in more than one place

warning: bound is defined in more than one place --> runtime/extensions/src/lib.rs:60:19 | 60 | fn extract_env<T, E: Ext>(env: &Environment<E, BufInBufOutState>) -> (u8, u8, u8, u8) | ^ 61 | where 62 | E: Ext<T = T>, | ^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_bound_locations = note: `#[warn(clippy::multiple_bound_locations)]` on by default
where
E: Ext<T = T>,
{
Expand Down Expand Up @@ -84,7 +88,7 @@
T: PopApiExtensionConfig + pallet_contracts::Config,
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
{
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>

Check warning on line 91 in runtime/extensions/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

bound is defined in more than one place

warning: bound is defined in more than one place --> runtime/extensions/src/lib.rs:91:10 | 91 | fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError> | ^ 92 | where 93 | E: Ext<T = T>, | ^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_bound_locations
where
E: Ext<T = T>,
{
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
Loading