Skip to content

Commit

Permalink
using Domain instead. fixing forwarder
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 19, 2024
1 parent 19cd56f commit c4ad57b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 198 deletions.
11 changes: 6 additions & 5 deletions pallets/axelar-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use cfg_traits::{
PreConditions,
};
use cfg_types::{
domain_address::{truncate_into_eth_address, DomainAddress},
domain_address::{truncate_into_eth_address, Domain},
EVMChainId,
};
use ethabi::{Contract, Function, Param, ParamType, Token};
Expand Down Expand Up @@ -131,7 +131,7 @@ pub mod pallet {
/// The target of the messages coming from other chains
type Receiver: MessageReceiver<
Middleware = Self::Middleware,
Origin = DomainAddress,
Origin = Domain,
Message = Vec<u8>,
>;

Expand Down Expand Up @@ -239,12 +239,13 @@ pub mod pallet {

match config.domain {
DomainConfig::Evm(EvmConfig { chain_id, .. }) => {
let source_address_bytes = decode_var_source::<EVM_ADDRESS_LEN>(source_address)
.ok_or(Error::<T>::InvalidSourceAddress)?;
let _source_address_bytes =
decode_var_source::<EVM_ADDRESS_LEN>(source_address)
.ok_or(Error::<T>::InvalidSourceAddress)?;

T::Receiver::receive(
AxelarId::Evm(chain_id).into(),
DomainAddress::Evm(chain_id, source_address_bytes.into()),
Domain::Evm(chain_id),
payload.to_vec(),
)
}
Expand Down
53 changes: 15 additions & 38 deletions pallets/liquidity-pools-forwarder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod tests;
use core::fmt::Debug;

use cfg_traits::liquidity_pools::{LpMessageForwarded, MessageReceiver, MessageSender};
use cfg_types::domain_address::{Domain, DomainAddress};
use cfg_types::domain_address::Domain;
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
use frame_system::pallet_prelude::OriginFor;
pub use pallet::*;
Expand All @@ -46,11 +46,6 @@ use sp_std::convert::TryInto;

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ForwardInfo {
/// Refers to the chain from which the message originates.
///
/// Example: Assume a three-hop A -> B -> C, then this refers to the domain
/// of A.
pub(crate) source_domain: Domain,
/// Refers to contract on forwarding chain.
///
/// Example: Assume A -> B -> C, then this refers to the forwarding
Expand Down Expand Up @@ -92,7 +87,7 @@ pub mod pallet {
/// The entity which acts on unwrapped messages.
type MessageReceiver: MessageReceiver<
Middleware = Self::RouterId,
Origin = DomainAddress,
Origin = Domain,
Message = Self::Message,
>;

Expand All @@ -106,13 +101,11 @@ pub mod pallet {
/// Forwarding info was set
ForwarderSet {
router_id: T::RouterId,
source_domain: Domain,
forwarding_contract: H160,
},
/// Forwarding info was removed
ForwarderRemoved {
router_id: T::RouterId,
source_domain: Domain,
forwarding_contract: H160,
},
}
Expand Down Expand Up @@ -151,22 +144,19 @@ pub mod pallet {
pub fn set_forwarder(
origin: OriginFor<T>,
router_id: T::RouterId,
source_domain: Domain,
forwarding_contract: H160,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

RouterForwarding::<T>::insert(
&router_id,
ForwardInfo {
source_domain,
contract: forwarding_contract,
},
);

Self::deposit_event(Event::<T>::ForwarderSet {
router_id,
source_domain,
forwarding_contract,
});

Expand All @@ -187,7 +177,6 @@ pub mod pallet {
.map(|info| {
Self::deposit_event(Event::<T>::ForwarderRemoved {
router_id,
source_domain: info.source_domain,
forwarding_contract: info.contract,
});
})
Expand All @@ -207,7 +196,7 @@ pub mod pallet {
) -> DispatchResult {
let msg = RouterForwarding::<T>::get(&router_id)
.map(|info| {
T::Message::try_wrap_forward(info.source_domain, info.contract, message.clone())
T::Message::try_wrap_forward(Domain::Centrifuge, info.contract, message.clone())
})
.unwrap_or_else(|| {
ensure!(!message.is_forwarded(), Error::<T>::ForwardInfoNotFound);
Expand All @@ -221,42 +210,30 @@ pub mod pallet {
impl<T: Config> MessageReceiver for Pallet<T> {
type Message = T::Message;
type Middleware = T::RouterId;
type Origin = DomainAddress;
type Origin = Domain;

fn receive(
router_id: T::RouterId,
forwarding_domain_address: DomainAddress,
forwarding_domain: Domain,
message: T::Message,
) -> DispatchResult {
// Message can be unwrapped iff it was forwarded
//
// NOTE: Contract address irrelevant here because it is only necessary for
// outbound forwarded messages
let (lp_message, domain_address) = match (
RouterForwarding::<T>::get(&router_id),
let (lp_message, domain) = match (
RouterForwarding::<T>::get(&router_id).is_some(),
message.clone().unwrap_forwarded(),
) {
(Some(info), Some((source_domain, _contract, lp_message))) => {
ensure!(
info.source_domain == source_domain,
Error::<T>::SourceDomainMismatch
);

let domain_address = DomainAddress::Evm(
info.source_domain
.get_evm_chain_id()
.expect("Domain not Centrifuge; qed"),
info.contract,
);
Ok((lp_message, domain_address))
// NOTE: Contract address irrelevant here because it is only necessary for
// outbound forwarded messages
(true, Some((source_domain, _contract, lp_message))) => {
Ok((lp_message, source_domain))
}
(Some(_), None) => Err(Error::<T>::UnwrappingFailed),
(None, None) => Ok((message, forwarding_domain_address)),
(None, Some((_, _, _))) => Err(Error::<T>::ForwardInfoNotFound),
(true, None) => Err(Error::<T>::UnwrappingFailed),
(false, None) => Ok((message, forwarding_domain)),
(false, Some((_, _, _))) => Err(Error::<T>::ForwardInfoNotFound),
}
.map_err(|e: Error<T>| e)?;

T::MessageReceiver::receive(router_id, domain_address, lp_message)
T::MessageReceiver::receive(router_id, domain, lp_message)
}
}
}
Loading

0 comments on commit c4ad57b

Please sign in to comment.