Skip to content

Commit acf4147

Browse files
committed
Update queries
Signed-off-by: Danil <deniallugo@gmail.com>
1 parent 8fa81cc commit acf4147

File tree

8 files changed

+171
-113
lines changed

8 files changed

+171
-113
lines changed

core/lib/dal/.sqlx/query-b98e3790de305017c8fa5fba4c0c783b3710ee47f88edce1b17c2b8fa21dadd3.json

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

core/lib/dal/.sqlx/query-bdfd7e9d4462ac9cf6f91fced84355e6aec05ba4af297a03169e3122a67ae53e.json

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

core/lib/dal/src/transactions_dal.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,11 +2113,12 @@ impl TransactionsDal<'_, '_> {
21132113
Ok(data)
21142114
}
21152115

2116-
pub async fn get_call_trace(&mut self, tx_hash: H256) -> DalResult<Option<Call>> {
2116+
pub async fn get_call_trace(&mut self, tx_hash: H256) -> DalResult<Option<(Call, usize)>> {
21172117
let row = sqlx::query!(
21182118
r#"
21192119
SELECT
2120-
protocol_version
2120+
protocol_version,
2121+
index_in_block
21212122
FROM
21222123
transactions
21232124
INNER JOIN miniblocks ON transactions.miniblock_number = miniblocks.number
@@ -2155,7 +2156,12 @@ impl TransactionsDal<'_, '_> {
21552156
.with_arg("tx_hash", &tx_hash)
21562157
.fetch_optional(self.storage)
21572158
.await?
2158-
.map(|call_trace| parse_call_trace(&call_trace.call_trace, protocol_version)))
2159+
.map(|call_trace| {
2160+
(
2161+
parse_call_trace(&call_trace.call_trace, protocol_version),
2162+
row.index_in_block.unwrap_or_default() as usize,
2163+
)
2164+
}))
21592165
}
21602166

21612167
pub(crate) async fn get_tx_by_hash(&mut self, hash: H256) -> DalResult<Option<Transaction>> {
@@ -2227,7 +2233,7 @@ mod tests {
22272233
.await
22282234
.unwrap();
22292235

2230-
let call_trace = conn
2236+
let (call_trace, _) = conn
22312237
.transactions_dal()
22322238
.get_call_trace(tx_hash)
22332239
.await

core/lib/types/src/api/mod.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,17 @@ pub struct TracerConfig {
726726
pub tracer_config: CallTracerConfig,
727727
}
728728

729+
impl Default for TracerConfig {
730+
fn default() -> Self {
731+
TracerConfig {
732+
tracer: SupportedTracers::CallTracer,
733+
tracer_config: CallTracerConfig {
734+
only_top_call: false,
735+
},
736+
}
737+
}
738+
}
739+
729740
#[derive(Debug, Clone, Serialize, Deserialize)]
730741
#[serde(rename_all = "camelCase")]
731742
pub enum BlockStatus {
@@ -741,12 +752,51 @@ pub enum CallTracerBlockResult {
741752
CallTrace(Vec<ResultDebugCall>),
742753
FlatCallTrace(Vec<DebugCallFlat>),
743754
}
755+
impl CallTracerBlockResult {
756+
pub fn unwrap_flatten(self) -> Vec<DebugCallFlat> {
757+
match self {
758+
Self::CallTrace(_) => {
759+
unreachable!()
760+
}
761+
Self::FlatCallTrace(a) => a,
762+
}
763+
}
764+
765+
pub fn unwrap_default(self) -> Vec<ResultDebugCall> {
766+
match self {
767+
Self::CallTrace(a) => a,
768+
Self::FlatCallTrace(_) => {
769+
unreachable!()
770+
}
771+
}
772+
}
773+
}
744774

745775
#[derive(Debug, Serialize, Deserialize, Clone)]
746776
#[serde(untagged)]
747777
pub enum CallTracerResult {
748778
CallTrace(DebugCall),
749-
FlattCallTrace(Vec<DebugCallFlat>),
779+
FlatCallTrace(Vec<DebugCallFlat>),
780+
}
781+
782+
impl CallTracerResult {
783+
pub fn unwrap_flatten(self) -> Vec<DebugCallFlat> {
784+
match self {
785+
Self::CallTrace(_) => {
786+
unreachable!()
787+
}
788+
Self::FlatCallTrace(a) => a,
789+
}
790+
}
791+
792+
pub fn unwrap_default(self) -> DebugCall {
793+
match self {
794+
Self::CallTrace(a) => a,
795+
Self::FlatCallTrace(_) => {
796+
unreachable!()
797+
}
798+
}
799+
}
750800
}
751801

752802
#[derive(Debug, Clone, Serialize, Deserialize)]

core/lib/types/src/debug_flat_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct DebugCallFlat {
99
pub action: Action,
1010
pub result: Option<CallResult>,
1111
pub subtraces: usize,
12-
pub traceaddress: Vec<usize>,
12+
pub trace_address: Vec<usize>,
1313
pub transaction_position: usize,
1414
pub transaction_hash: H256,
1515
pub r#type: DebugCallType,

core/node/api_server/src/web3/namespaces/debug.rs

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,26 @@ impl DebugNamespace {
4949
call: Call,
5050
index: usize,
5151
transaction_hash: H256,
52-
tracer_option: Option<TracerConfig>,
52+
tracer_option: TracerConfig,
5353
) -> CallTracerResult {
54-
let (only_top_call, flatten) = tracer_option
55-
.map(|options| {
56-
(
57-
options.tracer_config.only_top_call,
58-
matches!(options.tracer, SupportedTracers::FlatCallTracer),
59-
)
60-
})
61-
.unwrap_or((false, false));
62-
if flatten {
63-
let mut calls = vec![];
64-
let mut traces = vec![index];
65-
Self::map_flatten_call(
54+
match tracer_option.tracer {
55+
SupportedTracers::CallTracer => CallTracerResult::CallTrace(Self::map_default_call(
6656
call,
67-
&mut calls,
68-
&mut traces,
69-
only_top_call,
70-
index,
71-
transaction_hash,
72-
);
73-
CallTracerResult::FlattCallTrace(calls)
74-
} else {
75-
CallTracerResult::CallTrace(Self::map_default_call(call, only_top_call))
57+
tracer_option.tracer_config.only_top_call,
58+
)),
59+
SupportedTracers::FlatCallTracer => {
60+
let mut calls = vec![];
61+
let mut traces = vec![index];
62+
Self::flatten_call(
63+
call,
64+
&mut calls,
65+
&mut traces,
66+
tracer_option.tracer_config.only_top_call,
67+
index,
68+
transaction_hash,
69+
);
70+
CallTracerResult::FlatCallTrace(calls)
71+
}
7672
}
7773
}
7874
pub(crate) fn map_default_call(call: Call, only_top_call: bool) -> DebugCall {
@@ -104,7 +100,7 @@ impl DebugNamespace {
104100
}
105101
}
106102

107-
fn map_flatten_call(
103+
fn flatten_call(
108104
call: Call,
109105
calls: &mut Vec<DebugCallFlat>,
110106
trace_address: &mut Vec<usize>,
@@ -139,7 +135,7 @@ impl DebugNamespace {
139135
},
140136
result,
141137
subtraces,
142-
traceaddress: trace_address.clone(), // Clone the current trace address
138+
trace_address: trace_address.clone(), // Clone the current trace address
143139
transaction_position,
144140
transaction_hash,
145141
r#type: DebugCallType::Call,
@@ -148,7 +144,7 @@ impl DebugNamespace {
148144
if !only_top_call {
149145
for (number, call) in call.calls.into_iter().enumerate() {
150146
trace_address.push(number);
151-
Self::map_flatten_call(
147+
Self::flatten_call(
152148
call,
153149
calls,
154150
trace_address,
@@ -187,20 +183,33 @@ impl DebugNamespace {
187183
.await
188184
.map_err(DalError::generalize)?;
189185

190-
let mut calls = vec![];
191-
let mut flat_calls = vec![];
192-
for (call_trace, hash, index) in call_traces {
193-
match Self::map_call(call_trace, index, hash, options) {
194-
CallTracerResult::CallTrace(call) => calls.push(ResultDebugCall { result: call }),
195-
CallTracerResult::FlattCallTrace(mut call) => flat_calls.append(&mut call),
186+
let options = options.unwrap_or_default();
187+
let result = match options.tracer {
188+
SupportedTracers::CallTracer => CallTracerBlockResult::CallTrace(
189+
call_traces
190+
.into_iter()
191+
.map(|(call, _, _)| ResultDebugCall {
192+
result: Self::map_default_call(call, options.tracer_config.only_top_call),
193+
})
194+
.collect(),
195+
),
196+
SupportedTracers::FlatCallTracer => {
197+
let mut flat_calls = vec![];
198+
for (call, tx_hash, tx_index) in call_traces {
199+
let mut traces = vec![tx_index];
200+
Self::flatten_call(
201+
call,
202+
&mut flat_calls,
203+
&mut traces,
204+
options.tracer_config.only_top_call,
205+
tx_index,
206+
tx_hash,
207+
);
208+
}
209+
CallTracerBlockResult::FlatCallTrace(flat_calls)
196210
}
197-
}
198-
199-
if calls.is_empty() && !flat_calls.is_empty() {
200-
Ok(CallTracerBlockResult::FlatCallTrace(flat_calls))
201-
} else {
202-
Ok(CallTracerBlockResult::CallTrace(calls))
203-
}
211+
};
212+
Ok(result)
204213
}
205214

206215
pub async fn debug_trace_transaction_impl(
@@ -214,7 +223,14 @@ impl DebugNamespace {
214223
.get_call_trace(tx_hash)
215224
.await
216225
.map_err(DalError::generalize)?;
217-
Ok(call_trace.map(|call_trace| Self::map_call(call_trace, 0, tx_hash, options)))
226+
Ok(call_trace.map(|(call_trace, index_in_block)| {
227+
Self::map_call(
228+
call_trace,
229+
index_in_block,
230+
tx_hash,
231+
options.unwrap_or_default(),
232+
)
233+
}))
218234
}
219235

220236
pub async fn debug_trace_call_impl(
@@ -226,10 +242,7 @@ impl DebugNamespace {
226242
let block_id = block_id.unwrap_or(BlockId::Number(BlockNumber::Pending));
227243
self.current_method().set_block_id(block_id);
228244

229-
let only_top_call = options
230-
.as_ref()
231-
.map(|options| options.tracer_config.only_top_call)
232-
.unwrap_or(false);
245+
let options = options.unwrap_or_default();
233246

234247
let mut connection = self.state.acquire_connection().await?;
235248
let block_args = self
@@ -264,7 +277,7 @@ impl DebugNamespace {
264277

265278
// We don't need properly trace if we only need top call
266279
let tracing_params = OneshotTracingParams {
267-
trace_calls: !only_top_call,
280+
trace_calls: !options.tracer_config.only_top_call,
268281
};
269282

270283
let connection = self.state.acquire_connection().await?;

core/node/api_server/src/web3/tests/debug.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ impl HttpTest for TraceBlockTest {
6363
let block_traces = match block_id {
6464
api::BlockId::Number(number) => client.trace_block_by_number(number, None).await?,
6565
api::BlockId::Hash(hash) => client.trace_block_by_hash(hash, None).await?,
66-
};
66+
}
67+
.unwrap_default();
6768

68-
let CallTracerBlockResult::CallTrace(block_traces) = block_traces else {
69-
unreachable!()
70-
};
7169
assert_eq!(block_traces.len(), tx_results.len()); // equals to the number of transactions in the block
7270
for (trace, tx_result) in block_traces.iter().zip(&tx_results) {
7371
let result = &trace.result;
@@ -140,11 +138,8 @@ impl HttpTest for TraceBlockFlatTest {
140138
},
141139
}),
142140
)
143-
.await?;
144-
145-
let CallTracerBlockResult::FlatCallTrace(block_traces) = &block_traces else {
146-
unreachable!()
147-
};
141+
.await?
142+
.unwrap_flatten();
148143

149144
// A transaction with 2 nested calls will convert into 3 Flattened calls.
150145
// Also in this test, all tx have the same # of nested calls
@@ -155,10 +150,10 @@ impl HttpTest for TraceBlockFlatTest {
155150

156151
// First tx has 2 nested calls, thus 2 sub-traces
157152
assert_eq!(block_traces[0].subtraces, 2);
158-
assert_eq!(block_traces[0].traceaddress, [0]);
153+
assert_eq!(block_traces[0].trace_address, [0]);
159154
// Second flat-call (fist nested call) do not have nested calls
160155
assert_eq!(block_traces[1].subtraces, 0);
161-
assert_eq!(block_traces[1].traceaddress, [0, 0]);
156+
assert_eq!(block_traces[1].trace_address, [0, 0]);
162157

163158
let top_level_call_indexes = [0, 3, 6];
164159
let top_level_traces = top_level_call_indexes
@@ -231,13 +226,11 @@ impl HttpTest for TraceTransactionTest {
231226
.map(|call| DebugNamespace::map_default_call(call.clone(), false))
232227
.collect();
233228

234-
let CallTracerResult::CallTrace(result) = client
229+
let result = client
235230
.trace_transaction(tx_results[0].hash, None)
236231
.await?
237232
.context("no transaction traces")?
238-
else {
239-
unreachable!()
240-
};
233+
.unwrap_default();
241234
assert_eq!(result.from, Address::zero());
242235
assert_eq!(result.to, BOOTLOADER_ADDRESS);
243236
assert_eq!(result.gas, tx_results[0].transaction.gas_limit());

0 commit comments

Comments
 (0)