Skip to content

Commit

Permalink
Ensure the DomainChainAllowlistUpdate storage is always exist for a g…
Browse files Browse the repository at this point in the history
…iven domain

Even DomainChainAllowlistUpdate is an empty value for a given domain it is still
necessary to generate a proof-of-empty-value storage proof for the fraud proof

Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed May 7, 2024
1 parent ceeb20b commit 0865c11
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/pallet-domains/src/domain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use scale_info::TypeInfo;
use sp_core::Get;
use sp_domains::{
derive_domain_block_hash, DomainBundleLimit, DomainId, DomainsDigestItem,
DomainsTransfersTracker, OperatorAllowList, RuntimeId, RuntimeType,
DomainsTransfersTracker, OnDomainInstantiated, OperatorAllowList, RuntimeId, RuntimeType,
};
use sp_runtime::traits::{CheckedAdd, Zero};
use sp_runtime::DigestItem;
Expand Down Expand Up @@ -293,6 +293,7 @@ pub(crate) fn do_instantiate_domain<T: Config>(
);

import_genesis_receipt::<T>(domain_id, genesis_receipt);
T::OnDomainInstantiated::on_domain_instantiated(domain_id);

frame_system::Pallet::<T>::deposit_log(DigestItem::domain_instantiation(domain_id));

Expand Down
7 changes: 5 additions & 2 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ mod pallet {
use sp_domains::bundle_producer_election::ProofOfElectionError;
use sp_domains::{
BundleDigest, ConfirmedDomainBlock, DomainBundleSubmitted, DomainId,
DomainsTransfersTracker, EpochIndex, GenesisDomain, OperatorAllowList, OperatorId,
OperatorPublicKey, RuntimeId, RuntimeObject, RuntimeType,
DomainsTransfersTracker, EpochIndex, GenesisDomain, OnDomainInstantiated,
OperatorAllowList, OperatorId, OperatorPublicKey, RuntimeId, RuntimeObject, RuntimeType,
};
use sp_domains_fraud_proof::fraud_proof::FraudProof;
use sp_domains_fraud_proof::InvalidTransactionCode;
Expand Down Expand Up @@ -389,6 +389,9 @@ mod pallet {

/// Post hook to notify accepted domain bundles in previous block.
type DomainBundleSubmitted: DomainBundleSubmitted;

/// A hook to call after a domain is instantiated
type OnDomainInstantiated: OnDomainInstantiated;
}

#[pallet::pallet]
Expand Down
1 change: 1 addition & 0 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ impl pallet_domains::Config for Test {
type BundleLongevity = BundleLongevity;
type ConsensusSlotProbability = SlotProbability;
type DomainBundleSubmitted = ();
type OnDomainInstantiated = ();
type Balance = Balance;
}

Expand Down
9 changes: 9 additions & 0 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,15 @@ impl DomainBundleSubmitted for () {
fn domain_bundle_submitted(_domain_id: DomainId) {}
}

/// A hook to call after a domain is instantiated
pub trait OnDomainInstantiated {
fn on_domain_instantiated(domain_id: DomainId);
}

impl OnDomainInstantiated for () {
fn on_domain_instantiated(_domain_id: DomainId) {}
}

pub type ExecutionReceiptFor<DomainHeader, CBlock, Balance> = ExecutionReceipt<
NumberFor<CBlock>,
<CBlock as BlockT>::Hash,
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ impl pallet_domains::Config for Runtime {
type MaxInitialDomainAccounts = MaxInitialDomainAccounts;
type MinInitialDomainAccountBalance = MinInitialDomainAccountBalance;
type DomainBundleSubmitted = Messenger;
type OnDomainInstantiated = Messenger;
type Balance = Balance;
}

Expand Down
25 changes: 20 additions & 5 deletions domains/pallets/messenger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;
use scale_info::TypeInfo;
use sp_core::U256;
use sp_domains::DomainId;
use sp_domains::{DomainAllowlistUpdates, DomainId};
use sp_messenger::messages::{
ChainId, ChannelId, CrossDomainMessage, FeeModel, Message, MessageId, Nonce,
};
Expand Down Expand Up @@ -292,8 +292,9 @@ mod pallet {
#[pallet::getter(fn chain_allowlist)]
pub(super) type ChainAllowlist<T: Config> = StorageValue<_, BTreeSet<ChainId>, ValueQuery>;

/// A temporary storage to store any allowlist updates to domain.
/// Will be cleared in the next block once the previous block has a domain bundle.
/// A storage to store any allowlist updates to domain. The updates will be cleared in the next block
/// once the previous block has a domain bundle, but a empty value should be left because in the invalid
/// extrinsic root fraud proof the prover need to generate a proof-of-empty-value for the domain.
#[pallet::storage]
#[pallet::getter(fn domain_chain_allowlist_updates)]
pub(super) type DomainChainAllowlistUpdate<T: Config> =
Expand Down Expand Up @@ -1211,7 +1212,7 @@ mod pallet {
pub fn domain_chains_allowlist_update(
domain_id: DomainId,
) -> Option<DomainAllowlistUpdates> {
DomainChainAllowlistUpdate::<T>::get(domain_id)
DomainChainAllowlistUpdate::<T>::get(domain_id).filter(|updates| !updates.is_empty())
}

pub fn domain_allow_list_update_storage_key(domain_id: DomainId) -> Vec<u8> {
Expand Down Expand Up @@ -1251,6 +1252,20 @@ where

impl<T: Config> sp_domains::DomainBundleSubmitted for Pallet<T> {
fn domain_bundle_submitted(domain_id: DomainId) {
DomainChainAllowlistUpdate::<T>::remove(domain_id);
// NOTE: clear the updates leave an empty value but does not delete the value for the
// domain completely because in the invalid extrinsic root fraud proof the prover need
// to generate a proof-of-empty-value for the domain.
DomainChainAllowlistUpdate::<T>::mutate(domain_id, |maybe_updates| {
if let Some(ref mut updates) = maybe_updates {
updates.clear();
}
});
}
}

// TODO: runtime mitigation code for 3h
impl<T: Config> sp_domains::OnDomainInstantiated for Pallet<T> {
fn on_domain_instantiated(domain_id: DomainId) {
DomainChainAllowlistUpdate::<T>::insert(domain_id, DomainAllowlistUpdates::default());
}
}
1 change: 1 addition & 0 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ impl pallet_domains::Config for Runtime {
type MaxInitialDomainAccounts = MaxInitialDomainAccounts;
type MinInitialDomainAccountBalance = MinInitialDomainAccountBalance;
type DomainBundleSubmitted = Messenger;
type OnDomainInstantiated = Messenger;
type Balance = Balance;
}

Expand Down

0 comments on commit 0865c11

Please sign in to comment.