Skip to content

feat: implement state and export, record alerts and sends in the ledger - #6

Draft
devblac wants to merge 2 commits into
mainfrom
feat/state-export
Draft

devblac wants to merge 2 commits into
mainfrom
feat/state-export

Conversation

@devblac

@devblac devblac commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Why

The README documents watch-tower state and watch-tower export, but both were stubs that printed TODO. Making export alerts useful also exposed a bigger gap: the runner never wrote to the alerts or sends tables (only dedupe keys and cursors), so there was nothing to export or audit.

What changed

feat(engine): record alerts and sink deliveries in the ledger

  • Every alert that passes predicates, rate limit and dedupe is stored before any sink is called, with a JSON payload (rule_id, chain, source_id, height, hash, txhash, log_index, app_id, args) and an event fingerprint (rule|source|txhash|logIndex|appID).
  • Each sink attempt is recorded as sent or failed. A failing sink still aborts the pass as before, but now leaves a record.
  • An arg that can't be JSON-encoded is dropped from the payload (with args_error) instead of stopping alerting.
  • What gets sent is unchanged; --dry-run still writes nothing.
  • Store: new ListCursors and ListAlerts (per-sink sends, rule / since / limit filters).

feat(cli): implement state and export commands

  • state: per source, cursor height, chain head, lag, confirmations, hash and last update.
    • --format table|json; --offline skips RPC calls; --db reads the ledger without loading the config.
    • Flags sources not started yet and cursors for sources no longer in the config; an unreachable RPC is reported on its row and the rest still prints.
  • export alerts|cursors: --format json|csv, -o file; for alerts --rule, --since (duration like 24h or RFC3339) and --limit N, including each alert's per-sink status.
  • Both commands refuse a missing database instead of silently creating an empty one.

Reviewer notes

  • Dedupe is still the only thing that prevents re-sending on replays. Replaying blocks already processed, on a rule without dedupe, resends the alert and records it again (same fingerprint, new id). Alert ids are random, so the ledger does not enforce exactly-once by event.
  • This branch does not depend on fix(evm): make uint256 predicates and ABI-less event decoding work #5 (EVM predicate fixes); they can merge in either order.
  • Ordering and --since filtering of alerts happen in Go: rows written with CURRENT_TIMESTAMP and with bound time values use different text layouts in SQLite.
  • response_code in sends stays 0: the Sender interface only returns an error.

Testing

  • New tests: internal/storage/store_list_test.go, internal/engine/runner_ledger_test.go, cmd/watch-tower/state_export_test.go (commands run end to end through the root command against a seeded DB; head lookups use a fake).
  • go vet ./... and go test ./... pass; smoke-tested the built binary's help and missing-DB error.
  • Not run against a live RPC.

🤖 Generated with Claude Code

devblac and others added 2 commits November 27, 2026 13:15
The runner only wrote dedupe keys and cursors; the alerts and sends
tables were never populated, so there was nothing to export or audit.

Each alert that passes predicates, rate limit and dedupe is now stored
before any sink is called, with a JSON payload and an event fingerprint,
and every sink attempt is recorded as sent or failed. Sending behaviour
is unchanged and dry-run still writes nothing. Adds ListCursors and
ListAlerts (with per-sink sends and rule/since/limit filters) to the
store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both commands were stubs that printed TODO although the README
documents them.

- state: per-source cursor height, chain head, lag, confirmations,
  hash and last update; flags --format table|json, --offline, --db.
  Flags sources not started yet and cursors no longer in the config;
  an unreachable RPC is reported on its row.
- export alerts|cursors: --format json|csv, -o file, and for alerts
  --rule, --since (duration or RFC3339) and --limit, including each
  alert's per-sink delivery status.

--db reads the ledger without loading the config, and both commands
refuse a missing database instead of creating an empty one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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