Skip to content

Commit

Permalink
feat: impl blacklist/whitelist middleware (#5)
Browse files Browse the repository at this point in the history
* done

* clean up code

* improve tests

* add more tests, polish code and fix `enabled`

* update demo config

* clean up tests
  • Loading branch information
yjhmelody authored Jun 21, 2024
1 parent ef99aed commit 13b854e
Show file tree
Hide file tree
Showing 12 changed files with 800 additions and 405 deletions.
13 changes: 11 additions & 2 deletions configs/demo_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extensions:
max_batch_size: 10
cors: all
whitelist:
eth_call_whitelist:
eth_call:
# allow 0x01 to call 0x02
- from: 0000000000000000000000000000000000000001
to: 0000000000000000000000000000000000000002
Expand All @@ -26,12 +26,21 @@ extensions:
# allow 0x02 to create contract
- from: 0000000000000000000000000000000000000002
to: create
tx_whitelist:
tx:
# allow 0x01 to call or create in tx.
- from: 0000000000000000000000000000000000000001
blacklist:
eth_call:
# not allow 0x02 to call 0x01
- from: 0000000000000000000000000000000000000002
to: 0000000000000000000000000000000000000001
tx:
# not allow 0x03 to call or create in tx.
- from: 0000000000000000000000000000000000000003
middlewares:
methods:
- whitelist
- blacklist
- response
- block_tag
- cache
Expand Down
2 changes: 1 addition & 1 deletion src/config/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct MethodParam {
pub inject: bool,
}

#[derive(Deserialize, Validate, Debug)]
#[derive(Deserialize, Validate, Debug, Clone)]
#[garde(allow_unvalidated)]
pub struct RpcMethod {
pub method: String,
Expand Down
49 changes: 49 additions & 0 deletions src/extensions/list/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::utils::AddressRule;
use async_trait::async_trait;
use serde::Deserialize;

use super::{Extension, ExtensionRegistry};

/// The address whitelist for `eth_call/eth_sendRawTransaction` rpc.
#[derive(Deserialize, Debug, Clone)]
pub struct WhitelistConfig {
#[serde(default)]
pub eth_call: Vec<AddressRule>,
#[serde(default)]
pub tx: Vec<AddressRule>,
}

/// The address blacklist for `eth_call/eth_sendRawTransaction` rpc.
#[derive(Deserialize, Debug, Clone)]
pub struct BlacklistConfig {
#[serde(default)]
pub eth_call: Vec<AddressRule>,
#[serde(default)]
pub tx: Vec<AddressRule>,
}

pub struct Whitelist {
pub config: WhitelistConfig,
}

pub struct BlackList {
pub config: BlacklistConfig,
}

#[async_trait]
impl Extension for Whitelist {
type Config = WhitelistConfig;

async fn from_config(config: &Self::Config, _registry: &ExtensionRegistry) -> Result<Self, anyhow::Error> {
Ok(Self { config: config.clone() })
}
}

#[async_trait]
impl Extension for BlackList {
type Config = BlacklistConfig;

async fn from_config(config: &Self::Config, _registry: &ExtensionRegistry) -> Result<Self, anyhow::Error> {
Ok(Self { config: config.clone() })
}
}
5 changes: 3 additions & 2 deletions src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ pub mod api;
pub mod cache;
pub mod client;
pub mod event_bus;
pub mod list;
pub mod merge_subscription;
pub mod prometheus;
pub mod rate_limit;
pub mod server;
pub mod telemetry;
pub mod validator;
pub mod whitelist;

#[async_trait]
pub trait Extension: Sized {
Expand Down Expand Up @@ -146,5 +146,6 @@ define_all_extensions! {
rate_limit: rate_limit::RateLimitBuilder,
prometheus: prometheus::Prometheus,
validator: validator::Validator,
whitelist: whitelist::Whitelist,
whitelist: list::Whitelist,
blacklist: list::BlackList,
}
50 changes: 0 additions & 50 deletions src/extensions/whitelist/mod.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/middlewares/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub async fn create_method_middleware(
use super::methods::*;

match name {
"whitelist" => whitelist::WhitelistMiddleware::build(method, extensions).await,
"whitelist" => list::WhitelistMiddleware::build(method, extensions).await,
"blacklist" => list::BlacklistMiddleware::build(method, extensions).await,

"response" => response::ResponseMiddleware::build(method, extensions).await,
"upstream" => upstream::UpstreamMiddleware::build(method, extensions).await,
Expand Down
Loading

0 comments on commit 13b854e

Please sign in to comment.