Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Jul 30, 2024
1 parent 76d494f commit c82647f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/parachain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
-p snowbridge-ethereum
-p snowbridge-router-primitives
-p snowbridge-runtime-common
-p snowbridge-runtime-test-common
-p bridge-hub-rococo-runtime
-p bridge-hub-rococo-integration-tests
-p asset-hub-rococo-integration-tests
Expand Down
76 changes: 72 additions & 4 deletions bridges/snowbridge/runtime/test-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use parachains_runtimes_test_utils::{
};
use snowbridge_core::{ChannelId, ParaId};
use snowbridge_pallet_ethereum_client_fixtures::*;
use sp_core::{H160, U256};
use sp_core::{Get, H160, U256};
use sp_keyring::AccountKeyring::*;
use sp_runtime::{traits::Header, AccountId32, DigestItem, SaturatedConversion, Saturating};
use xcm::{
Expand Down Expand Up @@ -466,23 +466,37 @@ pub fn ethereum_extrinsic<Runtime>(
let initial_checkpoint = make_checkpoint();
let update = make_finalized_header_update();
let sync_committee_update = make_sync_committee_update();
let mut invalid_update = make_finalized_header_update();
let mut invalid_sync_committee_update = make_sync_committee_update();
invalid_update.finalized_header.slot = 4354;
invalid_sync_committee_update.finalized_header.slot = 4354;

let alice = Alice;
let alice_account = alice.to_account_id();
<pallet_balances::Pallet<Runtime>>::mint_into(
&alice_account.into(),
&alice_account.clone().into(),
10_000_000_000_000_u128.saturated_into::<BalanceOf<Runtime>>(),
)
.unwrap();
let balance_before =
<pallet_balances::Pallet<Runtime>>::free_balance(&alice_account.clone().into());

assert_ok!(<snowbridge_pallet_ethereum_client::Pallet<Runtime>>::force_checkpoint(
RuntimeHelper::<Runtime>::root_origin(),
initial_checkpoint,
initial_checkpoint.clone(),
));
let balance_after_checkpoint =
<pallet_balances::Pallet<Runtime>>::free_balance(&alice_account.clone().into());

let update_call: <Runtime as pallet_utility::Config>::RuntimeCall =
snowbridge_pallet_ethereum_client::Call::<Runtime>::submit {
update: Box::new(*update),
update: Box::new(*update.clone()),
}
.into();

let invalid_update_call: <Runtime as pallet_utility::Config>::RuntimeCall =
snowbridge_pallet_ethereum_client::Call::<Runtime>::submit {
update: Box::new(*invalid_update),
}
.into();

Expand All @@ -492,12 +506,66 @@ pub fn ethereum_extrinsic<Runtime>(
}
.into();

let invalid_update_sync_committee_call: <Runtime as pallet_utility::Config>::RuntimeCall =
snowbridge_pallet_ethereum_client::Call::<Runtime>::submit {
update: Box::new(*invalid_sync_committee_update),
}
.into();

// Finalized header update
let update_outcome = construct_and_apply_extrinsic(alice, update_call.into());
assert_ok!(update_outcome);
let balance_after_update =
<pallet_balances::Pallet<Runtime>>::free_balance(&alice_account.clone().into());

// Invalid finalized header update
let invalid_update_outcome =
construct_and_apply_extrinsic(alice, invalid_update_call.into());
assert_err!(
invalid_update_outcome,
snowbridge_pallet_ethereum_client::Error::<Runtime>::InvalidUpdateSlot
);
let balance_after_invalid_update =
<pallet_balances::Pallet<Runtime>>::free_balance(&alice_account.clone().into());

// Sync committee update
let sync_committee_outcome =
construct_and_apply_extrinsic(alice, update_sync_committee_call.into());
assert_ok!(sync_committee_outcome);
let balance_after_sync_com_update =
<pallet_balances::Pallet<Runtime>>::free_balance(&alice_account.clone().into());

// Invalid sync committee update
let invalid_sync_committee_outcome =
construct_and_apply_extrinsic(alice, invalid_update_sync_committee_call.into());
assert_err!(
invalid_sync_committee_outcome,
snowbridge_pallet_ethereum_client::Error::<Runtime>::InvalidUpdateSlot
);
let balance_after_invalid_sync_com_update =
<pallet_balances::Pallet<Runtime>>::free_balance(&alice_account.clone().into());

// Assert paid operations are charged and free operations are free
// Checkpoint is a free operation
assert!(balance_before == balance_after_checkpoint);
let gap =
<Runtime as snowbridge_pallet_ethereum_client::Config>::FreeHeadersInterval::get();
// Large enough header gap is free
if gap.is_some() &&
update.finalized_header.slot >=
initial_checkpoint.header.slot + gap.unwrap() as u64
{
assert!(balance_after_checkpoint == balance_after_update);
} else {
// Otherwise paid
assert!(balance_after_checkpoint > balance_after_update);
}
// An invalid update is paid
assert!(balance_after_update > balance_after_invalid_update);
// A successful sync committee update is free
assert!(balance_after_invalid_update == balance_after_sync_com_update);
// An invalid sync committee update is paid
assert!(balance_after_sync_com_update > balance_after_invalid_sync_com_update);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

use bp_polkadot_core::Signature;
use bridge_hub_rococo_runtime::{
bridge_common_config, bridge_to_bulletin_config, bridge_to_westend_config,
bridge_common_config, bridge_to_bulletin_config,
bridge_to_ethereum_config::EthereumGatewayAddress,
bridge_to_westend_config,
xcm_config::{RelayNetwork, TokenLocation, XcmConfig},
AllPalletsWithoutSystem, BridgeRejectObsoleteHeadersAndMessages, EthereumGatewayAddress,
Executive, ExistentialDeposit, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall,
RuntimeEvent, RuntimeOrigin, SessionKeys, SignedExtra, TransactionPayment, UncheckedExtrinsic,
AllPalletsWithoutSystem, BridgeRejectObsoleteHeadersAndMessages, Executive, ExistentialDeposit,
ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, SessionKeys,
SignedExtra, TransactionPayment, UncheckedExtrinsic,
};
use bridge_hub_test_utils::SlotDurations;
use codec::{Decode, Encode};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ parameter_types! {
impl snowbridge_pallet_ethereum_client::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type ForkVersions = ChainForkVersions;
type FreeHeadersInterval = ConstU32<32>;
type WeightInfo = crate::weights::snowbridge_pallet_ethereum_client::WeightInfo<Runtime>;
}

Expand Down

0 comments on commit c82647f

Please sign in to comment.