From c4ad57bcb945a80b579f54cf057c8e495cfa784b Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 19 Aug 2024 16:14:39 +0200 Subject: [PATCH] using Domain instead. fixing forwarder --- pallets/axelar-router/src/lib.rs | 11 +- pallets/liquidity-pools-forwarder/src/lib.rs | 53 +++----- pallets/liquidity-pools-gateway/src/lib.rs | 119 +++--------------- .../liquidity-pools-gateway/src/message.rs | 4 +- .../src/message_processing.rs | 41 +++--- pallets/liquidity-pools/src/inbound.rs | 14 +-- pallets/liquidity-pools/src/lib.rs | 21 ++-- runtime/common/src/routing.rs | 6 +- 8 files changed, 71 insertions(+), 198 deletions(-) diff --git a/pallets/axelar-router/src/lib.rs b/pallets/axelar-router/src/lib.rs index a0efe04c7f..2a2e5c0ab7 100644 --- a/pallets/axelar-router/src/lib.rs +++ b/pallets/axelar-router/src/lib.rs @@ -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}; @@ -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, >; @@ -239,12 +239,13 @@ pub mod pallet { match config.domain { DomainConfig::Evm(EvmConfig { chain_id, .. }) => { - let source_address_bytes = decode_var_source::(source_address) - .ok_or(Error::::InvalidSourceAddress)?; + let _source_address_bytes = + decode_var_source::(source_address) + .ok_or(Error::::InvalidSourceAddress)?; T::Receiver::receive( AxelarId::Evm(chain_id).into(), - DomainAddress::Evm(chain_id, source_address_bytes.into()), + Domain::Evm(chain_id), payload.to_vec(), ) } diff --git a/pallets/liquidity-pools-forwarder/src/lib.rs b/pallets/liquidity-pools-forwarder/src/lib.rs index 61db46ece4..e972f69c60 100644 --- a/pallets/liquidity-pools-forwarder/src/lib.rs +++ b/pallets/liquidity-pools-forwarder/src/lib.rs @@ -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::*; @@ -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 @@ -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, >; @@ -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, }, } @@ -151,7 +144,6 @@ pub mod pallet { pub fn set_forwarder( origin: OriginFor, router_id: T::RouterId, - source_domain: Domain, forwarding_contract: H160, ) -> DispatchResult { T::AdminOrigin::ensure_origin(origin)?; @@ -159,14 +151,12 @@ pub mod pallet { RouterForwarding::::insert( &router_id, ForwardInfo { - source_domain, contract: forwarding_contract, }, ); Self::deposit_event(Event::::ForwarderSet { router_id, - source_domain, forwarding_contract, }); @@ -187,7 +177,6 @@ pub mod pallet { .map(|info| { Self::deposit_event(Event::::ForwarderRemoved { router_id, - source_domain: info.source_domain, forwarding_contract: info.contract, }); }) @@ -207,7 +196,7 @@ pub mod pallet { ) -> DispatchResult { let msg = RouterForwarding::::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::::ForwardInfoNotFound); @@ -221,42 +210,30 @@ pub mod pallet { impl MessageReceiver for Pallet { 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::::get(&router_id), + let (lp_message, domain) = match ( + RouterForwarding::::get(&router_id).is_some(), message.clone().unwrap_forwarded(), ) { - (Some(info), Some((source_domain, _contract, lp_message))) => { - ensure!( - info.source_domain == source_domain, - Error::::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::::UnwrappingFailed), - (None, None) => Ok((message, forwarding_domain_address)), - (None, Some((_, _, _))) => Err(Error::::ForwardInfoNotFound), + (true, None) => Err(Error::::UnwrappingFailed), + (false, None) => Ok((message, forwarding_domain)), + (false, Some((_, _, _))) => Err(Error::::ForwardInfoNotFound), } .map_err(|e: Error| e)?; - T::MessageReceiver::receive(router_id, domain_address, lp_message) + T::MessageReceiver::receive(router_id, domain, lp_message) } } } diff --git a/pallets/liquidity-pools-gateway/src/lib.rs b/pallets/liquidity-pools-gateway/src/lib.rs index c7d58c7706..af1d5cdd92 100644 --- a/pallets/liquidity-pools-gateway/src/lib.rs +++ b/pallets/liquidity-pools-gateway/src/lib.rs @@ -34,7 +34,7 @@ use cfg_traits::liquidity_pools::{ MessageHash, MessageProcessor, MessageQueue, MessageReceiver, MessageSender, OutboundMessageHandler, RouterProvider, }; -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::{ensure_signed, OriginFor}; use message::GatewayMessage; @@ -108,10 +108,7 @@ pub mod pallet { type RouterProvider: RouterProvider; /// The type that processes inbound messages. - type InboundMessageHandler: InboundMessageHandler< - Sender = DomainAddress, - Message = Self::Message, - >; + type InboundMessageHandler: InboundMessageHandler; type WeightInfo: WeightInfo; @@ -144,12 +141,6 @@ pub mod pallet { session_id: T::SessionId, }, - /// An instance was added to a domain. - InstanceAdded { instance: DomainAddress }, - - /// An instance was removed from a domain. - InstanceRemoved { instance: DomainAddress }, - /// The domain hook address was initialized or updated. DomainHookAddressSet { domain: Domain, @@ -158,27 +149,27 @@ pub mod pallet { /// An inbound message was processed. InboundMessageProcessed { - domain_address: DomainAddress, + domain: Domain, message_hash: MessageHash, router_id: T::RouterId, }, /// An inbound message proof was processed. InboundProofProcessed { - domain_address: DomainAddress, + domain: Domain, message_hash: MessageHash, router_id: T::RouterId, }, /// An inbound message was executed. InboundMessageExecuted { - domain_address: DomainAddress, + domain: Domain, message_hash: MessageHash, }, /// An outbound message was sent. OutboundMessageSent { - domain_address: DomainAddress, + domain: Domain, message_hash: MessageHash, router_id: T::RouterId, }, @@ -214,15 +205,6 @@ pub mod pallet { pub type Routers = StorageValue<_, BoundedVec, ValueQuery>; - /// Storage that contains a limited number of whitelisted instances of - /// deployed liquidity pools for a particular domain. - /// - /// This can only be modified by an admin. - #[pallet::storage] - #[pallet::getter(fn allowlist)] - pub type Allowlist = - StorageDoubleMap<_, Blake2_128Concat, Domain, Blake2_128Concat, DomainAddress, ()>; - /// Stores the hook address of a domain required for particular LP messages. /// /// Lifetime: Indefinitely. @@ -304,20 +286,8 @@ pub mod pallet { /// Pending inbound entry not found. PendingInboundEntryNotFound, - /// Message proof cannot be retrieved. - MessageProofRetrieval, - - /// Recovery message not found. - RecoveryMessageNotFound, - /// Not enough routers are stored for a domain. NotEnoughRoutersForDomain, - - /// The messages of 2 inbound entries do not match. - InboundEntryMessageMismatch, - - /// The domain addresses of 2 inbound entries do not match. - InboundEntryDomainAddressMismatch, } #[pallet::call] @@ -348,52 +318,6 @@ pub mod pallet { Ok(()) } - /// Add a known instance of a deployed liquidity pools integration for a - /// specific domain. - #[pallet::weight(T::WeightInfo::add_instance())] - #[pallet::call_index(1)] - pub fn add_instance(origin: OriginFor, instance: DomainAddress) -> DispatchResult { - T::AdminOrigin::ensure_origin(origin)?; - - ensure!( - instance.domain() != Domain::Centrifuge, - Error::::DomainNotSupported - ); - - ensure!( - !Allowlist::::contains_key(instance.domain(), instance.clone()), - Error::::InstanceAlreadyAdded, - ); - - Allowlist::::insert(instance.domain(), instance.clone(), ()); - - Self::deposit_event(Event::InstanceAdded { instance }); - - Ok(()) - } - - /// Remove an instance from a specific domain. - #[pallet::weight(T::WeightInfo::remove_instance())] - #[pallet::call_index(2)] - pub fn remove_instance(origin: OriginFor, instance: DomainAddress) -> DispatchResult { - T::AdminOrigin::ensure_origin(origin.clone())?; - - ensure!( - Allowlist::::contains_key(instance.domain(), instance.clone()), - Error::::UnknownInstance, - ); - - Allowlist::::remove(instance.domain(), instance.clone()); - - Self::deposit_event(Event::InstanceRemoved { instance }); - - Ok(()) - } - - // Deprecated: receive_message with call_index(5) - // - // NOTE: If required, should be exposed by router. - /// Set the address of the domain hook /// /// Can only be called by `AdminOrigin`. @@ -457,13 +381,13 @@ pub mod pallet { #[pallet::call_index(11)] pub fn execute_message_recovery( origin: OriginFor, - domain_address: DomainAddress, + domain: Domain, message_hash: MessageHash, router_id: T::RouterId, ) -> DispatchResult { T::AdminOrigin::ensure_origin(origin)?; - let router_ids = Self::get_router_ids_for_domain(domain_address.domain())?; + let router_ids = Self::get_router_ids_for_domain(domain)?; ensure!( router_ids.iter().any(|x| x == &router_id), @@ -500,7 +424,7 @@ pub mod pallet { &router_ids, session_id, expected_proof_count, - domain_address, + domain, )?; Self::deposit_event(Event::::MessageRecoveryExecuted { @@ -623,18 +547,14 @@ pub mod pallet { fn process(msg: Self::Message) -> (DispatchResult, Weight) { match msg { GatewayMessage::Inbound { - domain_address, + domain, message, router_id, } => { let mut counter = 0; - let res = Self::process_inbound_message( - domain_address, - message, - router_id, - &mut counter, - ); + let res = + Self::process_inbound_message(domain, message, router_id, &mut counter); let weight = match counter { 0 => LP_DEFENSIVE_WEIGHT / 10, @@ -666,20 +586,11 @@ pub mod pallet { impl MessageReceiver for Pallet { type Message = T::Message; type Middleware = T::RouterId; - type Origin = DomainAddress; - - fn receive( - router_id: T::RouterId, - origin_address: DomainAddress, - message: T::Message, - ) -> DispatchResult { - ensure!( - Allowlist::::contains_key(origin_address.domain(), origin_address.clone()), - Error::::UnknownInstance, - ); + type Origin = Domain; + fn receive(router_id: T::RouterId, domain: Domain, message: T::Message) -> DispatchResult { let gateway_message = GatewayMessage::::Inbound { - domain_address: origin_address, + domain, message, router_id, }; diff --git a/pallets/liquidity-pools-gateway/src/message.rs b/pallets/liquidity-pools-gateway/src/message.rs index 6680179a8a..1f3f45e552 100644 --- a/pallets/liquidity-pools-gateway/src/message.rs +++ b/pallets/liquidity-pools-gateway/src/message.rs @@ -1,11 +1,11 @@ -use cfg_types::domain_address::DomainAddress; +use cfg_types::domain_address::Domain; use frame_support::pallet_prelude::{Decode, Encode, MaxEncodedLen, TypeInfo}; /// Message type used by the LP gateway. #[derive(Debug, Encode, Decode, Clone, Eq, MaxEncodedLen, PartialEq, TypeInfo)] pub enum GatewayMessage { Inbound { - domain_address: DomainAddress, + domain: Domain, message: Message, router_id: RouterId, }, diff --git a/pallets/liquidity-pools-gateway/src/message_processing.rs b/pallets/liquidity-pools-gateway/src/message_processing.rs index 5d681f41bd..ebfe533538 100644 --- a/pallets/liquidity-pools-gateway/src/message_processing.rs +++ b/pallets/liquidity-pools-gateway/src/message_processing.rs @@ -2,7 +2,7 @@ use cfg_traits::liquidity_pools::{ InboundMessageHandler, LpMessageBatch, LpMessageHash, LpMessageProof, MessageHash, MessageQueue, RouterProvider, }; -use cfg_types::domain_address::{Domain, DomainAddress}; +use cfg_types::domain_address::Domain; use frame_support::{ dispatch::DispatchResult, ensure, @@ -29,7 +29,7 @@ pub struct MessageEntry { /// /// NOTE - the `RouterProvider` ensures that we cannot have the same message /// entry, for the same message, for different domain addresses. - pub domain_address: DomainAddress, + pub domain: Domain, /// The LP message. pub message: T::Message, @@ -91,7 +91,7 @@ impl InboundEntry { pub fn create( message: T::Message, session_id: T::SessionId, - domain_address: DomainAddress, + domain: Domain, expected_proof_count: u32, ) -> Self { if message.is_proof_message() { @@ -102,7 +102,7 @@ impl InboundEntry { } else { InboundEntry::Message(MessageEntry { session_id, - domain_address, + domain, message, expected_proof_count, }) @@ -322,7 +322,7 @@ impl Pallet { router_ids: &[T::RouterId], session_id: T::SessionId, expected_proof_count: u32, - domain_address: DomainAddress, + domain: Domain, ) -> DispatchResult { let mut message = None; let mut votes = 0; @@ -351,10 +351,10 @@ impl Pallet { if let Some(msg) = message { Self::execute_post_voting_dispatch(message_hash, router_ids, expected_proof_count)?; - T::InboundMessageHandler::handle(domain_address.clone(), msg)?; + T::InboundMessageHandler::handle(domain, msg)?; Self::deposit_event(Event::::InboundMessageExecuted { - domain_address, + domain, message_hash, }) } @@ -398,12 +398,12 @@ impl Pallet { /// Iterates over a batch of messages and checks if the requirements for /// processing each message are met. pub(crate) fn process_inbound_message( - domain_address: DomainAddress, + domain: Domain, message: T::Message, router_id: T::RouterId, counter: &mut u64, ) -> DispatchResult { - let router_ids = Self::get_router_ids_for_domain(domain_address.domain())?; + let router_ids = Self::get_router_ids_for_domain(domain)?; let session_id = SessionIdStore::::get(); let expected_proof_count = Self::get_expected_proof_count(&router_ids)?; @@ -412,29 +412,20 @@ impl Pallet { let message_hash = submessage.get_message_hash(); - let inbound_entry: InboundEntry = InboundEntry::create( - submessage.clone(), - session_id, - domain_address.clone(), - expected_proof_count, - ); + let inbound_entry: InboundEntry = + InboundEntry::create(submessage.clone(), session_id, domain, expected_proof_count); inbound_entry.validate(&router_ids, &router_id.clone())?; Self::upsert_pending_entry(message_hash, &router_id, inbound_entry)?; - Self::deposit_processing_event( - domain_address.clone(), - submessage, - message_hash, - router_id.clone(), - ); + Self::deposit_processing_event(domain, submessage, message_hash, router_id.clone()); Self::execute_if_requirements_are_met( message_hash, &router_ids, session_id, expected_proof_count, - domain_address.clone(), + domain, )?; } @@ -442,20 +433,20 @@ impl Pallet { } fn deposit_processing_event( - domain_address: DomainAddress, + domain: Domain, message: T::Message, message_hash: MessageHash, router_id: T::RouterId, ) { if message.is_proof_message() { Self::deposit_event(Event::::InboundProofProcessed { - domain_address, + domain, message_hash, router_id, }) } else { Self::deposit_event(Event::::InboundMessageProcessed { - domain_address, + domain, message_hash, router_id, }) diff --git a/pallets/liquidity-pools/src/inbound.rs b/pallets/liquidity-pools/src/inbound.rs index 2437493215..89520e6ec9 100644 --- a/pallets/liquidity-pools/src/inbound.rs +++ b/pallets/liquidity-pools/src/inbound.rs @@ -151,7 +151,7 @@ impl Pallet { investor: T::AccountId, amount: ::Balance, currency_index: GeneralCurrencyIndexOf, - sending_domain: DomainAddress, + sending_domain: Domain, ) -> DispatchResult { let invest_id = Self::derive_invest_id(pool_id, tranche_id)?; let payout_currency = Self::try_get_currency_id(currency_index)?; @@ -160,7 +160,7 @@ impl Pallet { // origination domain T::Tokens::transfer( invest_id.into(), - &sending_domain.domain().into_account(), + &sending_domain.into_account(), &investor, amount, Preservation::Expendable, @@ -186,7 +186,7 @@ impl Pallet { tranche_id: T::TrancheId, investor: T::AccountId, currency_index: GeneralCurrencyIndexOf, - destination: DomainAddress, + destination: Domain, ) -> DispatchResult { let invest_id = Self::derive_invest_id(pool_id, tranche_id)?; let currency_u128 = currency_index.index; @@ -198,7 +198,7 @@ impl Pallet { T::Tokens::transfer( invest_id.into(), &investor, - &destination.domain().into_account(), + &destination.into_account(), amount, Preservation::Expendable, )?; @@ -211,11 +211,7 @@ impl Pallet { tranche_tokens_payout: amount.into(), }; - T::OutboundMessageHandler::handle( - T::TreasuryAccount::get(), - destination.domain(), - message, - )?; + T::OutboundMessageHandler::handle(T::TreasuryAccount::get(), destination, message)?; Ok(()) } diff --git a/pallets/liquidity-pools/src/lib.rs b/pallets/liquidity-pools/src/lib.rs index 657df1978c..28c7f7557c 100644 --- a/pallets/liquidity-pools/src/lib.rs +++ b/pallets/liquidity-pools/src/lib.rs @@ -283,10 +283,7 @@ pub mod pallet { pub enum Event { /// An incoming LP message was /// detected and is further processed - IncomingMessage { - sender: DomainAddress, - message: Message, - }, + IncomingMessage { sender: Domain, message: Message }, } #[pallet::error] @@ -1207,12 +1204,12 @@ pub mod pallet { impl InboundMessageHandler for Pallet { type Message = Message; - type Sender = DomainAddress; + type Sender = Domain; #[transactional] - fn handle(sender: DomainAddress, msg: Message) -> DispatchResult { + fn handle(sender: Domain, msg: Message) -> DispatchResult { Self::deposit_event(Event::::IncomingMessage { - sender: sender.clone(), + sender, message: msg.clone(), }); @@ -1233,7 +1230,7 @@ pub mod pallet { } => Self::handle_tranche_tokens_transfer( pool_id.into(), tranche_id.into(), - sender.domain(), + sender, DomainAddress::new(domain.try_into()?, receiver), amount.into(), ), @@ -1246,7 +1243,7 @@ pub mod pallet { } => Self::handle_deposit_request( pool_id.into(), tranche_id.into(), - DomainAddress::new(sender.domain(), investor).account(), + DomainAddress::new(sender, investor).account(), currency.into(), amount.into(), ), @@ -1259,7 +1256,7 @@ pub mod pallet { } => Self::handle_redeem_request( pool_id.into(), tranche_id.into(), - DomainAddress::new(sender.domain(), investor).account(), + DomainAddress::new(sender, investor).account(), amount.into(), currency.into(), sender, @@ -1272,7 +1269,7 @@ pub mod pallet { } => Self::handle_cancel_deposit_request( pool_id.into(), tranche_id.into(), - DomainAddress::new(sender.domain(), investor).account(), + DomainAddress::new(sender, investor).account(), currency.into(), ), Message::CancelRedeemRequest { @@ -1283,7 +1280,7 @@ pub mod pallet { } => Self::handle_cancel_redeem_request( pool_id.into(), tranche_id.into(), - DomainAddress::new(sender.domain(), investor).account(), + DomainAddress::new(sender, investor).account(), currency.into(), sender, ), diff --git a/runtime/common/src/routing.rs b/runtime/common/src/routing.rs index bb507d30df..f9e87985d4 100644 --- a/runtime/common/src/routing.rs +++ b/runtime/common/src/routing.rs @@ -3,7 +3,7 @@ use cfg_traits::{ liquidity_pools::{LpMessageSerializer, MessageReceiver, MessageSender, RouterProvider}, PreConditions, }; -use cfg_types::domain_address::{Domain, DomainAddress}; +use cfg_types::domain_address::Domain; use frame_support::{ dispatch::DispatchResult, pallet_prelude::{Decode, Encode, MaxEncodedLen, TypeInfo}, @@ -105,11 +105,11 @@ where impl MessageReceiver for MessageSerializer where - Receiver: MessageReceiver, + Receiver: MessageReceiver, { type Message = Vec; type Middleware = RouterId; - type Origin = DomainAddress; + type Origin = Domain; fn receive( middleware: Self::Middleware,