Bitcoin Service Usage: Persistent Idempotent Metering
Bitcoin Service Usage: Persistent Idempotent Metering
Deduplicate usage events in a local SQLite ledger.
Design and trust boundary
Use a persistent database in deployment; the in-memory fixture only demonstrates transaction semantics. Authenticate the producer and scope event identifiers to a tenant and service before recording. Repeated identical events are harmless; conflicting replays must be investigated. A usage ledger is not a Bitcoin transaction ledger.
Independent reference implementation
1import sqlite3
2
3def record(db, event_id, units):
4 if not isinstance(event_id, str) or not 0 < len(event_id) <= 128 or type(units) is not int or not 0 < units <= 1000000:
5 raise ValueError("invalid event")
6 with db:
7 db.execute("INSERT INTO usage VALUES (?, ?) ON CONFLICT(id) DO NOTHING", (event_id, units))
8 actual = db.execute("SELECT units FROM usage WHERE id=?", (event_id,)).fetchone()[0]
9 if actual != units:
10 raise ValueError("conflicting replay")
11
12db = sqlite3.connect(":memory:")
13try:
14 db.execute("CREATE TABLE usage(id TEXT PRIMARY KEY, units INTEGER NOT NULL)")
15 record(db, "event-1", 4)
16 record(db, "event-1", 4)
17 assert db.execute("SELECT SUM(units) FROM usage").fetchone()[0] == 4
18 try:
19 record(db, "event-1", 5)
20 except ValueError:
21 pass
22 else:
23 raise AssertionError("conflict accepted")
24finally:
25 db.close()Operational boundaries
Run with Python 3. The assertions use synthetic data only. No SEND, wallet credentials or live API calls are included. Use a separate authorized execution service for actual settlement. Preserve the WAB Engine API and immutable signed fee law. These are proposed application patterns, not interoperability standards or production financial systems.
Related research
https://thamer.org/learn/bitcoin-for-ai-agents-open-integration-guide
https://thamer.org/api
