Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement bundle limit runtime api to limit bundle weight and size #2568

Merged
merged 10 commits into from
Mar 11, 2024
Merged
19 changes: 17 additions & 2 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,13 +1644,28 @@ impl<T: Config> Pallet<T> {
.ok_or(BundleError::InvalidDomainId)?
.domain_config;

// TODO: check bundle weight with `domain_config.max_block_weight`
let domain_bundle_limit = calculate_max_bundle_weight_and_size(
DomainBlockLimit {
max_block_size: domain_config.max_block_size,
max_block_weight: domain_config.max_block_weight,
},
T::ConsensusSlotProbability::get(),
domain_config.bundle_slot_probability,
)
.ok_or(BundleError::UnableToCalculateBundleLimit)?;

ensure!(
opaque_bundle.size() <= domain_config.max_block_size,
opaque_bundle.size() <= domain_bundle_limit.max_bundle_size,
BundleError::BundleTooLarge
);

ensure!(
opaque_bundle
.estimated_weight()
.any_lte(domain_bundle_limit.max_bundle_weight),
NingLin-P marked this conversation as resolved.
Show resolved Hide resolved
BundleError::BundleTooHeavy
);

Self::check_extrinsics_root(opaque_bundle)?;

let proof_of_election = &sealed_header.header.proof_of_election;
Expand Down
23 changes: 16 additions & 7 deletions domains/client/domain-operator/src/domain_bundle_proposer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,22 @@ where
.maybe_clear(self.consensus_client.info().best_hash);

let bundle_vrf_hash = U256::from_be_bytes(proof_of_election.vrf_hash());
let domain_block_limit = self
let domain_bundle_limit = self
.consensus_client
.runtime_api()
.domain_block_limit(self.consensus_client.info().best_hash, self.domain_id)?
.domain_bundle_limit(self.consensus_client.info().best_hash, self.domain_id)?
.ok_or_else(|| {
sp_blockchain::Error::Application(
format!("Domain block limit for {:?} not found", self.domain_id).into(),
format!("Domain bundle limit for {:?} not found", self.domain_id).into(),
)
})?
.map_err(|arithmetic_error| {
sp_blockchain::Error::Application(
format!(
"Unable to calculate Domain bundle limit for {:?} due to error: {:?}",
self.domain_id, arithmetic_error
)
.into(),
)
})?;
let mut extrinsics = Vec::new();
Expand Down Expand Up @@ -199,11 +208,11 @@ where
})?;
let next_estimated_bundle_weight =
estimated_bundle_weight.saturating_add(tx_weight);
if next_estimated_bundle_weight.any_gt(domain_block_limit.max_block_weight) {
if next_estimated_bundle_weight.any_gt(domain_bundle_limit.max_bundle_weight) {
if skipped < MAX_SKIPPED_TRANSACTIONS
&& Percent::from_rational(
estimated_bundle_weight.ref_time(),
domain_block_limit.max_block_weight.ref_time(),
domain_bundle_limit.max_bundle_weight.ref_time(),
) < BUNDLE_UTILIZATION_THRESHOLD
{
skipped += 1;
Expand All @@ -213,9 +222,9 @@ where
}

let next_bundle_size = bundle_size + pending_tx_data.encoded_size() as u32;
if next_bundle_size > domain_block_limit.max_block_size {
if next_bundle_size > domain_bundle_limit.max_bundle_size {
if skipped < MAX_SKIPPED_TRANSACTIONS
&& Percent::from_rational(bundle_size, domain_block_limit.max_block_size)
&& Percent::from_rational(bundle_size, domain_bundle_limit.max_bundle_size)
< BUNDLE_UTILIZATION_THRESHOLD
{
skipped += 1;
Expand Down