Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 3 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
36 changes: 35 additions & 1 deletion client/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use sp_inherents::InherentData;
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as BlockT, DigestFor, Hash as HashT, Header as HeaderT},
Percent, SaturatedConversion,
};
use std::{marker::PhantomData, pin::Pin, sync::Arc, time};

Expand All @@ -57,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 @@ -71,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 @@ -95,12 +106,29 @@ 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,
_phantom: PhantomData,
}
}

/// Set soft deadline percentage.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be moved into the impl block below in line 177.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done.

///
/// 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<A, B, C> ProposerFactory<A, B, C, EnableProofRecording> {
Expand All @@ -123,6 +151,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 Down Expand Up @@ -183,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 @@ -228,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 @@ -338,7 +369,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