Skip to content

Commit ca24d8f

Browse files
committed
graph: Introduce CheapClone and use it a bit
1 parent 59b7aa4 commit ca24d8f

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

chain/ethereum/src/ethereum_adapter.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use graph::components::ethereum::{EthereumAdapter as EthereumAdapterTrait, *};
1212
use graph::prelude::{
1313
debug, err_msg, error, ethabi, format_err,
1414
futures03::{self, compat::Future01CompatExt, FutureExt, StreamExt, TryStreamExt},
15-
hex, retry, stream, tiny_keccak, trace, warn, web3, ChainStore, DynTryFuture, Error,
16-
EthereumCallCache, Logger, TimeoutError,
15+
hex, retry, stream, tiny_keccak, trace, warn, web3, ChainStore, CheapClone, DynTryFuture,
16+
Error, EthereumCallCache, Logger, TimeoutError,
1717
};
1818
use web3::api::Web3;
1919
use web3::transports::batch::Batch;
@@ -62,6 +62,15 @@ lazy_static! {
6262
.expect("invalid GRAPH_ETHEREUM_REQUEST_RETRIES env var");
6363
}
6464

65+
impl<T: web3::Transport> CheapClone for EthereumAdapter<T> {
66+
fn cheap_clone(&self) -> Self {
67+
Self {
68+
web3: self.web3.cheap_clone(),
69+
metrics: self.metrics.cheap_clone(),
70+
}
71+
}
72+
}
73+
6574
impl<T> EthereumAdapter<T>
6675
where
6776
T: web3::BatchTransport + Send + Sync + 'static,
@@ -168,7 +177,7 @@ where
168177
subgraph_metrics: Arc<SubgraphEthRpcMetrics>,
169178
from: u64,
170179
to: u64,
171-
filter: EthGetLogsFilter,
180+
filter: Arc<EthGetLogsFilter>,
172181
too_many_logs_fingerprints: &'static [&'static str],
173182
) -> impl Future<Item = Vec<Log>, Error = TimeoutError<web3::error::Error>> {
174183
let eth_adapter = self.clone();
@@ -276,7 +285,9 @@ where
276285
}
277286

278287
// Collect all event sigs
279-
let eth = self.clone();
288+
let eth = self.cheap_clone();
289+
let filter = Arc::new(filter);
290+
280291
let step = match filter.contracts.is_empty() {
281292
// `to - from + 1` blocks will be scanned.
282293
false => to - from,
@@ -287,10 +298,10 @@ where
287298
// node returns an error that signifies the request is to heavy to process, the range will
288299
// be broken down to smaller steps.
289300
futures03::stream::try_unfold((from, step), move |(start, step)| {
290-
let logger = logger.clone();
291-
let filter = filter.clone();
292-
let eth = eth.clone();
293-
let subgraph_metrics = subgraph_metrics.clone();
301+
let logger = logger.cheap_clone();
302+
let filter = filter.cheap_clone();
303+
let eth = eth.cheap_clone();
304+
let subgraph_metrics = subgraph_metrics.cheap_clone();
294305

295306
async move {
296307
if start > to {
@@ -305,10 +316,10 @@ where
305316
let res = eth
306317
.logs_with_sigs(
307318
&logger,
308-
subgraph_metrics.clone(),
319+
subgraph_metrics.cheap_clone(),
309320
start,
310321
end,
311-
filter.clone(),
322+
filter.cheap_clone(),
312323
TOO_MANY_LOGS_FINGERPRINTS,
313324
)
314325
.compat()
@@ -1035,12 +1046,18 @@ where
10351046
to: u64,
10361047
log_filter: EthereumLogFilter,
10371048
) -> DynTryFuture<'static, Vec<Log>, Error> {
1038-
let eth: Self = self.clone();
1049+
let eth: Self = self.cheap_clone();
10391050
let logger = logger.clone();
10401051

10411052
futures03::stream::iter(log_filter.eth_get_logs_filters().map(move |filter| {
1042-
eth.clone()
1043-
.log_stream(logger.clone(), subgraph_metrics.clone(), from, to, filter)
1053+
eth.cheap_clone()
1054+
.log_stream(
1055+
logger.cheap_clone(),
1056+
subgraph_metrics.cheap_clone(),
1057+
from,
1058+
to,
1059+
filter,
1060+
)
10441061
.into_stream()
10451062
}))
10461063
.flatten()

graph/src/cheap_clone.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use slog::Logger;
2+
use std::rc::Rc;
3+
use std::sync::Arc;
4+
5+
/// Things that, in the context of an application such as Graph Node, are fast to clone.
6+
pub trait CheapClone: Clone {
7+
fn cheap_clone(&self) -> Self {
8+
self.clone()
9+
}
10+
}
11+
12+
impl<T: ?Sized> CheapClone for Rc<T> {}
13+
impl<T: ?Sized> CheapClone for Arc<T> {}
14+
impl CheapClone for Logger {}

graph/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub mod ext;
1313
/// Logging utilities
1414
pub mod log;
1515

16+
/// `CheapClone` trait.
17+
pub mod cheap_clone;
18+
1619
/// Module with mocks for different parts of the system.
1720
pub mod mock {
1821
pub use crate::components::ethereum::MockEthereumAdapter;
@@ -99,6 +102,7 @@ pub mod prelude {
99102
};
100103
pub use crate::components::{EventConsumer, EventProducer};
101104

105+
pub use crate::cheap_clone::CheapClone;
102106
pub use crate::data::graphql::{SerializableValue, TryFromValue, ValueMap};
103107
pub use crate::data::query::{
104108
Query, QueryError, QueryExecutionError, QueryResult, QueryVariables,

0 commit comments

Comments
 (0)