1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use alloy_rpc_types_trace::geth::{
    GethDebugBuiltInTracerType,
    GethDebugTracerType::{BuiltInTracer, JsTracer},
    GethDebugTracingCallOptions, GethDebugTracingOptions, GethTrace, NoopFrame,
    TraceResult,
};
use async_trait::async_trait;
use cfx_rpc_eth_api::DebugApiServer;
use cfx_rpc_eth_types::{BlockNumber, TransactionRequest};
use cfx_rpc_utils::error::jsonrpsee_error_helpers::invalid_params_msg;
use cfx_types::{AddressSpaceUtil, Space, H256, U256};
use cfxcore::{
    errors::Error as CoreError, ConsensusGraph, ConsensusGraphTrait,
    SharedConsensusGraph,
};
use geth_tracer::to_alloy_h256;
use jsonrpsee::core::RpcResult;
use primitives::{
    Block, BlockHashOrEpochNumber, BlockHeaderBuilder, EpochNumber,
};
use std::sync::Arc;

pub struct DebugApi {
    consensus: SharedConsensusGraph,
    max_estimation_gas_limit: Option<U256>,
}

impl DebugApi {
    pub fn new(
        consensus: SharedConsensusGraph, max_estimation_gas_limit: Option<U256>,
    ) -> Self {
        DebugApi {
            consensus,
            max_estimation_gas_limit,
        }
    }

    pub fn consensus_graph(&self) -> &ConsensusGraph {
        self.consensus
            .as_any()
            .downcast_ref::<ConsensusGraph>()
            .expect("downcast should succeed")
    }

    pub fn get_block_epoch_num(
        &self, block: BlockNumber,
    ) -> Result<u64, String> {
        let num = match block {
            BlockNumber::Num(block_number) => block_number,
            BlockNumber::Latest
            | BlockNumber::Safe
            | BlockNumber::Finalized => {
                let epoch_num = block.try_into().expect("should success");
                self.consensus_graph()
                    .get_height_from_epoch_number(epoch_num)?
            }
            BlockNumber::Hash {
                hash,
                require_canonical,
            } => self
                .consensus_graph()
                .get_block_epoch_number_with_pivot_check(
                    &hash,
                    require_canonical,
                )
                .map_err(|err| err.to_string())?,
            _ => return Err("not supported".to_string()),
        };
        Ok(num)
    }

    pub fn trace_call(
        &self, mut request: TransactionRequest,
        block_number: Option<BlockNumber>,
        opts: Option<GethDebugTracingCallOptions>,
    ) -> Result<GethTrace, CoreError> {
        if request.from.is_none() {
            return Err(CoreError::InvalidParam(
                "from is required".to_string(),
                Default::default(),
            ));
        }

        let opts = opts.unwrap_or_default();
        let block_num = block_number.unwrap_or_default();

        let epoch_num = self
            .get_block_epoch_num(block_num)
            .map_err(|err| CoreError::Msg(err))?;

        // validate epoch state
        self.consensus_graph()
            .validate_stated_epoch(&EpochNumber::Number(epoch_num))
            .map_err(|err| CoreError::Msg(err))?;

        let epoch_block_hashes = self
            .consensus_graph()
            .get_block_hashes_by_epoch(EpochNumber::Number(epoch_num))
            .map_err(|err| CoreError::Msg(err))?;
        let epoch_id = epoch_block_hashes
            .last()
            .ok_or(CoreError::Msg("should have block hash".to_string()))?;

        // nonce auto fill
        if request.nonce.is_none() {
            let nonce = self.consensus_graph().next_nonce(
                request.from.unwrap().with_evm_space(),
                BlockHashOrEpochNumber::EpochNumber(EpochNumber::Number(
                    epoch_num,
                )),
                "num",
            )?;
            request.nonce = Some(nonce);
        }

        // construct blocks from call_request
        let chain_id = self.consensus.best_chain_id();
        // debug trace call has a fixed large gas limit.
        let signed_tx = request.sign_call(
            chain_id.in_evm_space(),
            self.max_estimation_gas_limit,
        )?;
        let epoch_blocks = self
            .consensus_graph()
            .data_man
            .blocks_by_hash_list(
                &epoch_block_hashes,
                true, /* update_cache */
            )
            .ok_or(CoreError::Msg("blocks should exist".to_string()))?;
        let pivot_block = epoch_blocks
            .last()
            .ok_or(CoreError::Msg("should have block".to_string()))?;

        let header = BlockHeaderBuilder::new()
            .with_base_price(pivot_block.block_header.base_price())
            .with_parent_hash(pivot_block.block_header.hash())
            .with_height(epoch_num + 1)
            .with_timestamp(pivot_block.block_header.timestamp() + 1)
            .with_gas_limit(*pivot_block.block_header.gas_limit())
            .build();
        let block = Block::new(header, vec![Arc::new(signed_tx)]);
        let blocks: Vec<Arc<Block>> = vec![Arc::new(block)];

        let traces = self.consensus_graph().collect_blocks_geth_trace(
            *epoch_id,
            epoch_num,
            &blocks,
            opts.tracing_options,
            None,
        )?;

        let res = traces
            .first()
            .ok_or(CoreError::Msg("trace generation failed".to_string()))?;

        Ok(res.trace.clone())
    }

    pub fn trace_block_by_num(
        &self, block_num: u64, opts: Option<GethDebugTracingOptions>,
    ) -> Result<Vec<TraceResult>, CoreError> {
        let opts = opts.unwrap_or_default();
        let epoch_traces = self
            .consensus_graph()
            .collect_epoch_geth_trace(block_num, None, opts)?;

        let result = epoch_traces
            .into_iter()
            .filter(|val| val.space == Space::Ethereum)
            .map(|val| TraceResult::Success {
                result: val.trace,
                tx_hash: Some(to_alloy_h256(val.tx_hash)),
            })
            .collect();
        Ok(result)
    }

    pub fn trace_transaction(
        &self, hash: H256, opts: Option<GethDebugTracingOptions>,
    ) -> Result<GethTrace, CoreError> {
        let opts = opts.unwrap_or_default();

        // early return if tracer is not supported or NoopTracer is requested
        if let Some(tracer_type) = &opts.tracer {
            match tracer_type {
                BuiltInTracer(builtin_tracer) => match builtin_tracer {
                    GethDebugBuiltInTracerType::FourByteTracer => (),
                    GethDebugBuiltInTracerType::CallTracer => {
                        // pre check config
                        let _ = opts
                            .tracer_config
                            .clone()
                            .into_call_config()
                            .map_err(|err| {
                            CoreError::Msg(err.to_string())
                        })?;
                        ()
                    }
                    GethDebugBuiltInTracerType::PreStateTracer => {
                        // pre check config
                        let _ = opts
                            .tracer_config
                            .clone()
                            .into_pre_state_config()
                            .map_err(|err| CoreError::Msg(err.to_string()))?;
                        ()
                    }
                    GethDebugBuiltInTracerType::NoopTracer => {
                        return Ok(GethTrace::NoopTracer(NoopFrame::default()))
                    }
                    GethDebugBuiltInTracerType::MuxTracer => {
                        return Err(CoreError::Msg("not supported".to_string()))
                    }
                },
                JsTracer(_) => {
                    return Err(CoreError::Msg("not supported".to_string()))
                }
            }
        }

        let tx_index = self
            .consensus
            .get_data_manager()
            .transaction_index_by_hash(&hash, false /* update_cache */)
            .ok_or(CoreError::Msg("invalid tx hash".to_string()))?;

        let epoch_num = self
            .consensus
            .get_block_epoch_number(&tx_index.block_hash)
            .ok_or(CoreError::Msg("invalid tx hash".to_string()))?;

        let epoch_traces = self.consensus_graph().collect_epoch_geth_trace(
            epoch_num,
            Some(hash),
            opts,
        )?;

        // filter by tx hash
        let trace = epoch_traces
            .into_iter()
            .find(|val| val.tx_hash == hash)
            .map(|val| val.trace)
            .ok_or(CoreError::Msg("trace generation failed".to_string()))?;

        Ok(trace)
    }
}

#[async_trait]
impl DebugApiServer for DebugApi {
    async fn db_get(&self, _key: String) -> RpcResult<Option<String>> {
        Ok(Some("To be implemented!".into()))
    }

    async fn debug_trace_transaction(
        &self, tx_hash: H256, opts: Option<GethDebugTracingOptions>,
    ) -> RpcResult<GethTrace> {
        self.trace_transaction(tx_hash, opts).map_err(|e| e.into())
    }

    async fn debug_trace_block_by_hash(
        &self, block_hash: H256, opts: Option<GethDebugTracingOptions>,
    ) -> RpcResult<Vec<TraceResult>> {
        let epoch_num = self
            .consensus_graph()
            .get_block_epoch_number_with_pivot_check(&block_hash, false)?;

        self.trace_block_by_num(epoch_num, opts)
            .map_err(|e| e.into())
    }

    async fn debug_trace_block_by_number(
        &self, block: BlockNumber, opts: Option<GethDebugTracingOptions>,
    ) -> RpcResult<Vec<TraceResult>> {
        let num = self
            .get_block_epoch_num(block)
            .map_err(|e| invalid_params_msg(&e))?;

        self.trace_block_by_num(num, opts).map_err(|e| e.into())
    }

    async fn debug_trace_call(
        &self, request: TransactionRequest, block_number: Option<BlockNumber>,
        opts: Option<GethDebugTracingCallOptions>,
    ) -> RpcResult<GethTrace> {
        self.trace_call(request, block_number, opts)
            .map_err(|e| e.into())
    }
}