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

Commit

Permalink
Merge branch 'ao-past-session-slashing-runtime' into ao-past-session-…
Browse files Browse the repository at this point in the history
…slashing-client

* ao-past-session-slashing-runtime:
  Companion: wasm-builder support stable Rust (#6967)
  Fix feature (#6966)
  configuration: backport async backing parameters from the feature branch (#6961)
  Histogram support in runtime metrics (#6935)
  Bump openssl from 0.10.38 to 0.10.48 (#6955)
  Proxy for Nomination Pools (#6846)
  Nomination Pools migration v5: RewardPool fix (#6957)
  • Loading branch information
ordian committed Mar 29, 2023
2 parents 0f3a964 + 2ba635b commit 55c7947
Show file tree
Hide file tree
Showing 24 changed files with 469 additions and 251 deletions.
389 changes: 202 additions & 187 deletions Cargo.lock

Large diffs are not rendered by default.

64 changes: 44 additions & 20 deletions node/metrics/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@
//! tracing support. This requires that the custom profiler (`TraceHandler`) to be
//! registered in substrate via a `logger_hook()`. Events emitted from runtime are
//! then captured/processed by the `TraceHandler` implementation.
//!
//! Don't add logs in this file because it gets executed before the logger is
//! initialized and they won't be delivered. Add println! statements if you need
//! to debug this code.
#![cfg(feature = "runtime-metrics")]

use codec::Decode;
use primitives::{
metric_definitions::{CounterDefinition, CounterVecDefinition},
metric_definitions::{CounterDefinition, CounterVecDefinition, HistogramDefinition},
RuntimeMetricLabelValues, RuntimeMetricOp, RuntimeMetricUpdate,
};
use std::{
collections::hash_map::HashMap,
sync::{Arc, Mutex, MutexGuard},
};
use substrate_prometheus_endpoint::{
register, Counter, CounterVec, Opts, PrometheusError, Registry, U64,
register, Counter, CounterVec, Histogram, HistogramOpts, Opts, PrometheusError, Registry, U64,
};
mod parachain;

const LOG_TARGET: &'static str = "metrics::runtime";

/// Holds the registered Prometheus metric collections.
#[derive(Clone, Default)]
pub struct Metrics {
counter_vecs: Arc<Mutex<HashMap<String, CounterVec<U64>>>>,
counters: Arc<Mutex<HashMap<String, Counter<U64>>>>,
histograms: Arc<Mutex<HashMap<String, Histogram>>>,
}

/// Runtime metrics wrapper.
Expand Down Expand Up @@ -80,7 +83,20 @@ impl RuntimeMetricsProvider {
})
}

/// Increment a counter with labels by a value.
/// Register a histogram metric
pub fn register_histogram(&self, hist: HistogramDefinition) {
self.with_histograms_lock_held(|mut hashmap| {
hashmap.entry(hist.name.to_owned()).or_insert(register(
Histogram::with_opts(
HistogramOpts::new(hist.name, hist.description).buckets(hist.buckets.to_vec()),
)?,
&self.0,
)?);
return Ok(())
})
}

/// Increment a counter with labels by a value
pub fn inc_counter_vec_by(&self, name: &str, value: u64, labels: &RuntimeMetricLabelValues) {
self.with_counter_vecs_lock_held(|mut hashmap| {
hashmap.entry(name.to_owned()).and_modify(|counter_vec| {
Expand All @@ -101,28 +117,35 @@ impl RuntimeMetricsProvider {
})
}

/// Observe a histogram. `value` should be in `ns`.
pub fn observe_histogram(&self, name: &str, value: u128) {
self.with_histograms_lock_held(|mut hashmap| {
hashmap
.entry(name.to_owned())
.and_modify(|histogram| histogram.observe(value as f64 / 1_000_000_000.0)); // ns to sec
Ok(())
})
}

fn with_counters_lock_held<F>(&self, do_something: F)
where
F: FnOnce(MutexGuard<'_, HashMap<String, Counter<U64>>>) -> Result<(), PrometheusError>,
{
let _ = self.1.counters.lock().map(do_something).or_else(|error| {
gum::error!(target: LOG_TARGET, "Cannot acquire the counter hashmap lock: {:?}", error);
Err(error)
});
let _ = self.1.counters.lock().map(do_something).or_else(|error| Err(error));
}

fn with_counter_vecs_lock_held<F>(&self, do_something: F)
where
F: FnOnce(MutexGuard<'_, HashMap<String, CounterVec<U64>>>) -> Result<(), PrometheusError>,
{
let _ = self.1.counter_vecs.lock().map(do_something).or_else(|error| {
gum::error!(
target: LOG_TARGET,
"Cannot acquire the countervec hashmap lock: {:?}",
error
);
Err(error)
});
let _ = self.1.counter_vecs.lock().map(do_something).or_else(|error| Err(error));
}

fn with_histograms_lock_held<F>(&self, do_something: F)
where
F: FnOnce(MutexGuard<'_, HashMap<String, Histogram>>) -> Result<(), PrometheusError>,
{
let _ = self.1.histograms.lock().map(do_something).or_else(|error| Err(error));
}
}

Expand All @@ -149,8 +172,8 @@ impl sc_tracing::TraceHandler for RuntimeMetricsProvider {
Ok(update_op) => {
self.parse_metric_update(update_op);
},
Err(e) => {
gum::error!(target: LOG_TARGET, "TraceEvent decode failed: {:?}", e);
Err(_) => {
// do nothing
},
}
}
Expand All @@ -165,6 +188,8 @@ impl RuntimeMetricsProvider {
self.inc_counter_vec_by(update.metric_name(), value, labels),
RuntimeMetricOp::IncrementCounter(value) =>
self.inc_counter_by(update.metric_name(), value),
RuntimeMetricOp::ObserveHistogram(value) =>
self.observe_histogram(update.metric_name(), value),
}
}

Expand All @@ -191,7 +216,6 @@ impl RuntimeMetricsProvider {
pub fn logger_hook() -> impl FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration) -> () {
|logger_builder, config| {
if config.prometheus_registry().is_none() {
gum::debug!(target: LOG_TARGET, "Prometheus registry is not configured.",);
return
}
let registry = config.prometheus_registry().cloned().unwrap();
Expand Down
3 changes: 2 additions & 1 deletion node/metrics/src/runtime/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use primitives::metric_definitions::{
PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS,
PARACHAIN_INHERENT_DATA_BITFIELDS_PROCESSED, PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED,
PARACHAIN_INHERENT_DATA_DISPUTE_SETS_INCLUDED, PARACHAIN_INHERENT_DATA_DISPUTE_SETS_PROCESSED,
PARACHAIN_INHERENT_DATA_WEIGHT,
PARACHAIN_INHERENT_DATA_WEIGHT, PARACHAIN_VERIFY_DISPUTE_SIGNATURE,
};

/// Register the parachain runtime metrics.
Expand All @@ -35,4 +35,5 @@ pub fn register_metrics(runtime_metrics_provider: &RuntimeMetricsProvider) {
runtime_metrics_provider.register_countervec(PARACHAIN_INHERENT_DATA_CANDIDATES_PROCESSED);
runtime_metrics_provider
.register_countervec(PARACHAIN_CREATE_INHERENT_BITFIELDS_SIGNATURE_CHECKS);
runtime_metrics_provider.register_histogram(PARACHAIN_VERIFY_DISPUTE_SIGNATURE);
}
5 changes: 4 additions & 1 deletion node/test/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ tokio = { version = "1.24.2", features = ["macros"] }

[features]
runtime-metrics=["polkadot-test-runtime/runtime-metrics"]
runtime-benchmarks=["polkadot-test-runtime/runtime-benchmarks"]
runtime-benchmarks=[
"polkadot-test-runtime/runtime-benchmarks",
"polkadot-service/runtime-benchmarks",
]
4 changes: 0 additions & 4 deletions parachain/test-parachains/adder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
//! Basic parachain that adds a number as part of its state.
#![no_std]
#![cfg_attr(
not(feature = "std"),
feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler)
)]

use parity_scale_codec::{Decode, Encode};
use tiny_keccak::{Hasher as _, Keccak};
Expand Down
1 change: 1 addition & 0 deletions parachain/test-parachains/halt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition.workspace = true

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
rustversion = "1.0.6"

[features]
default = [ "std" ]
Expand Down
14 changes: 13 additions & 1 deletion parachain/test-parachains/halt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@
use substrate_wasm_builder::WasmBuilder;

fn main() {
WasmBuilder::new().with_current_project().export_heap_base().build()
WasmBuilder::new().with_current_project().export_heap_base().build();

enable_alloc_error_handler();
}

#[rustversion::before(1.68)]
fn enable_alloc_error_handler() {
if !cfg!(feature = "std") {
println!("cargo:rustc-cfg=enable_alloc_error_handler");
}
}

#[rustversion::since(1.68)]
fn enable_alloc_error_handler() {}
9 changes: 3 additions & 6 deletions parachain/test-parachains/halt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
//! Basic parachain that executes forever.
#![no_std]
#![cfg_attr(
not(feature = "std"),
feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler)
)]
#![cfg_attr(enable_alloc_error_handler, feature(alloc_error_handler))]

// Make the WASM binary available.
#[cfg(feature = "std")]
Expand All @@ -39,10 +36,10 @@ pub fn wasm_binary_unwrap() -> &'static [u8] {
#[panic_handler]
#[no_mangle]
pub fn panic(_info: &core::panic::PanicInfo) -> ! {
core::intrinsics::abort()
core::arch::wasm32::unreachable();
}

#[cfg(not(feature = "std"))]
#[cfg(enable_alloc_error_handler)]
#[alloc_error_handler]
#[no_mangle]
pub fn oom(_: core::alloc::Layout) -> ! {
Expand Down
4 changes: 0 additions & 4 deletions parachain/test-parachains/undying/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
//! Basic parachain that adds a number as part of its state.
#![no_std]
#![cfg_attr(
not(feature = "std"),
feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler)
)]

use parity_scale_codec::{Decode, Encode};
use sp_std::vec::Vec;
Expand Down
20 changes: 20 additions & 0 deletions primitives/src/v4/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum RuntimeMetricOp {
IncrementCounterVec(u64, RuntimeMetricLabelValues),
/// Increment a counter metric by value.
IncrementCounter(u64),
/// Observe histogram value
ObserveHistogram(u128),
}

/// Runtime metric update event.
Expand Down Expand Up @@ -127,6 +129,16 @@ pub mod metric_definitions {
pub labels: &'a [&'static str],
}

/// `Histogram` metric definition
pub struct HistogramDefinition<'a> {
/// The name of the metric.
pub name: &'static str,
/// The description of the metric.
pub description: &'static str,
/// The buckets for the histogram
pub buckets: &'a [f64],
}

/// Counts parachain inherent data weights. Use `before` and `after` labels to differentiate
/// between the weight before and after filtering.
pub const PARACHAIN_INHERENT_DATA_WEIGHT: CounterVecDefinition = CounterVecDefinition {
Expand Down Expand Up @@ -176,4 +188,12 @@ pub mod metric_definitions {
description: "Counts the number of bitfields signature checked in `enter_inner`.",
labels: &["validity"],
};

/// Measures how much time does it take to verify a single validator signature of a dispute statement
pub const PARACHAIN_VERIFY_DISPUTE_SIGNATURE: HistogramDefinition =
HistogramDefinition {
name: "polkadot_parachain_verify_dispute_signature",
description: "How much time does it take to verify a single validator signature of a dispute statement, in seconds",
buckets: &[0.0, 0.00005, 0.00006, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.3, 0.5, 1.0],
};
}
23 changes: 22 additions & 1 deletion primitives/src/vstaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,26 @@
//! Staging Primitives.
// Put any primitives used by staging APIs functions here
pub use crate::v4::*;
use sp_std::prelude::*;

pub mod slashing;
use parity_scale_codec::{Decode, Encode};
use primitives::RuntimeDebug;
use scale_info::TypeInfo;

/// Candidate's acceptance limitations for asynchronous backing per relay parent.
#[derive(RuntimeDebug, Copy, Clone, PartialEq, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct AsyncBackingParams {
/// The maximum number of para blocks between the para head in a relay parent
/// and a new candidate. Restricts nodes from building arbitrary long chains
/// and spamming other validators.
///
/// When async backing is disabled, the only valid value is 0.
pub max_candidate_depth: u32,
/// How many ancestors of a relay parent are allowed to build candidates on top
/// of.
///
/// When async backing is disabled, the only valid value is 0.
pub allowed_ancestry_len: u32,
}
11 changes: 10 additions & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ pub enum ProxyType {
CancelProxy,
Auction,
Society,
NominationPools,
}

impl Default for ProxyType {
Expand Down Expand Up @@ -1024,6 +1025,9 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
RuntimeCall::FastUnstake(..)
)
},
ProxyType::NominationPools => {
matches!(c, RuntimeCall::NominationPools(..) | RuntimeCall::Utility(..))
},
ProxyType::IdentityJudgement => matches!(
c,
RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. }) |
Expand Down Expand Up @@ -1475,12 +1479,17 @@ impl Get<Perbill> for NominationPoolsMigrationV4OldPallet {

/// All migrations that will run on the next runtime upgrade.
///
/// Should be cleared after every release.
/// This contains the combined migrations of the last 10 releases. It allows to skip runtime
/// upgrades in case governance decides to do so.
#[allow(deprecated)]
pub type Migrations = (
// 0.9.40
pallet_nomination_pools::migration::v4::MigrateToV4<
Runtime,
NominationPoolsMigrationV4OldPallet,
>,
// Unreleased - add new migrations here:
pallet_nomination_pools::migration::v5::MigrateToV5<Runtime>,
parachains_configuration::migration::v5::MigrateToV5<Runtime>,
);

Expand Down
3 changes: 2 additions & 1 deletion runtime/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sp-std = { package = "sp-std", git = "https://github.com/paritytech/substrate",
sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
parity-scale-codec = { version = "3.4.0", default-features = false }
primitives = { package = "polkadot-primitives", path = "../../primitives", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }

bs58 = { version = "0.4.0", default-features = false, features = ["alloc"] }

Expand All @@ -21,4 +22,4 @@ std = [
"primitives/std",
"bs58/std"
]
runtime-metrics = ["sp-tracing/with-tracing"]
runtime-metrics = ["sp-tracing/with-tracing", "frame-benchmarking"]
Loading

0 comments on commit 55c7947

Please sign in to comment.