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

Commit

Permalink
Fee paid on BH
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jul 22, 2024
1 parent e13d082 commit d765b37
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 48 deletions.
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: 2 additions & 2 deletions bridges/snowbridge/pallets/inbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ pub mod pallet {
let _ = match message {
VersionedMessage::V1(MessageV1 {
command: Command::RewardRelay { message_id, relay_address },
..
}) => T::Payer::reward_relay(message_id, relay_address)
chain_id,
}) => T::Payer::reward_relay(chain_id, message_id, relay_address)
.map_err(|e| Error::<T>::PayReward(e)),
_ => Ok(()),
}?;
Expand Down
64 changes: 36 additions & 28 deletions bridges/snowbridge/pallets/outbound-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,52 @@ bridge-hub-common = { path = "../../../../cumulus/parachains/runtimes/bridge-hub
snowbridge-core = { path = "../../primitives/core", default-features = false, features = ["serde"] }
snowbridge-outbound-queue-merkle-tree = { path = "merkle-tree", default-features = false }
ethabi = { package = "ethabi-decode", version = "1.0.0", default-features = false }
xcm = { package = "staging-xcm", path = "../../../../polkadot/xcm", default-features = false }
xcm-executor = { package = "staging-xcm-executor", path = "../../../../polkadot/xcm/xcm-executor", default-features = false }

[dev-dependencies]
pallet-message-queue = { path = "../../../../substrate/frame/message-queue", default-features = false }
sp-keyring = { path = "../../../../substrate/primitives/keyring" }
pallet-balances = { path = "../../../../substrate/frame/balances" }

[features]
default = ["std"]
std = [
"bridge-hub-common/std",
"codec/std",
"ethabi/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-message-queue/std",
"scale-info/std",
"serde/std",
"snowbridge-core/std",
"snowbridge-outbound-queue-merkle-tree/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"bridge-hub-common/std",
"codec/std",
"ethabi/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-message-queue/std",
"scale-info/std",
"serde/std",
"snowbridge-core/std",
"snowbridge-outbound-queue-merkle-tree/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"xcm-executor/std",
"xcm/std",
]
runtime-benchmarks = [
"bridge-hub-common/runtime-benchmarks",
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"bridge-hub-common/runtime-benchmarks",
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-executor/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-message-queue/try-runtime",
"sp-runtime/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"pallet-message-queue/try-runtime",
"sp-runtime/try-runtime",
]
49 changes: 39 additions & 10 deletions bridges/snowbridge/pallets/outbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ use bridge_hub_common::{AggregateMessageOrigin, CustomDigestItem};
use codec::Decode;
use frame_support::{
storage::StorageStreamIter,
traits::{tokens::Balance, Contains, Defensive, EnqueueMessage, Get, ProcessMessageError},
traits::{
fungible::Inspect, tokens::Balance, Contains, Defensive, EnqueueMessage, Get,
ProcessMessageError,
},
weights::{Weight, WeightToFee},
};
pub use pallet::*;
use snowbridge_core::{
outbound::{Fee, GasMeter, QueuedMessage, VersionedQueuedMessage, ETHER_DECIMALS},
BasicOperatingMode, ChannelId,
Expand All @@ -124,16 +128,22 @@ use sp_runtime::{
use sp_std::prelude::*;
pub use types::{CommittedMessage, ProcessMessageOriginOf};
pub use weights::WeightInfo;
use xcm::prelude::{Junction::*, Location, NetworkId};
use xcm_executor::traits::ConvertLocation;

pub use pallet::*;
type BalanceOf<T> =
<<T as pallet::Config>::Token as Inspect<<T as frame_system::Config>::AccountId>>::Balance;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{
pallet_prelude::*,
traits::fungible::{Inspect, Mutate},
};
use frame_system::pallet_prelude::*;
use snowbridge_core::PricingParameters;
use sp_arithmetic::FixedU128;
use snowbridge_core::{PayRewardError, PricingParameters};
use sp_arithmetic::{traits::SaturatedConversion, FixedU128};

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand Down Expand Up @@ -173,6 +183,9 @@ pub mod pallet {

/// Weight information for extrinsics in this pallet
type WeightInfo: WeightInfo;

/// Message relayers are rewarded with this asset
type Token: Mutate<Self::AccountId> + Inspect<Self::AccountId>;
}

#[pallet::event]
Expand Down Expand Up @@ -437,11 +450,27 @@ pub mod pallet {
Ok(())
}

pub(crate) fn unlock_fee(message_id: H256, _relay: H160) -> DispatchResult {
let _fee = <LockedFee<T>>::get(message_id);
// Todo: mint fee to the sovereign of the beneficiary
// let sovereign: AccountId = sovereign_of(_relay);
// T::Token::mint_into(sovereign,_fee);
pub(crate) fn unlock_fee(
chain_id: u64,
message_id: H256,
relay: H160,
) -> Result<(), PayRewardError>
where
<T as frame_system::Config>::AccountId: From<[u8; 32]>,
{
let fee: BalanceOf<T> = <LockedFee<T>>::get(message_id).saturated_into();

let account = snowbridge_core::SovereignIdOf::convert_location(&Location::new(
2,
[
GlobalConsensus(NetworkId::Ethereum { chain_id }),
AccountKey20 { network: None, key: relay.into() },
],
))
.ok_or(PayRewardError::AccountIdConversionFailed)?;
let sovereign_account: T::AccountId = account.into();

T::Token::mint_into(&sovereign_account, fee).map_err(|_| PayRewardError::CantMint)?;
<LockedFee<T>>::remove(message_id);
Ok(())
}
Expand Down
30 changes: 27 additions & 3 deletions bridges/snowbridge/pallets/outbound-queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ use snowbridge_core::{
};
use sp_core::{ConstU32, ConstU8, H160, H256};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup, Keccak256},
AccountId32, BuildStorage, FixedU128,
traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Keccak256, Verify},
BuildStorage, FixedU128, MultiSignature,
};
use sp_std::marker::PhantomData;

type Block = frame_system::mocking::MockBlock<Test>;
type AccountId = AccountId32;

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>},
}
);

pub type Signature = MultiSignature;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = Everything;
Expand All @@ -45,10 +48,30 @@ impl frame_system::Config for Test {
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u128>;
type Nonce = u64;
type Block = Block;
}

parameter_types! {
pub const ExistentialDeposit: u128 = 1;
}
impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = u128;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeHoldReason = ();
type RuntimeFreezeReason = ();
}

parameter_types! {
pub const HeapSize: u32 = 32 * 1024;
pub const MaxStale: u32 = 32;
Expand Down Expand Up @@ -93,6 +116,7 @@ impl crate::Config for Test {
type Channels = Everything;
type WeightToFee = IdentityFee<u128>;
type WeightInfo = ();
type Token = Balances;
}

fn setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ impl<T: Config> SendMessageFeeProvider for Pallet<T> {
}
}

impl<T: Config> PayMaster for Pallet<T> {
impl<T: Config> PayMaster for Pallet<T>
where
<T as frame_system::Config>::AccountId: From<[u8; 32]>,
{
/// The local component of the message processing fees in native currency
fn reward_relay(message_id: H256, relay: H160) -> Result<(), PayRewardError> {
Self::unlock_fee(message_id, relay).map_err(|_| PayRewardError::Other)
fn reward_relay(chain_id: u64, message_id: H256, relay: H160) -> Result<(), PayRewardError> {
Self::unlock_fee(chain_id, message_id, relay)
}
}
8 changes: 6 additions & 2 deletions bridges/snowbridge/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,20 @@ pub type AgentIdOf = HashedDescription<H256, (DescribeHere, DescribeFamily<Descr
#[derive(Clone, Encode, Decode, RuntimeDebug, PalletError, TypeInfo)]
#[cfg_attr(feature = "std", derive(PartialEq))]
pub enum PayRewardError {
AccountIdConversionFailed,
CantMint,
Other,
}

pub trait PayMaster {
fn reward_relay(message_id: H256, relay: H160) -> Result<(), PayRewardError>;
fn reward_relay(chain_id: u64, message_id: H256, relay: H160) -> Result<(), PayRewardError>;
}

impl PayMaster for () {
fn reward_relay(_: H256, _: H160) -> Result<(), PayRewardError> {
fn reward_relay(_: u64, _: H256, _: H160) -> Result<(), PayRewardError> {
Ok(())
}
}

pub type SovereignIdOf =
HashedDescription<[u8; 32], (DescribeHere, DescribeFamily<DescribeAllTerminal>)>;
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl snowbridge_pallet_outbound_queue::Config for Runtime {
type WeightInfo = crate::weights::snowbridge_pallet_outbound_queue::WeightInfo<Runtime>;
type PricingParameters = EthereumSystem;
type Channels = EthereumSystem;
type Token = Balances;
}

#[cfg(any(feature = "std", feature = "fast-runtime", feature = "runtime-benchmarks", test))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl snowbridge_pallet_outbound_queue::Config for Runtime {
type WeightInfo = crate::weights::snowbridge_pallet_outbound_queue::WeightInfo<Runtime>;
type PricingParameters = EthereumSystem;
type Channels = EthereumSystem;
type Token = Balances;
}

#[cfg(any(feature = "std", feature = "fast-runtime", feature = "runtime-benchmarks", test))]
Expand Down

0 comments on commit d765b37

Please sign in to comment.