Skip to content

Implement blockchain.scripthash.get_mempool Electrum RPC method#1294

Open
PeterXMR wants to merge 1 commit into
romanz:masterfrom
PeterXMR:implement-scripthash-get-mempool-1241
Open

Implement blockchain.scripthash.get_mempool Electrum RPC method#1294
PeterXMR wants to merge 1 commit into
romanz:masterfrom
PeterXMR:implement-scripthash-get-mempool-1241

Conversation

@PeterXMR
Copy link
Copy Markdown

Refs #1241 — checks off the blockchain.scripthash.get_mempool item on the v1.6 protocol checklist.

Summary

blockchain.scripthash.get_mempool is required by Electrum protocol v1.6 and was not previously implemented in electrs.

Response shape matches blockchain.scripthash.get_history but filtered to mempool entries only — {tx_hash, height, fee} per entry, where height is 0 if all inputs are confirmed and -1 otherwise.

v1.6 ordering requirement: the spec mandates that get_mempool results "must be sorted ... same order as computing scripthash status". This implementation is a filter over the existing status history, so it shares the sort key with the status hash by construction — no separate ordering logic needed.

Test plan

  • 1 new unit test (test_is_unconfirmed) in status::tests plus dispatch reuses the existing HistoryEntry JSON shape covered by test_txinfo_json
  • cargo test --lib — 22/22 pass
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo fmt --check — clean
  • Live tested against Bitcoin Core v31.0.0 on regtest. Set up a wallet, mined 101 blocks for mature coinbase, then sendtoaddress to a new address to produce a mempool entry:
$ printf '{"id":0,"method":"server.version","params":["test","1.4"]}
{"id":1,"method":"blockchain.scripthash.subscribe","params":["3c1f5806df22f16793a08cfb0e41070fabcf3c178d385072c15faec46b71a07c"]}
{"id":2,"method":"blockchain.scripthash.get_mempool","params":["3c1f5806df22f16793a08cfb0e41070fabcf3c178d385072c15faec46b71a07c"]}
{"id":3,"method":"blockchain.scripthash.get_history","params":["3c1f5806df22f16793a08cfb0e41070fabcf3c178d385072c15faec46b71a07c"]}
' | nc -w 4 127.0.0.1 60401

{"id":0,"jsonrpc":"2.0","result":["electrs/0.11.1","1.4"]}
{"id":1,"jsonrpc":"2.0","result":"c2eee7ea99189036be6b18f0c0e5d2ff2440977ff36717fcecf1f9449a6ff25a"}
{"id":2,"jsonrpc":"2.0","result":[{"fee":2820,"height":0,"tx_hash":"c8010e16520b033300d78ac385cdc1f59bed8ed0783d8836ec2009d0aa9c5583"}]}
{"id":3,"jsonrpc":"2.0","result":[{"fee":2820,"height":0,"tx_hash":"c8010e16520b033300d78ac385cdc1f59bed8ed0783d8836ec2009d0aa9c5583"}]}

After mining one block to confirm the tx, get_mempool correctly filters it out while get_history now reports it as confirmed:

{"id":2,"jsonrpc":"2.0","result":[]}
{"id":3,"jsonrpc":"2.0","result":[{"height":102,"tx_hash":"c8010e16520b033300d78ac385cdc1f59bed8ed0783d8836ec2009d0aa9c5583"}]}

This method is required by Electrum protocol v1.6 and was not
previously implemented in electrs.

The response is the same shape as `blockchain.scripthash.get_history`
but filtered to mempool entries only — `{tx_hash, height, fee}` per
entry, where `height` is `0` if all inputs are confirmed and `-1`
otherwise. Mempool ordering matches the existing scripthash status
hash calculation, satisfying the v1.6 requirement of a canonical
order.

Implemented as a filter over the existing status history rather than
a recomputation, so it is O(history) and shares its sort key with the
status hash by construction.

Spec: https://electrum-protocol.readthedocs.io/en/latest/protocol-methods.html#blockchain-scripthash-get-mempool

Refs romanz#1241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant