Skip to content

Add getchaintxstats RPC to rpc-extractor, expose metrics - #312

Merged
0xB10C merged 5 commits into
peer-observer:masterfrom
deadmanoz:feat/getchaintxstats
Jan 6, 2026
Merged

0xB10C merged 5 commits into
peer-observer:masterfrom
deadmanoz:feat/getchaintxstats

Conversation

@deadmanoz

Copy link
Copy Markdown
Collaborator

DRAFT REASON: This PR requires rust-bitcoin/corepc#430 which fixes the tx_rate field type from i64 to f64 to match Bitcoin Core's actual return type. So this is currently draft - a small change to rpc_extractor.rs is required once upstream changes are merged (to remove tx_rate values being truncated to integers).

Add support for the getchaintxstats RPC to the rpc-extractor and expose the data as Prometheus metrics. This RPC allows for the below new metrics.

The getchaintxstats RPC computes statistics about the total number and rate of transactions in the chain. We use the Bitcoin Core defaults:

  • Window size: ~1 month of blocks
  • End block: Current chain tip

See: Bitcoin Core RPC documentation

Metric Description
rpc_chaintxstats_tx_count Total transactions in chain up to window end
rpc_chaintxstats_window_final_block_height Height of final block in window
rpc_chaintxstats_window_block_count Window size in blocks
rpc_chaintxstats_window_tx_count Transactions in the window
rpc_chaintxstats_window_interval Window duration in seconds
rpc_chaintxstats_tx_rate Average tx/second in window (float)

Note: window_final_block_hash is included in the protobuf event but not exposed as a Prometheus metric (block hashes are strings, not numeric values). I don't think a hash/string is a meaningful metric.

Changes include:

  • Add getchaintxstats RPC polling to rpc-extractor with --disable-getchaintxstats flag
  • Add ChainTxStats protobuf message definition
  • Add Prometheus metrics for chain transaction statistics
  • Add integration test for the new RPC
  • Fix typo: 'quering' → 'querying' in help text

@0xB10C

0xB10C commented Dec 17, 2025

Copy link
Copy Markdown
Collaborator

Looks good to me! Could add integration tests for:

  • the new RPC being supported in extractors/rpc/tests/integration.rs
  • the new metrics added in tools/metrics/tests/integration.rs

feel free to add them in the same commit as the new feature

I think rust-bitcoin/corepc#430 isn't far from being merged. Going to wait until that's merged until I set this up for testing

@0xB10C

0xB10C commented Dec 17, 2025

Copy link
Copy Markdown
Collaborator

I think this RPC is fetched every 10s, right? Not sure if it's needed that frequently. It will return the same result at the same height anyway? Could add a factor of 60 or so, so that it's only fetched every 10 minutes..

@GuiSchet

Copy link
Copy Markdown
Contributor

yes, RPC fetchs every 10s, maybe other solution could be check last height

@deadmanoz
deadmanoz force-pushed the feat/getchaintxstats branch from e58629b to f991fd6 Compare December 18, 2025 02:45
@deadmanoz

deadmanoz commented Dec 18, 2025 •

Copy link
Copy Markdown
Collaborator Author

Looks good to me! Could add integration tests for:

  • the new RPC being supported in extractors/rpc/tests/integration.rs
  • the new metrics added in tools/metrics/tests/integration.rs

feel free to add them in the same commit as the new feature

I think rust-bitcoin/corepc#430 isn't far from being merged. Going to wait until that's merged until I set this up for testing

Added a metrics integration test, there was already an RPC integration test. Both in the same commit.

Here's the output from running them with --nocapture

RPC integration test:

2025-12-18T02:50:39.030Z INFO  [rpc_extractor] Querying getchaintxstats enabled: true
2025-12-18T02:50:39.031Z DEBUG [corepc] request: getchaintxstats []
2025-12-18T02:50:39.033Z TRACE [corepc] response for getchaintxstats: {"time":1296688602,"txcount":1,"window_final_block_hash":"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206","window_final_block_height":0,"window_block_count":0}
2025-12-18T02:50:39.033Z TRACE [shared::protobuf::event] creating new Event: RpcExtractor(Rpc { rpc_event: Some(ChainTxStats(ChainTxStats { time: 1296688602, tx_count: 1, window_final_block_hash: "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", window_final_block_height: 0, window_block_count: 0, window_tx_count: None, window_interval: None, tx_rate: None })) })
2025-12-18T02:50:39.034Z INFO  [rpc_extractor] rpc_extractor received shutdown signal.

Metrics integration test:

test that the chaintxstats metrics work
2025-12-18T02:55:10.679Z TRACE [shared::protobuf::event] creating new Event: RpcExtractor(Rpc { rpc_event: Some(ChainTxStats(ChainTxStats { time: 1766024797, tx_count: 1285474630, window_final_block_hash: "00000000000000000001df7f8bf0eb2f537e6f1b4ddd54afa52279dc894fb451", window_final_block_height: 928348, window_block_count: 4320, window_tx_count: Some(2635071), window_interval: Some(13305840), tx_rate: Some(5.049518589821679) })) })
...
2025-12-18T02:55:11.693Z DEBUG [integration] publishing: Event { timestamp: 1766026510679, peer_observer_event: Some(RpcExtractor(Rpc { rpc_event: Some(ChainTxStats(ChainTxStats { time: 1766024797, tx_count: 1285474630, window_final_block_hash: "00000000000000000001df7f8bf0eb2f537e6f1b4ddd54afa52279dc894fb451", window_final_block_height: 928348, window_block_count: 4320, window_tx_count: Some(2635071), window_interval: Some(13305840), tx_rate: Some(5.049518589821679) })) })) }

@deadmanoz

Copy link
Copy Markdown
Collaborator Author

I think this RPC is fetched every 10s, right? Not sure if it's needed that frequently. It will return the same result at the same height anyway? Could add a factor of 60 or so, so that it's only fetched every 10 minutes..

Yeah, fair point.

I think having a slower rate (e.g. the multiplier) rather than some event (e.g. block hash/height change) is the way to go, we have a more deterministic data source. So I'll propose this!

@deadmanoz

Copy link
Copy Markdown
Collaborator Author

Here was my original approach. But I think having a separate tokio::time::interval for "slower" RPC queries is probably better, as it will allow for a cleaner separation of default rate queries (the existing queries) and slower rate queries (such as getchaintxstats).

Screenshot 2025-12-18 at 11 26 44 am

@deadmanoz
deadmanoz force-pushed the feat/getchaintxstats branch from f991fd6 to c98d84a Compare December 18, 2025 03:37
@deadmanoz

Copy link
Copy Markdown
Collaborator Author

Confirmed queries are ~10min apart. Currently erroring because of upstream bug:

2025-12-18T03:51:04.519Z ERROR [rpc_extractor] Could not fetch and publish 'getchaintxstats': RPC error: JSON-RPC error: JSON decode error: invalid type: floating point `5.04260536630741`, expected i64 at line 1 column 272
...
2025-12-18T04:01:04.525Z ERROR [rpc_extractor] Could not fetch and publish 'getchaintxstats': RPC error: JSON-RPC error: JSON decode error: invalid type: floating point `5.04260536630741`, expected i64 at line 1 column 272

Metrics are available, again currently empty due to bug:

❯ curl -s http://127.0.0.1:8282/metrics | grep chaintxstats
# HELP peerobserver_rpc_chaintxstats_tx_count Total number of transactions in the chain up to the final block in the window.
# TYPE peerobserver_rpc_chaintxstats_tx_count gauge
peerobserver_rpc_chaintxstats_tx_count 0
# HELP peerobserver_rpc_chaintxstats_tx_rate Average rate of transactions per second in the window.
# TYPE peerobserver_rpc_chaintxstats_tx_rate gauge
peerobserver_rpc_chaintxstats_tx_rate 0
# HELP peerobserver_rpc_chaintxstats_window_block_count Size of the window in number of blocks.
# TYPE peerobserver_rpc_chaintxstats_window_block_count gauge
peerobserver_rpc_chaintxstats_window_block_count 0
# HELP peerobserver_rpc_chaintxstats_window_final_block_height Height of the final block in the window.
# TYPE peerobserver_rpc_chaintxstats_window_final_block_height gauge
peerobserver_rpc_chaintxstats_window_final_block_height 0
# HELP peerobserver_rpc_chaintxstats_window_interval Elapsed time in the window in seconds.
# TYPE peerobserver_rpc_chaintxstats_window_interval gauge
peerobserver_rpc_chaintxstats_window_interval 0
# HELP peerobserver_rpc_chaintxstats_window_tx_count Number of transactions in the window.
# TYPE peerobserver_rpc_chaintxstats_window_tx_count gauge
peerobserver_rpc_chaintxstats_window_tx_count 0

@0xB10C

0xB10C commented Dec 18, 2025

Copy link
Copy Markdown
Collaborator

But I think having a separate tokio::time::interval for "slower" RPC queries is probably better, as it will allow for a cleaner separation of default rate queries (the existing queries) and slower rate queries (such as getchaintxstats).

Agree, that works too. I think if we find our self to add a few more custom intervals, we could also have them as configuration arguments with reasonable defaults. This could replace the disable args, and setting the interval to 0s would disable them. But this is not needed for now.

@0xB10C

0xB10C commented Dec 18, 2025

Copy link
Copy Markdown
Collaborator

Did a review pass on c98d84a and code looked good. I'm afraid you have to rebase on #309 now.

@deadmanoz
deadmanoz force-pushed the feat/getchaintxstats branch from c98d84a to 8b3a3a8 Compare December 19, 2025 01:42
@deadmanoz

Copy link
Copy Markdown
Collaborator Author

Did a review pass on c98d84a and code looked good. I'm afraid you have to rebase on #309 now.

Rebased on master. Won't proceed further until upstream is done!

0xB10C added a commit that referenced this pull request Dec 30, 2025
This makes corepc support:

- cpu_load and inv_to_send in getpeerinfo (as before)
- addconnection
- getorphantxs

and includes rust-bitcoin/corepc#430 for #312
as well as other improvements in corepc master.

Also upgrading the node version feature to v30 from v29.
0xB10C added a commit that referenced this pull request Dec 30, 2025
This makes corepc support:

- cpu_load and inv_to_send in getpeerinfo (as before)
- addconnection
- getorphantxs

and includes rust-bitcoin/corepc#430 for #312
as well as other improvements in corepc master.

Also upgrading the node version feature to v30 from v29.
@0xB10C

0xB10C commented Dec 30, 2025

Copy link
Copy Markdown
Collaborator

With #323, your fix is now included in master. Feel free to rebase and undraft.

@deadmanoz
deadmanoz force-pushed the feat/getchaintxstats branch from 43da236 to 3030c92 Compare December 31, 2025 13:31
@deadmanoz
deadmanoz marked this pull request as ready for review December 31, 2025 13:42
@deadmanoz

Copy link
Copy Markdown
Collaborator Author

With #323, your fix is now included in master. Feel free to rebase and undraft.

All good, whenever you've got capacity (happy new year!!)

Comment thread shared/src/protobuf/rpc_extractor.rs Outdated
Comment on lines +248 to +252
// Note: Bitcoin Core returns txrate as a float,
// but corepc deserialises it as i64!
// The following will be changed once the upstream PR is merged:
// https://github.com/rust-bitcoin/corepc/pull/430
tx_rate: stats.tx_rate.map(|r| r as f64),

@0xB10C 0xB10C Jan 5, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be removed now?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see you're doing this in 3030c92.

Please squash this fixup commit with the one introducing it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also squash 5ffc28e with the first commit.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did all of this!

Comment thread extractors/rpc/tests/integration.rs Outdated
Comment on lines +325 to +330
assert!(stats.window_tx_count.is_some());
assert!(stats.window_interval.is_some());
// And tx_rate is only present if window_interval > 0
if stats.window_interval.unwrap_or(0) > 0 {
assert!(stats.tx_rate.is_some());
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if we hit this in the test?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm doesn't seem like it. Inserting an assert doesn't fail the test. So probably best to either leave it out, or mine a few blocks to make sure it's executed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with leave it out

Add support for the `getchaintxstats` Bitcoin Core RPC, which provides
statistics about transaction counts and rates in the chain.

- Add ChainTxStats protobuf message with fields: time, tx_count,
  window_final_block_hash, window_final_block_height, window_block_count,
  window_tx_count, window_interval, tx_rate
- Add chain_tx_stats variant to RPCEvent oneof
- Add From<RPCGetChainTxStats> and Display impls for ChainTxStats
- Add --disable-getchaintxstats CLI flag
- Add getchaintxstats() fetch function in main loop
getchaintxstats doesn't need to be queried at the default
interval, use a dedicated tokio interval at 60x the base
query_interval.
@deadmanoz
deadmanoz force-pushed the feat/getchaintxstats branch from 3030c92 to c5af96b Compare January 6, 2026 09:11
@deadmanoz

deadmanoz commented Jan 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

FWIW here's a visualisation of the tx_rate metric (Grafana dashboard whipped up to check)

Screenshot 2026-01-06 at 5 12 35 pm

Is the build error transient? Or did they pull 30.0 and 30.1 because of the legacy wallet bug?

Edit: looks like 30.0 and 30.1 have been pulled: e.g. 404 on
https://bitcoincore.org/bin/bitcoin-core-30.1/

@0xB10C

0xB10C commented Jan 6, 2026

Copy link
Copy Markdown
Collaborator

Yeah, I think we can ignore the Ubuntu job for now. Could also add in our own binary again

@0xB10C 0xB10C left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@0xB10C
0xB10C merged commit 59c13e5 into peer-observer:master Jan 6, 2026
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.

3 participants