mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 07:37:57 +00:00
Make authorship soft deadline configurable. (#10125)
* Make soft deadline configurable. * cargo +nightly fmt --all * Move setter where it belongs.
This commit is contained in:
@@ -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};
|
||||
|
||||
@@ -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>,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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>
|
||||
@@ -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,
|
||||
@@ -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)>,
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user