Add device keys, revocation and libp2p gossip with sync
- identity: 12-word recovery phrase, device certificates, revocations, device key storage; the identity key is never stored on a node - listing: signed by a device with an embedded certificate, domain-separated signatures, ErrTimeWindow for clock-related failures, region helpers - store: revocations purge a device's listings; keyset-paged Since for sync - p2p: gossipsub topic per geohash region plus a global revocation topic, validators run before relaying, /allmende/1/sync catch-up protocol, bootstrap peers and mDNS - docs: key model, replication and conflict rules Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+155
-8
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
)
|
||||
|
||||
@@ -23,6 +24,8 @@ var (
|
||||
ErrInvalid = errors.New("store: invalid listing")
|
||||
// ErrNotFound is returned when a listing is absent, expired or deleted.
|
||||
ErrNotFound = errors.New("store: not found")
|
||||
// ErrRevoked is returned for listings signed by a revoked device.
|
||||
ErrRevoked = errors.New("store: device revoked")
|
||||
)
|
||||
|
||||
// Outcome tells the caller what Put did, so the p2p layer knows whether a
|
||||
@@ -54,6 +57,7 @@ const schema = `
|
||||
CREATE TABLE IF NOT EXISTS listings (
|
||||
author TEXT NOT NULL,
|
||||
id TEXT NOT NULL,
|
||||
device TEXT NOT NULL,
|
||||
seq INTEGER NOT NULL,
|
||||
hash TEXT NOT NULL,
|
||||
tombstone INTEGER NOT NULL,
|
||||
@@ -70,6 +74,16 @@ CREATE TABLE IF NOT EXISTS listings (
|
||||
) WITHOUT ROWID;
|
||||
CREATE INDEX IF NOT EXISTS listings_geohash ON listings (geohash);
|
||||
CREATE INDEX IF NOT EXISTS listings_expires ON listings (expires_at);
|
||||
CREATE INDEX IF NOT EXISTS listings_device ON listings (author, device);
|
||||
CREATE INDEX IF NOT EXISTS listings_created ON listings (created_at, author, id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS revocations (
|
||||
author TEXT NOT NULL,
|
||||
device TEXT NOT NULL,
|
||||
cert_expires_at INTEGER NOT NULL,
|
||||
data BLOB NOT NULL,
|
||||
PRIMARY KEY (author, device)
|
||||
) WITHOUT ROWID;
|
||||
`
|
||||
|
||||
// Open opens (creating if needed) the database at path. Use ":memory:" for
|
||||
@@ -125,6 +139,17 @@ func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
var revoked int
|
||||
err = tx.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM revocations WHERE author = ? AND device = ?`, l.Author, l.Device).
|
||||
Scan(&revoked)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if revoked > 0 {
|
||||
return Ignored, ErrRevoked
|
||||
}
|
||||
|
||||
var curSeq int64
|
||||
var curHash string
|
||||
err = tx.QueryRowContext(ctx,
|
||||
@@ -149,10 +174,10 @@ func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT OR REPLACE INTO listings
|
||||
(author, id, seq, hash, tombstone, created_at, expires_at, geohash,
|
||||
(author, id, device, seq, hash, tombstone, created_at, expires_at, geohash,
|
||||
kind, category, price_cents, area_m2, rooms_x2, data)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
l.Author, l.ID, int64(l.Seq), hash, boolInt(l.Tombstone), l.CreatedAt, l.ExpiresAt,
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
l.Author, l.ID, l.Device, int64(l.Seq), hash, boolInt(l.Tombstone), l.CreatedAt, l.ExpiresAt,
|
||||
l.Geohash, l.Kind, l.Category, price, l.AreaM2, l.RoomsX2, data)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
@@ -270,15 +295,137 @@ func (s *Store) Search(ctx context.Context, q Query) ([]*listing.Listing, error)
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Expire deletes everything whose lifetime has ended, tombstones included,
|
||||
// and returns how many rows were removed. A tombstone outlives any version it
|
||||
// supersedes because listings live at most 30 days from creation.
|
||||
// Cursor is a keyset position in the (created_at, author, id) order, used to
|
||||
// page through listings during sync. The zero Cursor is the very beginning.
|
||||
type Cursor struct {
|
||||
CreatedAt int64
|
||||
Author string
|
||||
ID string
|
||||
}
|
||||
|
||||
// CursorOf returns the cursor positioned at l.
|
||||
func CursorOf(l *listing.Listing) Cursor {
|
||||
return Cursor{CreatedAt: l.CreatedAt, Author: l.Author, ID: l.ID}
|
||||
}
|
||||
|
||||
// Since returns up to limit unexpired records (tombstones included, since
|
||||
// peers need them to learn about deletions) in region prefix that sort after
|
||||
// cur, oldest first.
|
||||
func (s *Store) Since(ctx context.Context, prefix string, cur Cursor, limit int) ([]*listing.Listing, error) {
|
||||
if !listing.ValidGeohashPrefix(prefix) {
|
||||
return nil, fmt.Errorf("store: invalid geohash prefix %q", prefix)
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT data FROM listings
|
||||
WHERE geohash >= ? AND geohash <= ? AND expires_at > ?
|
||||
AND (created_at, author, id) > (?, ?, ?)
|
||||
ORDER BY created_at, author, id LIMIT ?`,
|
||||
prefix, prefix+"~", s.clock().Unix(), cur.CreatedAt, cur.Author, cur.ID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []*listing.Listing
|
||||
for rows.Next() {
|
||||
var data []byte
|
||||
if err := rows.Scan(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l, err := decode(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, l)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// PutRevocation verifies and stores a revocation, and removes every listing
|
||||
// the revoked device signed. The owner re-publishes them from a new device.
|
||||
func (s *Store) PutRevocation(ctx context.Context, r *identity.Revocation) (Outcome, error) {
|
||||
if err := r.Check(s.difficulty); err != nil {
|
||||
return Ignored, fmt.Errorf("%w: %v", ErrInvalid, err)
|
||||
}
|
||||
if r.CertExpiresAt <= s.clock().Unix() {
|
||||
return Ignored, nil // the certificate is dead anyway
|
||||
}
|
||||
data, err := r.Bytes()
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
res, err := tx.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO revocations (author, device, cert_expires_at, data) VALUES (?, ?, ?, ?)`,
|
||||
r.Author, r.Device, r.CertExpiresAt, data)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return Ignored, nil // already known
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`DELETE FROM listings WHERE author = ? AND device = ?`, r.Author, r.Device); err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
return Inserted, nil
|
||||
}
|
||||
|
||||
// Revocations returns every revocation still relevant (certificate unexpired).
|
||||
func (s *Store) Revocations(ctx context.Context) ([]*identity.Revocation, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT data FROM revocations WHERE cert_expires_at > ? ORDER BY author, device`, s.clock().Unix())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []*identity.Revocation
|
||||
for rows.Next() {
|
||||
var data []byte
|
||||
if err := rows.Scan(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var r identity.Revocation
|
||||
if err := json.Unmarshal(data, &r); err != nil {
|
||||
return nil, fmt.Errorf("store: corrupt revocation: %w", err)
|
||||
}
|
||||
out = append(out, &r)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// IsRevoked reports whether the device has been revoked.
|
||||
func (s *Store) IsRevoked(ctx context.Context, author, device string) (bool, error) {
|
||||
var n int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM revocations WHERE author = ? AND device = ?`, author, device).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
// Expire deletes everything whose lifetime has ended (listings, tombstones
|
||||
// and revocations) and returns how many rows were removed. A tombstone
|
||||
// outlives any version it supersedes because listings live at most 30 days
|
||||
// from creation.
|
||||
func (s *Store) Expire(ctx context.Context) (int64, error) {
|
||||
res, err := s.db.ExecContext(ctx, `DELETE FROM listings WHERE expires_at <= ?`, s.clock().Unix())
|
||||
now := s.clock().Unix()
|
||||
res, err := s.db.ExecContext(ctx, `DELETE FROM listings WHERE expires_at <= ?`, now)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
n, _ := res.RowsAffected()
|
||||
res, err = s.db.ExecContext(ctx, `DELETE FROM revocations WHERE cert_expires_at <= ?`, now)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
m, _ := res.RowsAffected()
|
||||
return n + m, nil
|
||||
}
|
||||
|
||||
// Count returns the number of live listings.
|
||||
|
||||
Reference in New Issue
Block a user