Skip to content

Commit

Permalink
Merge branch 'main' into snap-sync-refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shamil-gadelshin authored Jul 18, 2024
2 parents 07f876e + 047f17a commit 5007ab7
Show file tree
Hide file tree
Showing 54 changed files with 3,308 additions and 1,361 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

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

50 changes: 32 additions & 18 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use frame_system::{Pallet as System, RawOrigin};
use sp_core::crypto::{Ss58Codec, UncheckedFrom};
use sp_core::ByteArray;
use sp_domains::{
dummy_opaque_bundle, ConfirmedDomainBlock, DomainId, ExecutionReceipt, OperatorAllowList,
OperatorId, OperatorPublicKey, OperatorSignature, PermissionedActionAllowedBy, RuntimeType,
dummy_opaque_bundle, BlockFees, DomainId, ExecutionReceipt, OperatorAllowList, OperatorId,
OperatorPublicKey, OperatorSignature, PermissionedActionAllowedBy, RuntimeType, Transfers,
};
use sp_domains_fraud_proof::fraud_proof::FraudProof;
use sp_runtime::traits::{CheckedAdd, One, Zero};
Expand Down Expand Up @@ -753,19 +753,26 @@ mod benchmarks {
do_finalize_domain_epoch_staking::<T>(domain_id)
.expect("finalize domain staking should success");

// Update the `LatestConfirmedDomainBlock` so unlock can success
// Update the `LatestConfirmedDomainExecutionReceipt` so unlock can success
let confirmed_domain_block_number =
Pallet::<T>::latest_confirmed_domain_block_number(domain_id)
+ T::StakeWithdrawalLockingPeriod::get()
+ One::one();
LatestConfirmedDomainBlock::<T>::insert(
LatestConfirmedDomainExecutionReceipt::<T>::insert(
domain_id,
ConfirmedDomainBlock {
block_number: confirmed_domain_block_number,
block_hash: Default::default(),
parent_block_receipt_hash: Default::default(),
state_root: Default::default(),
extrinsics_root: Default::default(),
ExecutionReceiptOf::<T> {
domain_block_number: confirmed_domain_block_number,
domain_block_hash: Default::default(),
domain_block_extrinsic_root: Default::default(),
parent_domain_block_receipt_hash: Default::default(),
consensus_block_number: Default::default(),
consensus_block_hash: Default::default(),
inboxed_bundles: vec![],
final_state_root: Default::default(),
execution_trace: vec![],
execution_trace_root: Default::default(),
block_fees: BlockFees::default(),
transfers: Transfers::default(),
},
);

Expand Down Expand Up @@ -797,19 +804,26 @@ mod benchmarks {
operator_id,
));

// Update the `LatestConfirmedDomainBlock` so unlock can success
// Update the `LatestConfirmedDomainExecutionReceipt` so unlock can success
let confirmed_domain_block_number =
Pallet::<T>::latest_confirmed_domain_block_number(domain_id)
+ T::StakeWithdrawalLockingPeriod::get()
+ One::one();
LatestConfirmedDomainBlock::<T>::insert(
LatestConfirmedDomainExecutionReceipt::<T>::insert(
domain_id,
ConfirmedDomainBlock {
block_number: confirmed_domain_block_number,
block_hash: Default::default(),
parent_block_receipt_hash: Default::default(),
state_root: Default::default(),
extrinsics_root: Default::default(),
ExecutionReceiptOf::<T> {
domain_block_number: confirmed_domain_block_number,
domain_block_hash: Default::default(),
domain_block_extrinsic_root: Default::default(),
parent_domain_block_receipt_hash: Default::default(),
consensus_block_number: Default::default(),
consensus_block_hash: Default::default(),
inboxed_bundles: vec![],
final_state_root: Default::default(),
execution_trace: vec![],
execution_trace_root: Default::default(),
block_fees: BlockFees::default(),
transfers: Transfers::default(),
},
);

Expand Down
72 changes: 46 additions & 26 deletions crates/pallet-domains/src/block_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
BalanceOf, BlockTree, BlockTreeNodeFor, BlockTreeNodes, Config, ConsensusBlockHash,
DomainBlockNumberFor, DomainHashingFor, DomainRuntimeUpgradeRecords, ExecutionInbox,
ExecutionReceiptOf, HeadReceiptExtended, HeadReceiptNumber, InboxedBundleAuthor,
LatestConfirmedDomainBlock, LatestSubmittedER, Pallet, ReceiptHashFor,
LatestConfirmedDomainExecutionReceipt, LatestSubmittedER, Pallet, ReceiptHashFor,
};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
Expand All @@ -18,8 +18,8 @@ use scale_info::TypeInfo;
use sp_core::Get;
use sp_domains::merkle_tree::MerkleTree;
use sp_domains::{
ChainId, ConfirmedDomainBlock, DomainId, DomainsTransfersTracker, ExecutionReceipt,
OnChainRewards, OperatorId, Transfers,
ChainId, DomainId, DomainsTransfersTracker, ExecutionReceipt, OnChainRewards, OperatorId,
Transfers,
};
use sp_runtime::traits::{BlockNumberProvider, CheckedSub, One, Saturating, Zero};
use sp_std::cmp::Ordering;
Expand Down Expand Up @@ -371,6 +371,11 @@ pub(crate) fn process_execution_receipt<T: Config>(
// its receipt's `extrinsics_root` anymore.
let _ = ExecutionInbox::<T>::clear_prefix((domain_id, to_prune), u32::MAX, None);

LatestConfirmedDomainExecutionReceipt::<T>::insert(
domain_id,
execution_receipt.clone(),
);

ConsensusBlockHash::<T>::remove(
domain_id,
execution_receipt.consensus_block_number,
Expand Down Expand Up @@ -405,18 +410,6 @@ pub(crate) fn process_execution_receipt<T: Config>(
T::OnChainRewards::on_chain_rewards(chain_id, reward)
});

LatestConfirmedDomainBlock::<T>::insert(
domain_id,
ConfirmedDomainBlock {
block_number: to_prune,
block_hash: execution_receipt.domain_block_hash,
parent_block_receipt_hash: execution_receipt
.parent_domain_block_receipt_hash,
state_root: execution_receipt.final_state_root,
extrinsics_root: execution_receipt.domain_block_extrinsic_root,
},
);

return Ok(Some(ConfirmedDomainBlockInfo {
domain_block_number: to_prune,
operator_ids,
Expand Down Expand Up @@ -559,16 +552,7 @@ pub(crate) fn import_genesis_receipt<T: Config>(
let er_hash = genesis_receipt.hash::<DomainHashingFor<T>>();
let domain_block_number = genesis_receipt.domain_block_number;

LatestConfirmedDomainBlock::<T>::insert(
domain_id,
ConfirmedDomainBlock {
block_number: domain_block_number,
block_hash: genesis_receipt.domain_block_hash,
parent_block_receipt_hash: Default::default(),
state_root: genesis_receipt.final_state_root,
extrinsics_root: genesis_receipt.domain_block_extrinsic_root,
},
);
LatestConfirmedDomainExecutionReceipt::<T>::insert(domain_id, genesis_receipt.clone());

let block_tree_node = BlockTreeNode {
execution_receipt: genesis_receipt,
Expand Down Expand Up @@ -616,10 +600,12 @@ mod tests {
use crate::tests::{
create_dummy_bundle_with_receipts, create_dummy_receipt, extend_block_tree,
extend_block_tree_from_zero, get_block_tree_node_at, new_test_ext_with_extensions,
register_genesis_domain, run_to_block, BlockTreePruningDepth, Test,
register_genesis_domain, run_to_block, BlockTreePruningDepth, Domains, Test,
};
use crate::FrozenDomains;
use frame_support::dispatch::RawOrigin;
use frame_support::{assert_err, assert_ok};
use frame_system::Origin;
use sp_core::H256;
use sp_domains::{BundleDigest, InboxedBundle, InvalidBundleType};

Expand Down Expand Up @@ -971,6 +957,40 @@ mod tests {
});
}

#[test]
fn test_prune_domain_execution_receipt() {
let creator = 0u128;
let operator_id = 1u64;
let mut ext = new_test_ext_with_extensions();
ext.execute_with(|| {
let domain_id = register_genesis_domain(creator, vec![operator_id]);
let _next_receipt = extend_block_tree_from_zero(domain_id, operator_id, 3);
let head_receipt_number = HeadReceiptNumber::<Test>::get(domain_id);

// freeze domain
assert!(!FrozenDomains::<Test>::get().contains(&domain_id));
Domains::freeze_domain(Origin::<Test>::Root.into(), domain_id).unwrap();
assert!(FrozenDomains::<Test>::get().contains(&domain_id));

// prune execution recept
let head_receipt_hash = BlockTree::<Test>::get(domain_id, head_receipt_number).unwrap();
Domains::prune_domain_execution_receipt(
Origin::<Test>::Root.into(),
domain_id,
head_receipt_hash,
)
.unwrap();
assert_eq!(
HeadReceiptNumber::<Test>::get(domain_id),
head_receipt_number - 1
);

// unfreeze domain
Domains::unfreeze_domain(Origin::<Test>::Root.into(), domain_id).unwrap();
assert!(!FrozenDomains::<Test>::get().contains(&domain_id));
})
}

#[test]
fn test_invalid_receipt() {
let creator = 0u128;
Expand Down
Loading

0 comments on commit 5007ab7

Please sign in to comment.