Skip to content

Commit

Permalink
Sort out AccountId<>Location conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Feb 7, 2025
1 parent 8262765 commit 8f0078c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
20 changes: 7 additions & 13 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub mod pallet {
/// Handler for XCM fees.
type XcmExecutor: ExecuteXcm<Self::RuntimeCall>;
/// Relayer Reward Payment
type RewardPayment: PaymentProcedure;
type RewardPayment: PaymentProcedure<Self::AccountId>;
/// Ethereum NetworkId
type EthereumNetwork: Get<NetworkId>;
/// Address of the Gateway contract.
Expand Down Expand Up @@ -200,10 +200,7 @@ pub mod pallet {
pub type OperatingMode<T: Config> = StorageValue<_, BasicOperatingMode, ValueQuery>;

#[pallet::call]
impl<T: Config> Pallet<T>
where
Location: From<<T as frame_system::Config>::AccountId>,
{
impl<T: Config> Pallet<T> where T::AccountId: Into<Location> {
/// Submit an inbound message originating from the Gateway contract on Ethereum
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::submit())]
Expand All @@ -219,7 +216,7 @@ pub mod pallet {
let message =
Message::try_from(&event.event_log).map_err(|_| Error::<T>::InvalidMessage)?;

Self::process_message(who.into(), message)
Self::process_message(who, message)
}

/// Halt or resume all pallet operations. May only be called by root.
Expand All @@ -236,11 +233,8 @@ pub mod pallet {
}
}

impl<T: Config> Pallet<T>
where
Location: From<<T as frame_system::Config>::AccountId>,
{
pub fn process_message(relayer: Location, message: Message) -> DispatchResult {
impl<T: Config> Pallet<T> where T::AccountId: Into<Location> {
pub fn process_message(relayer: T::AccountId, message: Message) -> DispatchResult {
// Verify that the message was submitted from the known Gateway contract
ensure!(T::GatewayAddress::get() == message.gateway, Error::<T>::InvalidGateway);

Expand Down Expand Up @@ -273,7 +267,7 @@ pub mod pallet {

fn send_xcm(
dest: Location,
fee_payer: Location,
fee_payer: T::AccountId,
xcm: Xcm<()>,
) -> Result<XcmHash, SendError> {
let (ticket, fee) = validate_send::<T::XcmSender>(dest, xcm)?;
Expand All @@ -285,7 +279,7 @@ pub mod pallet {
);
SendError::Fees
})?;
Self::deposit_event(Event::FeesPaid { paying: fee_payer, fees: fee });
Self::deposit_event(Event::FeesPaid { paying: fee_payer.into(), fees: fee });
T::XcmSender::deliver(ticket)
}
}
Expand Down
30 changes: 14 additions & 16 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
//! * `dry_run`: Convert xcm to InboundMessage
#![cfg_attr(not(feature = "std"), no_std)]
pub mod api;
pub mod envelope;
pub mod process_message_impl;
pub mod send_message_impl;
pub mod types;
Expand All @@ -66,19 +65,18 @@ use alloy_core::{
};
use bridge_hub_common::{AggregateMessageOrigin, CustomDigestItem};
use codec::Decode;
use envelope::Envelope;
use frame_support::{
storage::StorageStreamIter,
traits::{tokens::Balance, EnqueueMessage, Get, ProcessMessageError},
weights::{Weight, WeightToFee},
};
pub use pallet::*;
use snowbridge_core::{ether_asset, BasicOperatingMode, PaymentProcedure, TokenId};
use snowbridge_merkle_tree::merkle_root;
use snowbridge_outbound_queue_primitives::{
v2::{
abi::{CommandWrapper, OutboundMessageWrapper},
GasMeter, Message, OutboundCommandWrapper, OutboundMessage,
MessageReceipt
},
EventProof, VerificationError, Verifier,
};
Expand All @@ -90,7 +88,11 @@ use sp_runtime::{
use sp_std::prelude::*;
pub use types::{PendingOrder, ProcessMessageOriginOf};
pub use weights::WeightInfo;
use xcm::prelude::{Location, NetworkId, *};
use xcm::latest::{Location, NetworkId};

type MessageReceiptOf<T> = MessageReceipt<<T as frame_system::Config>::AccountId>;

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -136,7 +138,7 @@ pub mod pallet {
type GatewayAddress: Get<H160>;

/// Means of paying a relayer
type RewardPayment: PaymentProcedure;
type RewardPayment: PaymentProcedure<Self::AccountId>;

type ConvertAssetId: MaybeEquivalence<TokenId, Location>;

Expand Down Expand Up @@ -244,7 +246,7 @@ pub mod pallet {
}

#[pallet::call]
impl<T: Config> Pallet<T> {
impl<T: Config> Pallet<T> where T::AccountId: From<[u8; 32]> {
/// Halt or resume all pallet operations. May only be called by root.
#[pallet::call_index(0)]
#[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))]
Expand All @@ -259,8 +261,8 @@ pub mod pallet {
}

#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::submit_delivery_proof())]
pub fn submit_delivery_proof(
#[pallet::weight(T::WeightInfo::submit_delivery_receipt())]
pub fn submit_delivery_receipt(
origin: OriginFor<T>,
event: Box<EventProof>,
) -> DispatchResult {
Expand All @@ -271,23 +273,19 @@ pub mod pallet {
T::Verifier::verify(&event.event_log, &event.proof)
.map_err(|e| Error::<T>::Verification(e))?;

// Decode event log into an Envelope
let envelope =
Envelope::try_from(&event.event_log).map_err(|_| Error::<T>::InvalidEnvelope)?;
let receipt = MessageReceiptOf::<T>::try_from(&event.event_log).map_err(|_| Error::<T>::InvalidEnvelope)?;

// Verify that the message was submitted from the known Gateway contract
ensure!(T::GatewayAddress::get() == envelope.gateway, Error::<T>::InvalidGateway);
ensure!(T::GatewayAddress::get() == receipt.gateway, Error::<T>::InvalidGateway);

let nonce = envelope.nonce;
let nonce = receipt.nonce;

let order = <PendingOrders<T>>::get(nonce).ok_or(Error::<T>::InvalidPendingNonce)?;

// No fee for governance order
if !order.fee.is_zero() {
let reward_account_location =
AccountId32 { id: envelope.reward_address.into(), network: None }.into();
let ether = ether_asset(T::EthereumNetwork::get(), order.fee);
T::RewardPayment::pay_reward(reward_account_location, ether)
T::RewardPayment::pay_reward(receipt.reward_address, ether)
.map_err(|_| Error::<T>::RewardPaymentFailed)?;
}

Expand Down
4 changes: 2 additions & 2 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait WeightInfo {
fn do_process_message() -> Weight;
fn commit() -> Weight;
fn commit_single() -> Weight;
fn submit_delivery_proof() -> Weight;
fn submit_delivery_receipt() -> Weight;
}

// For backwards compatibility and tests.
Expand Down Expand Up @@ -80,7 +80,7 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().writes(1_u64))
}

fn submit_delivery_proof() -> Weight {
fn submit_delivery_receipt() -> Weight {
Weight::from_parts(70_000_000, 0)
.saturating_add(Weight::from_parts(0, 3601))
.saturating_add(RocksDbWeight::get().reads(2))
Expand Down
8 changes: 4 additions & 4 deletions bridges/snowbridge/primitives/core/src/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
use sp_runtime::traits::Debug;
use xcm::latest::{NetworkId, Location, Asset, Junction::GlobalConsensus};

pub trait PaymentProcedure {
pub trait PaymentProcedure<AccountId> {
/// Error that may be returned by the procedure.
type Error: Debug;

/// Pay reward to the relayer from the account with provided params.
fn pay_reward(
relayer: Location,
relayer: AccountId,
reward: Asset,
) -> Result<(), Self::Error>;
}

impl PaymentProcedure for () {
impl<AccountId> PaymentProcedure<AccountId> for () {
type Error = &'static str;

fn pay_reward(
_: Location,
_: AccountId,
_: Asset,
) -> Result<(), Self::Error> {
Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
use snowbridge_outbound_queue_primitives::Log;
use crate::Log;

use sp_core::{RuntimeDebug, H160};
use sp_std::prelude::*;
Expand All @@ -15,33 +15,39 @@ sol! {

/// An inbound message that has had its outer envelope decoded.
#[derive(Clone, RuntimeDebug)]
pub struct Envelope {
pub struct MessageReceipt<AccountId>
where
AccountId: From<[u8; 32]> + Clone
{
/// The address of the outbound queue on Ethereum that emitted this message as an event log
pub gateway: H160,
/// A nonce for enforcing replay protection and ordering.
/// The nonce of the dispatched message
pub nonce: u64,
/// Delivery status
pub success: bool,
/// The reward address
pub reward_address: [u8; 32],
pub reward_address: AccountId,
}

#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
pub enum EnvelopeDecodeError {
pub enum MessageReceiptDecodeError {
DecodeLogFailed,
DecodeAccountFailed,
}

impl TryFrom<&Log> for Envelope {
type Error = EnvelopeDecodeError;
impl<AccountId> TryFrom<&Log> for MessageReceipt<AccountId>
where
AccountId: From<[u8; 32]> + Clone
{
type Error = MessageReceiptDecodeError;

fn try_from(log: &Log) -> Result<Self, Self::Error> {
let topics: Vec<B256> = log.topics.iter().map(|x| B256::from_slice(x.as_ref())).collect();

let event = InboundMessageDispatched::decode_raw_log(topics, &log.data, true)
.map_err(|_| EnvelopeDecodeError::DecodeLogFailed)?;
.map_err(|_| MessageReceiptDecodeError::DecodeLogFailed)?;

let account = event.reward_address.into();
let account: AccountId = AccountId::from(event.reward_address.0);

Ok(Self {
gateway: log.address,
Expand Down
6 changes: 4 additions & 2 deletions bridges/snowbridge/primitives/outbound-queue/src/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod message;
pub mod converter;
pub mod message;
pub mod message_receipt;

pub use message::*;
pub use converter::*;
pub use message::*;
pub use message_receipt::*;

use codec::{Encode, Decode};
use scale_info::TypeInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<T: frame_system::Config> snowbridge_pallet_outbound_queue_v2::WeightInfo fo
.saturating_add(T::DbWeight::get().writes(1_u64))
}

fn submit_delivery_proof() -> Weight {
fn submit_delivery_receipt() -> Weight {
// Proof Size summary in bytes:
// Measured: `800`
// Estimated: `7200`
Expand Down

0 comments on commit 8f0078c

Please sign in to comment.