Skip to content

Commit 708632b

Browse files
committed
Allow an optional config struct for all trace calls
1 parent ba0ffda commit 708632b

3 files changed

Lines changed: 59 additions & 29 deletions

File tree

src/rpc/mod.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ pub struct RPCTrace {
121121
pub struct_logs: Vec<RPCStep>,
122122
}
123123

124+
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
125+
#[serde(rename_all = "camelCase")]
126+
pub struct RPCTraceConfig {
127+
pub disable_memory: bool,
128+
pub disable_stack: bool,
129+
pub disable_storage: bool,
130+
}
131+
124132
#[derive(Serialize, Deserialize, Debug, Clone)]
125133
#[serde(rename_all = "camelCase")]
126134
pub struct RPCBlockTrace {
@@ -134,11 +142,11 @@ pub struct RPCStep {
134142
pub error: String,
135143
pub gas: Hex<Gas>,
136144
pub gas_cost: Hex<Gas>,
137-
pub memory: Vec<Hex<M256>>,
138145
pub op: u8,
139146
pub pc: usize,
140-
pub stack: Vec<Hex<M256>>,
141-
pub storage: HashMap<Hex<U256>, Hex<M256>>,
147+
pub memory: Option<Vec<Hex<M256>>>,
148+
pub stack: Option<Vec<Hex<M256>>>,
149+
pub storage: Option<HashMap<Hex<U256>, Hex<M256>>>,
142150
}
143151

144152
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -260,15 +268,20 @@ build_rpc_trait! {
260268
#[rpc(name = "debug_getBlockRlp")]
261269
fn block_rlp(&self, usize) -> Result<Bytes, Error>;
262270
#[rpc(name = "debug_traceTransaction")]
263-
fn trace_transaction(&self, Hex<H256>) -> Result<RPCTrace, Error>;
271+
fn trace_transaction(&self, Hex<H256>, Trailing<RPCTraceConfig>)
272+
-> Result<RPCTrace, Error>;
264273
#[rpc(name = "debug_traceBlock")]
265-
fn trace_block(&self, Bytes) -> Result<RPCBlockTrace, Error>;
274+
fn trace_block(&self, Bytes, Trailing<RPCTraceConfig>)
275+
-> Result<RPCBlockTrace, Error>;
266276
#[rpc(name = "debug_traceBlockByNumber")]
267-
fn trace_block_by_number(&self, usize) -> Result<RPCBlockTrace, Error>;
277+
fn trace_block_by_number(&self, usize, Trailing<RPCTraceConfig>)
278+
-> Result<RPCBlockTrace, Error>;
268279
#[rpc(name = "debug_traceBlockByHash")]
269-
fn trace_block_by_hash(&self, Hex<H256>) -> Result<RPCBlockTrace, Error>;
280+
fn trace_block_by_hash(&self, Hex<H256>, Trailing<RPCTraceConfig>)
281+
-> Result<RPCBlockTrace, Error>;
270282
#[rpc(name = "debug_traceBlockFromFile")]
271-
fn trace_block_from_file(&self, String) -> Result<RPCBlockTrace, Error>;
283+
fn trace_block_from_file(&self, String, Trailing<RPCTraceConfig>)
284+
-> Result<RPCBlockTrace, Error>;
272285
#[rpc(name = "debug_dumpBlock")]
273286
fn dump_block(&self, usize) -> Result<RPCDump, Error>;
274287
}

src/rpc/serves.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{EthereumRPC, DebugRPC, Either, RPCTransaction, RPCTrace, RPCStep, RPCBlock, RPCLog, RPCReceipt, RPCLogFilter, RPCBlockTrace, RPCDump, RPCDumpAccount};
1+
use super::{EthereumRPC, DebugRPC, Either, RPCTransaction, RPCTrace, RPCStep, RPCBlock, RPCLog, RPCReceipt, RPCLogFilter, RPCBlockTrace, RPCDump, RPCDumpAccount, RPCTraceConfig};
22
use super::util::*;
33
use super::filter::*;
44
use super::serialize::*;
@@ -574,7 +574,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
574574
Ok(Bytes(rlp::encode(&block).to_vec()))
575575
}
576576

577-
fn trace_transaction(&self, hash: Hex<H256>) -> Result<RPCTrace, Error> {
577+
fn trace_transaction(&self, hash: Hex<H256>, config: Trailing<RPCTraceConfig>) -> Result<RPCTrace, Error> {
578+
let config = config.unwrap_or(RPCTraceConfig::default());
578579
let state = self.state.lock().unwrap();
579580

580581
let transaction = state.get_transaction_by_hash(hash.0)?;
@@ -592,7 +593,7 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
592593
}
593594
}
594595

595-
let (steps, vm) = replay_transaction::<P>(&stateful, transaction, &block, &last_hashes)?;
596+
let (steps, vm) = replay_transaction::<P>(&stateful, transaction, &block, &last_hashes, &config)?;
596597

597598
let gas = Hex(vm.real_used_gas());
598599
let return_value = Bytes(vm.out().into());
@@ -603,7 +604,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
603604
})
604605
}
605606

606-
fn trace_block(&self, block_rlp: Bytes) -> Result<RPCBlockTrace, Error> {
607+
fn trace_block(&self, block_rlp: Bytes, config: Trailing<RPCTraceConfig>) -> Result<RPCBlockTrace, Error> {
608+
let config = config.unwrap_or(RPCTraceConfig::default());
607609
let state = self.state.lock().unwrap();
608610
let block: Block = UntrustedRlp::new(&block_rlp.0).as_val()?;
609611
let last_hashes = state.get_last_256_block_hashes_by_number(block.header.number.as_usize());
@@ -612,7 +614,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
612614
let mut steps = Vec::new();
613615
for transaction in block.transactions.clone() {
614616
let (mut local_steps, vm) = replay_transaction::<P>(&stateful, transaction,
615-
&block, &last_hashes)?;
617+
&block, &last_hashes,
618+
&config)?;
616619
steps.append(&mut local_steps);
617620
let mut accounts = Vec::new();
618621
for account in vm.accounts() {
@@ -626,7 +629,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
626629
})
627630
}
628631

629-
fn trace_block_by_number(&self, number: usize) -> Result<RPCBlockTrace, Error> {
632+
fn trace_block_by_number(&self, number: usize, config: Trailing<RPCTraceConfig>) -> Result<RPCBlockTrace, Error> {
633+
let config = config.unwrap_or(RPCTraceConfig::default());
630634
let state = self.state.lock().unwrap();
631635
if number > state.block_height() {
632636
return Err(Error::NotFound);
@@ -638,7 +642,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
638642
let mut steps = Vec::new();
639643
for transaction in block.transactions.clone() {
640644
let (mut local_steps, vm) = replay_transaction::<P>(&stateful, transaction,
641-
&block, &last_hashes)?;
645+
&block, &last_hashes,
646+
&config)?;
642647
steps.append(&mut local_steps);
643648
let mut accounts = Vec::new();
644649
for account in vm.accounts() {
@@ -652,7 +657,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
652657
})
653658
}
654659

655-
fn trace_block_by_hash(&self, hash: Hex<H256>) -> Result<RPCBlockTrace, Error> {
660+
fn trace_block_by_hash(&self, hash: Hex<H256>, config: Trailing<RPCTraceConfig>) -> Result<RPCBlockTrace, Error> {
661+
let config = config.unwrap_or(RPCTraceConfig::default());
656662
let state = self.state.lock().unwrap();
657663
let block: Block = state.get_block_by_hash(hash.0)?;
658664
let last_hashes = state.get_last_256_block_hashes_by_number(block.header.number.as_usize());
@@ -661,7 +667,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
661667
let mut steps = Vec::new();
662668
for transaction in block.transactions.clone() {
663669
let (mut local_steps, vm) = replay_transaction::<P>(&stateful, transaction,
664-
&block, &last_hashes)?;
670+
&block, &last_hashes,
671+
&config)?;
665672
steps.append(&mut local_steps);
666673
let mut accounts = Vec::new();
667674
for account in vm.accounts() {
@@ -675,10 +682,11 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
675682
})
676683
}
677684

678-
fn trace_block_from_file(&self, path: String) -> Result<RPCBlockTrace, Error> {
685+
fn trace_block_from_file(&self, path: String, config: Trailing<RPCTraceConfig>) -> Result<RPCBlockTrace, Error> {
679686
use std::fs::File;
680687
use std::io::Read;
681688

689+
let config = config.unwrap_or(RPCTraceConfig::default());
682690
let mut file = File::open(path).unwrap();
683691
let mut buffer = Vec::new();
684692
file.read_to_end(&mut buffer).unwrap();
@@ -691,7 +699,8 @@ impl<P: 'static + Patch + Send> DebugRPC for MinerDebugRPC<P> {
691699
let mut steps = Vec::new();
692700
for transaction in block.transactions.clone() {
693701
let (mut local_steps, vm) = replay_transaction::<P>(&stateful, transaction,
694-
&block, &last_hashes)?;
702+
&block, &last_hashes,
703+
&config)?;
695704
steps.append(&mut local_steps);
696705
let mut accounts = Vec::new();
697706
for account in vm.accounts() {

src/rpc/util.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{EthereumRPC, Either, RPCStep, RPCTransaction, RPCBlock, RPCLog, RPCReceipt, RPCTopicFilter, RPCLogFilter};
1+
use super::{EthereumRPC, Either, RPCStep, RPCTransaction, RPCBlock, RPCLog, RPCReceipt, RPCTopicFilter, RPCLogFilter, RPCTraceConfig};
22
use super::filter::*;
33
use super::serialize::*;
44
use error::Error;
@@ -344,7 +344,8 @@ pub fn from_log_filter(state: &MinerState, filter: RPCLogFilter) -> Result<LogFi
344344
}
345345

346346
pub fn replay_transaction<P: Patch>(
347-
stateful: &MemoryStateful<'static>, transaction: Transaction, block: &Block, last_hashes: &[H256]
347+
stateful: &MemoryStateful<'static>, transaction: Transaction, block: &Block,
348+
last_hashes: &[H256], config: &RPCTraceConfig
348349
) -> Result<(Vec<RPCStep>, SeqTransactionVM<P>), Error> {
349350
let valid = stateful.to_valid::<P>(transaction)?;
350351
let mut vm = SeqTransactionVM::<P>::new(valid, HeaderParams::from(&block.header));
@@ -368,7 +369,12 @@ pub fn replay_transaction<P: Patch>(
368369
MachineStatus::ExitedErr(err) => format!("{:?}", err),
369370
_ => "".to_string(),
370371
};
371-
let memory = {
372+
let pc = machine.pc().position();
373+
let op = machine.pc().code()[pc];
374+
375+
let memory = if config.disable_memory {
376+
None
377+
} else {
372378
let mut ret = Vec::new();
373379
for i in 0..(if machine.state().memory.len() % 32 == 0 {
374380
machine.state().memory.len() / 32
@@ -378,19 +384,21 @@ pub fn replay_transaction<P: Patch>(
378384
ret.push(Hex(machine.state().memory.read(U256::from(i) *
379385
U256::from(32))));
380386
}
381-
ret
387+
Some(ret)
382388
};
383-
let pc = machine.pc().position();
384-
let op = machine.pc().code()[pc];
385-
let stack = {
389+
let stack = if config.disable_stack {
390+
None
391+
} else {
386392
let mut ret = Vec::new();
387393

388394
for i in 0..machine.state().stack.len() {
389395
ret.push(Hex(machine.state().stack.peek(i).unwrap()));
390396
}
391-
ret
397+
Some(ret)
392398
};
393-
let storage = {
399+
let storage = if config.disable_storage {
400+
None
401+
} else {
394402
let storage = machine.state().account_state.storage(
395403
machine.state().context.address);
396404

@@ -401,7 +409,7 @@ pub fn replay_transaction<P: Patch>(
401409
ret.insert(Hex(key), Hex(value));
402410
}
403411
}
404-
ret
412+
Some(ret)
405413
};
406414

407415
steps.push(RPCStep {

0 commit comments

Comments
 (0)