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

Integration tests: fudge support for the generic environment #1588

Merged
merged 29 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b6eb261
Add handle and base env structure
lemunozm Oct 10, 2023
ad491e2
evolving and state support for fudge env
lemunozm Oct 10, 2023
6c5f848
Support Client APIs
lemunozm Oct 10, 2023
fa48bea
simplify test cases
lemunozm Oct 10, 2023
5f17c11
Add spetial fudge methos
lemunozm Oct 10, 2023
c45a04f
Add T to FudgeHandle
lemunozm Oct 10, 2023
12beb07
sumbmit extrinsic works
lemunozm Oct 11, 2023
170b4a2
use correct nonce
lemunozm Oct 11, 2023
b6c8075
minor Env trait simplification
lemunozm Oct 11, 2023
3032d09
minor example reorder
lemunozm Oct 11, 2023
084d148
fudge support for all runtimes
lemunozm Oct 11, 2023
c94c1ad
fix nonce and divide api between submit now and later
lemunozm Oct 13, 2023
2fb97d5
minor organization
lemunozm Oct 13, 2023
bf6269a
init tests for loans
lemunozm Oct 13, 2023
21577d0
fix ongoing loans test
lemunozm Oct 14, 2023
f998636
fix compilation
lemunozm Oct 16, 2023
848339d
Merge remote-tracking branch 'origin/main' into integration-tests/fud…
lemunozm Oct 16, 2023
4b159c5
minor renames
lemunozm Oct 16, 2023
5d056ab
restore expect
lemunozm Oct 16, 2023
ef4d158
loan testing in progress
lemunozm Oct 17, 2023
774c241
Merge remote-tracking branch 'origin/main' into integration-tests/fud…
lemunozm Oct 18, 2023
ab7138b
fixed issue with asset_registry
lemunozm Oct 18, 2023
5e79525
add CurrencyInfo genesis utility
lemunozm Oct 18, 2023
93a0dda
extend loan tests, fix block by seconds
lemunozm Oct 20, 2023
6b29b2b
Api from Runtime into an associated type for expliciteness
lemunozm Oct 20, 2023
83bd44c
minor changes
lemunozm Oct 20, 2023
ed92aab
fix and extend loan testing
lemunozm Oct 20, 2023
a496e35
fix and extend loan testing
lemunozm Oct 20, 2023
744f59f
remove unused pallet sudo addition
lemunozm Oct 20, 2023
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: 3 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ pallet-uniques = { git = "https://github.com/paritytech/substrate", default-feat
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
pallet-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }

pallet-sudo = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }

## Substrate-Primitives
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
#sp-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-block-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-consensus-slots = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
Expand All @@ -50,6 +51,7 @@ fp-self-contained = { git = "https://github.com/PureStake/frontier", branch = "m
## Substrate-Client
node-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
#sc-consensus-uncles = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sc-service = { git = "https://github.com/paritytech/substrate", features = ["rocksdb", "test-helpers"], branch = "polkadot-v0.9.38" }
Expand Down
139 changes: 90 additions & 49 deletions runtime/integration-tests/src/generic/cases/example.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use cfg_primitives::{AuraId, Balance, CFG};
use cfg_primitives::{Balance, CFG};
use frame_support::traits::Get;

use crate::{
generic::{
environment::{Blocks, Env},
envs::runtime_env::RuntimeEnv,
envs::{
fudge_env::{FudgeEnv, FudgeSupport},
runtime_env::RuntimeEnv,
},
runtime::Runtime,
utils::genesis::Genesis,
},
Expand All @@ -16,11 +19,10 @@ fn transfer_balance<T: Runtime>() {
const FOR_FEES: Balance = 1 * CFG;

// Set up all GenesisConfig for your initial state
// You can choose `RuntimeEnv` by `FudgeEnv` to make it working with fudge
// environment.
let mut env = RuntimeEnv::<T>::from_storage(
Genesis::default()
.add(pallet_aura::GenesisConfig::<T> {
authorities: vec![AuraId::from(Keyring::Charlie.public())],
})
.add(pallet_balances::GenesisConfig::<T> {
balances: vec![(
Keyring::Alice.to_account_id(),
Expand All @@ -31,7 +33,57 @@ fn transfer_balance<T: Runtime>() {
);

// Call an extrinsic that would be processed immediately
env.submit(
let fee = env
.submit_now(
Keyring::Alice,
pallet_balances::Call::transfer {
dest: Keyring::Bob.into(),
value: TRANSFER,
},
)
.unwrap();

// Check for an even occurred in this block
env.check_event(pallet_balances::Event::Transfer {
from: Keyring::Alice.to_account_id(),
to: Keyring::Bob.to_account_id(),
amount: TRANSFER,
})
.unwrap();

// Check the state
env.state(|| {
assert_eq!(
pallet_balances::Pallet::<T>::free_balance(Keyring::Alice.to_account_id()),
T::ExistentialDeposit::get() + FOR_FEES - fee,
);
assert_eq!(
pallet_balances::Pallet::<T>::free_balance(Keyring::Bob.to_account_id()),
TRANSFER
);
});

// Pass blocks to evolve the system
env.pass(Blocks::ByNumber(1));
}

// Identical to `transfer_balance()` test but using fudge.
fn fudge_transfer_balance<T: Runtime + FudgeSupport>() {
const TRANSFER: Balance = 1000 * CFG;
const FOR_FEES: Balance = 1 * CFG;

let mut env = FudgeEnv::<T>::from_storage(
Genesis::default()
.add(pallet_balances::GenesisConfig::<T> {
balances: vec![(
Keyring::Alice.to_account_id(),
T::ExistentialDeposit::get() + FOR_FEES + TRANSFER,
)],
})
.storage(),
);

env.submit_later(
Keyring::Alice,
pallet_balances::Call::transfer {
dest: Keyring::Bob.into(),
Expand All @@ -40,6 +92,9 @@ fn transfer_balance<T: Runtime>() {
)
.unwrap();

// submit-later will only take effect if a block has passed
env.pass(Blocks::ByNumber(1));

// Check for an even occurred in this block
env.check_event(pallet_balances::Event::Transfer {
from: Keyring::Alice.to_account_id(),
Expand All @@ -48,11 +103,22 @@ fn transfer_balance<T: Runtime>() {
})
.unwrap();

// Pass blocks to evolve the system
env.pass(Blocks::ByNumber(1));
// Look for the fee for the last transaction
let fee = env
.find_event(|e| match e {
pallet_transaction_payment::Event::TransactionFeePaid { actual_fee, .. } => {
Some(actual_fee)
}
_ => None,
})
.unwrap();

// Check the state
env.state(|| {
assert_eq!(
pallet_balances::Pallet::<T>::free_balance(Keyring::Alice.to_account_id()),
T::ExistentialDeposit::get() + FOR_FEES - fee,
);
assert_eq!(
pallet_balances::Pallet::<T>::free_balance(Keyring::Bob.to_account_id()),
TRANSFER
Expand All @@ -61,13 +127,7 @@ fn transfer_balance<T: Runtime>() {
}

fn call_api<T: Runtime>() {
let env = RuntimeEnv::<T>::from_storage(
Genesis::default()
.add(pallet_aura::GenesisConfig::<T> {
authorities: vec![AuraId::from(Keyring::Charlie.public())],
})
.storage(),
);
let env = RuntimeEnv::<T>::from_storage(Default::default());

env.state(|| {
// Call to Core::version() API.
Expand All @@ -77,42 +137,23 @@ fn call_api<T: Runtime>() {
})
}

fn check_fee<T: Runtime>() {
let mut env = RuntimeEnv::<T>::from_storage(
Genesis::default()
.add(pallet_aura::GenesisConfig::<T> {
authorities: vec![AuraId::from(Keyring::Charlie.public())],
})
.add(pallet_balances::GenesisConfig::<T> {
balances: vec![(Keyring::Alice.to_account_id(), 1 * CFG)],
})
.storage(),
);
fn fudge_call_api<T: Runtime + FudgeSupport>() {
let env = FudgeEnv::<T>::from_storage(Default::default());

env.submit(
Keyring::Alice,
frame_system::Call::remark { remark: vec![] },
)
.unwrap();
// Exclusive from fudge environment.
// It uses a client to access the runtime api.
env.with_api(|api, latest| {
// We include the API we want to use
use sp_api::Core;

// Get the fee of the last submitted extrinsic
let fee = env.last_fee();
let result = api.version(&latest).unwrap();

env.state(|| {
assert_eq!(
pallet_balances::Pallet::<T>::free_balance(Keyring::Alice.to_account_id()),
1 * CFG - fee
);
});
assert_eq!(result, T::version());
assert_eq!(result, <T as frame_system::Config>::Version::get());
})
}

// Generate tests for all runtimes
crate::test_for_runtimes!((development, altair, centrifuge), transfer_balance);
crate::test_for_all_runtimes!(call_api);
crate::test_for_all_runtimes!(check_fee);

// Output: for `cargo test -p runtime-integration-tests transfer_balance`
// running 6 tests
// test generic::cases::example::transfer_balance::altair ... ok
// test generic::cases::example::transfer_balance::development ... ok
// test generic::cases::example::transfer_balance::centrifuge ... ok
crate::test_for_runtimes!([development, altair, centrifuge], transfer_balance);
crate::test_for_runtimes!(all, call_api);
crate::test_for_runtimes!(all, fudge_transfer_balance);
crate::test_for_runtimes!(all, fudge_call_api);
55 changes: 55 additions & 0 deletions runtime/integration-tests/src/generic/cases/loans.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use cfg_primitives::{Balance, CollectionId, ItemId, PoolId, CFG};
use frame_support::traits::Get;
use orml_traits::GetByKey;

use crate::{
generic::{
environment::{Blocks, Env},
envs::{
fudge_env::{FudgeEnv, FudgeSupport},
runtime_env::RuntimeEnv,
},
runtime::Runtime,
utils::{
self,
genesis::{self, Genesis, MUSD_CURRENCY_ID},
},
},
utils::accounts::Keyring,
};

const POOL_ADMIN: Keyring = Keyring::Admin;
const INVESTOR: Keyring = Keyring::Alice;
const BORROWER: Keyring = Keyring::Bob;

const FOR_FEES: Balance = 1 * CFG;

const POOL_A: PoolId = 23;
const NFT_A: (CollectionId, ItemId) = (1, ItemId(10));

fn borrow<T: Runtime + FudgeSupport>() {
let mut env = RuntimeEnv::<T>::from_storage(
Genesis::<T>::default()
.add(genesis::balances(T::ExistentialDeposit::get() + FOR_FEES))
.add(genesis::tokens(vec![(
MUSD_CURRENCY_ID,
T::ExistentialDeposits::get(&MUSD_CURRENCY_ID),
)]))
.add(genesis::assets(vec![MUSD_CURRENCY_ID]))
.storage(),
);

env.state_mut(|| {
utils::give_balance_to::<T>(POOL_ADMIN.id(), T::PoolDeposit::get());
utils::give_nft_to::<T>(BORROWER.id(), NFT_A);
});

env.state(|| {
assert_eq!(
pallet_uniques::Pallet::<T>::owner(NFT_A.0, NFT_A.1).unwrap(),
BORROWER.id(),
)
});
}

crate::test_for_runtimes!(all, borrow);
Loading
Loading