From b4336ed92b487bcd7db8508887a55e826f81deab Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Mon, 12 Feb 2024 15:42:09 +0530 Subject: [PATCH] define DomainsTransferTracking trait and an implement it for transporter Pallet --- Cargo.lock | 1 + crates/sp-domains/src/lib.rs | 14 ++++++ domains/pallets/messenger/Cargo.toml | 40 ++++++++--------- domains/pallets/transporter/Cargo.toml | 40 ++++++++--------- domains/pallets/transporter/src/lib.rs | 59 +++++++++++++++++++++++++- 5 files changed, 114 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 249a18f5b26..23a39008d89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7580,6 +7580,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", + "sp-domains", "sp-io", "sp-messenger", "sp-runtime", diff --git a/crates/sp-domains/src/lib.rs b/crates/sp-domains/src/lib.rs index 1e00bbb323c..317e3c2ecbd 100644 --- a/crates/sp-domains/src/lib.rs +++ b/crates/sp-domains/src/lib.rs @@ -1058,6 +1058,20 @@ impl ExtrinsicDigest { } } +/// Trait that tracks the balances on Domains. +pub trait DomainsTransfersTracker { + type Error; + + /// Marks transfer into domain. + fn transfer_in(domain_id: DomainId, amount: Balance) -> Result<(), Self::Error>; + + /// Marks a transfer from domain. + fn transfer_out(domain_id: DomainId, amount: Balance) -> Result<(), Self::Error>; + + /// Returns the total balance on domain. + fn balance_on_domain(domain_id: DomainId) -> Result; +} + pub type ExecutionReceiptFor = ExecutionReceipt< NumberFor, ::Hash, diff --git a/domains/pallets/messenger/Cargo.toml b/domains/pallets/messenger/Cargo.toml index e12493a5b4d..6235351bf5e 100644 --- a/domains/pallets/messenger/Cargo.toml +++ b/domains/pallets/messenger/Cargo.toml @@ -8,9 +8,9 @@ homepage = "https://subspace.network" repository = "https://github.com/subspace/subspace" description = "Subspace node pallet for cross domain and cross chain messaging" include = [ - "/src", - "/Cargo.toml", - "/README.md", + "/src", + "/Cargo.toml", + "/README.md", ] [dependencies] @@ -36,24 +36,24 @@ sp-state-machine = { version = "0.28.0", git = "https://github.com/subspace/polk [features] default = ["std"] std = [ - "codec/std", - "frame-benchmarking?/std", - "frame-support/std", - "frame-system/std", - "log/std", - "scale-info/std", - "sp-core/std", - "sp-domains/std", - "sp-messenger/std", - "sp-runtime/std", - "sp-std/std", - "sp-trie/std", + "codec/std", + "frame-benchmarking?/std", + "frame-support/std", + "frame-system/std", + "log/std", + "scale-info/std", + "sp-core/std", + "sp-domains/std", + "sp-messenger/std", + "sp-runtime/std", + "sp-std/std", + "sp-trie/std", ] try-runtime = ["frame-support/try-runtime"] runtime-benchmarks = [ - "frame-benchmarking", - "frame-benchmarking/runtime-benchmarks", - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", - "sp-messenger/runtime-benchmarks", + "frame-benchmarking", + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "sp-messenger/runtime-benchmarks", ] diff --git a/domains/pallets/transporter/Cargo.toml b/domains/pallets/transporter/Cargo.toml index 88636b6baea..034ce1a8004 100644 --- a/domains/pallets/transporter/Cargo.toml +++ b/domains/pallets/transporter/Cargo.toml @@ -8,19 +8,20 @@ homepage = "https://subspace.network" repository = "https://github.com/subspace/subspace" description = "Subspace node pallet to move funds between domains." include = [ - "/src", - "/Cargo.toml", - "/README.md", + "/src", + "/Cargo.toml", + "/README.md", ] [dependencies] codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] } -domain-runtime-primitives = { path = "../../primitives/runtime" , default-features = false } +domain-runtime-primitives = { path = "../../primitives/runtime", default-features = false } frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8", optional = true } frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } scale-info = { version = "2.7.0", default-features = false, features = ["derive"] } sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } +sp-domains = { version = "0.1.0", default-features = false, path = "../../../crates/sp-domains" } sp-messenger = { version = "0.1.0", default-features = false, path = "../../primitives/messenger" } sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } sp-std = { version = "8.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } @@ -32,22 +33,23 @@ sp-io = { version = "23.0.0", git = "https://github.com/subspace/polkadot-sdk", [features] default = ["std"] std = [ - "codec/std", - "domain-runtime-primitives/std", - "frame-benchmarking?/std", - "frame-support/std", - "frame-system/std", - "scale-info/std", - "sp-core/std", - "sp-messenger/std", - "sp-runtime/std", - "sp-std/std", + "codec/std", + "domain-runtime-primitives/std", + "frame-benchmarking?/std", + "frame-support/std", + "frame-system/std", + "scale-info/std", + "sp-core/std", + "sp-domains/std", + "sp-messenger/std", + "sp-runtime/std", + "sp-std/std", ] try-runtime = ["frame-support/try-runtime"] runtime-benchmarks = [ - "frame-benchmarking", - "frame-benchmarking/runtime-benchmarks", - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", - "sp-messenger/runtime-benchmarks", + "frame-benchmarking", + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "sp-messenger/runtime-benchmarks", ] diff --git a/domains/pallets/transporter/src/lib.rs b/domains/pallets/transporter/src/lib.rs index da4b2836e60..db1df15e5b6 100644 --- a/domains/pallets/transporter/src/lib.rs +++ b/domains/pallets/transporter/src/lib.rs @@ -21,10 +21,13 @@ use codec::{Decode, Encode}; use domain_runtime_primitives::{MultiAccountId, TryConvertBack}; +use frame_support::ensure; use frame_support::traits::Currency; pub use pallet::*; use scale_info::TypeInfo; +use sp_domains::DomainId; use sp_messenger::messages::ChainId; +use sp_runtime::traits::{CheckedAdd, CheckedSub, Get}; #[cfg(test)] mod mock; @@ -73,6 +76,7 @@ mod pallet { use frame_support::traits::{Currency, ExistenceRequirement, WithdrawReasons}; use frame_support::weights::Weight; use frame_system::pallet_prelude::*; + use sp_domains::DomainId; use sp_messenger::endpoint::{ Endpoint, EndpointHandler as EndpointHandlerT, EndpointId, EndpointRequest, EndpointResponse, Sender, @@ -89,7 +93,7 @@ mod pallet { /// Gets the chain_id of the current execution environment. type SelfChainId: Get; - /// Gets the endpoint_id of the this pallet in a given execution environment. + /// Gets the endpoint_id of this pallet in a given execution environment. type SelfEndpointId: Get; /// Currency used by this pallet. @@ -123,6 +127,12 @@ mod pallet { OptionQuery, >; + /// Domain balances. + #[pallet::storage] + #[pallet::getter(fn domain_balances)] + pub(super) type DomainBalances = + StorageMap<_, Identity, DomainId, BalanceOf, ValueQuery>; + /// Events emitted by pallet-transporter. #[pallet::event] #[pallet::generate_deposit(pub (super) fn deposit_event)] @@ -177,6 +187,12 @@ mod pallet { UnexpectedMessage, /// Emits when the account id type is invalid. InvalidAccountId, + /// Emits when from_chain do not have enough funds to finalize the transfer. + LowBalanceOnDomain, + /// Emits when the transfer tracking was called from non-consensus chain + NonConsensusChain, + /// Emits when balance overflow + BalanceOverflow, } #[pallet::call] @@ -335,3 +351,44 @@ mod pallet { } } } + +impl sp_domains::DomainsTransfersTracker> for Pallet { + type Error = Error; + + fn balance_on_domain(domain_id: DomainId) -> Result, Self::Error> { + ensure!( + T::SelfChainId::get().is_consensus_chain(), + Error::NonConsensusChain + ); + + Ok(DomainBalances::::get(domain_id)) + } + + fn transfer_in(domain_id: DomainId, amount: BalanceOf) -> Result<(), Self::Error> { + ensure!( + T::SelfChainId::get().is_consensus_chain(), + Error::NonConsensusChain + ); + + DomainBalances::::try_mutate(domain_id, |current_balance| { + *current_balance = current_balance + .checked_add(&amount) + .ok_or(Error::BalanceOverflow)?; + Ok(()) + }) + } + + fn transfer_out(domain_id: DomainId, amount: BalanceOf) -> Result<(), Self::Error> { + ensure!( + T::SelfChainId::get().is_consensus_chain(), + Error::NonConsensusChain + ); + + DomainBalances::::try_mutate(domain_id, |current_balance| { + *current_balance = current_balance + .checked_sub(&amount) + .ok_or(Error::LowBalanceOnDomain)?; + Ok(()) + }) + } +}