Add SQLite message and peer persistence (internal/store)

Each daemon writes to <data-dir>/messages.db on startup. Messages received
or sent are stored immediately; duplicate mids (INSERT OR IGNORE) are safe
to call multiple times. Peer aliases are upserted on peer_connected and
again after hello verification when the real nick is known.

Schema
- messages(mid UNIQUE, room, from_peer, body, sent_at) — mid is the YAW/2
  dedup key added in the proto migration; index on (room, sent_at) for
  efficient per-room queries.
- peers(peer_id PK, alias, last_seen) — cache of every peer ever seen,
  used to resolve hex ids to names when peers are offline.

Wiring
- store.Open called in cmd/daemon/main.go, passed to mesh.New.
- mesh.Mesh holds *store.Store (nil-safe; persistence is optional).
- mesh.SaveMessage called in dispatchPeerMessage (incoming) and ipc
  CmdSendMessage (outgoing) so the local node's own messages are stored.
- mesh.UpdatePeerAlias called after hello verification updates the alias
  with the verified nick rather than the placeholder short-id.

Messages only accumulate from join time forward — no history replay to
late-joining peers; each node's view starts from when it connected.

test-network.sh: added SQLite verification block that queries each node's
DB after the test and prints message + peer counts.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Fredrik Johansson
2026-06-21 18:04:42 +02:00
parent b648f7029f
commit cde611b261
9 changed files with 332 additions and 10 deletions

130
internal/store/store.go Normal file
View File

@@ -0,0 +1,130 @@
// Package store persists messages and peer info to a local SQLite database.
// Each daemon has its own database; nothing is shared across the network.
package store
import (
"database/sql"
"fmt"
"time"
_ "modernc.org/sqlite"
"github.com/waste-go/internal/proto"
)
const schema = `
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
mid TEXT NOT NULL UNIQUE,
room TEXT NOT NULL,
from_peer TEXT NOT NULL,
body TEXT NOT NULL,
sent_at DATETIME NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_messages_room ON messages (room, sent_at);
CREATE TABLE IF NOT EXISTS peers (
peer_id TEXT PRIMARY KEY,
alias TEXT NOT NULL,
last_seen DATETIME NOT NULL
);
`
// Store is a local SQLite-backed message and peer store.
type Store struct {
db *sql.DB
}
// Open opens (or creates) the SQLite database at path and runs migrations.
func Open(path string) (*Store, error) {
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
}
db.SetMaxOpenConns(1) // SQLite is single-writer
if _, err := db.Exec(schema); err != nil {
db.Close()
return nil, fmt.Errorf("migrate db: %w", err)
}
return &Store{db: db}, nil
}
// Close releases the database connection.
func (s *Store) Close() error {
return s.db.Close()
}
// SaveMessage persists a chat message. Duplicate mids are silently ignored
// (INSERT OR IGNORE), so calling this more than once is safe.
func (s *Store) SaveMessage(msg *proto.ChatMessage) error {
_, err := s.db.Exec(
`INSERT OR IGNORE INTO messages (mid, room, from_peer, body, sent_at)
VALUES (?, ?, ?, ?, ?)`,
msg.Mid, msg.Room, string(msg.From), msg.Body, msg.SentAt.UTC(),
)
return err
}
// SavePeer upserts a peer's alias and last-seen timestamp.
func (s *Store) SavePeer(peerID proto.PeerID, alias string) error {
_, err := s.db.Exec(
`INSERT INTO peers (peer_id, alias, last_seen)
VALUES (?, ?, ?)
ON CONFLICT(peer_id) DO UPDATE SET alias=excluded.alias, last_seen=excluded.last_seen`,
string(peerID), alias, time.Now().UTC(),
)
return err
}
// RecentMessages returns up to limit messages for a room, oldest first.
func (s *Store) RecentMessages(room string, limit int) ([]proto.ChatMessage, error) {
rows, err := s.db.Query(
`SELECT mid, from_peer, body, sent_at
FROM messages
WHERE room = ?
ORDER BY sent_at DESC
LIMIT ?`,
room, limit,
)
if err != nil {
return nil, err
}
defer rows.Close()
var msgs []proto.ChatMessage
for rows.Next() {
var m proto.ChatMessage
var from string
var sentAt time.Time
if err := rows.Scan(&m.Mid, &from, &m.Body, &sentAt); err != nil {
return nil, err
}
m.From = proto.PeerID(from)
m.Room = room
m.SentAt = sentAt
msgs = append(msgs, m)
}
// Reverse so oldest-first.
for i, j := 0, len(msgs)-1; i < j; i, j = i+1, j-1 {
msgs[i], msgs[j] = msgs[j], msgs[i]
}
return msgs, rows.Err()
}
// KnownPeers returns all peers seen since this daemon started storing data.
func (s *Store) KnownPeers() (map[proto.PeerID]string, error) {
rows, err := s.db.Query(`SELECT peer_id, alias FROM peers`)
if err != nil {
return nil, err
}
defer rows.Close()
out := make(map[proto.PeerID]string)
for rows.Next() {
var id, alias string
if err := rows.Scan(&id, &alias); err != nil {
return nil, err
}
out[proto.PeerID(id)] = alias
}
return out, rows.Err()
}