Page MenuHomePhabricator

[chronik] Electrum: add the blockchain.headers.subscribe endpoint
ClosedPublic

Authored by Fabien on May 8 2025, 21:05.

Details

Summary

Subscribe to get the new header when a block is connected.

Test Plan
./test/functional/test_runner.py chronik_electrum_*

In terminal 1:

./src/bitcoind -regtest -debug=chronik -chronik -chronikscripthashindex -chronikelectrumbind="127.0.0.1:50001"

In terminal 2 (can be done in several terminals):

echo '{"jsonrpc": "2.0", "method": "blockchain.headers.subscribe", "params": [], "id": "test"}' | nc 127.0.0.1 50001 | jq

in terminal 3:

./src/bitcoin-cli -regtest createwallet regtest
./src/bitcoin-cli -regtest -generate 1

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

tobias_ruck added inline comments.
chronik/chronik-indexer/src/subs.rs
65 ↗(On Diff #53909)

is reusing subs_block not an option?

chronik/chronik-indexer/src/subs.rs
65 ↗(On Diff #53909)

Could be, but I'm only interested in the connection event so having a different broadcast channel makes the code a bit simpler

chronik/chronik-indexer/src/subs.rs
65 ↗(On Diff #53909)

Well you add a lot of complexity in subs.rs (which is kind of a common shared module) in order to remove a if msg.msg_type != BlockMsgType::Connected { continue; } in your code, which seem suboptimal

Reuse block subscription. This requires to add a filter on the event but overall simplifies the code.

Fabien published this revision for review.May 9 2025, 10:00
Fabien planned changes to this revision.

still missing the tests

In the test plan it should probably be createwallet instead of create (that worked for me)

I tested it on mainnet as well, it seems to work

$ echo '{"jsonrpc": "2.0", "method": "blockchain.headers.subscribe", "params": [], "id": "test"}' | nc 127.0.0.1 50001 | jq
{
  "id": "test",
  "jsonrpc": "2.0",
  "result": {
    "height": 896521,
    "hex": "00801c2ca319ebf9bb2e220b07be5363cc97b05f33199a85d3c50c1c000000000000000075fc75fb01d6ae00db6c952475279e5d6857c66bc07baf3c9d9fd295035f705eb2e12168ecc4491816e2f1a3"
  }
}

$  echo '{"jsonrpc": "2.0", "method": "blockchain.headers.subscribe", "params": [], "id": "test"}' | nc electrum.bitcoinabc.org 50001 | jq
{
  "id": "test",
  "jsonrpc": "2.0",
  "result": {
    "height": 896521,
    "hex": "00801c2ca319ebf9bb2e220b07be5363cc97b05f33199a85d3c50c1c000000000000000075fc75fb01d6ae00db6c952475279e5d6857c66bc07baf3c9d9fd295035f705eb2e12168ecc4491816e2f1a3"
  }
}
This revision is now accepted and ready to land.May 12 2025, 12:30