Skip to content

Commit

Permalink
Implemented global switch default_fetch_blocks
Browse files Browse the repository at this point in the history
This change allows the users to globally enable `fetch_blocks` unless
specifi user config says otherwise. This effectively turns it from
whitelist to blacklist. While it's a good practice to stay away from
blacklists, I believe it can be justified in this case as the call is
mostly harmless.
  • Loading branch information
Kixunil committed Dec 4, 2020
1 parent ff86bd3 commit ccb7f23
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
6 changes: 5 additions & 1 deletion config_spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ doc = "The port of the real bitcoind."

[[param]]
name = "user"
type = "std::collections::HashMap<String, btc_rpc_proxy::User>"
type = "std::collections::HashMap<String, btc_rpc_proxy::users::input::User>"
merge_fn = "std::iter::Extend::extend"
default = "Default::default()"
argument = false
Expand Down Expand Up @@ -88,3 +88,7 @@ name = "tor_only"
type = "bool"
default = "false"
doc = "Use tor for non-.onion peer connections"

[[switch]]
name = "default_fetch_blocks"
doc = "Fetch blocks from peers for all users that do NOT specify fetch_blocks = false"
4 changes: 2 additions & 2 deletions src/create_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use std::time::Duration;

use anyhow::Error;
use btc_rpc_proxy::{AuthSource, Peers, RpcClient, State, TorState, Users};
use btc_rpc_proxy::{AuthSource, Peers, RpcClient, State, TorState};
use slog::Drain;
use tokio::sync::RwLock;

Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn create_state() -> Result<State, Error> {
bind: (config.bind_address, config.bind_port).into(),
rpc_client,
tor,
users: Users(config.user),
users: btc_rpc_proxy::users::input::map_default(config.user, config.default_fetch_blocks),
logger,
peer_timeout: Duration::from_secs(config.peer_timeout),
peers: RwLock::new(Arc::new(Peers::new())),
Expand Down
73 changes: 73 additions & 0 deletions src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ use crate::state::State;
#[cfg(feature = "old_rust")]
use crate::util::old_rust::StrCompat;

pub mod input {
use std::collections::{HashMap, HashSet};

#[derive(Debug, serde::Deserialize)]
pub struct User {
pub password: String,
pub allowed_calls: HashSet<String>,
#[serde(default)]
pub fetch_blocks: Option<bool>,
}

impl User {
fn map_default(self, default_fetch_blocks: bool) -> super::User {
super::User {
password: self.password,
allowed_calls: self.allowed_calls,
fetch_blocks: self.fetch_blocks.unwrap_or(default_fetch_blocks),
}
}
}

pub fn map_default(users: HashMap<String, User>, default_fetch_blocks: bool) -> super::Users {
super::Users(users.into_iter().map(|(name, user)| (name, user.map_default(default_fetch_blocks))).collect())
}
}

#[derive(Debug, serde::Deserialize)]
pub struct Users(pub HashMap<String, User>);
impl Users {
Expand Down Expand Up @@ -171,3 +197,50 @@ impl User {
}
}
}

#[cfg(test)]
mod tests {
use std::collections::{HashMap, HashSet};

fn check(input: Option<bool>, default: bool, expected: bool) {
let mut users = HashMap::new();
users.insert("satoshi".to_owned(), super::input::User {
password: "secret".to_owned(),
allowed_calls: HashSet::new(),
fetch_blocks: input,
});

let result = super::input::map_default(users, default);
assert_eq!(result.0["satoshi"].fetch_blocks, expected);
}

#[test]
fn default_fetch_blocks_none_false() {
check(None, false, false);
}

#[test]
fn default_fetch_blocks_none_true() {
check(None, true, true);
}

#[test]
fn default_fetch_blocks_some_false_false() {
check(Some(false), false, false);
}

#[test]
fn default_fetch_blocks_some_false_true() {
check(Some(false), true, false);
}

#[test]
fn default_fetch_blocks_some_true_false() {
check(Some(true), false, true);
}

#[test]
fn default_fetch_blocks_some_true_true() {
check(Some(true), true, true);
}
}

0 comments on commit ccb7f23

Please sign in to comment.