Skip to content

Commit

Permalink
Add blocknumber to LockedFee for easy scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Oct 21, 2024
1 parent 99ba809 commit ec16524
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
25 changes: 18 additions & 7 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ use snowbridge_core::{
BasicOperatingMode,
};
use sp_core::H256;
use sp_runtime::{traits::Hash, ArithmeticError, DigestItem};
use sp_runtime::{
traits::{BlockNumberProvider, Hash, Zero},
ArithmeticError, DigestItem,
};
use sp_std::prelude::*;
pub use types::{CommittedMessage, ProcessMessageOriginOf};
pub use types::{CommittedMessage, FeeWithBlockNumber, ProcessMessageOriginOf};
pub use weights::WeightInfo;

pub use pallet::*;
Expand Down Expand Up @@ -248,7 +251,8 @@ pub mod pallet {

/// Fee locked by nonce
#[pallet::storage]
pub type LockedFee<T: Config> = StorageMap<_, Twox64Concat, u64, u128, ValueQuery>;
pub type LockedFee<T: Config> =
StorageMap<_, Twox64Concat, u64, FeeWithBlockNumber<BlockNumberFor<T>>, OptionQuery>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T>
Expand Down Expand Up @@ -307,8 +311,8 @@ pub mod pallet {
ensure!(<LockedFee<T>>::contains_key(nonce), Error::<T>::PendingNonceNotExist);

// Todo: Reward relayer
// let fee = <LockedFee<T>>::get(nonce);
// T::RewardLeger::deposit(envelope.reward_address.into(), fee.into())?;
// let locked = <LockedFee<T>>::get(nonce);
// T::RewardLeger::deposit(envelope.reward_address.into(), locked.fee.into())?;
<LockedFee<T>>::remove(nonce);

Self::deposit_event(Event::MessageDeliveryProofReceived { nonce });
Expand Down Expand Up @@ -381,8 +385,15 @@ pub mod pallet {
Messages::<T>::append(Box::new(committed_message.clone()));
MessageLeaves::<T>::append(message_abi_encoded_hash);

<LockedFee<T>>::try_mutate(nonce, |amount| -> DispatchResult {
*amount = amount.checked_add(message.fee).ok_or(ArithmeticError::Overflow)?;
<LockedFee<T>>::try_mutate(nonce, |maybe_locked| -> DispatchResult {
let mut locked = maybe_locked.clone().unwrap_or_else(|| FeeWithBlockNumber {
fee: 0,
block_number: BlockNumberFor::<T>::zero(),
});
locked.block_number = frame_system::Pallet::<T>::current_block_number();
locked.fee =
locked.fee.checked_add(message.fee).ok_or(ArithmeticError::Overflow)?;
*maybe_locked = Some(locked);
Ok(())
})
.map_err(|_| Unsupported)?;
Expand Down
11 changes: 10 additions & 1 deletion bridges/snowbridge/pallets/outbound-queue-v2/src/types.rs
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 codec::{Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
use ethabi::Token;
use frame_support::{pallet_prelude::ConstU32, traits::ProcessMessage, BoundedVec};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -44,3 +44,12 @@ impl From<CommittedMessage> for Token {
Token::Tuple(message)
}
}

/// Fee with block number for easy fetch the pending message on relayer side
#[derive(Encode, Decode, TypeInfo, Clone, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
pub struct FeeWithBlockNumber<BlockNumber> {
/// The address of the outbound queue on Ethereum that emitted this message as an event log
pub fee: u128,
/// A nonce for enforcing replay protection and ordering.
pub block_number: BlockNumber,
}

0 comments on commit ec16524

Please sign in to comment.