Skip to content

Commit

Permalink
Allocating RPC logs entries only if --electrum-rpc-logging is set
Browse files Browse the repository at this point in the history
  • Loading branch information
rem1-dev committed Feb 16, 2024
1 parent 4f8e915 commit bfa5b9f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Config {
pub utxos_limit: usize,
pub electrum_txs_limit: usize,
pub electrum_banner: String,
pub rpc_logging: Option<RpcLogging>,
pub electrum_rpc_logging: Option<RpcLogging>,

#[cfg(feature = "liquid")]
pub parent_network: BNetwork,
Expand Down Expand Up @@ -187,8 +187,8 @@ impl Config {
.help("Welcome banner for the Electrum server, shown in the console to clients.")
.takes_value(true)
).arg(
Arg::with_name("rpc_logging")
.long("rpc-logging")
Arg::with_name("electrum_rpc_logging")
.long("electrum-rpc-logging")
.help(&rpc_logging_help)
.takes_value(true),
);
Expand Down Expand Up @@ -391,8 +391,8 @@ impl Config {
electrum_rpc_addr,
electrum_txs_limit: value_t_or_exit!(m, "electrum_txs_limit", usize),
electrum_banner,
rpc_logging: m
.value_of("rpc_logging")
electrum_rpc_logging: m
.value_of("electrum_rpc_logging")
.map(|option| RpcLogging::from(option)),
http_addr,
http_socket_file,
Expand Down
49 changes: 31 additions & 18 deletions src/electrum/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ fn get_status_hash(txs: Vec<(Txid, Option<BlockId>)>, query: &Query) -> Option<F
}
}

macro_rules! conditionally_log_rpc_event {
($self:ident, $event:expr) => {
if $self.rpc_logging.is_some() {
$self.log_rpc_event(&$event);
}
};
}

struct Connection {
query: Arc<Query>,
last_header_entry: Option<HeaderEntry>,
Expand Down Expand Up @@ -543,14 +551,18 @@ impl Connection {
&Value::Array(ref params),
Some(ref id),
) => {
let mut log_entries =
vec![("event", json!("rpc request")), ("method", json!(method))];

if let Some(RpcLogging::Full) = self.rpc_logging {
log_entries.push(("params", json!(params)));
}

self.log_rpc_event(&log_entries);
conditionally_log_rpc_event!(
self,
if let Some(RpcLogging::Full) = self.rpc_logging {
vec![
("event", json!("rpc request")),
("method", json!(method)),
("params", json!(params)),
]
} else {
vec![("event", json!("rpc request")), ("method", json!(method))]
}
);
method_info = method.clone();

self.handle_command(method, params, id)?
Expand All @@ -560,11 +572,14 @@ impl Connection {

let line = reply.to_string() + "\n";

self.log_rpc_event(&vec![
("event", json!("rpc response")),
("payload_size", json!(line.as_bytes().len())),
("method", json!(method_info)),
]);
conditionally_log_rpc_event!(
self,
vec![
("event", json!("rpc response")),
("payload_size", json!(line.as_bytes().len())),
("method", json!(method_info)),
]
);

self.send_values(&[reply])?
}
Expand Down Expand Up @@ -609,8 +624,7 @@ impl Connection {

pub fn run(mut self) {
self.stats.clients.inc();

self.log_rpc_event(&vec![("event", json!("connection established"))]);
conditionally_log_rpc_event!(self, vec![("event", json!("connection established"))]);

let reader = BufReader::new(self.stream.try_clone().expect("failed to clone TcpStream"));
let tx = self.chan.sender();
Expand All @@ -628,8 +642,7 @@ impl Connection {
.sub(self.status_hashes.len() as i64);

debug!("[{}] shutting down connection", self.addr);

self.log_rpc_event(&vec![("event", json!("connection closed"))]);
conditionally_log_rpc_event!(self, vec![("event", json!("connection closed"))]);

let _ = self.stream.shutdown(Shutdown::Both);
if let Err(err) = child.join().expect("receiver panicked") {
Expand Down Expand Up @@ -793,7 +806,7 @@ impl RPC {
let garbage_sender = garbage_sender.clone();
#[cfg(feature = "electrum-discovery")]
let discovery = discovery.clone();
let rpc_logging = config.rpc_logging.clone();
let rpc_logging = config.electrum_rpc_logging.clone();

let spawned = spawn_thread("peer", move || {
info!("[{}] connected peer", addr);
Expand Down

0 comments on commit bfa5b9f

Please sign in to comment.