Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Oct 31, 2024
1 parent 5d4e495 commit 1295cad
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 84 deletions.
16 changes: 8 additions & 8 deletions bridges/modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,21 +439,21 @@ pub mod pallet {
if value.is_zero() {
return Err(Error::<T, I>::InsufficientFunds.into());
}
let reward_asset = snowbridge_core::location::convert_token_address(
T::EthereumNetwork::get(),
T::WethAddress::get(),
);
let reward_balance: u128 =
TryInto::<u128>::try_into(value).map_err(|_| Error::<T, I>::InvalidAmount)?;
let deposit: Asset = (reward_asset, reward_balance).into();

let asset_hub_fee_asset: Asset = (Location::parent(), T::AssetHubXCMFee::get()).into();
let reward_location = snowbridge_core::location::convert_token_address(
T::EthereumNetwork::get(),
T::WethAddress::get(),
);
let reward_asset: Asset = (reward_location.clone(), reward_balance).into();
let fee_asset: Asset = (reward_location, T::AssetHubXCMFee::get()).into();

let xcm: Xcm<()> = alloc::vec![
DescendOrigin(PalletInstance(T::InboundQueuePalletInstance::get()).into()),
UniversalOrigin(GlobalConsensus(T::EthereumNetwork::get())),
ReserveAssetDeposited(deposit.clone().into()),
BuyExecution { fees: asset_hub_fee_asset, weight_limit: Unlimited },
ReserveAssetDeposited(reward_asset.clone().into()),
BuyExecution { fees: fee_asset, weight_limit: Unlimited },
DepositAsset { assets: AllCounted(1).into(), beneficiary: deposit_location.clone() },
SetAppendix(Xcm(alloc::vec![
RefundSurplus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ decl_test_parachains! {
PolkadotXcm: bridge_hub_westend_runtime::PolkadotXcm,
Balances: bridge_hub_westend_runtime::Balances,
EthereumSystem: bridge_hub_westend_runtime::EthereumSystem,
EthereumRewards: bridge_hub_westend_runtime::EthereumRewards,
EthereumInboundQueue: bridge_hub_westend_runtime::EthereumInboundQueue,
BridgeRelayers: bridge_hub_westend_runtime::BridgeRelayers,
}
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ use hex_literal::hex;
use snowbridge_core::rewards::RewardLedger;
use snowbridge_router_primitives::inbound::{
v1::{Command, Destination, MessageV1, VersionedMessage},
GlobalConsensusEthereumConvertsFor,
};
use sp_core::H256;
use testnet_parachains_constants::westend::snowbridge::EthereumNetwork;

const INITIAL_FUND: u128 = 5_000_000_000_000;
Expand Down Expand Up @@ -193,32 +191,30 @@ fn claim_rewards_works() {

let relayer = BridgeHubWestendSender::get();
let reward_address = AssetHubWestendReceiver::get();
type EthereumRewards = <BridgeHubWestend as BridgeHubWestendPallet>::EthereumRewards;
assert_ok!(EthereumRewards::deposit(relayer.clone().into(), 2 * ETH));
type BridgeRelayers = <BridgeHubWestend as BridgeHubWestendPallet>::BridgeRelayers;
assert_ok!(BridgeRelayers::deposit(relayer.clone().into(), 2 * ETH));

// Check that the message was sent
assert_expected_events!(
BridgeHubWestend,
vec![
RuntimeEvent::EthereumRewards(snowbridge_pallet_rewards::Event::RewardDeposited { .. }) => {},
RuntimeEvent::BridgeRelayers(pallet_bridge_relayers::Event::RewardDeposited { .. }) => {},
]
);

let message_id = H256::random();
let result = EthereumRewards::claim(
let relayer_location = Location::new(1, [Parachain(1000), Junction::AccountId32{ id: reward_address.into(), network: None}]);
let result = BridgeRelayers::claim(
RuntimeOrigin::signed(relayer.clone()),
reward_address.clone(),
ETH,
message_id,
relayer_location.clone(),
);
assert_ok!(result);

let events = BridgeHubWestend::events();
assert!(
events.iter().any(|event| matches!(
event,
RuntimeEvent::EthereumRewards(snowbridge_pallet_rewards::Event::RewardClaimed { account_id, deposit_address, value, message_id: _ })
if *account_id == relayer && *deposit_address == reward_address && *value == ETH,
RuntimeEvent::BridgeRelayers(pallet_bridge_relayers::Event::RewardClaimed { account_id, deposit_location, value, })
if *account_id == relayer && *deposit_location == relayer_location && *value > 1 *ETH,
)),
"RewardClaimed event with correct fields."
);
Expand All @@ -232,40 +228,3 @@ fn claim_rewards_works() {
);
})
}

#[test]
fn claiming_more_than_accrued_rewards_errors() {
BridgeHubWestend::execute_with(|| {
type RuntimeEvent = <BridgeHubWestend as Chain>::RuntimeEvent;
type RuntimeOrigin = <BridgeHubWestend as Chain>::RuntimeOrigin;

let relayer = BridgeHubWestendSender::get();
let reward_address = AssetHubWestendReceiver::get();
type EthereumRewards = <BridgeHubWestend as BridgeHubWestendPallet>::EthereumRewards;
assert_ok!(EthereumRewards::deposit(relayer.clone().into(), 2 * ETH));

// Check that the message was sent
assert_expected_events!(
BridgeHubWestend,
vec![
RuntimeEvent::EthereumRewards(snowbridge_pallet_rewards::Event::RewardDeposited { .. }) => {},
]
);

let message_id = H256::random();
let result = EthereumRewards::claim(
RuntimeOrigin::signed(relayer.clone()),
reward_address.clone(),
3 * ETH,
message_id,
);
assert_err!(
result,
DispatchError::Module(sp_runtime::ModuleError {
index: 86,
error: [1, 0, 0, 0],
message: Some("InsufficientFunds")
})
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
use super::{weights, AccountId, Balance, Balances, BlockNumber, Runtime, RuntimeEvent};
use bp_parachains::SingleParaStoredHeaderDataBuilder;
use frame_support::{parameter_types, traits::ConstU32};
use testnet_parachains_constants::rococo::snowbridge::EthereumNetwork;
use sp_core::H160;
use crate::xcm_config;
use crate::XcmRouter;
use sp_runtime::traits::ConstU128;
use sp_runtime::traits::ConstU8;

parameter_types! {
pub const RelayChainHeadersToKeep: u32 = 1024;
Expand Down Expand Up @@ -83,6 +89,17 @@ impl pallet_bridge_relayers::Config<RelayersForLegacyLaneIdsMessagesInstance> fo
>;
type WeightInfo = weights::pallet_bridge_relayers::WeightInfo<Runtime>;
type LaneId = bp_messages::LegacyLaneId;
type AssetHubParaId = ConstU32<ASSET_HUB_ID>;
type EthereumNetwork = EthereumNetwork;
type WethAddress = WethAddress;
#[cfg(not(feature = "runtime-benchmarks"))]
type XcmSender = XcmRouter;
#[cfg(feature = "runtime-benchmarks")]
type XcmSender = DoNothingRouter;
type Token = Balances;
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
type InboundQueuePalletInstance = ConstU8<80>;
type AssetHubXCMFee = ConstU128<1_000_000_000_000>;
}

/// Allows collect and claim rewards for relayers
Expand All @@ -105,8 +122,26 @@ impl pallet_bridge_relayers::Config<RelayersForPermissionlessLanesInstance> for
>;
type WeightInfo = weights::pallet_bridge_relayers::WeightInfo<Runtime>;
type LaneId = bp_messages::HashedLaneId;
type AssetHubParaId = ConstU32<ASSET_HUB_ID>;
type EthereumNetwork = EthereumNetwork;
type WethAddress = WethAddress;
#[cfg(not(feature = "runtime-benchmarks"))]
type XcmSender = XcmRouter;
#[cfg(feature = "runtime-benchmarks")]
type XcmSender = DoNothingRouter;
type Token = Balances;
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
type InboundQueuePalletInstance = ConstU8<80>;
type AssetHubXCMFee = ConstU128<1_000_000_000_000>;
}

parameter_types! {
pub WethAddress: H160 = H160(hex_literal::hex!("fff9976782d46cc05630d1f6ebab18b2324d6b14"));
}

pub const ASSET_HUB_ID: u32 = rococo_runtime_constants::system_parachain::ASSET_HUB_ID;


/// Add GRANDPA bridge pallet to track Rococo Bulletin chain.
pub type BridgeGrandpaRococoBulletinInstance = pallet_bridge_grandpa::Instance4;
impl pallet_bridge_grandpa::Config<BridgeGrandpaRococoBulletinInstance> for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,13 @@ impl<T: frame_system::Config> pallet_bridge_relayers::WeightInfo for WeightInfo<
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
fn claim() -> Weight {
// Proof Size summary in bytes:
// Measured: `80`
// Estimated: `3517`
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(44_000_000, 3517)
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(3_u64))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
use super::{weights, AccountId, Balance, Balances, BlockNumber, Runtime, RuntimeEvent};
use bp_messages::LegacyLaneId;
use frame_support::parameter_types;
use sp_core::H160;
use testnet_parachains_constants::westend::snowbridge::EthereumNetwork;
use crate::XcmRouter;
use crate::xcm_config;
use sp_runtime::traits::ConstU32;
use sp_runtime::traits::ConstU128;
use sp_runtime::traits::ConstU8;

parameter_types! {
pub storage RequiredStakeForStakeAndSlash: Balance = 1_000_000;
Expand All @@ -33,6 +40,12 @@ parameter_types! {
pub storage DeliveryRewardInBalance: u64 = 1_000_000;
}

parameter_types! {
pub WethAddress: H160 = H160(hex_literal::hex!("fff9976782d46cc05630d1f6ebab18b2324d6b14"));
}

pub const ASSET_HUB_ID: u32 = westend_runtime_constants::system_parachain::ASSET_HUB_ID;

/// Allows collect and claim rewards for relayers
pub type RelayersForLegacyLaneIdsMessagesInstance = ();
impl pallet_bridge_relayers::Config<RelayersForLegacyLaneIdsMessagesInstance> for Runtime {
Expand All @@ -53,4 +66,16 @@ impl pallet_bridge_relayers::Config<RelayersForLegacyLaneIdsMessagesInstance> fo
>;
type WeightInfo = weights::pallet_bridge_relayers::WeightInfo<Runtime>;
type LaneId = LegacyLaneId;

type AssetHubParaId = ConstU32<ASSET_HUB_ID>;
type EthereumNetwork = EthereumNetwork;
type WethAddress = WethAddress;
#[cfg(not(feature = "runtime-benchmarks"))]
type XcmSender = XcmRouter;
#[cfg(feature = "runtime-benchmarks")]
type XcmSender = DoNothingRouter;
type Token = Balances;
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
type InboundQueuePalletInstance = ConstU8<80>;
type AssetHubXCMFee = ConstU128<1_000_000_000_000>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

#[cfg(not(feature = "runtime-benchmarks"))]
use crate::XcmRouter;
use crate::BridgeRelayers;
use crate::{
xcm_config,
xcm_config::{TreasuryAccount, UniversalLocation},
Balances, EthereumInboundQueue, EthereumOutboundQueue, EthereumSystem, MessageQueue, Runtime,
RuntimeEvent, TransactionByteFee, EthereumRewards
RuntimeEvent, TransactionByteFee,
};
use parachains_common::{AccountId, Balance};
use snowbridge_beacon_primitives::{Fork, ForkVersions};
Expand All @@ -35,14 +36,15 @@ use testnet_parachains_constants::westend::{
fee::WeightToFee,
snowbridge::{EthereumLocation, EthereumNetwork, INBOUND_QUEUE_PALLET_INDEX},
};
use sp_runtime::traits::ConstU8;

use crate::xcm_config::RelayNetwork;
#[cfg(feature = "runtime-benchmarks")]
use benchmark_helpers::DoNothingRouter;
use frame_support::{parameter_types, weights::ConstantMultiplier};
use pallet_xcm::EnsureXcm;
use sp_runtime::{
traits::{ConstU32, ConstU8, Keccak256},
traits::{ConstU32, Keccak256},
FixedU128,
};
use xcm::prelude::{GlobalConsensus, InteriorLocation, Location, Parachain};
Expand Down Expand Up @@ -123,7 +125,7 @@ impl snowbridge_pallet_inbound_queue_v2::Config for Runtime {
type GatewayAddress = EthereumGatewayAddress;
#[cfg(feature = "runtime-benchmarks")]
type Helper = Runtime;
type RewardLedger = EthereumRewards;
type RewardLedger = BridgeRelayers;
type Token = Balances;
type WeightInfo = crate::weights::snowbridge_pallet_inbound_queue_v2::WeightInfo<Runtime>;
}
Expand Down Expand Up @@ -154,10 +156,9 @@ impl snowbridge_pallet_outbound_queue_v2::Config for Runtime {
type WeightToFee = WeightToFee;
type Verifier = snowbridge_pallet_ethereum_client::Pallet<Runtime>;
type GatewayAddress = EthereumGatewayAddress;
type RewardLedger = EthereumRewards;
type RewardLedger = BridgeRelayers;
type Token = Balances;
type WeightInfo = crate::weights::snowbridge_pallet_outbound_queue_v2::WeightInfo<Runtime>;
type RewardLedger = ();
}

#[cfg(any(feature = "std", feature = "fast-runtime", feature = "runtime-benchmarks", test))]
Expand Down Expand Up @@ -235,26 +236,6 @@ impl snowbridge_pallet_system::Config for Runtime {
type EthereumLocation = EthereumLocation;
}

parameter_types! {
pub WethAddress: H160 = H160(hex_literal::hex!("fff9976782d46cc05630d1f6ebab18b2324d6b14"));
}

pub const ASSET_HUB_ID: u32 = westend_runtime_constants::system_parachain::ASSET_HUB_ID;

impl snowbridge_pallet_rewards::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetHubParaId = ConstU32<ASSET_HUB_ID>;
type EthereumNetwork = EthereumNetwork;
type WethAddress = WethAddress;
#[cfg(not(feature = "runtime-benchmarks"))]
type XcmSender = XcmRouter;
#[cfg(feature = "runtime-benchmarks")]
type XcmSender = DoNothingRouter;
type WeightInfo = (); // TODO generate weights
type Token = Balances;
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
}

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmark_helpers {
use crate::{EthereumBeaconClient, Runtime, RuntimeOrigin};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ construct_runtime!(
EthereumSystem: snowbridge_pallet_system = 83,
EthereumInboundQueueV2: snowbridge_pallet_inbound_queue_v2 = 84,
EthereumOutboundQueueV2: snowbridge_pallet_outbound_queue_v2 = 85,
EthereumRewards: snowbridge_pallet_rewards = 86,

// Message Queue. Importantly, is registered last so that messages are processed after
// the `on_initialize` hooks of bridging pallets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,13 @@ impl<T: frame_system::Config> pallet_bridge_relayers::WeightInfo for WeightInfo<
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
fn claim() -> Weight {
// Proof Size summary in bytes:
// Measured: `80`
// Estimated: `3517`
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(44_000_000, 3517)
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(3_u64))
}
}

0 comments on commit 1295cad

Please sign in to comment.