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
use std::{future, sync::Arc, time::Duration};
use futures::StreamExt;

// #[derive(Clone)]
// pub struct RpcRequesters {
//     pub debub: Option<moonbeam_rpc_debug::DebugRequester>,
//     pub trace: Option<moonbeam_rpc_trace::CacheRequester>,
// }

pub struct SpawnTaskParams<'a, B: sp_api::BlockT, C, BE> {
    pub task_manager: &'a sc_service::TaskManager,
    pub client: Arc<C>,
    pub substrate_backend: Arc<BE>,
    pub frontier_backend: fc_db::Backend<B>,
    pub filter_pool: Option<fc_rpc_core::types::FilterPool>,
    pub overrides: Arc<fc_rpc::OverrideHandle<B>>,
    pub fee_history_cache: fc_rpc_core::types::FeeHistoryCache,
    pub fee_history_limit: fc_rpc_core::types::FeeHistoryCacheLimit,
    pub sync: Arc<sc_network_sync::SyncingService<B>>,
    pub pubsub_notification_sinks: Arc<fc_mapping_sync::EthereumBlockNotificationSinks<
        fc_mapping_sync::EthereumBlockNotification<B>>>,
    pub prometheus_registry: Option<substrate_prometheus_endpoint::Registry>,
}

pub fn spawn_evm_tracing<B, C, BE>(
    spawn_task_params: SpawnTaskParams<B, C, BE>,
)
    where
        B: sp_runtime::traits::Block<Hash=sp_core::H256> + Send + Sync + 'static,
        B::Header: sp_api::HeaderT<Number=u32>,
        C: sc_client_api::StorageProvider<B, BE> + Sync + Send + 'static
            + sc_client_api::BlockOf
            + sp_api::ProvideRuntimeApi<B>
            + sc_client_api::HeaderBackend<B>
            + sc_client_api::BlockchainEvents<B>
            + sp_blockchain::HeaderMetadata<B, Error=sp_blockchain::Error>,
        C::Api: fp_rpc::EthereumRuntimeRPCApi<B>
            // + moonbeam_rpc_primitives_debug::DebugRuntimeApi<B>
            + sp_block_builder::BlockBuilder<B>,
        BE: sc_client_api::Backend<B> + 'static,
        BE::State: sc_client_api::StateBackend<sp_runtime::traits::BlakeTwo256>,
{
    let SpawnTaskParams {
        task_manager,
        client,
        substrate_backend,
        frontier_backend,
        filter_pool,
        overrides,
        fee_history_cache,
        fee_history_limit,
        sync,
        pubsub_notification_sinks,
        prometheus_registry: _,
    } = spawn_task_params;

    match frontier_backend {
        fc_db::Backend::KeyValue(db) => {
            task_manager.spawn_essential_handle().spawn(
                "frontier-mapping-sync",
                Some("frontier"),
                fc_mapping_sync::kv::MappingSyncWorker::new(
                    client.import_notification_stream(),
                    Duration::new(1, 0),
                    client.clone(),
                    substrate_backend,
                    overrides.clone(),
                    Arc::new(db),
                    3,
                    0,
                    fc_mapping_sync::SyncStrategy::Normal,
                    sync,
                    pubsub_notification_sinks,
                ).for_each(|()| future::ready(())),
            );
        }
        fc_db::Backend::Sql(db) => {
            task_manager.spawn_essential_handle().spawn(
                "frontier-mapping-sync",
                Some("frontier"),
                fc_mapping_sync::sql::SyncWorker::run(
                    client.clone(),
                    substrate_backend,
                    Arc::new(db),
                    client.import_notification_stream(),
                    fc_mapping_sync::sql::SyncWorkerConfig {
                        check_indexed_blocks_interval: Duration::from_secs(60),
                        read_notification_timeout: Duration::from_secs(10),
                    },
                    fc_mapping_sync::SyncStrategy::Normal,
                    sync,
                    pubsub_notification_sinks,
                ),
            );
        }
    }

    if let Some(filter_pool) = filter_pool {
        const FILTER_RETAIN_THRESHOLD: u64 = 100;
        task_manager.spawn_essential_handle().spawn(
            "frontier-filter-pool",
            Some("frontier"),
            fc_rpc::EthTask::filter_pool_task(client.clone(), filter_pool, FILTER_RETAIN_THRESHOLD),
        )
    }

    task_manager.spawn_essential_handle().spawn(
        "frontier-fee-history",
        Some("frontier"),
        fc_rpc::EthTask::fee_history_task(
            client,
            overrides,
            fee_history_cache,
            fee_history_limit,
        ),
    );

    // if evm_tracing_config.ethapi.contains(&EthApi::Debug)
    //     || evm_tracing_config.ethapi.contains(&EthApi::Trace) {
    //     let permit_pool = Arc::new(tokio::sync::Semaphore::new(evm_tracing_config.ethapi_max_permits as usize));
    //
    //     let (trace_filter_task, trace_filter_requester) =
    //         if evm_tracing_config.ethapi.contains(&EthApi::Trace) {
    //             let (trace_filter_task, trace_filter_requester) = moonbeam_rpc_trace::CacheTask::create(
    //                 client.clone(),
    //                 substrate_backend.clone(),
    //                 Duration::from_secs(evm_tracing_config.ethapi_trace_cache_duration),
    //                 Arc::clone(&permit_pool),
    //                 overrides.clone(),
    //                 prometheus_registry
    //             );
    //             (Some(trace_filter_task), Some(trace_filter_requester))
    //         } else {
    //             (None, None)
    //         };
    //
    //     let (debug_task, debug_requester) =
    //         if evm_tracing_config.ethapi.contains(&EthApi::Debug) {
    //             let (debug_task, debug_requester) = moonbeam_rpc_debug::DebugHandler::task(
    //                 client,
    //                 substrate_backend,
    //                 match frontier_backend {
    //                     fc_db::Backend::KeyValue(db) => Arc::new(db),
    //                     fc_db::Backend::Sql(db) => Arc::new(db),
    //                 },
    //                 Arc::clone(&permit_pool),
    //                 overrides,
    //                 evm_tracing_config.tracing_raw_max_memory_usage,
    //             );
    //             (Some(debug_task), Some(debug_requester))
    //         } else {
    //             (None, None)
    //         };
    //
    //     if let Some(trace_filter_task) = trace_filter_task {
    //         task_manager.spawn_essential_handle().spawn(
    //             "frontier-trace-filter",
    //             Some("eth-tracing"),
    //             trace_filter_task,
    //         );
    //     }
    //
    //     if let Some(debug_task) = debug_task {
    //         task_manager.spawn_essential_handle().spawn(
    //             "frontier-debug",
    //             Some("eth-tracing"),
    //             debug_task,
    //         );
    //     }
    //
    //     RpcRequesters {
    //         debub: debug_requester,
    //         trace: trace_filter_requester,
    //     }
    // } else {
    //     RpcRequesters {
    //         debub: None,
    //         trace: None,
    //     }
    // }
}