Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Make authorship soft deadline configurable. #10125

Merged
merged 5 commits into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions client/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use sp_inherents::InherentData;
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as BlockT, Hash as HashT, Header as HeaderT},
Digest,
Digest, Percent, SaturatedConversion,
};
use std::{marker::PhantomData, pin::Pin, sync::Arc, time};

Expand All @@ -58,6 +58,8 @@ use sc_proposer_metrics::MetricsLink as PrometheusMetrics;
/// transferred to other nodes.
pub const DEFAULT_BLOCK_SIZE_LIMIT: usize = 4 * 1024 * 1024 + 512;

const DEFAULT_SOFT_DEADLINE_PERCENT: Percent = Percent::from_percent(50);

/// [`Proposer`] factory.
pub struct ProposerFactory<A, B, C, PR> {
spawn_handle: Box<dyn SpawnNamed>,
Expand All @@ -72,6 +74,14 @@ pub struct ProposerFactory<A, B, C, PR> {
/// If no `block_size_limit` is passed to [`sp_consensus::Proposer::propose`], this block size
/// limit will be used.
default_block_size_limit: usize,
/// Soft deadline percentage of hard deadline.
///
/// The value is used to compute soft deadline during block production.
/// The soft deadline indicates where we should stop attempting to add transactions
/// to the block, which exhaust resources. After soft deadline is reached,
/// we switch to a fixed-amount mode, in which after we see `MAX_SKIPPED_TRANSACTIONS`
/// transactions which exhaust resrouces, we will conclude that the block is full.
soft_deadline_percent: Percent,
telemetry: Option<TelemetryHandle>,
/// When estimating the block size, should the proof be included?
include_proof_in_block_size_estimation: bool,
Expand All @@ -96,6 +106,7 @@ impl<A, B, C> ProposerFactory<A, B, C, DisableProofRecording> {
transaction_pool,
metrics: PrometheusMetrics::new(prometheus),
default_block_size_limit: DEFAULT_BLOCK_SIZE_LIMIT,
soft_deadline_percent: DEFAULT_SOFT_DEADLINE_PERCENT,
telemetry,
client,
include_proof_in_block_size_estimation: false,
Expand Down Expand Up @@ -124,6 +135,7 @@ impl<A, B, C> ProposerFactory<A, B, C, EnableProofRecording> {
transaction_pool,
metrics: PrometheusMetrics::new(prometheus),
default_block_size_limit: DEFAULT_BLOCK_SIZE_LIMIT,
soft_deadline_percent: DEFAULT_SOFT_DEADLINE_PERCENT,
telemetry,
include_proof_in_block_size_estimation: true,
_phantom: PhantomData,
Expand All @@ -147,6 +159,22 @@ impl<A, B, C, PR> ProposerFactory<A, B, C, PR> {
pub fn set_default_block_size_limit(&mut self, limit: usize) {
self.default_block_size_limit = limit;
}

/// Set soft deadline percentage.
///
/// The value is used to compute soft deadline during block production.
/// The soft deadline indicates where we should stop attempting to add transactions
/// to the block, which exhaust resources. After soft deadline is reached,
/// we switch to a fixed-amount mode, in which after we see `MAX_SKIPPED_TRANSACTIONS`
/// transactions which exhaust resrouces, we will conclude that the block is full.
///
/// Setting the value too low will significantly limit the amount of transactions
/// we try in case they exhaust resources. Setting the value too high can
/// potentially open a DoS vector, where many "exhaust resources" transactions
/// are being tried with no success, hence block producer ends up creating an empty block.
pub fn set_soft_deadline(&mut self, percent: Percent) {
self.soft_deadline_percent = percent;
}
}

impl<B, Block, C, A, PR> ProposerFactory<A, B, C, PR>
Expand Down Expand Up @@ -184,6 +212,7 @@ where
now,
metrics: self.metrics.clone(),
default_block_size_limit: self.default_block_size_limit,
soft_deadline_percent: self.soft_deadline_percent,
telemetry: self.telemetry.clone(),
_phantom: PhantomData,
include_proof_in_block_size_estimation: self.include_proof_in_block_size_estimation,
Expand Down Expand Up @@ -229,6 +258,7 @@ pub struct Proposer<B, Block: BlockT, C, A: TransactionPool, PR> {
metrics: PrometheusMetrics,
default_block_size_limit: usize,
include_proof_in_block_size_estimation: bool,
soft_deadline_percent: Percent,
telemetry: Option<TelemetryHandle>,
_phantom: PhantomData<(B, PR)>,
}
Expand Down Expand Up @@ -340,7 +370,10 @@ where
// proceed with transactions
// We calculate soft deadline used only in case we start skipping transactions.
let now = (self.now)();
let soft_deadline = now + deadline.saturating_duration_since(now) / 2;
let left = deadline.saturating_duration_since(now);
let left_micros: u64 = left.as_micros().saturated_into();
let soft_deadline =
now + time::Duration::from_micros(self.soft_deadline_percent.mul_floor(left_micros));
let block_timer = time::Instant::now();
let mut skipped = 0;
let mut unqueue_invalid = Vec::new();
Expand Down