Skip to content

Commit

Permalink
Integrate bridges initialization logic into humanode-runtime (#719)
Browse files Browse the repository at this point in the history
* Integrate pallet-balanced-currency-swap-bridges-initializer into humanode-runtime

* Use bridges initialization logic in tests

* Add is_balanced check at chain_spec construction

* Separate assert to verify balance currency swap
  • Loading branch information
dmitrylavrenov authored Aug 16, 2023
1 parent 026273a commit f302192
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

24 changes: 19 additions & 5 deletions crates/humanode-peer/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ fn testnet_genesis(
)],
total_claimable: Some(DEV_ACCOUNT_BALANCE),
},
balanced_currency_swap_bridges_initializer: Default::default(),
evm_to_native_swap_bridge: Default::default(),
native_to_evm_swap_bridge: Default::default(),
}
Expand All @@ -406,23 +407,36 @@ mod tests {

use super::*;

fn assert_genesis_config(chain_spec_result: Result<ChainSpec, String>) {
chain_spec_result.unwrap().build_storage().unwrap();
fn assert_genesis_config(
chain_spec_result: Result<ChainSpec, String>,
) -> sp_core::storage::Storage {
chain_spec_result.unwrap().build_storage().unwrap()
}

fn assert_balanced_currency_swap(storage: sp_core::storage::Storage) {
Into::<sp_io::TestExternalities>::into(storage).execute_with(move || {
assert!(
humanode_runtime::BalancedCurrencySwapBridgesInitializer::is_balanced().unwrap()
);
});
}

#[test]
fn local_testnet_config_works() {
assert_genesis_config(local_testnet_config());
let storage = assert_genesis_config(local_testnet_config());
assert_balanced_currency_swap(storage);
}

#[test]
fn development_config_works() {
assert_genesis_config(development_config());
let storage = assert_genesis_config(development_config());
assert_balanced_currency_swap(storage);
}

#[test]
fn benchmark_config_works() {
assert_genesis_config(benchmark_config());
let storage = assert_genesis_config(benchmark_config());
assert_balanced_currency_swap(storage);
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions crates/humanode-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ eip712-account-claim = { version = "0.1", path = "../eip712-account-claim", defa
eip712-common = { version = "0.1", path = "../eip712-common", default-features = false }
eip712-token-claim = { version = "0.1", path = "../eip712-token-claim", default-features = false }
keystore-bioauth-account-id = { version = "0.1", path = "../keystore-bioauth-account-id", default-features = false }
pallet-balanced-currency-swap-bridges-initializer = { version = "0.1", path = "../pallet-balanced-currency-swap-bridges-initializer", default-features = false }
pallet-bioauth = { version = "0.1", path = "../pallet-bioauth", default-features = false }
pallet-bootnodes = { version = "0.1", path = "../pallet-bootnodes", default-features = false }
pallet-bridge-pot-currency-swap = { version = "0.1", path = "../pallet-bridge-pot-currency-swap", default-features = false }
Expand Down Expand Up @@ -157,6 +158,7 @@ std = [
"libsecp256k1/std",
"pallet-authorship/std",
"pallet-babe/std",
"pallet-balanced-currency-swap-bridges-initializer/std",
"pallet-balances/std",
"pallet-base-fee/std",
"pallet-bioauth/std",
Expand Down Expand Up @@ -225,6 +227,7 @@ try-runtime = [
"frame-try-runtime/try-runtime",
"pallet-authorship/try-runtime",
"pallet-babe/try-runtime",
"pallet-balanced-currency-swap-bridges-initializer/try-runtime",
"pallet-balances/try-runtime",
"pallet-base-fee/try-runtime",
"pallet-bioauth/try-runtime",
Expand Down
21 changes: 19 additions & 2 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,22 @@ impl pallet_bridge_pot_currency_swap::Config<BridgeInstanceEvmToNativeSwap> for
type GenesisVerifier = currency_swap::GenesisVerifier;
}

parameter_types! {
pub TreasuryPotAccountId: AccountId = TreasuryPot::account_id();
}

impl pallet_balanced_currency_swap_bridges_initializer::Config for Runtime {
type EvmAccountId = EvmAccountId;
type NativeCurrency = Balances;
type EvmCurrency = EvmBalances;
type BalanceConverterEvmToNative = Identity;
type BalanceConverterNativeToEvm = Identity;
type NativeEvmBridgePot = NativeToEvmSwapBridgePotAccountId;
type NativeTreasuryPot = TreasuryPotAccountId;
type EvmNativeBridgePot = EvmToNativeSwapBridgePotAccountId;
type WeightInfo = ();
}

// Create the runtime by composing the FRAME pallets that were previously
// configured.
construct_runtime!(
Expand Down Expand Up @@ -859,8 +875,9 @@ construct_runtime!(
NativeToEvmSwapBridgePot: pallet_pot::<Instance4> = 33,
EvmToNativeSwapBridgePot: pallet_pot::<Instance5> = 34,
CurrencySwap: pallet_currency_swap = 35,
NativeToEvmSwapBridge: pallet_bridge_pot_currency_swap::<Instance1> = 36,
EvmToNativeSwapBridge: pallet_bridge_pot_currency_swap::<Instance2> = 37,
BalancedCurrencySwapBridgesInitializer: pallet_balanced_currency_swap_bridges_initializer = 36,
NativeToEvmSwapBridge: pallet_bridge_pot_currency_swap::<Instance1> = 37,
EvmToNativeSwapBridge: pallet_bridge_pot_currency_swap::<Instance2> = 38,
}
);

Expand Down
11 changes: 4 additions & 7 deletions crates/humanode-runtime/src/tests/claims_and_vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ fn new_test_ext() -> sp_io::TestExternalities {
let config = GenesisConfig {
balances: BalancesConfig {
balances: {
let pot_accounts = pot_accounts.clone();
endowed_accounts
.iter()
.cloned()
Expand Down Expand Up @@ -188,12 +187,9 @@ fn new_test_ext() -> sp_io::TestExternalities {
vec![(
EvmToNativeSwapBridgePot::account_id(),
fp_evm::GenesisAccount {
balance: (INIT_BALANCE * (endowed_accounts.len() + pot_accounts.len()) as u128 +
// Own bridge pot minimum balance.
<EvmBalances as frame_support::traits::Currency<EvmAccountId>>::minimum_balance() +
// `TokenClaimsPot` minimum balance.
2 * VESTING_BALANCE + <Balances as frame_support::traits::Currency<AccountId>>::minimum_balance()
)
balance: <EvmBalances as frame_support::traits::Currency<
EvmAccountId,
>>::minimum_balance()
.into(),
code: Default::default(),
nonce: Default::default(),
Expand Down Expand Up @@ -354,6 +350,7 @@ fn prepare_genesis_json(token_claims: &str, token_claim_pot_balance: u128) -> St
"evmToNativeSwapBridgePot": {{
"initialState": "Initialized"
}},
"balancedCurrencySwapBridgesInitializer": null,
"nativeToEvmSwapBridge": null,
"evmToNativeSwapBridge": null
}}"#
Expand Down
36 changes: 25 additions & 11 deletions crates/humanode-runtime/src/tests/currency_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ fn new_test_ext_with() -> sp_io::TestExternalities {
let bootnodes = vec![account_id("Alice")];

let endowed_accounts = vec![account_id("Alice"), account_id("Bob")];
let pot_accounts = vec![TreasuryPot::account_id(), FeesPot::account_id()];
let pot_accounts = vec![FeesPot::account_id()];

let evm_endowed_accounts = vec![evm_account_id("EvmAlice"), evm_account_id("EvmBob")];
// Build test genesis.
let config = GenesisConfig {
balances: BalancesConfig {
balances: {
let pot_accounts = pot_accounts.clone();
endowed_accounts
.iter()
.cloned()
.chain(pot_accounts.into_iter())
.map(|k| (k, INIT_BALANCE))
.chain(
[(
TreasuryPot::account_id(),
10 * INIT_BALANCE,
),
(
TokenClaimsPot::account_id(),
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
),
(
NativeToEvmSwapBridgePot::account_id(),
INIT_BALANCE * evm_endowed_accounts.len() as u128 +
// Own bridge pot minimum balance.
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
)]
.into_iter(),
)
Expand Down Expand Up @@ -91,12 +92,10 @@ fn new_test_ext_with() -> sp_io::TestExternalities {
[(
EvmToNativeSwapBridgePot::account_id(),
fp_evm::GenesisAccount {
balance: (INIT_BALANCE * (endowed_accounts.len() + pot_accounts.len()) as u128 +
// Own bridge pot minimum balance.
<EvmBalances as frame_support::traits::Currency<EvmAccountId>>::minimum_balance() +
// `TokenClaimsPot` minimum balance.
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance()
).into(),
balance: <EvmBalances as frame_support::traits::Currency<
EvmAccountId,
>>::minimum_balance()
.into(),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
Expand All @@ -115,6 +114,19 @@ fn new_test_ext_with() -> sp_io::TestExternalities {
storage.into()
}

/// This test verifies that bridges initialization has been applied at genesis.
#[test]
fn currencies_are_balanced() {
// Build the state from the config.
new_test_ext_with().execute_with(move || {
assert_eq!(
BalancedCurrencySwapBridgesInitializer::initializer_version(),
pallet_balanced_currency_swap_bridges_initializer::CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert!(BalancedCurrencySwapBridgesInitializer::is_balanced().unwrap());
})
}

/// This test verifies that swap native call works in the happy path.
#[test]
fn currency_swap_native_call_works() {
Expand All @@ -136,6 +148,7 @@ fn currency_swap_native_call_works() {
));

// Assert state changes.
assert!(BalancedCurrencySwapBridgesInitializer::is_balanced().unwrap());
assert_eq!(
Balances::total_balance(&account_id("Alice")),
alice_balance_before - swap_balance
Expand Down Expand Up @@ -204,6 +217,7 @@ fn currency_swap_precompile_call_works() {
assert_eq!(execinfo.logs, Vec::new());

// Assert state changes.
assert!(BalancedCurrencySwapBridgesInitializer::is_balanced().unwrap());
assert_eq!(
Balances::total_balance(&FeesPot::account_id()),
fees_pot_balance_before + expected_fee
Expand Down
16 changes: 8 additions & 8 deletions crates/humanode-runtime/src/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ fn new_test_ext_with() -> sp_io::TestExternalities {
let authorities = vec![authority_keys("Alice")];
let bootnodes = vec![account_id("Alice")];
let endowed_accounts = vec![account_id("Alice"), account_id("Bob")];
let pot_accounts = vec![TreasuryPot::account_id(), FeesPot::account_id()];
let pot_accounts = vec![FeesPot::account_id()];
// Build test genesis.
let config = GenesisConfig {
balances: BalancesConfig {
balances: {
let pot_accounts = pot_accounts.clone();
endowed_accounts
.iter()
.cloned()
.chain(pot_accounts.into_iter())
.map(|k| (k, INIT_BALANCE))
.chain(
[(
TreasuryPot::account_id(),
10 * INIT_BALANCE
),
(
TokenClaimsPot::account_id(),
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
),
Expand Down Expand Up @@ -70,12 +73,9 @@ fn new_test_ext_with() -> sp_io::TestExternalities {
vec![(
EvmToNativeSwapBridgePot::account_id(),
fp_evm::GenesisAccount {
balance: (INIT_BALANCE * (endowed_accounts.len() + pot_accounts.len()) as u128 +
// Own bridge pot minimum balance.
<EvmBalances as frame_support::traits::Currency<EvmAccountId>>::minimum_balance() +
// `TokenClaimsPot` minimum balance.
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance()
)
balance: <EvmBalances as frame_support::traits::Currency<
EvmAccountId,
>>::minimum_balance()
.into(),
code: Default::default(),
nonce: Default::default(),
Expand Down
20 changes: 9 additions & 11 deletions crates/humanode-runtime/src/tests/fixed_supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ fn new_test_ext_with() -> sp_io::TestExternalities {
let bootnodes = vec![account_id("Alice")];

let endowed_accounts = vec![account_id("Alice"), account_id("Bob")];
let pot_accounts = vec![TreasuryPot::account_id(), FeesPot::account_id()];
let pot_accounts = vec![FeesPot::account_id()];

let evm_endowed_accounts = vec![evm_account_id("EvmAlice"), evm_account_id("EvmBob")];
// Build test genesis.
let config = GenesisConfig {
balances: BalancesConfig {
balances: {
let pot_accounts = pot_accounts.clone();
endowed_accounts
.iter()
.cloned()
.chain(pot_accounts.into_iter())
.map(|k| (k, INIT_BALANCE))
.chain(
[(
TreasuryPot::account_id(), 10 * INIT_BALANCE
),
(
TokenClaimsPot::account_id(),
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
),
(
NativeToEvmSwapBridgePot::account_id(),
INIT_BALANCE * evm_endowed_accounts.len() as u128 +
// Own bridge pot minimum balance.
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance(),
)]
.into_iter(),
)
Expand Down Expand Up @@ -95,12 +95,10 @@ fn new_test_ext_with() -> sp_io::TestExternalities {
[(
EvmToNativeSwapBridgePot::account_id(),
fp_evm::GenesisAccount {
balance: (INIT_BALANCE * (endowed_accounts.len() + pot_accounts.len()) as u128 +
// Own bridge pot minimum balance.
<EvmBalances as frame_support::traits::Currency<EvmAccountId>>::minimum_balance() +
// `TokenClaimsPot` minimum balance.
<Balances as frame_support::traits::Currency<AccountId>>::minimum_balance()
).into(),
balance: <EvmBalances as frame_support::traits::Currency<
EvmAccountId,
>>::minimum_balance()
.into(),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
Expand Down
3 changes: 2 additions & 1 deletion crates/humanode-runtime/src/tests/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn works() {
"evmToNativeSwapBridgePot": {
"initialState": "Initialized"
},
"balancedCurrencySwapBridgesInitializer": null,
"nativeToEvmSwapBridge": null,
"evmToNativeSwapBridge": null
}"#;
Expand All @@ -139,7 +140,7 @@ fn unknown_field() {
`feesPot`, `tokenClaimsPot`, `transactionPayment`, `session`, `chainProperties`, \
`ethereumChainId`, `sudo`, `grandpa`, `ethereum`, `evm`, `dynamicFee`, `baseFee`, \
`imOnline`, `evmAccountsMapping`, `tokenClaims`, `nativeToEvmSwapBridgePot`, \
`evmToNativeSwapBridgePot`, `nativeToEvmSwapBridge`, `evmToNativeSwapBridge` at line 1 column 6"
`evmToNativeSwapBridgePot`, `balancedCurrencySwapBridgesInitializer`, `nativeToEvmSwapBridge`, `evmToNativeSwapBridge` at line 1 column 6"
);
}

Expand Down

0 comments on commit f302192

Please sign in to comment.