Skip to content

Commit afe5e68

Browse files
committed
Refactor to add .cycles_cost() method
1 parent 8fd5e1f commit afe5e68

File tree

5 files changed

+414
-292
lines changed

5 files changed

+414
-292
lines changed

src/candid_rpc/mod.rs

Lines changed: 57 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,15 @@
11
mod cketh_conversion;
2-
#[cfg(test)]
3-
mod tests;
42

5-
use crate::rpc_client::{EthRpcClient, ReducedResult};
6-
use crate::types::MetricRpcMethod;
7-
use crate::{
8-
add_metric_entry,
9-
providers::resolve_rpc_service,
10-
types::{MetricRpcHost, ResolvedRpcService, RpcMethod},
11-
};
3+
use crate::rpc_client::EthRpcClient;
4+
use crate::types::RpcMethod;
125
use candid::Nat;
136
use canhttp::http::json::JsonRpcRequest;
14-
use canhttp::multi::{ReductionError, Timestamp};
7+
use canhttp::multi::Timestamp;
158
use ethers_core::{types::Transaction, utils::rlp};
169
use evm_rpc_types::{
1710
BlockTag, GetLogsArgs, Hex, Hex32, MultiRpcResult, Nat256, RpcError, RpcResult, ValidationError,
1811
};
1912

20-
fn process_result<T>(
21-
method: impl Into<MetricRpcMethod> + Clone,
22-
result: ReducedResult<T>,
23-
) -> MultiRpcResult<T> {
24-
match result {
25-
Ok(value) => MultiRpcResult::Consistent(Ok(value)),
26-
Err(err) => match err {
27-
ReductionError::ConsistentError(err) => MultiRpcResult::Consistent(Err(err)),
28-
ReductionError::InconsistentResults(multi_call_results) => {
29-
let results: Vec<_> = multi_call_results.into_iter().collect();
30-
results.iter().for_each(|(service, _service_result)| {
31-
if let Ok(ResolvedRpcService::Provider(provider)) =
32-
resolve_rpc_service(service.clone())
33-
{
34-
add_metric_entry!(
35-
inconsistent_responses,
36-
(
37-
method.clone().into(),
38-
MetricRpcHost(
39-
provider
40-
.hostname()
41-
.unwrap_or_else(|| "(unknown)".to_string())
42-
)
43-
),
44-
1
45-
)
46-
}
47-
});
48-
MultiRpcResult::Inconsistent(results)
49-
}
50-
},
51-
}
52-
}
53-
5413
/// Adapt the `EthRpcClient` to the `Candid` interface used by the EVM-RPC canister.
5514
pub struct CandidRpcClient {
5615
client: EthRpcClient,
@@ -70,70 +29,72 @@ impl CandidRpcClient {
7029
pub async fn eth_get_logs(
7130
&self,
7231
args: evm_rpc_types::GetLogsArgs,
73-
max_block_range: u32,
7432
) -> MultiRpcResult<Vec<evm_rpc_types::LogEntry>> {
7533
use crate::candid_rpc::cketh_conversion::{from_log_entries, into_get_logs_param};
76-
process_result(
77-
RpcMethod::EthGetLogs,
78-
self.client.eth_get_logs(into_get_logs_param(args)).await,
79-
)
80-
.map(from_log_entries)
34+
self.client
35+
.eth_get_logs(into_get_logs_param(args))
36+
.send_and_reduce()
37+
.await
38+
.map(from_log_entries)
39+
}
40+
41+
pub async fn eth_get_logs_request_cost(
42+
&self,
43+
args: evm_rpc_types::GetLogsArgs,
44+
) -> RpcResult<u128> {
45+
use crate::candid_rpc::cketh_conversion::into_get_logs_param;
46+
self.client
47+
.eth_get_logs(into_get_logs_param(args))
48+
.cycles_cost()
49+
.await
8150
}
8251

8352
pub async fn eth_get_block_by_number(
8453
&self,
8554
block: BlockTag,
8655
) -> MultiRpcResult<evm_rpc_types::Block> {
8756
use crate::candid_rpc::cketh_conversion::{from_block, into_block_spec};
88-
process_result(
89-
RpcMethod::EthGetBlockByNumber,
90-
self.client
91-
.eth_get_block_by_number(into_block_spec(block))
92-
.await,
93-
)
94-
.map(from_block)
57+
self.client
58+
.eth_get_block_by_number(into_block_spec(block))
59+
.send_and_reduce()
60+
.await
61+
.map(from_block)
9562
}
9663

9764
pub async fn eth_get_transaction_receipt(
9865
&self,
9966
hash: Hex32,
10067
) -> MultiRpcResult<Option<evm_rpc_types::TransactionReceipt>> {
10168
use crate::candid_rpc::cketh_conversion::{from_transaction_receipt, into_hash};
102-
process_result(
103-
RpcMethod::EthGetTransactionReceipt,
104-
self.client
105-
.eth_get_transaction_receipt(into_hash(hash))
106-
.await,
107-
)
108-
.map(|option| option.map(from_transaction_receipt))
69+
self.client
70+
.eth_get_transaction_receipt(into_hash(hash))
71+
.send_and_reduce()
72+
.await
73+
.map(|option| option.map(from_transaction_receipt))
10974
}
11075

11176
pub async fn eth_get_transaction_count(
11277
&self,
11378
args: evm_rpc_types::GetTransactionCountArgs,
11479
) -> MultiRpcResult<Nat256> {
11580
use crate::candid_rpc::cketh_conversion::into_get_transaction_count_params;
116-
process_result(
117-
RpcMethod::EthGetTransactionCount,
118-
self.client
119-
.eth_get_transaction_count(into_get_transaction_count_params(args))
120-
.await,
121-
)
122-
.map(Nat256::from)
81+
self.client
82+
.eth_get_transaction_count(into_get_transaction_count_params(args))
83+
.send_and_reduce()
84+
.await
85+
.map(Nat256::from)
12386
}
12487

12588
pub async fn eth_fee_history(
12689
&self,
12790
args: evm_rpc_types::FeeHistoryArgs,
12891
) -> MultiRpcResult<evm_rpc_types::FeeHistory> {
12992
use crate::candid_rpc::cketh_conversion::{from_fee_history, into_fee_history_params};
130-
process_result(
131-
RpcMethod::EthFeeHistory,
132-
self.client
133-
.eth_fee_history(into_fee_history_params(args))
134-
.await,
135-
)
136-
.map(from_fee_history)
93+
self.client
94+
.eth_fee_history(into_fee_history_params(args))
95+
.send_and_reduce()
96+
.await
97+
.map(from_fee_history)
13798
}
13899

139100
pub async fn eth_send_raw_transaction(
@@ -142,25 +103,23 @@ impl CandidRpcClient {
142103
) -> MultiRpcResult<evm_rpc_types::SendRawTransactionStatus> {
143104
use crate::candid_rpc::cketh_conversion::from_send_raw_transaction_result;
144105
let transaction_hash = get_transaction_hash(&raw_signed_transaction_hex);
145-
process_result(
146-
RpcMethod::EthSendRawTransaction,
147-
self.client
148-
.eth_send_raw_transaction(raw_signed_transaction_hex.to_string())
149-
.await,
150-
)
151-
.map(|result| from_send_raw_transaction_result(transaction_hash.clone(), result))
106+
self.client
107+
.eth_send_raw_transaction(raw_signed_transaction_hex.to_string())
108+
.send_and_reduce()
109+
.await
110+
.map(|result| from_send_raw_transaction_result(transaction_hash.clone(), result))
152111
}
153112

154113
pub async fn eth_call(
155114
&self,
156115
args: evm_rpc_types::CallArgs,
157116
) -> MultiRpcResult<evm_rpc_types::Hex> {
158117
use crate::candid_rpc::cketh_conversion::{from_data, into_eth_call_params};
159-
process_result(
160-
RpcMethod::EthCall,
161-
self.client.eth_call(into_eth_call_params(args)).await,
162-
)
163-
.map(from_data)
118+
self.client
119+
.eth_call(into_eth_call_params(args))
120+
.send_and_reduce()
121+
.await
122+
.map(from_data)
164123
}
165124

166125
pub async fn multi_request(&self, json_rpc_payload: String) -> MultiRpcResult<String> {
@@ -173,19 +132,14 @@ impl CandidRpcClient {
173132
)))
174133
}
175134
};
176-
process_result(
177-
MetricRpcMethod {
178-
method: request.method().to_string(),
179-
is_manual_request: true,
180-
},
181-
self.client
182-
.multi_request(
183-
RpcMethod::Custom(request.method().to_string()),
184-
request.params(),
185-
)
186-
.await,
187-
)
188-
.map(String::from)
135+
self.client
136+
.multi_request(
137+
RpcMethod::Custom(request.method().to_string()),
138+
request.params(),
139+
)
140+
.send_and_reduce()
141+
.await
142+
.map(String::from)
189143
}
190144
}
191145

src/candid_rpc/tests.rs

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

src/main.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,29 @@ pub async fn eth_get_logs(
4747
return MultiRpcResult::Consistent(Err(err));
4848
}
4949
match CandidRpcClient::new(source, Some(RpcConfig::from(config)), now()) {
50-
Ok(source) => source.eth_get_logs(args, max_block_range).await,
50+
Ok(source) => source.eth_get_logs(args).await,
5151
Err(err) => Err(err).into(),
5252
}
5353
}
5454

55+
#[update(name = "eth_getLogsRequestCost")]
56+
pub async fn eth_get_logs_request_cost(
57+
source: RpcServices,
58+
config: Option<evm_rpc_types::GetLogsRpcConfig>,
59+
args: evm_rpc_types::GetLogsArgs,
60+
) -> RpcResult<u128> {
61+
let config = config.unwrap_or_default();
62+
let max_block_range = config.max_block_range_or_default();
63+
validate_get_logs_block_range(&args, max_block_range)?;
64+
if is_demo_active() {
65+
return Ok(0);
66+
}
67+
match CandidRpcClient::new(source, Some(RpcConfig::from(config)), now()) {
68+
Ok(source) => source.eth_get_logs_request_cost(args).await,
69+
Err(err) => Err(err),
70+
}
71+
}
72+
5573
#[update(name = "eth_getBlockByNumber")]
5674
pub async fn eth_get_block_by_number(
5775
source: RpcServices,

0 commit comments

Comments
 (0)