Skip to content

Commit

Permalink
Init RuntimeLogger automatically for each runtime api call (parityt…
Browse files Browse the repository at this point in the history
…ech#8128)

* Init `RuntimeLogger` automatically for each runtime api call

This pr change the runtime api in such a way to always and automatically
enable the `RuntimeLogger`. This enables the user to use `log` or
`tracing` from inside the runtime to create log messages. As logging
introduces some extra code and especially increases the size of the wasm
blob. It is advised to disable all logging completely with
`sp-api/disable-logging` when doing the wasm builds for the on-chain
wasm runtime.

Besides these changes, the pr also brings most of the logging found in
frame to the same format "runtime::*".

* Update frame/im-online/src/lib.rs

Co-authored-by: Guillaume Thiolliere <[email protected]>

* Update test-utils/runtime/Cargo.toml

* Fix test

* Don't use tracing in the runtime, as we don't support it :D

* Fixes

Co-authored-by: Guillaume Thiolliere <[email protected]>
  • Loading branch information
bkchr and gui1117 authored Mar 1, 2021
1 parent e84d2ae commit dd29596
Show file tree
Hide file tree
Showing 65 changed files with 571 additions and 422 deletions.
26 changes: 24 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0", default-features =
serde = { version = "1.0.102", optional = true }
static_assertions = "1.1.0"
hex-literal = { version = "0.3.1", optional = true }
log = { version = "0.4.14", default-features = false }

# primitives
sp-authority-discovery = { version = "3.0.0", default-features = false, path = "../../../primitives/authority-discovery" }
Expand Down Expand Up @@ -156,6 +157,7 @@ std = [
"pallet-society/std",
"pallet-recovery/std",
"pallet-vesting/std",
"log/std",
"frame-try-runtime/std",
]
runtime-benchmarks = [
Expand Down
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use sp_std::prelude::*;
use frame_support::{
construct_runtime, parameter_types, debug, RuntimeDebug,
construct_runtime, parameter_types, RuntimeDebug,
weights::{
Weight, IdentityFee,
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
Expand Down Expand Up @@ -845,7 +845,7 @@ impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for R
);
let raw_payload = SignedPayload::new(call, extra)
.map_err(|e| {
debug::warn!("Unable to create signed payload: {:?}", e);
log::warn!("Unable to create signed payload: {:?}", e);
})
.ok()?;
let signature = raw_payload
Expand Down
2 changes: 2 additions & 0 deletions frame/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sp-session = { version = "3.0.0", default-features = false, path = "../../primit
sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
sp-timestamp = { version = "3.0.0", default-features = false, path = "../../primitives/timestamp" }
log = { version = "0.4.14", default-features = false }

[dev-dependencies]
pallet-balances = { version = "3.0.0", path = "../balances" }
Expand Down Expand Up @@ -59,6 +60,7 @@ std = [
"sp-staking/std",
"sp-std/std",
"sp-timestamp/std",
"log/std",
]
runtime-benchmarks = ["frame-benchmarking"]
try-runtime = ["frame-support/try-runtime"]
22 changes: 13 additions & 9 deletions frame/babe/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
//! definition.
//!
use frame_support::{
debug,
traits::{Get, KeyOwnerProofSystem},
};
use frame_support::traits::{Get, KeyOwnerProofSystem};
use sp_consensus_babe::{EquivocationProof, Slot};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity,
Expand Down Expand Up @@ -163,8 +160,15 @@ where
let call = Call::report_equivocation_unsigned(equivocation_proof, key_owner_proof);

match SubmitTransaction::<T, Call<T>>::submit_unsigned_transaction(call.into()) {
Ok(()) => debug::info!("Submitted BABE equivocation report."),
Err(e) => debug::error!("Error submitting equivocation report: {:?}", e),
Ok(()) => log::info!(
target: "runtime::babe",
"Submitted BABE equivocation report.",
),
Err(e) => log::error!(
target: "runtime::babe",
"Error submitting equivocation report: {:?}",
e,
),
}

Ok(())
Expand All @@ -186,9 +190,9 @@ impl<T: Config> frame_support::unsigned::ValidateUnsigned for Module<T> {
match source {
TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ }
_ => {
debug::warn!(
target: "babe",
"rejecting unsigned report equivocation transaction because it is not local/in-block."
log::warn!(
target: "runtime::babe",
"rejecting unsigned report equivocation transaction because it is not local/in-block.",
);

return InvalidTransaction::Call.into();
Expand Down
2 changes: 2 additions & 0 deletions frame/balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
log = { version = "0.4.14", default-features = false }

[dev-dependencies]
sp-io = { version = "3.0.0", path = "../../primitives/io" }
Expand All @@ -36,6 +37,7 @@ std = [
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"log/std",
]
runtime-benchmarks = ["frame-benchmarking"]
try-runtime = ["frame-support/try-runtime"]
6 changes: 4 additions & 2 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Update the account entry for `who`, given the locks.
fn update_locks(who: &T::AccountId, locks: &[BalanceLock<T::Balance>]) {
if locks.len() as u32 > T::MaxLocks::get() {
frame_support::debug::warn!(
log::warn!(
target: "runtime::balances",
"Warning: A user has more currency locks than expected. \
A runtime configuration adjustment may be needed."
);
Expand Down Expand Up @@ -790,7 +791,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
// No providers for the locks. This is impossible under normal circumstances
// since the funds that are under the lock will themselves be stored in the
// account and therefore will need a reference.
frame_support::debug::warn!(
log::warn!(
target: "runtime::balances",
"Warning: Attempt to introduce lock consumer reference, yet no providers. \
This is unexpected but should be safe."
);
Expand Down
2 changes: 2 additions & 0 deletions frame/benchmarking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sp-io = { version = "3.0.0", path = "../../primitives/io", default-features = fa
sp-storage = { version = "3.0.0", path = "../../primitives/storage", default-features = false }
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
log = { version = "0.4.14", default-features = false }

[dev-dependencies]
hex-literal = "0.3.1"
Expand All @@ -40,4 +41,5 @@ std = [
"frame-support/std",
"frame-system/std",
"linregress",
"log/std",
]
12 changes: 9 additions & 3 deletions frame/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ pub use utils::*;
pub use analysis::{Analysis, BenchmarkSelector, RegressionModel};
#[doc(hidden)]
pub use sp_io::storage::root as storage_root;
#[doc(hidden)]
pub use sp_runtime::traits::Zero;
#[doc(hidden)]
pub use frame_support;
#[doc(hidden)]
pub use paste;
#[doc(hidden)]
pub use sp_storage::TrackedStorageKey;
#[doc(hidden)]
pub use log;

/// Construct pallet benchmarks for weighing dispatchables.
///
Expand Down Expand Up @@ -751,7 +757,7 @@ macro_rules! impl_benchmark {
closure_to_benchmark()?;
} else {
// Time the extrinsic logic.
frame_support::debug::trace!(
$crate::log::trace!(
target: "benchmark",
"Start Benchmark: {:?}", c
);
Expand All @@ -764,12 +770,12 @@ macro_rules! impl_benchmark {
let elapsed_extrinsic = finish_extrinsic - start_extrinsic;
// Commit the changes to get proper write count
$crate::benchmarking::commit_db();
frame_support::debug::trace!(
$crate::log::trace!(
target: "benchmark",
"End Benchmark: {} ns", elapsed_extrinsic
);
let read_write_count = $crate::benchmarking::read_write_count();
frame_support::debug::trace!(
$crate::log::trace!(
target: "benchmark",
"Read/Write Count {:?}", read_write_count
);
Expand Down
2 changes: 2 additions & 0 deletions frame/collective/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
log = { version = "0.4.14", default-features = false }

[dev-dependencies]
hex-literal = "0.3.1"
Expand All @@ -38,6 +39,7 @@ std = [
"frame-support/std",
"sp-runtime/std",
"frame-system/std",
"log/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
Loading

0 comments on commit dd29596

Please sign in to comment.