Skip to content

Commit

Permalink
wip: intermediate
Browse files Browse the repository at this point in the history
  • Loading branch information
mustermeiszer committed Feb 6, 2024
1 parent f731286 commit 82aaa1a
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 34 deletions.
29 changes: 22 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jsonrpsee = { version = "0.16.2", features = ["server", "macros"] }
url = "2.2.2"
tempfile = "3.1.0"
strum = { version = "0.24", default-features = false, features = ["derive"] }
serde_json = "1.0.107"

# Cumulus
cumulus-pallet-aura-ext = { git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.43" }
Expand Down
3 changes: 2 additions & 1 deletion runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ documentation.workspace = true
build = "build.rs"

[dependencies]
ethabi = { workspace = true, features = ["std"] }
ethabi = { workspace = true, features = ["std", "full-serde"] }
ethereum = { workspace = true, features = ["std"] }
fudge = { workspace = true }
fudge-core = { workspace = true }
Expand All @@ -23,6 +23,7 @@ serde = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing-subscriber = { workspace = true }
serde_json = { workspace = true }

node-primitives = { workspace = true, features = ["std"] }
sc-block-builder = { workspace = true }
Expand Down
8 changes: 1 addition & 7 deletions runtime/integration-tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@ fn main() {
.expect("OsStr is utf-8. qed");

match Command::new("forge")
.args(&[
"build",
"--extra-output-files",
"abi",
"--out",
out_dir_build,
])
.args(&["build", "--out", out_dir_build])
.output()
{
Ok(o) if o.status.success() => {
Expand Down
29 changes: 29 additions & 0 deletions runtime/integration-tests/src/generic/cases/lp/deploy_pool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use crate::{
generic::{config::Runtime, env::EvmEnv, envs::runtime_env::RuntimeEnv},
utils::accounts::Keyring,
};

#[test]
fn _test() {
deploy::<centrifuge_runtime::Runtime>()
}

fn deploy<T: Runtime>() {
let mut env = RuntimeEnv::<T>::default().load_contracts();

env.deploy("AxelarScript", "deployer", Keyring::Alice, None);
}

crate::test_for_runtimes!(all, deploy);
15 changes: 15 additions & 0 deletions runtime/integration-tests/src/generic/cases/lp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

mod utils {}

pub mod deploy_pool;
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/generic/cases/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use xcm::{

use crate::{
generic::{
cases::liquidity_pools::utils::setup_xcm,
cases::liquidity_pools_transfers::utils::setup_xcm,
config::Runtime,
env::Env,
envs::{
Expand Down
4 changes: 4 additions & 0 deletions runtime/integration-tests/src/generic/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ pub trait Runtime:
+ pallet_evm_chain_id::Config
+ pallet_remarks::Config<RuntimeCall = Self::RuntimeCallExt, Remark = Remark>
+ pallet_utility::Config<RuntimeCall = Self::RuntimeCallExt>
+ pallet_evm::Config<
Runner = pallet_evm::runner::stack::Runner<Self>,
Currency = pallet_balances::Pallet<Self>,
>
{
/// Just the RuntimeCall type, but redefined with extra bounds.
/// You can add `From` bounds in order to convert pallet calls to
Expand Down
19 changes: 19 additions & 0 deletions runtime/integration-tests/src/generic/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cfg_primitives::{Address, Balance, BlockNumber, Index};
use cfg_traits::{IntoSeconds, Seconds};
use ethabi::Token;
use parity_scale_codec::Encode;
use sp_runtime::{
generic::{Era, SignedPayload},
Expand Down Expand Up @@ -158,6 +159,24 @@ pub trait Env<T: Runtime>: Default {
fn __priv_build_block(&mut self, i: BlockNumber);
}

pub trait EvmEnv<T: Runtime>: Env<T> {
fn load_contracts(self) -> Self;

fn deploy(
&mut self,
what: impl Into<String>,
name: impl Into<String>,
who: Keyring,
args: Option<&[Token]>,
);

fn call(&mut self);

fn mut_call(&mut self);

fn view(&self);
}

pub mod utils {
use super::*;

Expand Down
89 changes: 87 additions & 2 deletions runtime/integration-tests/src/generic/envs/runtime_env.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use std::{cell::RefCell, marker::PhantomData, mem, rc::Rc};
use std::{cell::RefCell, collections::HashMap, marker::PhantomData, mem, rc::Rc};

use cfg_primitives::{AuraId, Balance, BlockNumber, Header};
use cfg_types::ParaId;
use cumulus_primitives_core::PersistedValidationData;
use cumulus_primitives_parachain_inherent::ParachainInherentData;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use ethabi::{
ethereum_types::{H160, U256},
Contract, Token,
};
use frame_support::{
dispatch::GetDispatchInfo,
inherent::{InherentData, ProvideInherent},
traits::GenesisBuild,
};
use pallet_evm::{FeeCalculator, Runner};
use parity_scale_codec::Encode;
use sp_api::runtime_decl_for_core::CoreV4;
use sp_block_builder::runtime_decl_for_block_builder::BlockBuilderV6;
Expand All @@ -25,7 +30,8 @@ use sp_timestamp::Timestamp;
use crate::{
generic::{
config::Runtime,
env::{utils, Env},
env::{utils, Env, EvmEnv},
utils::{evm, evm::ContractInfo, ESSENTIAL},
},
utils::accounts::Keyring,
};
Expand All @@ -37,6 +43,8 @@ pub struct RuntimeEnv<T: Runtime> {
sibling_ext: Rc<RefCell<sp_io::TestExternalities>>,
pending_extrinsics: Vec<(Keyring, T::RuntimeCallExt)>,
pending_xcm: Vec<(ParaId, Vec<u8>)>,
sol_contracts: Option<HashMap<String, ContractInfo>>,
deployed_contracts: HashMap<String, (H160, Contract)>,
_config: PhantomData<T>,
}

Expand All @@ -46,6 +54,81 @@ impl<T: Runtime> Default for RuntimeEnv<T> {
}
}

const GAS_LIMIT: u64 = 5_000_000;
const VALIDATE: bool = true;
const TRANSACTIONAL: bool = true;

impl<T: Runtime> EvmEnv<T> for RuntimeEnv<T> {
fn load_contracts(mut self) -> Self {
self.sol_contracts = Some(evm::fetch_contracts());
self
}

fn deploy(
&mut self,
what: impl Into<String>,
name: impl Into<String>,
who: Keyring,
args: Option<&[Token]>,
) {
let info = self
.sol_contracts
.expect("Need to load_contracts first")
.get(&what.into())
.expect("Unknown contract");

let init = match (info.contract.constructor(), args) {
(None, None) => info.bytecode.to_vec(),
(Some(constructor), Some(args)) => constructor
.encode_input(info.bytecode.to_vec(), args)
.expect(ESSENTIAL),
(Some(constructor), None) => constructor
.encode_input(info.bytecode.to_vec(), &[])
.expect(ESSENTIAL),
(None, Some(_)) => panic!("Contract expects constructor arguments."),
};

let address = self.parachain_state_mut(|| {
let (base_fee, _) = <T as pallet_evm::Config>::FeeCalculator::min_gas_price();

<T as pallet_evm::Config>::Runner::create(
who.into(),
init,
0u8.into(),
GAS_LIMIT,
Some(base_fee),
None,
None,
Vec::new(),
// NOTE: Taken from pallet-evm implementation
VALIDATE,
// NOTE: Taken from pallet-evm implementation
TRANSACTIONAL,
None,
None,
<T as pallet_evm::Config>::config(),
)
.expect(ESSENTIAL)
.value
});

self.deployed_contracts
.insert(name.into(), (H160::from(address.0), info.contract.clone()));
}

fn call(&mut self) {
todo!()
}

fn mut_call(&mut self) {
todo!()
}

fn view(&self) {
todo!()
}
}

impl<T: Runtime> Env<T> for RuntimeEnv<T> {
fn from_parachain_storage(parachain_storage: Storage) -> Self {
Self::from_storage(Default::default(), parachain_storage, Default::default())
Expand Down Expand Up @@ -83,6 +166,8 @@ impl<T: Runtime> Env<T> for RuntimeEnv<T> {
sibling_ext: Rc::new(RefCell::new(sibling_ext)),
pending_extrinsics: Vec::default(),
pending_xcm: Vec::default(),
sol_contracts: None,
deployed_contracts: HashMap::new(),
_config: PhantomData,
}
}
Expand Down
Loading

0 comments on commit 82aaa1a

Please sign in to comment.