Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/electrum/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use electrs_macros::trace;
use bitcoin::consensus::encode::serialize_hex;
#[cfg(feature = "liquid")]
use elements::encode::serialize_hex;
use crate::chain::Txid;
use crate::chain::{BlockHash, Txid};
use crate::config::{Config, RpcLogging};
use crate::electrum::{get_electrum_height, ProtocolVersion};
use crate::errors::*;
Expand Down Expand Up @@ -161,6 +161,9 @@ struct Connection {
// Mempool touched-epoch snapshot from this connection's last periodic tick;
// None forces a full subscription scan (first tick / after pruning).
mempool_touched_epoch: Option<u64>,
// Chain tip at this connection's last periodic tick, tracked whether or not the
// client subscribed to headers: a new block forces the full subscription scan.
last_tick_tip: Option<BlockHash>,
stream: TcpStream,
addr: SocketAddr,
sender: SyncSender<Message>,
Expand Down Expand Up @@ -196,6 +199,7 @@ impl Connection {
last_header_entry: None, // disable header subscription for now
status_hashes: HashMap::new(),
mempool_touched_epoch: None,
last_tick_tip: None,
stream,
addr,
sender,
Expand Down Expand Up @@ -635,6 +639,11 @@ impl Connection {
"params": [header]}));
}
}
let tip = self.query.chain().best_hash();
if self.last_tick_tip != Some(tip) {
tip_changed = true;
self.last_tick_tip = Some(tip);
}
// Delta-driven fast path: on a mempool-only tick (no new block), only
// re-check subscriptions whose scripthash the mempool actually touched
// since our last tick. A new block can affect any subscription (and the
Expand Down
152 changes: 152 additions & 0 deletions tests/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,155 @@ fn write_and_read(stream: &mut TcpStream, write: &str) -> String {
}
std::str::from_utf8(&result).unwrap().to_string()
}

/// Electrum protocol script hash of an address: SHA256 of the scriptPubKey, reversed, as hex
#[cfg(not(feature = "liquid"))]
fn electrum_scripthash(addr: &bitcoin::Address) -> String {
use bitcoin::hashes::{sha256, Hash};
use bitcoin::hex::DisplayHex;
let mut hash = sha256::Hash::hash(addr.script_pubkey().as_bytes()).to_byte_array();
hash.reverse();
hash.to_lower_hex_string()
}

/// Read one line, or None if nothing arrives within `timeout`
#[cfg(not(feature = "liquid"))]
fn read_line_within(stream: &mut TcpStream, timeout: std::time::Duration) -> Option<String> {
stream.set_read_timeout(Some(timeout)).unwrap();
let mut result = vec![];
loop {
let mut buf = [0u8];
match stream.read_exact(&mut buf) {
Ok(()) if buf[0] == b'\n' => break,
Ok(()) => result.push(buf[0]),
Err(e)
if result.is_empty()
&& matches!(
e.kind(),
std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
) =>
{
return None
}
Err(e) => panic!("read failed: {}", e),
}
}
stream.set_read_timeout(None).unwrap();
Some(String::from_utf8(result).unwrap())
}

/// Subscribe to `scripthash` on a fresh connection and run one periodic tick, so the
/// connection has a baseline for the next tick.
#[cfg(not(feature = "liquid"))]
fn subscribe_and_tick(
electrum_server: &ElectrumRPC,
electrum_addr: std::net::SocketAddr,
scripthash: &str,
headers: bool,
) -> TcpStream {
let mut stream = TcpStream::connect(electrum_addr).unwrap();
if headers {
write_and_read(
&mut stream,
"{\"jsonrpc\": \"2.0\", \"method\": \"blockchain.headers.subscribe\", \"id\": 1}",
);
}
let s = write_and_read(
&mut stream,
&format!(
"{{\"jsonrpc\": \"2.0\", \"method\": \"blockchain.scripthash.subscribe\", \"params\": [\"{}\"], \"id\": 2}}",
scripthash
),
);
assert_eq!(s, "{\"id\":2,\"jsonrpc\":\"2.0\",\"result\":null}");
electrum_server.notify();
// nothing changed, so the first tick sends nothing
assert_eq!(
read_line_within(&mut stream, std::time::Duration::from_secs(2)),
None
);
stream
}

#[cfg(not(feature = "liquid"))]
fn expect_scripthash_notification(stream: &mut TcpStream, scripthash: &str) {
// A headers subscriber may get a header notification first; skip it.
loop {
let line = read_line_within(stream, std::time::Duration::from_secs(10))
.expect("no blockchain.scripthash.subscribe notification after the periodic tick");
let v: electrumd::jsonrpc::serde_json::Value =
electrumd::jsonrpc::serde_json::from_str(&line).unwrap();
if v["method"] == "blockchain.scripthash.subscribe" {
assert_eq!(v["params"][0].as_str(), Some(scripthash));
assert!(v["params"][1].is_string(), "status should be set: {}", line);
return;
}
assert_eq!(v["method"], "blockchain.headers.subscribe", "{}", line);
}
}

/// A subscriber is notified when a mempool tx touches its scripthash.
#[cfg_attr(not(feature = "liquid"), test)]
#[cfg_attr(feature = "liquid", allow(dead_code))]
fn test_electrum_subscription_notified_on_mempool_tx() -> Result<()> {
let (electrum_server, electrum_addr, mut tester) = common::init_electrum_tester()?;
let addr = tester.newaddress()?;
let scripthash = electrum_scripthash(&addr);
let mut stream = subscribe_and_tick(&electrum_server, electrum_addr, &scripthash, false);

tester.send(&addr, "0.1 BTC".parse().unwrap())?;
electrum_server.notify();
expect_scripthash_notification(&mut stream, &scripthash);
Ok(())
}

/// A subscriber is notified when a block confirms a tx for its scripthash that electrs
/// never saw in its mempool (e.g. mined between two mempool syncs, or never relayed).
/// The connection did not subscribe to headers.
#[cfg_attr(not(feature = "liquid"), test)]
#[cfg_attr(feature = "liquid", allow(dead_code))]
fn test_electrum_subscription_notified_on_block_without_mempool_tx() -> Result<()> {
let (electrum_server, electrum_addr, mut tester) = common::init_electrum_tester()?;
let addr = tester.newaddress()?;
let scripthash = electrum_scripthash(&addr);
let mut stream = subscribe_and_tick(&electrum_server, electrum_addr, &scripthash, false);

// Send and mine before electrs syncs, so the tx goes straight into the chain index.
let client = tester.node_client();
let _: bitcoin::Txid = client.call("sendtoaddress", &[addr.to_string().into(), 0.1.into()])?;
let miner: bitcoin::Address<bitcoin::address::NetworkUnchecked> =
client.call("getnewaddress", &[])?;
let _: Vec<bitcoin::BlockHash> = client.call(
"generatetoaddress",
&[1.into(), miner.assume_checked().to_string().into()],
)?;
tester.sync()?;

electrum_server.notify();
expect_scripthash_notification(&mut stream, &scripthash);
Ok(())
}

/// Same as above for a connection that also subscribed to headers.
#[cfg_attr(not(feature = "liquid"), test)]
#[cfg_attr(feature = "liquid", allow(dead_code))]
fn test_electrum_subscription_notified_on_block_with_headers_subscription() -> Result<()> {
let (electrum_server, electrum_addr, mut tester) = common::init_electrum_tester()?;
let addr = tester.newaddress()?;
let scripthash = electrum_scripthash(&addr);
let mut stream = subscribe_and_tick(&electrum_server, electrum_addr, &scripthash, true);

let client = tester.node_client();
let _: bitcoin::Txid = client.call("sendtoaddress", &[addr.to_string().into(), 0.1.into()])?;
let miner: bitcoin::Address<bitcoin::address::NetworkUnchecked> =
client.call("getnewaddress", &[])?;
let _: Vec<bitcoin::BlockHash> = client.call(
"generatetoaddress",
&[1.into(), miner.assume_checked().to_string().into()],
)?;
tester.sync()?;

electrum_server.notify();
expect_scripthash_notification(&mut stream, &scripthash);
Ok(())
}