Skip to content

Commit

Permalink
define DomainsTransferTracking trait and an implement it for transpor…
Browse files Browse the repository at this point in the history
…ter Pallet
  • Loading branch information
vedhavyas committed Feb 12, 2024
1 parent 6f917e9 commit b4336ed
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,20 @@ impl ExtrinsicDigest {
}
}

/// Trait that tracks the balances on Domains.
pub trait DomainsTransfersTracker<Balance> {
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<Balance, Self::Error>;
}

pub type ExecutionReceiptFor<DomainHeader, CBlock, Balance> = ExecutionReceipt<
NumberFor<CBlock>,
<CBlock as BlockT>::Hash,
Expand Down
40 changes: 20 additions & 20 deletions domains/pallets/messenger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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",
]
40 changes: 21 additions & 19 deletions domains/pallets/transporter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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",
]
59 changes: 58 additions & 1 deletion domains/pallets/transporter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -89,7 +93,7 @@ mod pallet {
/// Gets the chain_id of the current execution environment.
type SelfChainId: Get<ChainId>;

/// 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<EndpointId>;

/// Currency used by this pallet.
Expand Down Expand Up @@ -123,6 +127,12 @@ mod pallet {
OptionQuery,
>;

/// Domain balances.
#[pallet::storage]
#[pallet::getter(fn domain_balances)]
pub(super) type DomainBalances<T: Config> =
StorageMap<_, Identity, DomainId, BalanceOf<T>, ValueQuery>;

/// Events emitted by pallet-transporter.
#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -335,3 +351,44 @@ mod pallet {
}
}
}

impl<T: Config> sp_domains::DomainsTransfersTracker<BalanceOf<T>> for Pallet<T> {
type Error = Error<T>;

fn balance_on_domain(domain_id: DomainId) -> Result<BalanceOf<T>, Self::Error> {
ensure!(
T::SelfChainId::get().is_consensus_chain(),
Error::NonConsensusChain
);

Ok(DomainBalances::<T>::get(domain_id))
}

fn transfer_in(domain_id: DomainId, amount: BalanceOf<T>) -> Result<(), Self::Error> {
ensure!(
T::SelfChainId::get().is_consensus_chain(),
Error::NonConsensusChain
);

DomainBalances::<T>::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<T>) -> Result<(), Self::Error> {
ensure!(
T::SelfChainId::get().is_consensus_chain(),
Error::NonConsensusChain
);

DomainBalances::<T>::try_mutate(domain_id, |current_balance| {
*current_balance = current_balance
.checked_sub(&amount)
.ok_or(Error::LowBalanceOnDomain)?;
Ok(())
})
}
}

0 comments on commit b4336ed

Please sign in to comment.