Skip to content

Commit

Permalink
fix encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Jan 23, 2025
1 parent 959c662 commit 61e2a28
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 22 deletions.
100 changes: 88 additions & 12 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@ use sp_std::prelude::*;

use alloy_core::{primitives::B256, sol, sol_types::SolEvent};

/**
struct AsNativeTokenERC20 {
address token_id;
uint128 value;
}
struct AsForeignTokenERC20 {
bytes32 token_id;
uint128 value;
}
**/
sol! {
event OutboundMessageAccepted(uint64 indexed nonce, uint128 fee, bytes payload);
struct EthereumAsset {
uint8 kind;
bytes data;
}
struct Payload {
address origin;
EthereumAsset[] assets;
bytes xcm;
bytes claimer;
uint128 value;
uint128 executionFee;
uint128 relayerFee;
}
event OutboundMessageAccepted(uint64 nonce, Payload payload);
}

/// An inbound message that has had its outer envelope decoded.
Expand All @@ -18,10 +41,8 @@ pub struct Envelope {
pub gateway: H160,
/// A nonce for enforcing replay protection and ordering.
pub nonce: u64,
/// Total fee paid in Ether on Ethereum, should cover all the cost
pub fee: u128,
/// The inner payload generated from the source application.
pub payload: Vec<u8>,
pub payload: Payload,
}

#[derive(Copy, Clone, RuntimeDebug)]
Expand All @@ -31,16 +52,71 @@ impl TryFrom<&Log> for Envelope {
type Error = EnvelopeDecodeError;

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

let event = OutboundMessageAccepted::decode_raw_log(topics, &log.data, true)
.map_err(|_| EnvelopeDecodeError)?;
// Decode the Solidity event from raw logs
let event = OutboundMessageAccepted::decode_raw_log(topics, &log.data, true).map_err(
|decode_err| {
println!("error is {decode_err}");
log::error!(
target: "snowbridge-inbound-queue:v2",
"💫 decode error {:?}",
decode_err
);
EnvelopeDecodeError
},
)?;

// event.nonce is a `u64`
// event.payload is already the typed `Payload` struct
Ok(Self { gateway: log.address, nonce: event.nonce, payload: event.payload })
}
}

impl core::fmt::Debug for Payload {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Payload")
.field("origin", &self.origin)
.field("assets", &self.assets)
.field("xcm", &self.xcm)
.field("claimer", &self.claimer)
.field("value", &self.value)
.field("executionFee", &self.executionFee)
.field("relayerFee", &self.relayerFee)
.finish()
}
}

impl core::fmt::Debug for EthereumAsset {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("EthereumAsset")
.field("kind", &self.kind)
.field("data", &self.data)
.finish()
}
}

#[cfg(test)]
mod tests {
use crate::{envelope::Log, Envelope};
use frame_support::assert_ok;
use hex_literal::hex;
use sp_core::H160;

#[test]
fn test_decode() {
let log = Log{
address: hex!("b8ea8cb425d85536b158d661da1ef0895bb92f1d").into(),
topics: vec![hex!("b61699d45635baed7500944331ea827538a50dbfef79180f2079e9185da627aa").into()],
data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000b8ea8cb425d85536b158d661da1ef0895bb92f1d00000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000001dcd6500000000000000000000000000000000000000000000000000000000003b9aca000000000000000000000000000000000000000000000000000000000059682f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002cdeadbeef774667629726ec1fabebcec0d9139bd1c8f72a23deadbeef0000000000000000000000001dcd650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").to_vec(),
};

let result = Envelope::try_from(&log);
assert_ok!(result.clone());
let envelope = result.unwrap();

Ok(Self {
gateway: log.address,
nonce: event.nonce,
fee: event.fee,
payload: event.payload.into(),
})
assert_eq!(H160::from(hex!("b8ea8cb425d85536b158d661da1ef0895bb92f1d")), envelope.gateway);
assert_eq!(hex!("B8EA8cB425d85536b158d661da1ef0895Bb92F1D"), envelope.payload.origin);
}
}
14 changes: 7 additions & 7 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ pub mod pallet {
ensure!(!Nonce::<T>::get(envelope.nonce.into()), Error::<T>::InvalidNonce);

// Decode payload into `MessageV2`
let message = MessageV2::decode_all(&mut envelope.payload.as_ref())
.map_err(|_| Error::<T>::InvalidPayload)?;
//let message = MessageV2::decode_all(&mut envelope.payload.as_ref())
// .map_err(|_| Error::<T>::InvalidPayload)?;

let origin_account_location = Self::account_to_location(who)?;
//let origin_account_location = Self::account_to_location(who)?;

let (xcm, _relayer_reward) =
Self::do_convert(message, origin_account_location.clone())?;
//let (xcm, _relayer_reward) =
// Self::do_convert(message, origin_account_location.clone())?;

// Todo: Deposit fee(in Ether) to RewardLeger which should cover all of:
// T::RewardLeger::deposit(who, relayer_reward.into())?;
Expand All @@ -242,8 +242,8 @@ pub mod pallet {
// Set nonce flag to true
Nonce::<T>::set(envelope.nonce.into());

let message_id = Self::send_xcm(xcm, T::AssetHubParaId::get())?;
Self::deposit_event(Event::MessageReceived { nonce: envelope.nonce, message_id });
//let message_id = Self::send_xcm(xcm, T::AssetHubParaId::get())?;
//Self::deposit_event(Event::MessageReceived { nonce: envelope.nonce, message_id });

Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
use super::*;

use crate::{mock::*, Error};
use frame_support::{assert_noop, assert_ok};
use snowbridge_core::inbound::Proof;
use sp_keyring::AccountKeyring as Keyring;
use sp_keyring::sr25519::Keyring;
use sp_runtime::DispatchError;

use crate::{mock::*, Error};

#[test]
fn test_submit_with_invalid_gateway() {
new_tester().execute_with(|| {
Expand Down

0 comments on commit 61e2a28

Please sign in to comment.