From 4b66d0a3e9527e786e75a0d58450ece8f81f7268 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Mon, 16 Sep 2024 15:26:10 +0200 Subject: [PATCH] handle batch requests as array --- src/electrum/server.rs | 13 +++++++++++-- tests/electrum.rs | 6 ++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/electrum/server.rs b/src/electrum/server.rs index cc8e2136..8b8b2b93 100644 --- a/src/electrum/server.rs +++ b/src/electrum/server.rs @@ -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 diff --git a/tests/electrum.rs b/tests/electrum.rs index 9c6ce5b7..3dacf74b 100644 --- a/tests/electrum.rs +++ b/tests/electrum.rs @@ -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 {