-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add new eth_filter_module rpc module * add rpc endpoints * update arguments * add FilterChanges type * update changelog * fix clppy * add eth_getLogs endpoint * update suggestion
- Loading branch information
Showing
7 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use super::Error; | ||
use crate::{ | ||
context::Context, | ||
jrpc::eth_types::{ | ||
filter::{Filter, FilterChanges}, | ||
log::Log, | ||
number::Number, | ||
}, | ||
}; | ||
|
||
pub fn new_filter(_filter: Filter, _context: &Context) -> Result<Number, Error> { | ||
// TODO implement | ||
Ok(0.into()) | ||
} | ||
|
||
pub fn new_block_filter(_context: &Context) -> Result<Number, Error> { | ||
// TODO implement | ||
Ok(0.into()) | ||
} | ||
|
||
pub fn new_pending_transaction_filter(_context: &Context) -> Result<Number, Error> { | ||
// TODO implement | ||
Ok(0.into()) | ||
} | ||
|
||
pub fn uninstall_filter(_filter_id: Number, _context: &Context) -> Result<bool, Error> { | ||
// TODO implement | ||
Ok(true) | ||
} | ||
|
||
pub fn get_filter_changes(_filter_id: Number, _context: &Context) -> Result<FilterChanges, Error> { | ||
// TODO implement | ||
Ok(FilterChanges::Empty) | ||
} | ||
|
||
pub fn get_filter_logs(_filter_id: Number, _context: &Context) -> Result<Vec<Log>, Error> { | ||
// TODO implement | ||
Ok(vec![Log::build()]) | ||
} | ||
|
||
pub fn get_logs(_filter: Filter, _context: &Context) -> Result<FilterChanges, Error> { | ||
// TODO implement | ||
Ok(FilterChanges::Empty) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::context::ContextLock; | ||
use jsonrpsee_http_server::RpcModule; | ||
|
||
mod logic; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error {} | ||
|
||
pub fn eth_filter_module(context: ContextLock) -> RpcModule<ContextLock> { | ||
let mut module = RpcModule::new(context); | ||
|
||
module | ||
.register_async_method("eth_newFilter", |params, context| async move { | ||
let context = context.read().await; | ||
let filter = params.parse()?; | ||
logic::new_filter(filter, &context) | ||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) | ||
}) | ||
.unwrap(); | ||
|
||
module | ||
.register_async_method("eth_newBlockFilter", |_, context| async move { | ||
let context = context.read().await; | ||
logic::new_block_filter(&context) | ||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) | ||
}) | ||
.unwrap(); | ||
|
||
module | ||
.register_async_method("eth_newPendingTransactionFilter", |_, context| async move { | ||
let context = context.read().await; | ||
logic::new_pending_transaction_filter(&context) | ||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) | ||
}) | ||
.unwrap(); | ||
|
||
module | ||
.register_async_method("eth_uninstallFilter", |params, context| async move { | ||
let context = context.read().await; | ||
let filter_id = params.parse()?; | ||
logic::uninstall_filter(filter_id, &context) | ||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) | ||
}) | ||
.unwrap(); | ||
|
||
module | ||
.register_async_method("eth_getFilterChanges", |params, context| async move { | ||
let context = context.read().await; | ||
let filter_id = params.parse()?; | ||
logic::get_filter_changes(filter_id, &context) | ||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) | ||
}) | ||
.unwrap(); | ||
|
||
module | ||
.register_async_method("eth_getFilterLogs", |params, context| async move { | ||
let context = context.read().await; | ||
let filter_id = params.parse()?; | ||
logic::get_filter_logs(filter_id, &context) | ||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) | ||
}) | ||
.unwrap(); | ||
|
||
module | ||
.register_async_method("eth_getLogs", |params, context| async move { | ||
let context = context.read().await; | ||
let filter = params.parse()?; | ||
logic::get_logs(filter, &context) | ||
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) | ||
}) | ||
.unwrap(); | ||
module | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use super::{block_number::BlockNumber, log::Log}; | ||
use chain_evm::ethereum_types::{H160, H256}; | ||
use serde::{Deserialize, Serialize, Serializer}; | ||
|
||
/// Variadic value | ||
#[derive(Debug, Deserialize, PartialEq, Eq)] | ||
#[serde(untagged)] | ||
pub enum VariadicValue<T> { | ||
/// Single | ||
Single(T), | ||
/// List | ||
Multiple(Vec<T>), | ||
/// None | ||
Null, | ||
} | ||
|
||
/// Filter Address | ||
pub type FilterAddress = VariadicValue<H160>; | ||
/// Topic, supports `A` | `null` | `[A,B,C]` | `[A,[B,C]]` | [null,[B,C]] | [null,[null,C]] | ||
pub type Topic = VariadicValue<VariadicValue<H256>>; | ||
|
||
/// Filter | ||
#[derive(Debug, Deserialize, PartialEq, Eq)] | ||
#[serde(deny_unknown_fields)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Filter { | ||
/// From Block | ||
from_block: Option<BlockNumber>, | ||
/// To Block | ||
to_block: Option<BlockNumber>, | ||
/// Address | ||
address: FilterAddress, | ||
/// Topics | ||
topics: Topic, | ||
/// Block hash | ||
block_hash: Option<H256>, | ||
} | ||
|
||
/// Results of the filter_changes RPC. | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum FilterChanges { | ||
#[allow(dead_code)] | ||
/// New logs. | ||
Logs(Vec<Log>), | ||
#[allow(dead_code)] | ||
/// New hashes (block or transactions) | ||
Hashes(Vec<H256>), | ||
/// Empty result, | ||
Empty, | ||
} | ||
|
||
impl Serialize for FilterChanges { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
match *self { | ||
FilterChanges::Logs(ref logs) => logs.serialize(s), | ||
FilterChanges::Hashes(ref hashes) => hashes.serialize(s), | ||
FilterChanges::Empty => (&[] as &[serde_json::Value]).serialize(s), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn filter_address_deserialize() { | ||
let fa_single: FilterAddress = | ||
serde_json::from_str(r#""0x0000000000000000000000000000000000000000""#).unwrap(); | ||
let fa_multiple: FilterAddress = | ||
serde_json::from_str(r#"["0x0000000000000000000000000000000000000000"]"#).unwrap(); | ||
let fa_null: FilterAddress = serde_json::from_str(r#"null"#).unwrap(); | ||
|
||
assert_eq!(fa_single, FilterAddress::Single(H160::zero())); | ||
assert_eq!(fa_multiple, FilterAddress::Multiple(vec![H160::zero()])); | ||
assert_eq!(fa_null, FilterAddress::Null); | ||
} | ||
|
||
#[test] | ||
fn topic_deserialize() { | ||
let t_single_single: Topic = serde_json::from_str( | ||
r#""0x0000000000000000000000000000000000000000000000000000000000000000""#, | ||
) | ||
.unwrap(); | ||
let t_single_multiple: Topic = serde_json::from_str( | ||
r#"["0x0000000000000000000000000000000000000000000000000000000000000000"]"#, | ||
) | ||
.unwrap(); | ||
let t_multiple_multiple_1: Topic = serde_json::from_str( | ||
r#"["0x0000000000000000000000000000000000000000000000000000000000000000",["0x0000000000000000000000000000000000000000000000000000000000000000"]]"#, | ||
) | ||
.unwrap(); | ||
let t_multiple_multiple_2: Topic = serde_json::from_str( | ||
r#"[null,["0x0000000000000000000000000000000000000000000000000000000000000000"]]"#, | ||
) | ||
.unwrap(); | ||
let t_null: Topic = serde_json::from_str(r#"null"#).unwrap(); | ||
|
||
assert_eq!( | ||
t_single_single, | ||
Topic::Single(<VariadicValue<H256>>::Single(H256::zero())) | ||
); | ||
assert_eq!( | ||
t_single_multiple, | ||
Topic::Single(<VariadicValue<H256>>::Multiple(vec![H256::zero()])) | ||
); | ||
assert_eq!( | ||
t_multiple_multiple_1, | ||
Topic::Multiple(vec![ | ||
<VariadicValue<H256>>::Single(H256::zero()), | ||
<VariadicValue<H256>>::Multiple(vec![H256::zero()]) | ||
]) | ||
); | ||
assert_eq!( | ||
t_multiple_multiple_2, | ||
Topic::Multiple(vec![ | ||
<VariadicValue<H256>>::Null, | ||
<VariadicValue<H256>>::Multiple(vec![H256::zero()]) | ||
]) | ||
); | ||
assert_eq!(t_null, Topic::Single(<VariadicValue<H256>>::Null)); | ||
} | ||
|
||
#[test] | ||
fn filter_changes_serialize() { | ||
let fc_log = FilterChanges::Logs(vec![Log::build()]); | ||
let fc_hashes = FilterChanges::Hashes(vec![H256::zero()]); | ||
let fc_empty = FilterChanges::Empty; | ||
|
||
assert_eq!( | ||
serde_json::to_string(&fc_log).unwrap(), | ||
r#"[{"removed":true,"logIndex":"0x1","transactionIndex":"0x1","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","blockNumber":"0x1","address":"0x0000000000000000000000000000000000000000","data":"0x","topics":[]}]"# | ||
); | ||
assert_eq!( | ||
serde_json::to_string(&fc_hashes).unwrap(), | ||
r#"["0x0000000000000000000000000000000000000000000000000000000000000000"]"# | ||
); | ||
assert_eq!(serde_json::to_string(&fc_empty).unwrap(), r#"[]"#); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters