Skip to content

Commit

Permalink
Merge branch 'main' of github.com:subspace/subspace into fix-ci-test
Browse files Browse the repository at this point in the history
  • Loading branch information
NingLin-P committed Feb 16, 2024
2 parents dd57a51 + d1bebad commit 68da2d7
Show file tree
Hide file tree
Showing 52 changed files with 1,801 additions and 563 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion crates/pallet-domains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "h
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" }
log = { version = "0.4.20", default-features = false }
pallet-balances = { 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-consensus-slots = { version = "0.10.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-consensus-subspace = { version = "0.1.0", default-features = false, path = "../sp-consensus-subspace" }
Expand All @@ -33,7 +34,7 @@ subspace-runtime-primitives = { version = "0.1.0", default-features = false, pat

[dev-dependencies]
domain-pallet-executive = { version = "0.1.0", default-features = false, path = "../../domains/pallets/executive" }
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
hex-literal = "0.4.1"
pallet-timestamp = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
pallet-block-fees = { version = "0.1.0", default-features = false, path = "../../domains/pallets/block-fees" }
sp-externalities = { version = "0.19.0", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
Expand All @@ -49,6 +50,7 @@ std = [
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"scale-info/std",
"sp-consensus-slots/std",
"sp-consensus-subspace/std",
Expand Down
2 changes: 2 additions & 0 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mod benchmarks {
bundle_slot_probability: (1, 1),
target_bundles_per_block: 10,
operator_allow_list: OperatorAllowList::Anyone,
initial_balances: Default::default(),
};

#[extrinsic_call]
Expand Down Expand Up @@ -434,6 +435,7 @@ mod benchmarks {
bundle_slot_probability: (1, 1),
target_bundles_per_block: 10,
operator_allow_list: OperatorAllowList::Anyone,
initial_balances: Default::default(),
};

assert_ok!(Domains::<T>::instantiate_domain(
Expand Down
80 changes: 77 additions & 3 deletions crates/pallet-domains/src/block_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use frame_support::{ensure, PalletError};
use scale_info::TypeInfo;
use sp_core::Get;
use sp_domains::merkle_tree::MerkleTree;
use sp_domains::{ConfirmedDomainBlock, DomainId, ExecutionReceipt, OperatorId};
use sp_domains::{
ChainId, ConfirmedDomainBlock, DomainId, DomainsTransfersTracker, ExecutionReceipt, OperatorId,
Transfers,
};
use sp_runtime::traits::{BlockNumberProvider, CheckedSub, One, Saturating, Zero};
use sp_std::cmp::Ordering;
use sp_std::collections::btree_map::BTreeMap;
Expand All @@ -34,6 +37,9 @@ pub enum Error {
InvalidExecutionTrace,
UnavailableConsensusBlockHash,
InvalidStateRoot,
BalanceOverflow,
DomainTransfersTracking,
InvalidDomainTransfers,
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -344,6 +350,21 @@ pub(crate) fn process_execution_receipt<T: Config>(
execution_receipt.consensus_block_number,
);

let block_fees = execution_receipt
.block_fees
.total_fees()
.ok_or(Error::BalanceOverflow)?;

ensure!(
execution_receipt
.transfers
.is_valid(ChainId::Domain(domain_id)),
Error::InvalidDomainTransfers
);

update_domain_transfers::<T>(domain_id, &execution_receipt.transfers, block_fees)
.map_err(|_| Error::DomainTransfersTracking)?;

LatestConfirmedDomainBlock::<T>::insert(
domain_id,
ConfirmedDomainBlock {
Expand Down Expand Up @@ -380,6 +401,59 @@ pub(crate) fn process_execution_receipt<T: Config>(
Ok(None)
}

type TransferTrackerError<T> =
<<T as Config>::DomainsTransfersTracker as DomainsTransfersTracker<BalanceOf<T>>>::Error;

/// Updates domain transfers for following scenarios
/// 1. Block fees are burned on domain
/// 2. Confirming incoming XDM transfers to the Domain
/// 3. Noting outgoing transfers from the domain
/// 4. Cancelling outgoing transfers from the domain.
fn update_domain_transfers<T: Config>(
domain_id: DomainId,
transfers: &Transfers<BalanceOf<T>>,
block_fees: BalanceOf<T>,
) -> Result<(), TransferTrackerError<T>> {
let Transfers {
transfers_in,
transfers_out,
transfers_rejected,
rejected_transfers_claimed,
} = transfers;

// confirm incoming transfers
let er_chain_id = ChainId::Domain(domain_id);
transfers_in
.iter()
.try_for_each(|(from_chain_id, amount)| {
T::DomainsTransfersTracker::confirm_transfer(*from_chain_id, er_chain_id, *amount)
})?;

// note outgoing transfers
transfers_out.iter().try_for_each(|(to_chain_id, amount)| {
T::DomainsTransfersTracker::note_transfer(er_chain_id, *to_chain_id, *amount)
})?;

// note rejected transfers
transfers_rejected
.iter()
.try_for_each(|(from_chain_id, amount)| {
T::DomainsTransfersTracker::reject_transfer(*from_chain_id, er_chain_id, *amount)
})?;

// claim rejected transfers
rejected_transfers_claimed
.iter()
.try_for_each(|(to_chain_id, amount)| {
T::DomainsTransfersTracker::claim_rejected_transfer(er_chain_id, *to_chain_id, *amount)
})?;

// deduct execution fees from domain
T::DomainsTransfersTracker::reduce_domain_balance(domain_id, block_fees)?;

Ok(())
}

fn add_new_receipt_to_block_tree<T: Config>(
domain_id: DomainId,
submitter: OperatorId,
Expand Down Expand Up @@ -687,7 +761,7 @@ mod tests {
H256::random(),
stale_receipt,
);
assert!(crate::Pallet::<Test>::submit_bundle(RawOrigin::None.into(), bundle,).is_err());
assert!(crate::Pallet::<Test>::submit_bundle(RawOrigin::None.into(), bundle).is_err());

assert_eq!(
BlockTreeNodes::<Test>::get(stale_receipt_hash)
Expand Down Expand Up @@ -735,7 +809,7 @@ mod tests {
H256::random(),
previous_head_receipt,
);
assert!(crate::Pallet::<Test>::submit_bundle(RawOrigin::None.into(), bundle,).is_err());
assert!(crate::Pallet::<Test>::submit_bundle(RawOrigin::None.into(), bundle).is_err());
});
}

Expand Down
Loading

0 comments on commit 68da2d7

Please sign in to comment.