Skip to content

Commit

Permalink
Merge pull request #8 from moondance-labs/diego-snowbridge-inbound-qu…
Browse files Browse the repository at this point in the history
…eue-reward-processor

Add reward processor in snowbridge inbound queue
  • Loading branch information
dimartiro authored Feb 10, 2025
2 parents 6633a6f + 167dda1 commit 0e6fe98
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 51 deletions.
57 changes: 39 additions & 18 deletions bridges/snowbridge/pallets/inbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ mod test;
pub mod xcm_message_processor;

use codec::{Decode, Encode};
use frame_support::traits::tokens::Fortitude;
use frame_support::traits::tokens::Preservation;
use frame_support::{
traits::{
fungible::{Inspect, Mutate},
tokens::{Fortitude, Preservation},
},
pallet_prelude::DispatchResult,
traits::fungible::{Inspect, Mutate},
weights::WeightToFee,
PalletError,
};
Expand Down Expand Up @@ -76,6 +76,37 @@ pub use pallet::*;

pub const LOG_TARGET: &str = "snowbridge-inbound-queue";

pub trait RewardProcessor<T: frame_system::Config> {
fn process_reward(who: T::AccountId, channel: Channel, message: Message) -> DispatchResult;
}

impl<T: frame_system::Config> RewardProcessor<T> for () {
fn process_reward(_who: T::AccountId, _channel: Channel, _message: Message) -> DispatchResult {
Ok(())
}
}

pub struct RewardThroughSovereign<T>(sp_std::marker::PhantomData<T>);

impl<T: pallet::Config> RewardProcessor<T> for RewardThroughSovereign<T> {
fn process_reward(who: T::AccountId, channel: Channel, message: Message) -> DispatchResult {
let length = message.encode().len() as u32;
let delivery_cost = pallet::Pallet::<T>::calculate_delivery_cost(length);
let sovereign_account: T::AccountId = sibling_sovereign_account::<T>(channel.para_id);

let amount = T::Token::reducible_balance(
&sovereign_account,
Preservation::Preserve,
Fortitude::Polite,
)
.min(delivery_cost);
if !amount.is_zero() {
T::Token::transfer(&sovereign_account, &who, amount, Preservation::Preserve)?;
}

Ok(())
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -139,6 +170,9 @@ pub mod pallet {

/// Process the message that was submitted
type MessageProcessor: MessageProcessor;

/// Process the reward to the relayer
type RewardProcessor: RewardProcessor<Self>;
}

#[pallet::hooks]
Expand Down Expand Up @@ -263,20 +297,7 @@ pub mod pallet {
}
})?;

// Reward relayer from the sovereign account of the destination parachain, only if funds
// are available
let sovereign_account = sibling_sovereign_account::<T>(channel.para_id);
let delivery_cost = Self::calculate_delivery_cost(message.encode().len() as u32);
let amount = T::Token::reducible_balance(
&sovereign_account,
Preservation::Preserve,
Fortitude::Polite,
)
.min(delivery_cost);
if !amount.is_zero() {
T::Token::transfer(&sovereign_account, &who, amount, Preservation::Preserve)?;
}

T::RewardProcessor::process_reward(who, channel.clone(), message)?;
T::MessageProcessor::process_message(channel, envelope)
}

Expand Down
3 changes: 2 additions & 1 deletion bridges/snowbridge/pallets/inbound-queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use snowbridge_router_primitives::inbound::MessageToXcm;
use sp_core::{H160, H256};
use sp_runtime::{
traits::{IdentifyAccount, IdentityLookup, MaybeEquivalence, Verify},
BuildStorage, FixedU128, MultiSignature, DispatchError,
BuildStorage, DispatchError, FixedU128, MultiSignature,
};
use sp_std::{convert::From, default::Default};
use xcm::{latest::SendXcm, prelude::*};
Expand Down Expand Up @@ -269,6 +269,7 @@ impl inbound_queue::Config for Test {
type MaxMessageSize = ConstU32<1024>;
type AssetTransactor = SuccessfulTransactor;
type MessageProcessor = (DummyPrefix, XcmMessageProcessor<Test>, DummySuffix); // We are passively testing if implementation of MessageProcessor trait works correctly for tuple
type RewardProcessor = RewardThroughSovereign<Self>;
}

pub fn last_events(n: usize) -> Vec<RuntimeEvent> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,35 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use crate::xcm_config::RelayNetwork;
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::XcmRouter;
use crate::{
xcm_config, xcm_config::UniversalLocation, Balances, EthereumInboundQueue,
EthereumOutboundQueue, EthereumSystem, MessageQueue, Runtime, RuntimeEvent, TransactionByteFee,
TreasuryAccount,
};
#[cfg(feature = "runtime-benchmarks")]
use benchmark_helpers::DoNothingRouter;
use bridge_hub_common::AggregateMessageOrigin;
use frame_support::{parameter_types, weights::ConstantMultiplier};
use pallet_xcm::EnsureXcm;
use parachains_common::{AccountId, Balance};
use snowbridge_beacon_primitives::{Fork, ForkVersions};
use snowbridge_core::{gwei, meth, AllowSiblingsOnly, ChannelId, PricingParameters, Rewards};
use sp_runtime::traits::Convert;
use snowbridge_pallet_inbound_queue::RewardThroughSovereign;
use snowbridge_router_primitives::{inbound::MessageToXcm, outbound::EthereumBlobExporter};
use sp_core::H160;
use sp_runtime::traits::Convert;
use sp_runtime::{
traits::{ConstU32, ConstU8, Keccak256},
FixedU128,
};
use testnet_parachains_constants::rococo::{
currency::*,
fee::WeightToFee,
snowbridge::{EthereumLocation, EthereumNetwork, INBOUND_QUEUE_PALLET_INDEX},
};
use bridge_hub_common::AggregateMessageOrigin;
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},
FixedU128,
};
use xcm::prelude::{GlobalConsensus, InteriorLocation, Location, Parachain};

/// Exports message to the Ethereum Gateway contract.
Expand Down Expand Up @@ -99,19 +100,19 @@ impl snowbridge_pallet_inbound_queue::Config for Runtime {
type WeightInfo = crate::weights::snowbridge_pallet_inbound_queue::WeightInfo<Runtime>;
type PricingParameters = EthereumSystem;
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
type MessageProcessor = snowbridge_pallet_inbound_queue::xcm_message_processor::XcmMessageProcessor<Runtime>;
type MessageProcessor =
snowbridge_pallet_inbound_queue::xcm_message_processor::XcmMessageProcessor<Runtime>;
type RewardProcessor = RewardThroughSovereign<Self>;
}


pub struct GetAggregateMessageOrigin;

impl Convert<ChannelId, AggregateMessageOrigin> for GetAggregateMessageOrigin {
fn convert(channel_id: ChannelId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Snowbridge(channel_id)
}
fn convert(channel_id: ChannelId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Snowbridge(channel_id)
}
}


impl snowbridge_pallet_outbound_queue::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Hashing = Keccak256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use crate::xcm_config::RelayNetwork;
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::XcmRouter;
use crate::{
Expand All @@ -22,27 +23,27 @@ use crate::{
Balances, EthereumInboundQueue, EthereumOutboundQueue, EthereumSystem, MessageQueue, Runtime,
RuntimeEvent, TransactionByteFee,
};
#[cfg(feature = "runtime-benchmarks")]
use benchmark_helpers::DoNothingRouter;
use bridge_hub_common::AggregateMessageOrigin;
use frame_support::{parameter_types, weights::ConstantMultiplier};
use pallet_xcm::EnsureXcm;
use parachains_common::{AccountId, Balance};
use snowbridge_beacon_primitives::{Fork, ForkVersions};
use snowbridge_core::{gwei, meth, AllowSiblingsOnly, ChannelId, PricingParameters, Rewards};
use snowbridge_pallet_inbound_queue::RewardThroughSovereign;
use snowbridge_router_primitives::{inbound::MessageToXcm, outbound::EthereumBlobExporter};
use sp_core::H160;
use testnet_parachains_constants::westend::{
currency::*,
fee::WeightToFee,
snowbridge::{EthereumLocation, EthereumNetwork, INBOUND_QUEUE_PALLET_INDEX},
};
use sp_runtime::traits::Convert;
use bridge_hub_common::AggregateMessageOrigin;
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},
FixedU128,
};
use testnet_parachains_constants::westend::{
currency::*,
fee::WeightToFee,
snowbridge::{EthereumLocation, EthereumNetwork, INBOUND_QUEUE_PALLET_INDEX},
};
use xcm::prelude::{GlobalConsensus, InteriorLocation, Location, Parachain};

pub const SLOTS_PER_EPOCH: u32 = snowbridge_pallet_ethereum_client::config::SLOTS_PER_EPOCH as u32;
Expand Down Expand Up @@ -101,15 +102,17 @@ impl snowbridge_pallet_inbound_queue::Config for Runtime {
type WeightInfo = crate::weights::snowbridge_pallet_inbound_queue::WeightInfo<Runtime>;
type PricingParameters = EthereumSystem;
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
type MessageProcessor = snowbridge_pallet_inbound_queue::xcm_message_processor::XcmMessageProcessor<Runtime>;
type MessageProcessor =
snowbridge_pallet_inbound_queue::xcm_message_processor::XcmMessageProcessor<Runtime>;
type RewardProcessor = RewardThroughSovereign<Self>;
}

pub struct GetAggregateMessageOrigin;

impl Convert<ChannelId, AggregateMessageOrigin> for GetAggregateMessageOrigin {
fn convert(channel_id: ChannelId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Snowbridge(channel_id)
}
fn convert(channel_id: ChannelId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Snowbridge(channel_id)
}
}

impl snowbridge_pallet_outbound_queue::Config for Runtime {
Expand Down

0 comments on commit 0e6fe98

Please sign in to comment.