Skip to content

feat(p2p/sensor): add ClickHouse database backend#961

Open
minhd-vu wants to merge 2 commits into
mainfrom
minhd-vu/sensor-clickhouse-backend
Open

feat(p2p/sensor): add ClickHouse database backend#961
minhd-vu wants to merge 2 commits into
mainfrom
minhd-vu/sensor-clickhouse-backend

Conversation

@minhd-vu

Copy link
Copy Markdown
Contributor

Summary

Adds a ClickHouse backend for the polycli p2p sensor, as a cheaper alternative to GCP Datastore. Selectable via --database=clickhouse --clickhouse-dsn=clickhouse://user:pass@host:9000/sensor.

Part of the sensor Datastore→ClickHouse migration (Datastore currently ~$14–15k/mo, dominated by read-before-write dedup ops; ClickHouse's append-only model + columnar storage removes that).

What's included

  • p2p/database/clickhouse.go — implements the Database interface:
    • Client-side batched, append-only writes via a generic drop-on-full rowBatcher (buffered channel + background flusher). The Write* calls only enqueue, so the sensor hot path never blocks; a slow/down DB drops rows rather than exhausting memory. No read-before-write.
    • Uses ReplacingMergeTree semantics on the target schema for dedup/merge.
    • Recovers and stores the block signer at ingest (util.Ecrecover), consistent with --validate-block-signer and the data-analysis tools, so signer-based analytics don't re-ecrecover per query.
    • HasBlock is a cheap indexed point lookup; NodeList is the recent-distinct-peers query.
  • cmd/p2p/sensor/sensor.go--database=clickhouse case, --clickhouse-dsn flag, params field, help text.
  • p2p/database/clickhouse_test.go — integration test, skipped unless POLYCLI_TEST_CLICKHOUSE_DSN is set; verifies write batching, field round-trip, and signer recovery against a real ClickHouse.
  • Adds github.com/ClickHouse/clickhouse-go/v2.

Testing

make build, go vet, gofmt, and go test ./p2p/database/ all pass. Integration test verified against a dockerized ClickHouse:

POLYCLI_TEST_CLICKHOUSE_DSN=clickhouse://sensor:...@localhost:9000/sensor \
  go test ./p2p/database/ -run TestClickHouseWrites -v

Notes

  • Datastore/JSON/none backends are unchanged; default remains none.
  • The target table schema lives in the sensor-network-tools repo (separate PR).

🤖 Generated with Claude Code

minhd-vu and others added 2 commits July 17, 2026 11:21
Add a ClickHouse backend for the p2p sensor as an alternative to GCP
Datastore, selectable with --database=clickhouse --clickhouse-dsn.

- Implements the Database interface with client-side batched, append-only
  writes (no read-before-write) via a generic drop-on-full rowBatcher, so the
  sensor hot path never blocks on the database.
- Recovers and stores the block signer at ingest (util.Ecrecover), matching
  the sensor's block-signer validation and the data-analysis tools.
- Adds an integration test (skipped unless POLYCLI_TEST_CLICKHOUSE_DSN is set)
  verifying writes and signer round-trip against a real server.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Persist the remaining header fields (uncle hash, roots, logs bloom, extra
data, mix digest, nonce) so downstream consumers (panoptichain, data-analysis)
can reconstruct the header and re-run bor/clique ecrecover. Cheap on
ClickHouse: the Datastore cost was index bloat, not these values.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

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