Skip to content

Commit

Permalink
Fix abi encode with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Nov 4, 2024
1 parent 6e32583 commit fb77c06
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
13 changes: 8 additions & 5 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use snowbridge_core::{
use snowbridge_merkle_tree::{merkle_proof, MerkleProof};
use snowbridge_router_primitives::outbound::v2::XcmConverter;
use sp_core::Get;
use sp_std::vec::Vec;
use sp_std::{default::Default, vec::Vec};
use xcm::{
latest::Location,
prelude::{Parachain, Xcm},
Expand All @@ -41,10 +41,10 @@ where
&xcm,
T::EthereumNetwork::get(),
AgentIdOf::convert_location(&Location::new(1, Parachain(1000)))
.ok_or(DryRunError::ConvertFailed)?,
.ok_or(DryRunError::ConvertLocationFailed)?,
);

let message: Message = converter.convert().map_err(|_| DryRunError::ConvertFailed)?;
let message: Message = converter.convert().map_err(|_| DryRunError::ConvertXcmFailed)?;

let fee = Fee::from(crate::Pallet::<T>::calculate_local_fee());

Expand All @@ -58,8 +58,11 @@ where
})
.collect();

let committed_message =
InboundMessage { origin: message.origin.0.to_vec(), nonce: 0, commands };
let committed_message = InboundMessage {
origin: message.origin,
nonce: Default::default(),
commands: commands.try_into().map_err(|_| DryRunError::ConvertXcmFailed)?,
};

Ok((committed_message, fee))
}
25 changes: 19 additions & 6 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ use frame_support::{
};
use snowbridge_core::{
inbound::Message as DeliveryMessage,
outbound::v2::{CommandWrapper, Fee, GasMeter, InboundMessage, Message},
outbound::v2::{CommandWrapper, Fee, GasMeter, InboundMessage, InboundMessageWrapper, Message},
BasicOperatingMode, RewardLedger, TokenId,
};
use snowbridge_merkle_tree::merkle_root;
Expand All @@ -131,19 +131,23 @@ pub use pallet::*;

use alloy_sol_types::SolValue;

use alloy_primitives::FixedBytes;

use sp_runtime::traits::TrailingZeroInput;

use sp_runtime::traits::MaybeEquivalence;

use xcm::prelude::{Location, NetworkId};

use snowbridge_core::inbound::{VerificationError, Verifier};

use sp_core::H160;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use snowbridge_core::inbound::{VerificationError, Verifier};
use sp_core::H160;

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand Down Expand Up @@ -389,14 +393,23 @@ pub mod pallet {
.collect();

// Construct the final committed message
let committed_message =
InboundMessage { origin: message.origin.0.to_vec(), nonce, commands };
let inbound_message = InboundMessage {
origin: message.origin,
nonce,
commands: commands.clone().try_into().map_err(|_| Corrupt)?,
};

let committed_message = InboundMessageWrapper {
origin: FixedBytes::from(message.origin.as_fixed_bytes()),
nonce,
commands,
};

// ABI-encode and hash the prepared message
let message_abi_encoded = committed_message.abi_encode();
let message_abi_encoded_hash = <T as Config>::Hashing::hash(&message_abi_encoded);

Messages::<T>::append(Box::new(committed_message.clone()));
Messages::<T>::append(Box::new(inbound_message));
MessageLeaves::<T>::append(message_abi_encoded_hash);

<PendingOrders<T>>::try_mutate(nonce, |maybe_locked| -> DispatchResult {
Expand Down
13 changes: 8 additions & 5 deletions bridges/snowbridge/pallets/outbound-queue-v2/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_primitives::FixedBytes;
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
use crate::{mock::*, *};
Expand All @@ -12,7 +13,7 @@ use frame_support::{
use codec::Encode;
use snowbridge_core::{
outbound::{
v2::{primary_governance_origin, Command, SendMessage},
v2::{primary_governance_origin, Command, InboundMessageWrapper, SendMessage},
SendError,
},
ChannelId, ParaId,
Expand Down Expand Up @@ -261,10 +262,12 @@ fn encode_mock_message() {
})
.collect();

// Todo: print the abi-encoded message and try to decode with solidity test
let committed_message =
InboundMessage { origin: message.origin.0.to_vec(), nonce: 1, commands };
// print the abi-encoded message and decode with solidity test
let committed_message = InboundMessageWrapper {
origin: FixedBytes::from(message.origin.as_fixed_bytes()),
nonce: 1,
commands,
};
let message_abi_encoded = committed_message.abi_encode();
// print_hex(message_abi_encoded.as_slice());
println!("{}", HexDisplay::from(&message_abi_encoded));
}
8 changes: 1 addition & 7 deletions bridges/snowbridge/pallets/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ use xcm_executor::traits::ConvertLocation;

use snowbridge_core::{
gwei, meth,
outbound::{
v1::ConstantGasMeter,
v2::{
DefaultOutboundQueue, Fee as FeeV2, Message as MessageV2, SendMessage as SendMessageV2,
},
SendError, SendMessageFeeProvider,
},
outbound::{v1::ConstantGasMeter, v2::DefaultOutboundQueue},
sibling_sovereign_account, AgentId, AllowSiblingsOnly, ParaId, PricingParameters, Rewards,
};
use sp_runtime::{
Expand Down
3 changes: 2 additions & 1 deletion bridges/snowbridge/primitives/core/src/outbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ pub enum SendError {

#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
pub enum DryRunError {
ConvertFailed,
ConvertLocationFailed,
ConvertXcmFailed,
}
19 changes: 13 additions & 6 deletions bridges/snowbridge/primitives/core/src/outbound/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ use alloy_primitives::{Address, FixedBytes};
use alloy_sol_types::SolValue;

sol! {
#[derive(Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(PartialEq))]
struct InboundMessage {
struct InboundMessageWrapper {
// origin
bytes origin;
bytes32 origin;
// Message nonce
uint64 nonce;
// Commands
CommandWrapper[] commands;
}

#[derive(Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(PartialEq))]
#[derive(Encode, Decode, RuntimeDebug, PartialEq,TypeInfo)]
struct CommandWrapper {
uint8 kind;
uint64 gas;
Expand Down Expand Up @@ -90,6 +87,16 @@ sol! {
}
}

#[derive(Encode, Decode, TypeInfo, PartialEq, Clone, RuntimeDebug)]
pub struct InboundMessage {
/// Origin
pub origin: H256,
/// Nonce
pub nonce: u64,
/// Commands
pub commands: BoundedVec<CommandWrapper, ConstU32<MAX_COMMANDS>>,
}

pub const MAX_COMMANDS: u32 = 8;

/// A message which can be accepted by implementations of `/[`SendMessage`\]`
Expand Down

0 comments on commit fb77c06

Please sign in to comment.