client/rpc/impls/cfx/
cfx_filter.rs

1// Copyright 2022 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5use std::sync::Arc;
6
7use cfx_addr::Network;
8use cfx_rpc_cfx_api::CfxFilterRpcServer;
9use cfx_rpc_cfx_impl::CfxFilterHandler;
10use cfx_rpc_utils::error::jsonrpc_error_helpers::error_object_owned_to_jsonrpc_error;
11use cfx_types::{H128, H256};
12use cfxcore::{channel::Channel, SharedConsensusGraph, SharedTransactionPool};
13use jsonrpc_core::Result as JsonRpcResult;
14use tokio::runtime::Runtime;
15
16use crate::rpc::{
17    traits::cfx_filter::CfxFilter,
18    types::{CfxFilterChanges, CfxRpcLogFilter, Log},
19};
20
21/// Cfx filter rpc implementation for a full node.
22pub struct CfxFilterClient {
23    inner: CfxFilterHandler,
24}
25
26impl CfxFilterClient {
27    /// Creates new Cfx filter client.
28    pub fn new(
29        consensus: SharedConsensusGraph, tx_pool: SharedTransactionPool,
30        epochs_ordered: Arc<Channel<(u64, Vec<H256>)>>, executor: Arc<Runtime>,
31        poll_lifetime: u32, logs_filter_max_limit: Option<usize>,
32        network: Network,
33    ) -> Self {
34        CfxFilterClient {
35            inner: CfxFilterHandler::new(
36                consensus,
37                tx_pool,
38                epochs_ordered,
39                executor,
40                poll_lifetime,
41                logs_filter_max_limit,
42                network,
43            ),
44        }
45    }
46}
47
48impl CfxFilter for CfxFilterClient {
49    fn new_filter(&self, filter: CfxRpcLogFilter) -> JsonRpcResult<H128> {
50        self.inner
51            .new_filter(filter)
52            .map_err(error_object_owned_to_jsonrpc_error)
53    }
54
55    fn new_block_filter(&self) -> JsonRpcResult<H128> {
56        self.inner
57            .new_block_filter()
58            .map_err(error_object_owned_to_jsonrpc_error)
59    }
60
61    fn new_pending_transaction_filter(&self) -> JsonRpcResult<H128> {
62        self.inner
63            .new_pending_transaction_filter()
64            .map_err(error_object_owned_to_jsonrpc_error)
65    }
66
67    fn filter_changes(&self, index: H128) -> JsonRpcResult<CfxFilterChanges> {
68        self.inner
69            .filter_changes(index)
70            .map_err(error_object_owned_to_jsonrpc_error)
71    }
72
73    fn filter_logs(&self, index: H128) -> JsonRpcResult<Vec<Log>> {
74        self.inner
75            .filter_logs(index)
76            .map_err(error_object_owned_to_jsonrpc_error)
77    }
78
79    fn uninstall_filter(&self, index: H128) -> JsonRpcResult<bool> {
80        self.inner
81            .uninstall_filter(index)
82            .map_err(error_object_owned_to_jsonrpc_error)
83    }
84}