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:
+172
-24
@@ -19,15 +19,26 @@ var (
|
||||
)
|
||||
|
||||
type env struct {
|
||||
s *Store
|
||||
now time.Time
|
||||
key *identity.Identity
|
||||
s *Store
|
||||
now time.Time
|
||||
root *identity.Identity
|
||||
dev *identity.Device
|
||||
}
|
||||
|
||||
func newDevice(t *testing.T, root *identity.Identity) *identity.Device {
|
||||
t.Helper()
|
||||
dev, err := identity.Enroll(root, t0.Add(-time.Hour), 180*24*time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return dev
|
||||
}
|
||||
|
||||
func newEnv(t *testing.T) *env {
|
||||
t.Helper()
|
||||
e := &env{now: t0}
|
||||
e.key, _ = identity.Generate()
|
||||
e.root, _, _ = identity.NewRoot()
|
||||
e.dev = newDevice(t, e.root)
|
||||
s, err := Open(":memory:", Options{Difficulty: difficulty, Clock: func() time.Time { return e.now }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -37,6 +48,14 @@ func newEnv(t *testing.T) *env {
|
||||
return e
|
||||
}
|
||||
|
||||
// sibling returns a second store sharing e's identity and clock.
|
||||
func (e *env) sibling(t *testing.T) *env {
|
||||
t.Helper()
|
||||
e2 := newEnv(t)
|
||||
e2.root, e2.dev = e.root, e.dev
|
||||
return e2
|
||||
}
|
||||
|
||||
func (e *env) listing(t *testing.T, mutate func(*listing.Listing)) *listing.Listing {
|
||||
t.Helper()
|
||||
id, _ := listing.NewID()
|
||||
@@ -53,12 +72,11 @@ func (e *env) listing(t *testing.T, mutate func(*listing.Listing)) *listing.List
|
||||
AreaM2: 50,
|
||||
RoomsX2: 4,
|
||||
Title: "Wohnung",
|
||||
Endpoint: "12D3KooWExamplePeerID",
|
||||
}
|
||||
if mutate != nil {
|
||||
mutate(l)
|
||||
}
|
||||
if err := l.Seal(e.key, difficulty); err != nil {
|
||||
if err := l.Seal(e.dev, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return l
|
||||
@@ -112,7 +130,7 @@ func TestHigherSeqWinsInAnyOrder(t *testing.T) {
|
||||
v2 := *v1
|
||||
v2.Seq = 2
|
||||
v2.Title = "Neuer Titel"
|
||||
if err := v2.Seal(e.key, difficulty); err != nil {
|
||||
if err := v2.Seal(e.dev, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -129,8 +147,7 @@ func TestHigherSeqWinsInAnyOrder(t *testing.T) {
|
||||
}
|
||||
|
||||
// Fresh store, natural order: second is a replacement.
|
||||
e2 := newEnv(t)
|
||||
e2.key = e.key
|
||||
e2 := e.sibling(t)
|
||||
e2.put(t, v1)
|
||||
if o := e2.put(t, &v2); o != Replaced {
|
||||
t.Fatalf("got %v want replaced", o)
|
||||
@@ -142,7 +159,7 @@ func TestEqualSeqConvergesOnSmallerHash(t *testing.T) {
|
||||
a := e.listing(t, nil)
|
||||
b := *a
|
||||
b.Title = "Anderer Titel"
|
||||
if err := b.Seal(e.key, difficulty); err != nil {
|
||||
if err := b.Seal(e.dev, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ha, _ := a.Hash()
|
||||
@@ -157,8 +174,7 @@ func TestEqualSeqConvergesOnSmallerHash(t *testing.T) {
|
||||
"loser first": {loser, winner},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
e2 := newEnv(t)
|
||||
e2.key = e.key
|
||||
e2 := e.sibling(t)
|
||||
for _, l := range order {
|
||||
e2.put(t, l)
|
||||
}
|
||||
@@ -170,19 +186,25 @@ func TestEqualSeqConvergesOnSmallerHash(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTombstoneDeletesAndBlocksResurrection(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
l := e.listing(t, nil)
|
||||
e.put(t, l)
|
||||
|
||||
func (e *env) tombstone(t *testing.T, l *listing.Listing) *listing.Listing {
|
||||
t.Helper()
|
||||
ts := &listing.Listing{
|
||||
Version: listing.Version, ID: l.ID, Seq: l.Seq + 1,
|
||||
CreatedAt: e.now.Unix(), ExpiresAt: e.now.Add(30 * 24 * time.Hour).Unix(),
|
||||
Tombstone: true, Geohash: l.Geohash,
|
||||
}
|
||||
if err := ts.Seal(e.key, difficulty); err != nil {
|
||||
if err := ts.Seal(e.dev, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ts
|
||||
}
|
||||
|
||||
func TestTombstoneDeletesAndBlocksResurrection(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
l := e.listing(t, nil)
|
||||
e.put(t, l)
|
||||
|
||||
ts := e.tombstone(t, l)
|
||||
if o := e.put(t, ts); o != Replaced {
|
||||
t.Fatalf("got %v want replaced", o)
|
||||
}
|
||||
@@ -197,8 +219,7 @@ func TestTombstoneDeletesAndBlocksResurrection(t *testing.T) {
|
||||
}
|
||||
|
||||
// A tombstone arriving before the listing it deletes still wins.
|
||||
e2 := newEnv(t)
|
||||
e2.key = e.key
|
||||
e2 := e.sibling(t)
|
||||
e2.put(t, ts)
|
||||
if o := e2.put(t, l); o != Ignored {
|
||||
t.Fatalf("listing after tombstone: %v", o)
|
||||
@@ -210,11 +231,11 @@ func TestSameIDDifferentAuthorsDoNotCollide(t *testing.T) {
|
||||
victim := e.listing(t, nil)
|
||||
e.put(t, victim)
|
||||
|
||||
other, _ := identity.Generate()
|
||||
otherRoot, _, _ := identity.NewRoot()
|
||||
squat := *victim
|
||||
squat.Seq = 99
|
||||
squat.Title = "Betrug"
|
||||
if err := squat.Seal(other, difficulty); err != nil {
|
||||
if err := squat.Seal(newDevice(t, otherRoot), difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e.put(t, &squat)
|
||||
@@ -225,6 +246,88 @@ func TestSameIDDifferentAuthorsDoNotCollide(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevocationVoidsDevice(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
stolen := e.dev
|
||||
mine := e.listing(t, nil)
|
||||
e.put(t, mine)
|
||||
|
||||
// The attacker hijacks the listing with the stolen device and a huge seq.
|
||||
hijack := *mine
|
||||
hijack.Seq = 1 << 40
|
||||
hijack.Title = "Betrug"
|
||||
if err := hijack.Seal(stolen, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if o := e.put(t, &hijack); o != Replaced {
|
||||
t.Fatalf("hijack: %v", o)
|
||||
}
|
||||
|
||||
// The owner restores from the recovery phrase and revokes the device.
|
||||
rev, err := identity.Revoke(e.root, stolen.Cert, e.now, difficulty)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if o, err := e.s.PutRevocation(ctx, rev); err != nil || o != Inserted {
|
||||
t.Fatalf("PutRevocation = %v, %v", o, err)
|
||||
}
|
||||
if _, err := e.s.Get(ctx, mine.Author, mine.ID); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("revoked device's listing still readable")
|
||||
}
|
||||
if _, err := e.s.Put(ctx, &hijack); !errors.Is(err, ErrRevoked) {
|
||||
t.Fatalf("revoked device accepted again: %v", err)
|
||||
}
|
||||
if o, _ := e.s.PutRevocation(ctx, rev); o != Ignored {
|
||||
t.Fatalf("duplicate revocation: %v", o)
|
||||
}
|
||||
|
||||
// The owner republishes from a new device with a low seq: it must win,
|
||||
// because the attacker's huge seq was purged with the revocation.
|
||||
newDev := newDevice(t, e.root)
|
||||
again := *mine
|
||||
again.Seq = 2
|
||||
if err := again.Seal(newDev, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if o := e.put(t, &again); o != Inserted {
|
||||
t.Fatalf("republish from new device: %v", o)
|
||||
}
|
||||
got, _ := e.s.Get(ctx, mine.Author, mine.ID)
|
||||
if got.Title != "Wohnung" {
|
||||
t.Fatalf("got %q", got.Title)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevocationValidation(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
other, _, _ := identity.NewRoot()
|
||||
// Someone else's identity cannot revoke our device.
|
||||
if _, err := identity.Revoke(other, e.dev.Cert, e.now, difficulty); err == nil {
|
||||
t.Fatal("foreign identity could create a revocation")
|
||||
}
|
||||
rev, _ := identity.Revoke(e.root, e.dev.Cert, e.now, difficulty)
|
||||
rev.Device = other.PublicKeyHex()
|
||||
if _, err := e.s.PutRevocation(ctx, rev); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatalf("tampered revocation: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevocationExpiresWithCertificate(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
rev, _ := identity.Revoke(e.root, e.dev.Cert, e.now, difficulty)
|
||||
e.s.PutRevocation(ctx, rev)
|
||||
if list, _ := e.s.Revocations(ctx); len(list) != 1 {
|
||||
t.Fatalf("got %d revocations, want 1", len(list))
|
||||
}
|
||||
e.now = time.Unix(e.dev.Cert.ExpiresAt+1, 0)
|
||||
if list, _ := e.s.Revocations(ctx); len(list) != 0 {
|
||||
t.Fatal("expired revocation still listed")
|
||||
}
|
||||
if n, _ := e.s.Expire(ctx); n != 1 {
|
||||
t.Fatalf("Expire removed %d, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
mk := func(geo, kind string, cents int64, area, rooms int) {
|
||||
@@ -278,6 +381,51 @@ func TestSearch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSinceKeysetPaging(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
// Five listings in the same second plus one tombstone and one outside the region.
|
||||
var all []*listing.Listing
|
||||
for i := 0; i < 5; i++ {
|
||||
l := e.listing(t, nil)
|
||||
e.put(t, l)
|
||||
all = append(all, l)
|
||||
}
|
||||
e.put(t, e.tombstone(t, all[0]))
|
||||
e.put(t, e.listing(t, func(l *listing.Listing) { l.Geohash = "u30000" }))
|
||||
|
||||
var got []*listing.Listing
|
||||
cur := Cursor{}
|
||||
for pages := 0; pages < 10; pages++ {
|
||||
page, err := e.s.Since(ctx, "u1x", cur, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page) == 0 {
|
||||
break
|
||||
}
|
||||
got = append(got, page...)
|
||||
cur = CursorOf(page[len(page)-1])
|
||||
}
|
||||
// 5 distinct listings; one of them is now a tombstone.
|
||||
if len(got) != 5 {
|
||||
t.Fatalf("paged %d records, want 5", len(got))
|
||||
}
|
||||
tombstones := 0
|
||||
seen := map[string]bool{}
|
||||
for _, l := range got {
|
||||
if seen[l.ID] {
|
||||
t.Fatalf("duplicate %s across pages", l.ID)
|
||||
}
|
||||
seen[l.ID] = true
|
||||
if l.Tombstone {
|
||||
tombstones++
|
||||
}
|
||||
}
|
||||
if tombstones != 1 {
|
||||
t.Fatalf("got %d tombstones, want 1 (peers need them to learn deletions)", tombstones)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpiry(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
l := e.listing(t, nil)
|
||||
@@ -298,14 +446,14 @@ func TestExpiry(t *testing.T) {
|
||||
|
||||
func TestPersistence(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "allmende.db")
|
||||
key, _ := identity.Generate()
|
||||
opt := Options{Difficulty: difficulty, Clock: func() time.Time { return t0 }}
|
||||
|
||||
s, err := Open(path, opt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e := &env{s: s, now: t0, key: key}
|
||||
e := newEnv(t)
|
||||
e.s = s
|
||||
l := e.listing(t, nil)
|
||||
e.put(t, l)
|
||||
s.Close()
|
||||
|
||||
Reference in New Issue
Block a user