Skip to content

Commit

Permalink
handle batch requests as array
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Sep 16, 2024
1 parent 99f5a8b commit 4b66d0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/electrum/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,17 @@ impl Connection {
match msg {
Message::Request(line) => {
let cmd: Value = from_str(&line).chain_err(|| "invalid JSON format")?;
let reply = self.handle_value(cmd, &empty_params, start_time)?;
self.send_values(&[reply])?
if let Value::Array(arr) = cmd {
let mut result = Vec::with_capacity(arr.len());
for el in arr {
let reply = self.handle_value(el, &empty_params, start_time)?;
result.push(reply)
}
self.send_values(&[Value::Array(result)])?
} else {
let reply = self.handle_value(cmd, &empty_params, start_time)?;
self.send_values(&[reply])?
}
}
Message::PeriodicUpdate => {
let values = self
Expand Down
6 changes: 6 additions & 0 deletions tests/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ fn test_electrum_raw() {
let s = write_and_read(&mut stream, write);
let expected = "{\"id\":0,\"jsonrpc\":\"2.0\",\"result\":[\"electrs-esplora 0.4.1\",\"1.4\"]}";
assert_eq!(s, expected);

let write = "[{\"jsonrpc\": \"2.0\", \"method\": \"server.version\", \"id\": 0}]";
let s = write_and_read(&mut stream, write);
let expected =
"[{\"id\":0,\"jsonrpc\":\"2.0\",\"result\":[\"electrs-esplora 0.4.1\",\"1.4\"]}]";
assert_eq!(s, expected);
}

fn write_and_read(stream: &mut TcpStream, write: &str) -> String {
Expand Down

0 comments on commit 4b66d0a

Please sign in to comment.