Skip to content

Commit

Permalink
feat: add --show-outputs flag (#269)
Browse files Browse the repository at this point in the history
* feat: add --show-outputs flag

* cargo fmt

* add API calls to manage show_coutputs variable
  • Loading branch information
dimazhornyk authored Mar 15, 2024
1 parent b566fcf commit b3ed0ff
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ etc/**/*.zbin
!.vscode/extensions.json
!.vscode/launch.json
!.vscode/tasks.json
.idea

*.log
.cache
48 changes: 48 additions & 0 deletions SUPPORTED_APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ The `status` options are:
| Namespace | API | <div style="width:130px">Status</div> | Description |
| --- | --- | --- | --- |
| [`CONFIG`](#config-namespace) | [`config_getShowCalls`](#config_getshowcalls) | `SUPPORTED` | Gets the current value of `show_calls` that's originally set with `--show-calls` option |
| [`CONFIG`](#config-namespace) | [`config_getShowOutputs`](#config_getshowoutputs) | `SUPPORTED` | Gets the current value of `show_outputs` that's originally set with `--show-outputs` option |
| [`CONFIG`](#config-namespace) | [`config_getCurrentTimestamp`](#config_getcurrenttimestamp) | `SUPPORTED` | Gets the value of `current_timestamp` for the node |
| [`CONFIG`](#config-namespace) | [`config_setResolveHashes`](#config_setresolvehashes) | `SUPPORTED` | Updates `resolve-hashes` to call OpenChain for human-readable ABI names in call traces |
| [`CONFIG`](#config-namespace) | [`config_setShowCalls`](#config_setshowcalls) | `SUPPORTED` | Updates `show_calls` to print more detailed call traces |
| [`CONFIG`](#config-namespace) | [`config_setShowOutputs`](#config_setshowoutputs) | `SUPPORTED` | Updates `show_outputs` to print calls outputs |
| [`CONFIG`](#config-namespace) | [`config_setShowStorageLogs`](#config_setshowstoragelogs) | `SUPPORTED` | Updates `show_storage_logs` to print storage log reads/writes |
| [`CONFIG`](#config-namespace) | [`config_setShowVmDetails`](#config_setshowvmdetails) | `SUPPORTED` | Updates `show_vm_details` to print more detailed results from vm execution |
| [`CONFIG`](#config-namespace) | [`config_setShowGasDetails`](#config_setshowgasdetails) | `SUPPORTED` | Updates `show_gas_details` to print more details about gas estimation and usage |
Expand Down Expand Up @@ -156,6 +158,29 @@ curl --request POST \
--data '{"jsonrpc": "2.0","id": "1","method": "config_getShowCalls","params": []}'
```

### `config_getShowOutputs`

[source](src/node/config.rs)

Gets the current value of `show_outputs` that's originally set with `--show-outputs` option

#### Arguments

+ _NONE_

#### Status

`SUPPORTED`

#### Example

```bash
curl --request POST \
--url http://localhost:8011/ \
--header 'content-type: application/json' \
--data '{"jsonrpc": "2.0","id": "1","method": "config_getShowOutputs","params": []}'
```

### `config_getCurrentTimestamp`

[source](src/node/config.rs)
Expand Down Expand Up @@ -202,6 +227,29 @@ curl --request POST \
--data '{"jsonrpc": "2.0","id": "1","method": "config_setShowCalls","params": ["all"]}'
```

### `config_setShowOutputs`

[source](src/node/config.rs)

Updates `show_outputs` to print calls outputs

#### Arguments

+ `value: boolean`

#### Status

`SUPPORTED`

#### Example

```bash
curl --request POST \
--url http://localhost:8011/ \
--header 'content-type: application/json' \
--data '{"jsonrpc": "2.0","id": "1","method": "config_setShowOutputs","params": [true]}'
```

### `config_setShowStorageLogs`

[source](src/node/config.rs)
Expand Down
30 changes: 27 additions & 3 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ pub fn print_event(event: &VmEvent, resolve_hashes: bool) {

/// Pretty-prints contents of a 'call' - including subcalls.
/// If skip_resolve is false, will try to contact openchain to resolve the ABI names.
pub fn print_call(call: &Call, padding: usize, show_calls: &ShowCalls, resolve_hashes: bool) {
pub fn print_call(
call: &Call,
padding: usize,
show_calls: &ShowCalls,
show_outputs: bool,
resolve_hashes: bool,
) {
let contract_type = KNOWN_ADDRESSES
.get(&call.to)
.cloned()
Expand Down Expand Up @@ -167,14 +173,26 @@ pub fn print_call(call: &Call, padding: usize, show_calls: &ShowCalls, resolve_h
)
};

let output = if show_outputs {
call.output
.as_slice()
.iter()
.map(|byte| format!("{:02x}", byte))
.collect::<Vec<_>>()
.join("")
} else {
"".to_string()
};

let pretty_print = format!(
"{}{:?} {} {} {} {} {}",
"{}{:?} {} {} {} {} {} {}",
" ".repeat(padding),
call.r#type,
address_to_human_readable(call.to)
.map(|x| format!("{:<52}", x))
.unwrap_or(format!("{:<52}", format!("{:?}", call.to).bold())),
function_signature,
output,
call.revert_reason
.as_ref()
.map(|s| format!("Revert: {}", s))
Expand All @@ -193,7 +211,13 @@ pub fn print_call(call: &Call, padding: usize, show_calls: &ShowCalls, resolve_h
}
}
for subcall in &call.calls {
print_call(subcall, padding + 2, show_calls, resolve_hashes);
print_call(
subcall,
padding + 2,
show_calls,
show_outputs,
resolve_hashes,
);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ struct Cli {
#[arg(long, default_value = "none")]
/// Show call debug information
show_calls: ShowCalls,
#[arg[long]]
/// Show call output
show_outputs: bool,
#[arg(long, default_value = "none")]
/// Show storage log information
show_storage_logs: ShowStorageLogs,
Expand Down Expand Up @@ -287,6 +290,7 @@ struct ForkArgs {
// If not set - will use the current finalized block from the network.
fork_at: Option<u64>,
}

#[derive(Debug, Parser)]
struct ReplayArgs {
/// Whether to fork from existing network.
Expand Down Expand Up @@ -357,6 +361,7 @@ async fn main() -> anyhow::Result<()> {
Some(observability),
InMemoryNodeConfig {
show_calls: opt.show_calls,
show_outputs: opt.show_outputs,
show_storage_logs: opt.show_storage_logs,
show_vm_details: opt.show_vm_details,
show_gas_details: opt.show_gas_details,
Expand Down
17 changes: 17 additions & 0 deletions src/namespaces/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ pub trait ConfigurationApiNamespaceT {
#[rpc(name = "config_getShowCalls", returns = "String")]
fn config_get_show_calls(&self) -> Result<String>;

/// Get the InMemoryNodeInner's show_outputs property as a boolean
///
/// # Returns
/// The current `show_outputs` value for the InMemoryNodeInner.
#[rpc(name = "config_getShowOutputs", returns = "bool")]
fn config_get_show_outputs(&self) -> Result<bool>;

/// Get the InMemoryNodeInner's current_timestamp property
///
/// # Returns
Expand All @@ -28,6 +35,16 @@ pub trait ConfigurationApiNamespaceT {
#[rpc(name = "config_setShowCalls", returns = "String")]
fn config_set_show_calls(&self, value: String) -> Result<String>;

/// Set show_outputs for the InMemoryNodeInner
///
/// # Parameters
/// - `value`: a bool value to update show_outputs to
///
/// # Returns
/// The updated/current `show_outputs` value for the InMemoryNodeInner.
#[rpc(name = "config_setShowOutputs", returns = "bool")]
fn config_set_show_outputs(&self, value: bool) -> Result<bool>;

/// Set show_storage_logs for the InMemoryNodeInner
///
/// # Parameters
Expand Down
27 changes: 27 additions & 0 deletions src/node/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> Configurat
.map(|reader| reader.show_calls.to_string())
}

fn config_get_show_outputs(&self) -> Result<bool> {
self.get_inner()
.read()
.map_err(|err| {
tracing::error!("failed acquiring lock: {:?}", err);
into_jsrpc_error(Web3Error::InternalError(anyhow::Error::msg(
"Failed to acquire read lock for inner node state.",
)))
})
.map(|reader| reader.show_outputs)
}

fn config_get_current_timestamp(&self) -> Result<u64> {
self.get_inner()
.read()
Expand Down Expand Up @@ -57,6 +69,21 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> Configurat
})
}

fn config_set_show_outputs(&self, value: bool) -> Result<bool> {
self.get_inner()
.write()
.map_err(|err| {
tracing::error!("failed acquiring lock: {:?}", err);
into_jsrpc_error(Web3Error::InternalError(anyhow::Error::msg(
"Failed to acquire write lock for inner node state.",
)))
})
.map(|mut writer| {
writer.show_outputs = value;
writer.show_outputs
})
}

fn config_set_show_storage_logs(&self, value: String) -> Result<String> {
let show_storage_logs = match value.parse::<ShowStorageLogs>() {
Ok(value) => value,
Expand Down
21 changes: 19 additions & 2 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ pub struct InMemoryNodeInner<S> {
pub fork_storage: ForkStorage<S>,
// Debug level information.
pub show_calls: ShowCalls,
// If true - will display the output of the calls.
pub show_outputs: bool,
// Displays storage logs.
pub show_storage_logs: ShowStorageLogs,
// Displays VM details.
Expand Down Expand Up @@ -876,6 +878,7 @@ pub struct Snapshot {
#[derive(Default, Debug, Clone)]
pub struct InMemoryNodeConfig {
pub show_calls: ShowCalls,
pub show_outputs: bool,
pub show_storage_logs: ShowStorageLogs,
pub show_vm_details: ShowVMDetails,
pub show_gas_details: ShowGasDetails,
Expand Down Expand Up @@ -935,6 +938,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
filters: Default::default(),
fork_storage: ForkStorage::new(fork, &config.system_contracts_options),
show_calls: config.show_calls,
show_outputs: config.show_outputs,
show_storage_logs: config.show_storage_logs,
show_vm_details: config.show_vm_details,
show_gas_details: config.show_gas_details,
Expand Down Expand Up @@ -968,6 +972,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
filters: Default::default(),
fork_storage: ForkStorage::new(fork, &config.system_contracts_options),
show_calls: config.show_calls,
show_outputs: config.show_outputs,
show_storage_logs: config.show_storage_logs,
show_vm_details: config.show_vm_details,
show_gas_details: config.show_gas_details,
Expand Down Expand Up @@ -1086,7 +1091,13 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {

tracing::info!("=== Call traces:");
for call in &call_traces {
formatter::print_call(call, 0, &inner.show_calls, inner.resolve_hashes);
formatter::print_call(
call,
0,
&inner.show_calls,
inner.show_outputs,
inner.resolve_hashes,
);
}

Ok(tx_result.result)
Expand Down Expand Up @@ -1423,7 +1434,13 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {

if inner.show_calls != ShowCalls::None {
for call in call_traces {
formatter::print_call(call, 0, &inner.show_calls, inner.resolve_hashes);
formatter::print_call(
call,
0,
&inner.show_calls,
inner.show_outputs,
inner.resolve_hashes,
);
}
}
tracing::info!("");
Expand Down
22 changes: 22 additions & 0 deletions test_endpoints.http
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ content-type: application/json
POST http://localhost:8011
content-type: application/json

{
"jsonrpc": "2.0",
"id": "1",
"method": "config_getShowOutputs",
"params": []
}

###
POST http://localhost:8011
content-type: application/json

{
"jsonrpc": "2.0",
"id": "1",
Expand All @@ -90,6 +101,17 @@ content-type: application/json
POST http://localhost:8011
content-type: application/json

{
"jsonrpc": "2.0",
"id": "1",
"method": "config_setShowOutputs",
"params": [true]
}

###
POST http://localhost:8011
content-type: application/json

{
"jsonrpc": "2.0",
"id": "1",
Expand Down

0 comments on commit b3ed0ff

Please sign in to comment.