Skip to content

Commit

Permalink
RPC: Shift evm debug logging RPCs to regular RPC (#2729)
Browse files Browse the repository at this point in the history
* Initial dev to port evm logging RPCs

* Revert "Revert "EVM: Fix create token (#2686)""

This reverts commit 5455aa5.

* Fix create token calls

* Remove cpp utf8 encoding

* Rename to DF23Height and DF24Height

* Shift dumpdb to regular RPC

* Add null check

* Add debug log account state to rust ffi

* Add logaccountstates into regular RPC

* Fmt cpp

* Fmt

* Revert CreateToken to bool arg

* Fix create token

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
sieniven and prasannavl authored Nov 29, 2023
1 parent 876a5d1 commit 79eaa98
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 95 deletions.
1 change: 1 addition & 0 deletions lib/Cargo.lock

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

33 changes: 29 additions & 4 deletions lib/ain-evm/src/storage/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use anyhow::format_err;
use ethereum::{BlockAny, TransactionV2};
use ethereum_types::{H160, H256, U256};
use log::debug;
use serde::{Deserialize, Serialize};

use super::{
db::{Column, ColumnName, LedgerColumn, Rocks, TypedColumn},
Expand All @@ -16,7 +15,7 @@ use crate::{
log::LogIndex,
receipt::Receipt,
storage::{db::columns, traits::LogStorage},
Result,
EVMError, Result,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -253,8 +252,6 @@ impl Rollback for BlockStore {
}
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DumpArg {
All,
Blocks,
Expand All @@ -264,6 +261,22 @@ pub enum DumpArg {
Logs,
}

impl TryFrom<String> for DumpArg {
type Error = EVMError;

fn try_from(arg: String) -> Result<Self> {
match arg.as_str() {
"all" => Ok(DumpArg::All),
"blocks" => Ok(DumpArg::Blocks),
"txs" => Ok(DumpArg::Txs),
"receipts" => Ok(DumpArg::Receipts),
"blockmap" => Ok(DumpArg::BlockMap),
"logs" => Ok(DumpArg::Logs),
_ => Err(format_err!("Invalid dump arg").into()),
}
}
}

impl BlockStore {
pub fn dump(&self, arg: &DumpArg, from: Option<&str>, limit: usize) -> Result<String> {
let s_to_u256 = |s| {
Expand All @@ -285,13 +298,19 @@ impl BlockStore {

fn dump_all(&self, limit: usize) -> Result<String> {
let mut out = String::new();
let response_max_size = usize::try_from(ain_cpp_imports::get_max_response_byte_size())
.map_err(|_| format_err!("failed to convert response size limit to usize"))?;

for arg in &[
DumpArg::Blocks,
DumpArg::Txs,
DumpArg::Receipts,
DumpArg::BlockMap,
DumpArg::Logs,
] {
if out.len() > response_max_size {
return Err(format_err!("exceed response max size limit").into());
}
writeln!(&mut out, "{}", self.dump(arg, None, limit)?)
.map_err(|_| format_err!("failed to write to stream"))?;
}
Expand All @@ -303,7 +322,13 @@ impl BlockStore {
C: TypedColumn + ColumnName,
{
let mut out = format!("{}\n", C::NAME);
let response_max_size = usize::try_from(ain_cpp_imports::get_max_response_byte_size())
.map_err(|_| format_err!("failed to convert response size limit to usize"))?;

for (k, v) in self.column::<C>().iter(from, limit) {
if out.len() > response_max_size {
return Err(format_err!("exceed response max size limit").into());
}
writeln!(&mut out, "{:?}: {:#?}", k, v)
.map_err(|_| format_err!("failed to write to stream"))?;
}
Expand Down
76 changes: 1 addition & 75 deletions lib/ain-grpc/src/rpc/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ use ain_evm::{
core::EthCallArgs,
evm::EVMServices,
executor::TxResponse,
storage::{
block_store::DumpArg,
traits::{ReceiptStorage, TransactionStorage},
},
storage::traits::{ReceiptStorage, TransactionStorage},
transaction::SignedTx,
};
use ethereum::Account;
use ethereum_types::{H256, U256};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use log::debug;
use rlp::{Decodable, Rlp};

use crate::{
call_request::CallRequest,
Expand All @@ -35,23 +30,6 @@ pub trait MetachainDebugRPC {
#[method(name = "traceTransaction")]
fn trace_transaction(&self, tx_hash: H256) -> RpcResult<TraceTransactionResult>;

// Dump full db
#[method(name = "dumpdb")]
fn dump_db(
&self,
arg: Option<DumpArg>,
from: Option<&str>,
limit: Option<&str>,
) -> RpcResult<String>;

// Log accounts state
#[method(name = "logaccountstates")]
fn log_account_states(&self) -> RpcResult<()>;

// Log block template state
#[method(name = "logblocktemplates")]
fn log_block_templates(&self) -> RpcResult<()>;

// Get transaction fee estimate
#[method(name = "feeEstimate")]
fn fee_estimate(&self, call: CallRequest) -> RpcResult<FeeEstimate>;
Expand Down Expand Up @@ -118,50 +96,6 @@ impl MetachainDebugRPCServer for MetachainDebugRPCModule {
})
}

fn dump_db(
&self,
arg: Option<DumpArg>,
start: Option<&str>,
limit: Option<&str>,
) -> RpcResult<String> {
self.is_enabled()?;

let default_limit = 100usize;
let limit = limit
.map_or(Ok(default_limit), |s| s.parse())
.map_err(to_custom_err)?;
self.handler
.storage
.dump_db(arg.unwrap_or(DumpArg::All), start, limit)
.map_err(to_custom_err)
}

fn log_account_states(&self) -> RpcResult<()> {
self.is_enabled()?;

let backend = self
.handler
.core
.get_latest_block_backend()
.expect("Error restoring backend");
let ro_handle = backend.ro_handle();

ro_handle.iter().for_each(|el| match el {
Ok((_, v)) => {
if let Ok(account) = Account::decode(&Rlp::new(&v)) {
debug!("[log_account_states] account {:?}", account);
} else {
debug!("[log_account_states] Error decoding account {:?}", v);
}
}
Err(e) => {
debug!("[log_account_states] Error on iter element {e}");
}
});

Ok(())
}

fn fee_estimate(&self, call: CallRequest) -> RpcResult<FeeEstimate> {
self.is_enabled()?;

Expand Down Expand Up @@ -226,12 +160,4 @@ impl MetachainDebugRPCServer for MetachainDebugRPCModule {
priority_fee,
})
}

fn log_block_templates(&self) -> RpcResult<()> {
self.is_enabled()?;

// let templates = &self.handler.core.block_templates;
// debug!("templates : {:#?}", templates);
Ok(())
}
}
1 change: 1 addition & 0 deletions lib/ain-rs-exports/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ain-evm = { path = "../ain-evm" }
ain-grpc = { path = "../ain-grpc" }
ain-contracts = { path = "../ain-contracts" }
ain-macros = { path = "../ain-macros" }
ain-cpp-imports = { path = "../ain-cpp-imports" }

ethereum.workspace = true
rlp.workspace = true
Expand Down
79 changes: 79 additions & 0 deletions lib/ain-rs-exports/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::fmt::Write;

use ain_evm::{services::SERVICES, storage::block_store::DumpArg, Result};
use ain_macros::ffi_fallible;
use anyhow::format_err;
use ethereum::Account;
use rlp::{Decodable, Rlp};

use crate::{ffi, prelude::*};

#[ffi_fallible]
pub fn debug_dump_db(arg: String, start: String, limit: String) -> Result<String> {
if !ain_cpp_imports::is_eth_debug_rpc_enabled() {
return Err("debug_* RPCs have not been enabled".into());
}

let arg = if arg.is_empty() {
DumpArg::All
} else {
DumpArg::try_from(arg)?
};

let default_limit = 100usize;
let limit = if limit.is_empty() {
default_limit
} else {
limit.as_str().parse().map_err(|_| "Invalid limit")?
};

let start = if start.is_empty() {
None
} else {
Some(start.as_str())
};

SERVICES.evm.storage.dump_db(arg, start, limit)
}

#[ffi_fallible]
pub fn debug_log_account_states() -> Result<String> {
if !ain_cpp_imports::is_eth_debug_rpc_enabled() {
return Err("debug_* RPCs have not been enabled".into());
}

let backend = SERVICES
.evm
.core
.get_latest_block_backend()
.expect("Error restoring backend");
let ro_handle = backend.ro_handle();

let mut out = String::new();
let response_max_size = usize::try_from(ain_cpp_imports::get_max_response_byte_size())
.map_err(|_| format_err!("failed to convert response size limit to usize"))?;

for el in ro_handle.iter() {
if out.len() > response_max_size {
return Err(format_err!("exceed response max size limit").into());
}

match el {
Ok((_, v)) => {
if let Ok(account) = Account::decode(&Rlp::new(&v)) {
writeln!(&mut out, "Account {:?}:", account)
.map_err(|_| format_err!("failed to write to stream"))?;
} else {
writeln!(&mut out, "Error decoding account {:?}", v)
.map_err(|_| format_err!("failed to write to stream"))?;
}
}
Err(e) => {
writeln!(&mut out, "Error on iter element {e}")
.map_err(|_| format_err!("failed to write to stream"))?;
}
};
}

Ok(out)
}
15 changes: 14 additions & 1 deletion lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod core;
mod debug;
mod evm;
mod prelude;
mod util;

use ain_evm::blocktemplate::BlockTemplate;

use crate::{core::*, evm::*, util::*};
use crate::{core::*, debug::*, evm::*, util::*};

pub struct BlockTemplateWrapper(Option<BlockTemplate>);

Expand Down Expand Up @@ -333,4 +334,16 @@ pub mod ffi {
raw_tx: &str,
);
}

// ========= Debug ==========
extern "Rust" {
fn debug_dump_db(
result: &mut CrossBoundaryResult,
arg: String,
start: String,
limit: String,
) -> String;

fn debug_log_account_states(result: &mut CrossBoundaryResult) -> String;
}
}
Loading

0 comments on commit 79eaa98

Please sign in to comment.