Skip to content

Commit

Permalink
adds tests, changes message format
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Dec 10, 2024
1 parent 30d0cf4 commit a35eee1
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 207 deletions.
2 changes: 1 addition & 1 deletion bridges/snowbridge/pallets/inbound-queue-v2/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
{
// Convert message to XCM
let dummy_origin = Location::new(0, AccountId32 { id: H256::zero().into(), network: None });
let xcm = T::MessageConverter::convert(message, dummy_origin)
let (xcm, _) = T::MessageConverter::convert(message, dummy_origin)
.map_err(|e| Error::<T>::ConvertMessage(e))?;

// Calculate fee. Consists of the cost of the "submit" extrinsic as well as the XCM execution
Expand Down
6 changes: 3 additions & 3 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ pub mod pallet {

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

let xcm = 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, envelope.fee.into())?;
// T::RewardLeger::deposit(who, relayer_reward.into())?;
// a. The submit extrinsic cost on BH
// b. The delivery cost to AH
// c. The execution cost on AH
Expand Down Expand Up @@ -277,7 +277,7 @@ pub mod pallet {
pub fn do_convert(
message: MessageV2,
origin_account_location: Location,
) -> Result<Xcm<()>, Error<T>> {
) -> Result<(Xcm<()>, u128), Error<T>> {
Ok(T::MessageConverter::convert(message, origin_account_location)
.map_err(|e| Error::<T>::ConvertMessage(e))?)
}
Expand Down
5 changes: 4 additions & 1 deletion bridges/snowbridge/pallets/inbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ parameter_types! {
pub const WethAddress: H160 = H160(WETH_ADDRESS);
pub const InboundQueuePalletInstance: u8 = 84;
pub AssetHubLocation: InteriorLocation = Parachain(1000).into();
pub UniversalLocation: InteriorLocation =
[GlobalConsensus(ByGenesis(WESTEND_GENESIS_HASH)), Parachain(1002)].into();
pub AssetHubFromEthereum: Location = Location::new(1,[GlobalConsensus(ByGenesis(WESTEND_GENESIS_HASH)),Parachain(1000)]);
}

impl inbound_queue_v2::Config for Test {
Expand All @@ -160,7 +163,7 @@ impl inbound_queue_v2::Config for Test {
type GatewayAddress = GatewayAddress;
type AssetHubParaId = ConstU32<1000>;
type MessageConverter =
MessageToXcm<EthereumNetwork, InboundQueuePalletInstance, MockTokenIdConvert, WethAddress>;
MessageToXcm<EthereumNetwork, InboundQueuePalletInstance, MockTokenIdConvert, WethAddress, UniversalLocation, AssetHubFromEthereum>;
type Token = Balances;
type Balance = u128;
#[cfg(feature = "runtime-benchmarks")]
Expand Down
159 changes: 0 additions & 159 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,162 +126,3 @@ fn test_set_operating_mode_root_only() {
});
}

#[test]
fn test_send_native_erc20_token_payload() {
new_tester().execute_with(|| {
// To generate test data: forge test --match-test testSendEther -vvvv
let payload = hex!("29e3b139f4393adda86303fcdaa35f60bb7092bf0030ef7dba020000000000000000000004005615deb798bb3e4dfa0139dfa1b3d433cc23b72f0000b2d3595bf00600000000000000000000").to_vec();
let message = MessageV2::decode(&mut payload.as_ref());
assert_ok!(message.clone());

let inbound_message = message.unwrap();

let expected_origin: H160 = hex!("29e3b139f4393adda86303fcdaa35f60bb7092bf").into();
let expected_token_id: H160 = hex!("5615deb798bb3e4dfa0139dfa1b3d433cc23b72f").into();
let expected_value = 500000000000000000u128;
let expected_xcm: Vec<u8> = vec![];
let expected_claimer: Option<Vec<u8>> = None;

assert_eq!(expected_origin, inbound_message.origin);
assert_eq!(1, inbound_message.assets.len());
if let Asset::NativeTokenERC20 { token_id, value } = &inbound_message.assets[0] {
assert_eq!(expected_token_id, *token_id);
assert_eq!(expected_value, *value);
} else {
panic!("Expected NativeTokenERC20 asset");
}
assert_eq!(expected_xcm, inbound_message.xcm);
assert_eq!(expected_claimer, inbound_message.claimer);
});
}

#[test]
fn test_send_foreign_erc20_token_payload() {
new_tester().execute_with(|| {
let payload = hex!("29e3b139f4393adda86303fcdaa35f60bb7092bf0030ef7dba0200000000000000000000040197874824853fb4ad04794ccfd1cc8d2a7463839cfcbc6a315a1045c60ab85f400000b2d3595bf00600000000000000000000").to_vec();
let message = MessageV2::decode(&mut payload.as_ref());
assert_ok!(message.clone());

let inbound_message = message.unwrap();

let expected_fee = 3_000_000_000_000u128;
let expected_origin: H160 = hex!("29e3b139f4393adda86303fcdaa35f60bb7092bf").into();
let expected_token_id: H256 = hex!("97874824853fb4ad04794ccfd1cc8d2a7463839cfcbc6a315a1045c60ab85f40").into();
let expected_value = 500000000000000000u128;
let expected_xcm: Vec<u8> = vec![];
let expected_claimer: Option<Vec<u8>> = None;

assert_eq!(expected_origin, inbound_message.origin);
assert_eq!(expected_fee, inbound_message.fee);
assert_eq!(1, inbound_message.assets.len());
if let Asset::ForeignTokenERC20 { token_id, value } = &inbound_message.assets[0] {
assert_eq!(expected_token_id, *token_id);
assert_eq!(expected_value, *value);
} else {
panic!("Expected ForeignTokenERC20 asset");
}
assert_eq!(expected_xcm, inbound_message.xcm);
assert_eq!(expected_claimer, inbound_message.claimer);
});
}

#[test]
fn test_register_token_inbound_message_with_xcm_and_claimer() {
new_tester().execute_with(|| {
let payload = hex!("5991a2df15a8f6a256d3ec51e99254cd3fb576a90030ef7dba020000000000000000000004005615deb798bb3e4dfa0139dfa1b3d433cc23b72f00000000000000000000000000000000300508020401000002286bee0a015029e3b139f4393adda86303fcdaa35f60bb7092bf").to_vec();
let message = MessageV2::decode(&mut payload.as_ref());
assert_ok!(message.clone());

let inbound_message = message.unwrap();

let expected_origin: H160 = hex!("5991a2df15a8f6a256d3ec51e99254cd3fb576a9").into();
let expected_token_id: H160 = hex!("5615deb798bb3e4dfa0139dfa1b3d433cc23b72f").into();
let expected_value = 0u128;
let expected_xcm: Vec<u8> = hex!("0508020401000002286bee0a").to_vec();
let expected_claimer: Option<Vec<u8>> = Some(hex!("29E3b139f4393aDda86303fcdAa35F60Bb7092bF").to_vec());

assert_eq!(expected_origin, inbound_message.origin);
assert_eq!(1, inbound_message.assets.len());
if let Asset::NativeTokenERC20 { token_id, value } = &inbound_message.assets[0] {
assert_eq!(expected_token_id, *token_id);
assert_eq!(expected_value, *value);
} else {
panic!("Expected NativeTokenERC20 asset");
}
assert_eq!(expected_xcm, inbound_message.xcm);
assert_eq!(expected_claimer, inbound_message.claimer);

// decode xcm
let versioned_xcm = VersionedXcm::<()>::decode_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut inbound_message.xcm.as_ref(),
);

assert_ok!(versioned_xcm.clone());

// Check if decoding was successful
let decoded_instructions = match versioned_xcm.unwrap() {
VersionedXcm::V5(decoded) => decoded,
_ => {
panic!("unexpected xcm version found")
}
};

let mut decoded_instructions = decoded_instructions.into_iter();
let decoded_first = decoded_instructions.next().take();
assert!(decoded_first.is_some());
let decoded_second = decoded_instructions.next().take();
assert!(decoded_second.is_some());
assert_eq!(ClearOrigin, decoded_second.unwrap(), "Second instruction (ClearOrigin) does not match.");
});
}

#[test]
fn encode_xcm() {
new_tester().execute_with(|| {
let total_fee_asset: xcm::opaque::latest::Asset =
(Location::parent(), 1_000_000_000).into();

let instructions: Xcm<()> =
vec![ReceiveTeleportedAsset(total_fee_asset.into()), ClearOrigin].into();

let versioned_xcm_message = VersionedXcm::V5(instructions.clone());

let xcm_bytes = VersionedXcm::encode(&versioned_xcm_message);
let hex_string = hex::encode(xcm_bytes.clone());

println!("xcm hex: {}", hex_string);

let versioned_xcm = VersionedXcm::<()>::decode_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut xcm_bytes.as_ref(),
);

assert_ok!(versioned_xcm.clone());

// Check if decoding was successful
let decoded_instructions = match versioned_xcm.unwrap() {
VersionedXcm::V5(decoded) => decoded,
_ => {
panic!("unexpected xcm version found")
},
};

let mut original_instructions = instructions.into_iter();
let mut decoded_instructions = decoded_instructions.into_iter();

let original_first = original_instructions.next().take();
let decoded_first = decoded_instructions.next().take();
assert_eq!(
original_first, decoded_first,
"First instruction (ReceiveTeleportedAsset) does not match."
);

let original_second = original_instructions.next().take();
let decoded_second = decoded_instructions.next().take();
assert_eq!(
original_second, decoded_second,
"Second instruction (ClearOrigin) does not match."
);
});
}
Loading

0 comments on commit a35eee1

Please sign in to comment.