From 8cb72571798a0d2d9ad089ba977c310ddb2b5c11 Mon Sep 17 00:00:00 2001 From: PG Herveou Date: Tue, 25 Apr 2023 13:46:20 +0200 Subject: [PATCH] contracts Add LOG_TARGET constant (#14002) * contracts Add LOG_TARGET constant * Update frame/contracts/src/lib.rs Co-authored-by: Sasha Gryaznov --------- Co-authored-by: Sasha Gryaznov --- frame/contracts/src/exec.rs | 6 +++--- frame/contracts/src/lib.rs | 7 +++++++ frame/contracts/src/storage/meter.rs | 6 +++--- frame/contracts/src/wasm/mod.rs | 6 +++--- frame/contracts/src/wasm/prepare.rs | 10 +++++----- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/frame/contracts/src/exec.rs b/frame/contracts/src/exec.rs index 574f4495e479f..46a611dbfd09b 100644 --- a/frame/contracts/src/exec.rs +++ b/frame/contracts/src/exec.rs @@ -19,7 +19,7 @@ use crate::{ gas::GasMeter, storage::{self, DepositAccount, WriteOutcome}, BalanceOf, CodeHash, Config, ContractInfo, ContractInfoOf, DebugBufferVec, Determinism, Error, - Event, Nonce, Pallet as Contracts, Schedule, System, + Event, Nonce, Pallet as Contracts, Schedule, System, LOG_TARGET, }; use frame_support::{ crypto::ecdsa::ECDSAExt, @@ -1002,7 +1002,7 @@ where } else { if let Some((msg, false)) = self.debug_message.as_ref().map(|m| (m, m.is_empty())) { log::debug!( - target: "runtime::contracts", + target: LOG_TARGET, "Execution finished with debug buffer: {}", core::str::from_utf8(msg).unwrap_or(""), ); @@ -1331,7 +1331,7 @@ where .try_extend(&mut msg.bytes()) .map_err(|_| { log::debug!( - target: "runtime::contracts", + target: LOG_TARGET, "Debug buffer (of {} bytes) exhausted!", DebugBufferVec::::bound(), ) diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index fa230a8cade14..118da36ae88a1 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -163,6 +163,13 @@ type OldWeight = u64; /// that this value makes sense for a memory location or length. const SENTINEL: u32 = u32::MAX; +/// The target that is used for the log output emitted by this crate. +/// +/// Hence you can use this target to selectively increase the log level for this crate. +/// +/// Example: `RUST_LOG=runtime::contracts=debug my_code --dev` +const LOG_TARGET: &str = "runtime::contracts"; + #[frame_support::pallet] pub mod pallet { use super::*; diff --git a/frame/contracts/src/storage/meter.rs b/frame/contracts/src/storage/meter.rs index 497ee03ac319a..51a0af574bcd5 100644 --- a/frame/contracts/src/storage/meter.rs +++ b/frame/contracts/src/storage/meter.rs @@ -19,7 +19,7 @@ use crate::{ storage::{ContractInfo, DepositAccount}, - BalanceOf, Config, Error, Inspect, Pallet, System, + BalanceOf, Config, Error, Inspect, Pallet, System, LOG_TARGET, }; use codec::Encode; use frame_support::{ @@ -502,7 +502,7 @@ impl Ext for ReservingExt { ); if let Err(err) = result { log::error!( - target: "runtime::contracts", + target: LOG_TARGET, "Failed to transfer storage deposit {:?} from origin {:?} to deposit account {:?}: {:?}", amount, origin, deposit_account, err, ); @@ -531,7 +531,7 @@ impl Ext for ReservingExt { ); if matches!(result, Err(_)) { log::error!( - target: "runtime::contracts", + target: LOG_TARGET, "Failed to refund storage deposit {:?} from deposit account {:?} to origin {:?}: {:?}", amount, deposit_account, origin, result, ); diff --git a/frame/contracts/src/wasm/mod.rs b/frame/contracts/src/wasm/mod.rs index c8f1be6dd9db1..4723f0c833654 100644 --- a/frame/contracts/src/wasm/mod.rs +++ b/frame/contracts/src/wasm/mod.rs @@ -43,7 +43,7 @@ use crate::{ exec::{ExecResult, Executable, ExportedFunction, Ext}, gas::GasMeter, AccountIdOf, BalanceOf, CodeHash, CodeVec, Config, Error, OwnerInfoOf, RelaxedCodeVec, - Schedule, + Schedule, LOG_TARGET, }; use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::dispatch::{DispatchError, DispatchResult}; @@ -326,7 +326,7 @@ impl Executable for PrefabWasmModule { }, ) .map_err(|msg| { - log::debug!(target: "runtime::contracts", "failed to instantiate code: {}", msg); + log::debug!(target: LOG_TARGET, "failed to instantiate code: {}", msg); Error::::CodeRejected })?; store.data_mut().set_memory(memory); @@ -335,7 +335,7 @@ impl Executable for PrefabWasmModule { .get_export(&store, function.identifier()) .and_then(|export| export.into_func()) .ok_or_else(|| { - log::error!(target: "runtime::contracts", "failed to find entry point"); + log::error!(target: LOG_TARGET, "failed to find entry point"); Error::::CodeRejected })?; diff --git a/frame/contracts/src/wasm/prepare.rs b/frame/contracts/src/wasm/prepare.rs index f0065d77a1c9a..14fec834733eb 100644 --- a/frame/contracts/src/wasm/prepare.rs +++ b/frame/contracts/src/wasm/prepare.rs @@ -25,7 +25,7 @@ use crate::{ wasm::{ runtime::AllowDeprecatedInterface, Determinism, Environment, OwnerInfo, PrefabWasmModule, }, - AccountIdOf, CodeVec, Config, Error, Schedule, + AccountIdOf, CodeVec, Config, Error, Schedule, LOG_TARGET, }; use codec::{Encode, MaxEncodedLen}; use sp_runtime::{traits::Hash, DispatchError}; @@ -379,7 +379,7 @@ where }) .validate_all(original_code) .map_err(|err| { - log::debug!(target: "runtime::contracts", "{}", err); + log::debug!(target: LOG_TARGET, "{}", err); (Error::::CodeRejected.into(), "validation of new code failed") })?; @@ -403,7 +403,7 @@ where Ok((code, memory_limits)) })() .map_err(|msg: &str| { - log::debug!(target: "runtime::contracts", "new code rejected: {}", msg); + log::debug!(target: LOG_TARGET, "new code rejected: {}", msg); (Error::::CodeRejected.into(), msg) })?; @@ -426,7 +426,7 @@ where }, ) .map_err(|err| { - log::debug!(target: "runtime::contracts", "{}", err); + log::debug!(target: LOG_TARGET, "{}", err); (Error::::CodeRejected.into(), "new code rejected after instrumentation") })?; } @@ -518,7 +518,7 @@ where InstrumentReason::Reinstrument, ) .map_err(|(err, msg)| { - log::error!(target: "runtime::contracts", "CodeRejected during reinstrument: {}", msg); + log::error!(target: LOG_TARGET, "CodeRejected during reinstrument: {}", msg); err }) .map(|(code, _)| code)