-
Notifications
You must be signed in to change notification settings - Fork 2
Add inbound queue #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2cbd5f0
a15eb8c
e5a2fa4
db1641f
4150bdc
d77d316
4db2776
d0190f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
| 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>); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably call it some other thing but it is likely that snowbridge will tell you how to name it. So for me this is good enough
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reason why I named it |
||
|
|
||
| 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(()) | ||
| } | ||
| } | ||
| 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! { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this macro?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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, | ||
| }) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.