Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Oct 30, 2024
1 parent 6e78187 commit c79b9b8
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 33 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

19 changes: 6 additions & 13 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
use super::*;

use bp_messages::{HashedLaneId, LaneIdType};
use bp_relayers::{PayRewardFromAccount, PaymentProcedure, RewardsAccountParams};
use frame_support::{derive_impl, parameter_types, traits::ConstU32};
use hex_literal::hex;
use snowbridge_beacon_primitives::{
Expand All @@ -11,21 +13,14 @@ use snowbridge_core::{
inbound::{Log, Proof, VerificationError},
TokenId,
};
use sp_core::H160;
use sp_core::{ConstU128, H160};
use sp_runtime::{
traits::{IdentifyAccount, IdentityLookup, MaybeEquivalence, Verify},
BuildStorage, MultiSignature,
};
use sp_std::{convert::From, default::Default};
use xcm::{latest::SendXcm, prelude::*};
use xcm_executor::traits::TransactAsset;
use xcm_executor::AssetsInHolding;
use sp_core::ConstU128;
use bp_relayers::{
PayRewardFromAccount, RewardsAccountParams,
};
use bp_relayers::PaymentProcedure;
use bp_messages::{HashedLaneId, LaneIdType};
use xcm_executor::{traits::TransactAsset, AssetsInHolding};

use crate::{self as inbound_queue};

Expand All @@ -38,8 +33,8 @@ frame_support::construct_runtime!(
System: frame_system::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
EthereumBeaconClient: snowbridge_pallet_ethereum_client::{Pallet, Call, Storage, Event<T>},
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
InboundQueue: inbound_queue::{Pallet, Call, Storage, Event<T>},
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -177,7 +172,7 @@ impl inbound_queue::Config for Test {
#[cfg(feature = "runtime-benchmarks")]
type Helper = Test;
type Token = Balances;
type RewardLedger = ();
type RewardLedger = BridgeRelayers;
}

parameter_types! {
Expand All @@ -188,7 +183,6 @@ pub type TestLaneIdType = HashedLaneId;

pub struct TestPaymentProcedure;


impl TestPaymentProcedure {
pub fn rewards_account(params: RewardsAccountParams<TestLaneIdType>) -> AccountId {
PayRewardFromAccount::<(), AccountId, TestLaneIdType>::rewards_account(params)
Expand Down Expand Up @@ -220,7 +214,6 @@ impl pallet_bridge_relayers::Config for Test {
type EthereumNetwork = EthereumNetwork;
type WethAddress = WethAddress;
type XcmSender = MockXcmSender;

type AssetTransactor = SuccessfulTransactor;
type AssetHubXCMFee = ConstU128<1_000_000_000_000u128>;
}
Expand Down
6 changes: 6 additions & 0 deletions bridges/snowbridge/pallets/outbound-queue-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ snowbridge-merkle-tree = { workspace = true }
[dev-dependencies]
pallet-message-queue = { workspace = true }
sp-keyring = { workspace = true, default-features = true }
pallet-bridge-relayers = { workspace = true, default-features = true }
bp-relayers = { workspace = true, default-features = true }
bp-messages = { workspace = true, default-features = true }
pallet-balances = { workspace = true, default-features = true }
xcm = { workspace = true }
xcm-executor = { workspace = true }

[features]
default = ["std"]
Expand Down
13 changes: 9 additions & 4 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@ use codec::Decode;
use envelope::Envelope;
use frame_support::{
storage::StorageStreamIter,
traits::{tokens::Balance, EnqueueMessage, Get, ProcessMessageError},
traits::{
fungible::{Inspect, Mutate},
tokens::Balance,
EnqueueMessage, Get, ProcessMessageError,
},
weights::{Weight, WeightToFee},
};
use snowbridge_core::{
inbound::Message as DeliveryMessage,
outbound::v2::{CommandWrapper, Fee, GasMeter, InboundMessage, Message},
BasicOperatingMode, rewards::RewardLedger,
rewards::RewardLedger,
BasicOperatingMode,
};
use snowbridge_merkle_tree::merkle_root;
use sp_core::H256;
Expand All @@ -126,7 +131,6 @@ use sp_runtime::{
use sp_std::prelude::*;
pub use types::{PendingOrder, ProcessMessageOriginOf};
pub use weights::WeightInfo;
use frame_support::traits::fungible::{Inspect, Mutate};

pub use pallet::*;

Expand Down Expand Up @@ -328,7 +332,8 @@ pub mod pallet {
let account = T::AccountId::decode(&mut &envelope.reward_address[..]).unwrap_or(
T::AccountId::decode(&mut TrailingZeroInput::zeroes()).expect("zero address"),
);
T::RewardLedger::deposit(account, order.fee.into())?;
let fee: BalanceOf<T> = order.fee.try_into().map_err(|_| <Error<T>>::InvalidFee)?;
T::RewardLedger::deposit(account, fee)?;

<PendingOrders<T>>::remove(nonce);

Expand Down
134 changes: 132 additions & 2 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use frame_support::{
BoundedVec,
};

use bp_messages::HashedLaneId;
use bp_relayers::{PayRewardFromAccount, PaymentProcedure, RewardsAccountParams};
use codec::Encode;
use hex_literal::hex;
use snowbridge_core::{
gwei,
Expand All @@ -18,12 +21,17 @@ use snowbridge_core::{
pricing::{PricingParameters, Rewards},
ParaId,
};
use sp_core::{ConstU32, H160, H256};
use sp_core::{ConstU128, ConstU32, H160, H256};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup, Keccak256},
AccountId32, BuildStorage, FixedU128,
};
use sp_std::marker::PhantomData;
use xcm::{
latest::SendXcm,
prelude::{SendError as XcmpSendError, *},
};
use xcm_executor::{traits::TransactAsset, AssetsInHolding};

type Block = frame_system::mocking::MockBlock<Test>;
type AccountId = AccountId32;
Expand All @@ -32,8 +40,10 @@ frame_support::construct_runtime!(
pub enum Test
{
System: frame_system::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event<T>},
OutboundQueue: crate::{Pallet, Storage, Event<T>},
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
}
);

Expand All @@ -47,6 +57,7 @@ impl frame_system::Config for Test {
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type AccountData = pallet_balances::AccountData<u128>;
type RuntimeEvent = RuntimeEvent;
type PalletInfo = PalletInfo;
type Nonce = u64;
Expand All @@ -72,6 +83,17 @@ impl pallet_message_queue::Config for Test {
type QueuePausedQuery = ();
}

parameter_types! {
pub const ExistentialDeposit: u128 = 1;
}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type Balance = Balance;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}

// Mock verifier
pub struct MockVerifier;

Expand All @@ -94,6 +116,8 @@ parameter_types! {
pub const GatewayAddress: H160 = H160(GATEWAY_ADDRESS);
}

type Balance = u128;

pub const DOT: u128 = 10_000_000_000;
impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
Expand All @@ -107,7 +131,81 @@ impl crate::Config for Test {
type Balance = u128;
type WeightToFee = IdentityFee<u128>;
type WeightInfo = ();
type RewardLedger = ();
type Token = Balances;
type RewardLedger = BridgeRelayers;
}

parameter_types! {
pub WethAddress: H160 = hex!("774667629726ec1FaBEbCEc0D9139bD1C8f72a23").into();
}

pub type TestLaneIdType = HashedLaneId;

pub struct TestPaymentProcedure;

impl TestPaymentProcedure {
pub fn rewards_account(params: RewardsAccountParams<TestLaneIdType>) -> AccountId {
PayRewardFromAccount::<(), AccountId, TestLaneIdType>::rewards_account(params)
}
}

impl PaymentProcedure<AccountId, Balance> for TestPaymentProcedure {
type Error = ();
type LaneId = TestLaneIdType;

fn pay_reward(
_relayer: &AccountId,
_lane_id: RewardsAccountParams<Self::LaneId>,
_reward: Balance,
) -> Result<(), Self::Error> {
Ok(())
}
}

parameter_types! {
pub const EthereumNetwork: xcm::v3::NetworkId = xcm::v3::NetworkId::Ethereum { chain_id: 11155111 };
}

impl pallet_bridge_relayers::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Reward = Balance;
type PaymentProcedure = TestPaymentProcedure;
type StakeAndSlash = ();
type WeightInfo = ();
type LaneId = TestLaneIdType;
type Token = Balances;
type AssetHubParaId = ConstU32<1000>;
type EthereumNetwork = EthereumNetwork;
type WethAddress = WethAddress;
type XcmSender = MockXcmSender;

type AssetTransactor = SuccessfulTransactor;
type AssetHubXCMFee = ConstU128<1_000_000_000_000u128>;
}

// Mock XCM sender that always succeeds
pub struct MockXcmSender;

impl SendXcm for MockXcmSender {
type Ticket = Xcm<()>;

fn validate(
dest: &mut Option<Location>,
xcm: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
if let Some(location) = dest {
match location.unpack() {
_ => Ok((xcm.clone().unwrap(), Assets::default())),
}
} else {
Ok((xcm.clone().unwrap(), Assets::default()))
}
}

fn deliver(xcm: Self::Ticket) -> core::result::Result<XcmHash, XcmpSendError> {
let hash = xcm.using_encoded(sp_io::hashing::blake2_256);
Ok(hash)
}
}

fn setup() {
Expand Down Expand Up @@ -193,3 +291,35 @@ pub fn mock_message(sibling_para_id: u32) -> Message {
.unwrap(),
}
}

pub struct SuccessfulTransactor;
impl TransactAsset for SuccessfulTransactor {
fn can_check_in(_origin: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
Ok(())
}

fn can_check_out(_dest: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
Ok(())
}

fn deposit_asset(_what: &Asset, _who: &Location, _context: Option<&XcmContext>) -> XcmResult {
Ok(())
}

fn withdraw_asset(
_what: &Asset,
_who: &Location,
_context: Option<&XcmContext>,
) -> Result<AssetsInHolding, XcmError> {
Ok(AssetsInHolding::default())
}

fn internal_transfer_asset(
_what: &Asset,
_from: &Location,
_to: &Location,
_context: &XcmContext,
) -> Result<AssetsInHolding, XcmError> {
Ok(AssetsInHolding::default())
}
}
Loading

0 comments on commit c79b9b8

Please sign in to comment.