From 5e684060d2ed8a00d0b256ae41d2e5887ef5fed9 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 2 Oct 2017 19:21:45 +0800 Subject: [PATCH] Move filters to a separate RPC trait --- src/rpc/filter.rs | 6 ++++++ src/rpc/mod.rs | 11 +++++++++-- src/rpc/serves.rs | 47 ++++++++++++++++++++++++++++++----------------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/rpc/filter.rs b/src/rpc/filter.rs index 277549f..5a64836 100644 --- a/src/rpc/filter.rs +++ b/src/rpc/filter.rs @@ -6,6 +6,7 @@ use std::str::FromStr; use std::collections::HashMap; use std::sync::{Arc, Mutex}; use blockchain::chain::HeaderHash; +use rpc::RPCLogFilter; use super::{RPCLog, Either}; use super::util::*; @@ -104,6 +105,11 @@ impl FilterManager { } } + pub fn from_log_filter(&self, log: RPCLogFilter) -> Result { + let state = self.state.lock().unwrap(); + from_log_filter(&state, log) + } + pub fn install_log_filter(&mut self, filter: LogFilter) -> usize { let id = self.filters.len(); self.filters.insert(id, Filter::Log(filter.clone())); diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index 7343ea2..ce77cb2 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -245,6 +245,13 @@ build_rpc_trait! { #[rpc(name = "eth_getCompilers")] fn compilers(&self) -> Result, Error>; + #[rpc(name = "eth_getLogs")] + fn logs(&self, RPCLogFilter) -> Result, Error>; + } +} + +build_rpc_trait! { + pub trait FilterRPC { #[rpc(name = "eth_newFilter")] fn new_filter(&self, RPCLogFilter) -> Result; #[rpc(name = "eth_newBlockFilter")] @@ -258,8 +265,6 @@ build_rpc_trait! { fn filter_changes(&self, String) -> Result, Vec>, Error>; #[rpc(name = "eth_getFilterLogs")] fn filter_logs(&self, String) -> Result, Error>; - #[rpc(name = "eth_getLogs")] - fn logs(&self, RPCLogFilter) -> Result, Error>; } } @@ -291,11 +296,13 @@ pub fn rpc_loop( state: Arc>, addr: &SocketAddr, channel: Sender ) { let rpc = serves::MinerEthereumRPC::

::new(state.clone(), channel); + let filter = serves::MinerFilterRPC::

::new(state.clone()); let debug = serves::MinerDebugRPC::

::new(state); let mut io = IoHandler::default(); io.extend_with(rpc.to_delegate()); + io.extend_with(filter.to_delegate()); io.extend_with(debug.to_delegate()); let server = ServerBuilder::new(io) diff --git a/src/rpc/serves.rs b/src/rpc/serves.rs index 7711126..e063202 100644 --- a/src/rpc/serves.rs +++ b/src/rpc/serves.rs @@ -1,4 +1,4 @@ -use super::{EthereumRPC, DebugRPC, Either, RPCTransaction, RPCTrace, RPCStep, RPCBlock, RPCLog, RPCReceipt, RPCLogFilter, RPCBlockTrace, RPCDump, RPCDumpAccount, RPCTraceConfig}; +use super::{EthereumRPC, FilterRPC, DebugRPC, Either, RPCTransaction, RPCTrace, RPCStep, RPCBlock, RPCLog, RPCReceipt, RPCLogFilter, RPCBlockTrace, RPCDump, RPCDumpAccount, RPCTraceConfig}; use super::util::*; use super::filter::*; use super::serialize::*; @@ -23,24 +23,28 @@ use std::marker::PhantomData; use jsonrpc_macros::Trailing; pub struct MinerEthereumRPC { - filter: Mutex, state: Arc>, channel: Sender, _patch: PhantomData

, } +pub struct MinerFilterRPC { + filter: Mutex, + _patch: PhantomData

, +} + pub struct MinerDebugRPC { state: Arc>, _patch: PhantomData

, } unsafe impl Sync for MinerEthereumRPC

{ } +unsafe impl Sync for MinerFilterRPC

{ } unsafe impl Sync for MinerDebugRPC

{ } impl MinerEthereumRPC

{ pub fn new(state: Arc>, channel: Sender) -> Self { MinerEthereumRPC { - filter: Mutex::new(FilterManager::new(state.clone())), channel, state, _patch: PhantomData, @@ -48,6 +52,15 @@ impl MinerEthereumRPC

{ } } +impl MinerFilterRPC

{ + pub fn new(state: Arc>) -> Self { + MinerFilterRPC { + filter: Mutex::new(FilterManager::new(state)), + _patch: PhantomData, + } + } +} + impl MinerDebugRPC

{ pub fn new(state: Arc>) -> Self { MinerDebugRPC { @@ -517,12 +530,21 @@ impl EthereumRPC for MinerEthereumRPC

{ Ok(Vec::new()) } + fn logs(&self, log: RPCLogFilter) -> Result, Error> { + let state = self.state.lock().unwrap(); + + match from_log_filter(&state, log) { + Ok(filter) => Ok(get_logs(&state, filter)?), + Err(_) => Ok(Vec::new()), + } + } +} + +impl FilterRPC for MinerFilterRPC

{ fn new_filter(&self, log: RPCLogFilter) -> Result { - let filter = { - let state = self.state.lock().unwrap(); - from_log_filter(&state, log)? - }; - let id = self.filter.lock().unwrap().install_log_filter(filter); + let mut filter = self.filter.lock().unwrap(); + let log_filter = filter.from_log_filter(log)?; + let id = filter.install_log_filter(log_filter); Ok(format!("0x{:x}", id)) } @@ -551,15 +573,6 @@ impl EthereumRPC for MinerEthereumRPC

{ let id = U256::from_str(&id)?.as_usize(); Ok(self.filter.lock().unwrap().get_logs(id)?) } - - fn logs(&self, log: RPCLogFilter) -> Result, Error> { - let state = self.state.lock().unwrap(); - - match from_log_filter(&state, log) { - Ok(filter) => Ok(get_logs(&state, filter)?), - Err(_) => Ok(Vec::new()), - } - } } impl DebugRPC for MinerDebugRPC

{