Skip to content

Commit 2c8d27d

Browse files
committed
Add tracing features
1 parent 68fd6d5 commit 2c8d27d

File tree

43 files changed

+1117
-1143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1117
-1143
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,6 @@ parameter_types! {
953953
pub CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(30);
954954
}
955955

956-
type EventRecord = frame_system::EventRecord<
957-
<Runtime as frame_system::Config>::RuntimeEvent,
958-
<Runtime as frame_system::Config>::Hash,
959-
>;
960-
961956
impl pallet_revive::Config for Runtime {
962957
type Time = Timestamp;
963958
type Currency = Balances;
@@ -2073,7 +2068,7 @@ impl_runtime_apis! {
20732068
}
20742069
}
20752070

2076-
impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber, EventRecord> for Runtime
2071+
impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber> for Runtime
20772072
{
20782073
fn balance(address: H160) -> U256 {
20792074
Revive::evm_balance(&address)
@@ -2108,7 +2103,7 @@ impl_runtime_apis! {
21082103
gas_limit: Option<Weight>,
21092104
storage_deposit_limit: Option<Balance>,
21102105
input_data: Vec<u8>,
2111-
) -> pallet_revive::ContractResult<pallet_revive::ExecReturnValue, Balance, EventRecord> {
2106+
) -> pallet_revive::ContractResult<pallet_revive::ExecReturnValue, Balance> {
21122107
let blockweights= <Runtime as frame_system::Config>::BlockWeights::get();
21132108
Revive::bare_call(
21142109
RuntimeOrigin::signed(origin),
@@ -2117,8 +2112,6 @@ impl_runtime_apis! {
21172112
gas_limit.unwrap_or(blockweights.max_block),
21182113
pallet_revive::DepositLimit::Balance(storage_deposit_limit.unwrap_or(u128::MAX)),
21192114
input_data,
2120-
pallet_revive::DebugInfo::UnsafeDebug,
2121-
pallet_revive::CollectEvents::UnsafeCollect,
21222115
)
21232116
}
21242117

@@ -2130,7 +2123,7 @@ impl_runtime_apis! {
21302123
code: pallet_revive::Code,
21312124
data: Vec<u8>,
21322125
salt: Option<[u8; 32]>,
2133-
) -> pallet_revive::ContractResult<pallet_revive::InstantiateReturnValue, Balance, EventRecord>
2126+
) -> pallet_revive::ContractResult<pallet_revive::InstantiateReturnValue, Balance>
21342127
{
21352128
let blockweights= <Runtime as frame_system::Config>::BlockWeights::get();
21362129
Revive::bare_instantiate(
@@ -2141,8 +2134,6 @@ impl_runtime_apis! {
21412134
code,
21422135
data,
21432136
salt,
2144-
pallet_revive::DebugInfo::UnsafeDebug,
2145-
pallet_revive::CollectEvents::UnsafeCollect,
21462137
)
21472138
}
21482139

substrate/bin/node/runtime/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,7 +3212,7 @@ impl_runtime_apis! {
32123212
}
32133213
}
32143214

3215-
impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber, EventRecord> for Runtime
3215+
impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber> for Runtime
32163216
{
32173217
fn balance(address: H160) -> U256 {
32183218
Revive::evm_balance(&address)
@@ -3247,16 +3247,14 @@ impl_runtime_apis! {
32473247
gas_limit: Option<Weight>,
32483248
storage_deposit_limit: Option<Balance>,
32493249
input_data: Vec<u8>,
3250-
) -> pallet_revive::ContractResult<pallet_revive::ExecReturnValue, Balance, EventRecord> {
3250+
) -> pallet_revive::ContractResult<pallet_revive::ExecReturnValue, Balance> {
32513251
Revive::bare_call(
32523252
RuntimeOrigin::signed(origin),
32533253
dest,
32543254
value,
32553255
gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block),
32563256
pallet_revive::DepositLimit::Balance(storage_deposit_limit.unwrap_or(u128::MAX)),
32573257
input_data,
3258-
pallet_revive::DebugInfo::UnsafeDebug,
3259-
pallet_revive::CollectEvents::UnsafeCollect,
32603258
)
32613259
}
32623260

@@ -3268,7 +3266,7 @@ impl_runtime_apis! {
32683266
code: pallet_revive::Code,
32693267
data: Vec<u8>,
32703268
salt: Option<[u8; 32]>,
3271-
) -> pallet_revive::ContractResult<pallet_revive::InstantiateReturnValue, Balance, EventRecord>
3269+
) -> pallet_revive::ContractResult<pallet_revive::InstantiateReturnValue, Balance>
32723270
{
32733271
Revive::bare_instantiate(
32743272
RuntimeOrigin::signed(origin),
@@ -3278,8 +3276,6 @@ impl_runtime_apis! {
32783276
code,
32793277
data,
32803278
salt,
3281-
pallet_revive::DebugInfo::UnsafeDebug,
3282-
pallet_revive::CollectEvents::UnsafeCollect,
32833279
)
32843280
}
32853281

substrate/frame/revive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ frame-support = { workspace = true }
3939
frame-system = { workspace = true }
4040
pallet-revive-fixtures = { workspace = true, optional = true }
4141
pallet-revive-proc-macro = { workspace = true }
42-
pallet-revive-uapi = { workspace = true, features = ["scale"] }
42+
pallet-revive-uapi = { workspace = true, features = ["scale", "serde"] }
4343
pallet-transaction-payment = { workspace = true }
4444
sp-api = { workspace = true }
4545
sp-arithmetic = { workspace = true }

substrate/frame/revive/README.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,6 @@ This module executes PolkaVM smart contracts. These can potentially be written i
4949
RISC-V. For now, the only officially supported languages are Solidity (via [`revive`](https://github.com/xermicus/revive))
5050
and Rust (check the `fixtures` directory for Rust examples).
5151

52-
## Debugging
53-
54-
Contracts can emit messages to the client when called as RPC through the
55-
[`debug_message`](https://paritytech.github.io/substrate/master/pallet_revive/trait.SyscallDocs.html#tymethod.debug_message)
56-
API.
57-
58-
Those messages are gathered into an internal buffer and sent to the RPC client. It is up to the individual client if
59-
and how those messages are presented to the user.
60-
61-
This buffer is also printed as a debug message. In order to see these messages on the node console the log level for the
62-
`runtime::revive` target needs to be raised to at least the `debug` level. However, those messages are easy to
63-
overlook because of the noise generated by block production. A good starting point for observing them on the console is
64-
using this command line in the root directory of the Substrate repository:
65-
66-
```bash
67-
cargo run --release -- --dev -lerror,runtime::revive=debug
68-
```
69-
70-
This raises the log level of `runtime::revive` to `debug` and all other targets to `error` in order to prevent them
71-
from spamming the console.
72-
73-
`--dev`: Use a dev chain spec `--tmp`: Use temporary storage for chain data (the chain state is deleted on exit)
74-
7552
## Host function tracing
7653

7754
For contract authors, it can be a helpful debugging tool to see which host functions are called, with which arguments,

substrate/frame/revive/fixtures/contracts/debug_message_logging_disabled.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

substrate/frame/revive/fixtures/contracts/debug_message_works.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! This fixture calls itself as many times as passed as argument.
19+
20+
#![no_std]
21+
#![no_main]
22+
23+
use common::input;
24+
use uapi::{HostFn, HostFnImpl as api};
25+
26+
#[no_mangle]
27+
#[polkavm_derive::polkavm_export]
28+
pub extern "C" fn deploy() {}
29+
30+
#[no_mangle]
31+
#[polkavm_derive::polkavm_export]
32+
pub extern "C" fn call() {
33+
input!(calls_left: u32, callee_addr: &[u8; 20],);
34+
35+
let next_input = (calls_left - 1).to_le_bytes();
36+
37+
api::deposit_event(&[], b"before");
38+
39+
// Call the callee
40+
api::call(
41+
uapi::CallFlags::empty(),
42+
callee_addr,
43+
0u64, // How much ref_time to devote for the execution. 0 = all.
44+
0u64, // How much proof_size to devote for the execution. 0 = all.
45+
None, // No deposit limit.
46+
&[0u8; 32], // Value transferred to the contract.
47+
&next_input,
48+
None,
49+
)
50+
.unwrap();
51+
52+
api::deposit_event(&[], b"after");
53+
54+
if calls_left == 0 {
55+
return
56+
}
57+
58+
// own address
59+
let mut addr = [0u8; 20];
60+
api::address(&mut addr);
61+
let mut input = [0u8; 24];
62+
63+
input[..4].copy_from_slice(&next_input);
64+
input[4..24].copy_from_slice(&callee_addr[..20]);
65+
66+
// recurse
67+
api::call(
68+
uapi::CallFlags::ALLOW_REENTRY,
69+
&addr,
70+
0u64, // How much ref_time to devote for the execution. 0 = all.
71+
0u64, // How much deposit_limit to devote for the execution. 0 = all.
72+
None, // No deposit limit.
73+
&[0u8; 32], // Value transferred to the contract.
74+
&input,
75+
None,
76+
)
77+
.unwrap();
78+
}

substrate/frame/revive/fixtures/contracts/debug_message_invalid_utf8.rs renamed to substrate/frame/revive/fixtures/contracts/tracing_callee.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
17-
18-
//! Emit a debug message with an invalid utf-8 code.
1917
#![no_std]
2018
#![no_main]
2119

2220
extern crate common;
23-
use uapi::{HostFn, HostFnImpl as api};
21+
use uapi::{HostFn, HostFnImpl as api, StorageFlags};
2422

2523
#[no_mangle]
2624
#[polkavm_derive::polkavm_export]
@@ -29,5 +27,7 @@ pub extern "C" fn deploy() {}
2927
#[no_mangle]
3028
#[polkavm_derive::polkavm_export]
3129
pub extern "C" fn call() {
32-
api::debug_message(b"\xFC").unwrap();
30+
// Burn some PoV, clear_storage consumes some PoV as in order to clear the storage we need to we
31+
// need to read its size first.
32+
api::clear_storage(StorageFlags::empty(), b"");
3333
}

substrate/frame/revive/proc-macro/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,7 @@ fn expand_functions(def: &EnvDef) -> TokenStream2 {
510510
quote! {
511511
// wrap body in closure to make sure the tracing is always executed
512512
let result = (|| #body)();
513-
if ::log::log_enabled!(target: "runtime::revive::strace", ::log::Level::Trace) {
514-
use core::fmt::Write;
515-
let mut w = sp_std::Writer::default();
516-
let _ = core::write!(&mut w, #trace_fmt_str, #( #trace_fmt_args, )* result);
517-
let msg = core::str::from_utf8(&w.inner()).unwrap_or_default();
518-
self.ext().append_debug_buffer(msg);
519-
}
513+
::log::trace!(target: "runtime::revive::strace", #trace_fmt_str, #( #trace_fmt_args, )* result);
520514
result
521515
}
522516
};

substrate/frame/revive/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ pallet-revive-fixtures = { workspace = true, default-features = true }
8585
static_init = { workspace = true }
8686
substrate-cli-test-utils = { workspace = true }
8787
subxt-signer = { workspace = true, features = ["unstable-eth"] }
88+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract TracingCaller {
5+
event TraceEvent(uint256 value, string message);
6+
address public callee;
7+
8+
constructor(address _callee) {
9+
require(_callee != address(0), "Callee address cannot be zero");
10+
callee = _callee;
11+
}
12+
13+
function start(uint256 counter) external {
14+
if (counter == 0) {
15+
// uint256 a = 1;
16+
// uint256 b = 0;
17+
// uint256 c = a / b;
18+
return;
19+
}
20+
21+
emit TraceEvent(counter, "before");
22+
TracingCallee(callee).consumeGas();
23+
emit TraceEvent(counter, "after");
24+
25+
try TracingCallee(callee).failingFunction() {
26+
} catch {
27+
}
28+
29+
try TracingCallee(callee).consumeGas{gas: 100}() {
30+
} catch {
31+
}
32+
33+
this.start(counter - 1);
34+
}
35+
}
36+
37+
contract TracingCallee {
38+
event GasConsumed(address indexed caller);
39+
40+
function consumeGas() external {
41+
// burn some gas
42+
for (uint256 i = 0; i < 10; i++) {
43+
uint256(keccak256(abi.encodePacked(i)));
44+
}
45+
46+
emit GasConsumed(msg.sender);
47+
}
48+
49+
function failingFunction() external pure {
50+
require(false, "This function always fails");
51+
}
52+
}
53+

0 commit comments

Comments
 (0)