forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* modify inbound queue to support custom message processing * passively test tuple trait implementation * rename XCM to Xcm everywhere * add comment explaining MessageProcessor
- Loading branch information
1 parent
5a449db
commit 3f3fb03
Showing
10 changed files
with
201 additions
and
112 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
bridges/snowbridge/pallets/inbound-queue/src/xcm_message_processor.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::{Error, Event, LOG_TARGET}; | ||
use codec::DecodeAll; | ||
use core::marker::PhantomData; | ||
use snowbridge_core::Channel; | ||
use snowbridge_router_primitives::inbound::envelope::Envelope; | ||
use snowbridge_router_primitives::inbound::{MessageProcessor, VersionedXcmMessage}; | ||
use sp_runtime::DispatchError; | ||
|
||
pub struct XcmMessageProcessor<T>(PhantomData<T>); | ||
|
||
impl<T> MessageProcessor for XcmMessageProcessor<T> | ||
where | ||
T: crate::Config, | ||
{ | ||
fn can_process_message(_channel: &Channel, envelope: &Envelope) -> bool { | ||
VersionedXcmMessage::decode_all(&mut envelope.payload.as_ref()).is_ok() | ||
} | ||
|
||
fn process_message(channel: Channel, envelope: Envelope) -> Result<(), DispatchError> { | ||
// Decode message into XCM | ||
let (xcm, fee) = match VersionedXcmMessage::decode_all(&mut envelope.payload.as_ref()) { | ||
Ok(message) => crate::Pallet::<T>::do_convert(envelope.message_id, message)?, | ||
Err(_) => return Err(Error::<T>::InvalidPayload.into()), | ||
}; | ||
|
||
log::info!( | ||
target: LOG_TARGET, | ||
"💫 xcm decoded as {:?} with fee {:?}", | ||
xcm, | ||
fee | ||
); | ||
|
||
// Burning fees for teleport | ||
crate::Pallet::<T>::burn_fees(channel.para_id, fee)?; | ||
|
||
// Attempt to send XCM to a dest parachain | ||
let message_id = crate::Pallet::<T>::send_xcm(xcm, channel.para_id)?; | ||
|
||
crate::Pallet::<T>::deposit_event(Event::MessageReceived { | ||
channel_id: envelope.channel_id, | ||
nonce: envelope.nonce, | ||
message_id, | ||
fee_burned: fee, | ||
}); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
bridges/snowbridge/primitives/router/src/inbound/envelope.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com> | ||
use snowbridge_core::{inbound::Log, ChannelId}; | ||
|
||
use sp_core::{RuntimeDebug, H160, H256}; | ||
use sp_std::prelude::*; | ||
|
||
use alloy_primitives::B256; | ||
use alloy_sol_types::{sol, SolEvent}; | ||
|
||
sol! { | ||
event OutboundMessageAccepted(bytes32 indexed channel_id, uint64 nonce, bytes32 indexed message_id, bytes payload); | ||
} | ||
|
||
/// An inbound message that has had its outer envelope decoded. | ||
#[derive(Clone, RuntimeDebug)] | ||
pub struct Envelope { | ||
/// The address of the outbound queue on Ethereum that emitted this message as an event log | ||
pub gateway: H160, | ||
/// The message Channel | ||
pub channel_id: ChannelId, | ||
/// A nonce for enforcing replay protection and ordering. | ||
pub nonce: u64, | ||
/// An id for tracing the message on its route (has no role in bridge consensus) | ||
pub message_id: H256, | ||
/// The inner payload generated from the source application. | ||
pub payload: Vec<u8>, | ||
} | ||
|
||
#[derive(Copy, Clone, RuntimeDebug)] | ||
pub struct EnvelopeDecodeError; | ||
|
||
impl TryFrom<&Log> for Envelope { | ||
type Error = EnvelopeDecodeError; | ||
|
||
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 = OutboundMessageAccepted::decode_log(topics, &log.data, true) | ||
.map_err(|_| EnvelopeDecodeError)?; | ||
|
||
Ok(Self { | ||
gateway: log.address, | ||
channel_id: ChannelId::from(event.channel_id.as_ref()), | ||
nonce: event.nonce, | ||
message_id: H256::from(event.message_id.as_ref()), | ||
payload: event.payload, | ||
}) | ||
} | ||
} |
Oops, something went wrong.