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
use std::vec;

use cfx_addr::Network;
use cfx_parity_trace_types::Action;
use cfx_rpc_cfx_impl::TraceHandler;
use cfx_rpc_cfx_types::{
    trace_eth::{LocalizedSetAuthTrace, LocalizedTrace as EthLocalizedTrace},
    PhantomBlock,
};
use cfx_rpc_common_impl::trace::{
    into_eth_localized_traces, primitive_traces_to_eth_localized_traces,
};
use cfx_rpc_eth_api::TraceApiServer;
use cfx_rpc_eth_types::{BlockNumber, Index, LocalizedTrace, TraceFilter};
use cfx_types::H256;
use cfx_util_macros::unwrap_option_or_return_result_none as unwrap_or_return;
use cfxcore::{errors::Result as CoreResult, SharedConsensusGraph};
use jsonrpc_core::Error as RpcError;
use jsonrpsee::{core::RpcResult, types::ErrorObjectOwned};
use log::warn;
use primitives::EpochNumber;
pub struct TraceApi {
    trace_handler: TraceHandler,
}

impl TraceApi {
    pub fn new(consensus: SharedConsensusGraph, network: Network) -> TraceApi {
        let trace_handler = TraceHandler::new(network, consensus);
        TraceApi { trace_handler }
    }

    pub fn get_block(
        &self, block_number: BlockNumber,
    ) -> CoreResult<Option<PhantomBlock>> {
        let phantom_block = match block_number {
            BlockNumber::Hash { hash, .. } => self
                .trace_handler
                .consensus_graph()
                .get_phantom_block_by_hash(
                    &hash, true, /* include_traces */
                )
                .map_err(RpcError::invalid_params)?,

            _ => self
                .trace_handler
                .consensus_graph()
                .get_phantom_block_by_number(
                    block_number.try_into()?,
                    None,
                    true, /* include_traces */
                )
                .map_err(RpcError::invalid_params)?,
        };

        Ok(phantom_block)
    }

    pub fn block_traces(
        &self, block_number: BlockNumber,
    ) -> CoreResult<Option<Vec<LocalizedTrace>>> {
        let phantom_block = self.get_block(block_number)?;

        unwrap_or_return!(phantom_block);

        let mut eth_traces = Vec::new();
        let block_number = phantom_block.pivot_header.height();
        let block_hash = phantom_block.pivot_header.hash();
        let network = self.trace_handler.network;

        for (idx, tx_traces) in phantom_block.traces.into_iter().enumerate() {
            let tx_hash = phantom_block.transactions[idx].hash();
            let tx_eth_traces = into_eth_localized_traces(
                &tx_traces.0,
                block_number,
                block_hash,
                tx_hash,
                idx,
                network,
            )
            .map_err(|e| {
                warn!("Internal error on trace reconstruction: {}", e);
                RpcError::internal_error()
            })?;
            eth_traces.extend(tx_eth_traces);
        }

        Ok(Some(eth_traces))
    }

    pub fn block_set_auth_traces(
        &self, block_number: BlockNumber,
    ) -> CoreResult<Option<Vec<LocalizedSetAuthTrace>>> {
        let phantom_block = self.get_block(block_number)?;

        unwrap_or_return!(phantom_block);

        let mut eth_traces = Vec::new();
        let block_number = phantom_block.pivot_header.height();
        let block_hash = phantom_block.pivot_header.hash();

        for (idx, tx_traces) in phantom_block.traces.into_iter().enumerate() {
            let tx_hash = phantom_block.transactions[idx].hash();

            let tx_eth_traces: Vec<LocalizedSetAuthTrace> = tx_traces
                .0
                .iter()
                .filter_map(|trace| match trace.action {
                    Action::SetAuth(ref set_auth) => {
                        Some(LocalizedSetAuthTrace::new(
                            set_auth,
                            idx,
                            tx_hash,
                            block_number,
                            block_hash,
                        ))
                    }
                    _ => None,
                })
                .collect();

            eth_traces.extend(tx_eth_traces);
        }

        Ok(Some(eth_traces))
    }

    pub fn filter_traces(
        &self, filter: TraceFilter,
    ) -> CoreResult<Vec<LocalizedTrace>> {
        let primitive_filter = filter.into_primitive()?;

        let Some(primitive_traces) = self
            .trace_handler
            .filter_primitives_traces_impl(primitive_filter)?
        else {
            return Ok(vec![]);
        };

        let traces = primitive_traces_to_eth_localized_traces(
            &primitive_traces,
            self.trace_handler.network,
        )
        .map_err(|e| {
            warn!("Internal error on trace reconstruction: {}", e);
            RpcError::internal_error()
        })?;
        Ok(traces)
    }

    pub fn transaction_traces(
        &self, tx_hash: H256,
    ) -> CoreResult<Option<Vec<EthLocalizedTrace>>> {
        let tx_index = self
            .trace_handler
            .data_man
            .transaction_index_by_hash(&tx_hash, false /* update_cache */);

        unwrap_or_return!(tx_index);

        let epoch_num = self
            .trace_handler
            .consensus
            .get_block_epoch_number(&tx_index.block_hash);

        unwrap_or_return!(epoch_num);

        let phantom_block = self
            .trace_handler
            .consensus_graph()
            .get_phantom_block_by_number(
                EpochNumber::Number(epoch_num),
                None,
                true, /* include_traces */
            )
            .map_err(RpcError::invalid_params)?;

        unwrap_or_return!(phantom_block);

        // find tx corresponding to `tx_hash`
        let id = phantom_block
            .transactions
            .iter()
            .position(|tx| tx.hash() == tx_hash);

        unwrap_or_return!(id);

        let tx = &phantom_block.transactions[id];
        let tx_traces = phantom_block.traces[id].clone();

        let network = self.trace_handler.network;

        let eth_traces = into_eth_localized_traces(
            &tx_traces.0,
            epoch_num,
            phantom_block.pivot_header.hash(),
            tx.hash(),
            id,
            network,
        )
        .map_err(|e| {
            warn!("Internal error on trace reconstruction: {}", e);
            RpcError::internal_error()
        })?;

        Ok(Some(eth_traces))
    }
}

#[async_trait::async_trait]
impl TraceApiServer for TraceApi {
    async fn block_traces(
        &self, block_number: BlockNumber,
    ) -> RpcResult<Option<Vec<LocalizedTrace>>> {
        self.block_traces(block_number).map_err(|err| err.into())
    }

    async fn filter_traces(
        &self, filter: TraceFilter,
    ) -> RpcResult<Vec<LocalizedTrace>> {
        self.filter_traces(filter).map_err(|err| err.into())
    }

    async fn transaction_traces(
        &self, tx_hash: H256,
    ) -> RpcResult<Option<Vec<EthLocalizedTrace>>> {
        self.transaction_traces(tx_hash).map_err(|err| err.into())
    }

    async fn block_set_auth_traces(
        &self, block_number: BlockNumber,
    ) -> RpcResult<Option<Vec<LocalizedSetAuthTrace>>> {
        self.block_set_auth_traces(block_number)
            .map_err(|err| err.into())
    }

    async fn trace_get(
        &self, tx_hash: H256, indices: Vec<Index>,
    ) -> RpcResult<Option<LocalizedTrace>> {
        if indices.is_empty() {
            return Ok(None);
        }
        let Some(traces) = self
            .transaction_traces(tx_hash)
            .map_err(|err| ErrorObjectOwned::from(err))?
        else {
            return Ok(None);
        };
        let index = indices[0].value();
        Ok(traces.get(index).cloned())
    }
}