From 332e51d26192a6557b19be56ac90cd23d77a2418 Mon Sep 17 00:00:00 2001 From: Dionysos <75300347+ice-dionysos@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:12:59 +0100 Subject: [PATCH 1/2] database: count: handle masterkey --- database/query/query_where_builder.go | 6 +- .../query_where_dependencies_parser_test.go | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/database/query/query_where_builder.go b/database/query/query_where_builder.go index f915bc3..2aa78a3 100644 --- a/database/query/query_where_builder.go +++ b/database/query/query_where_builder.go @@ -527,7 +527,7 @@ select 6400, unixepoch(), 0, - f.reference_id, + case when f.kind = 3 then '' else f.reference_id end as id, coalesce(evr.pubkey, ''), coalesce(evr.master_pubkey, ''), '', @@ -544,7 +544,7 @@ select end as jtags from event_counters f -inner join events evr on f.reference_id in (evr.id, evr.pubkey) +inner join ` + cteName + ` evr on evr.kind = :` + (filterID + "kind") + ` and f.reference_id in (evr.id, evr.pubkey, evr.master_pubkey) where `) } else { @@ -690,6 +690,8 @@ group by e.pubkey, e.master_pubkey`) w.WriteString(" AND f.reference_id IN (") if filter.Reduce.Kinds[1] == nostr.KindFollowList { w.WriteString(w.createWhereForDepFilter(filterID, cteName, "pubkey", &filter.Start)) + w.WriteString(" UNION ALL ") + w.WriteString(w.createWhereForDepFilter(filterID, cteName, "master_pubkey", &filter.Start)) } else { w.WriteString(w.createWhereForDepFilter(filterID, cteName, "id", &filter.Start)) } diff --git a/database/query/query_where_dependencies_parser_test.go b/database/query/query_where_dependencies_parser_test.go index 40c4fed..b1e4e25 100644 --- a/database/query/query_where_dependencies_parser_test.go +++ b/database/query/query_where_dependencies_parser_test.go @@ -598,5 +598,83 @@ func TestSelectWithDependencies(t *testing.T) { } } } + t.Run("Delegated", func(t *testing.T) { + const ( + masterPrivate = `612be8342c593ba8a592f34c462e65a63c8233bad13aea022c5dcb3656a975d560f97174e3fc1c8decee03bfed97157a1a8db0d1140b8792958cd57f6de252e0` + userPrivate = `f66568ed325fac494593d5a191a591ca0e3cd4b04141350aa4703776f603e5769c1a22718581dc75961acc4b49f43935356bbecfc4cdb4229b12c31017a8a70c` + ) + masterPublic, err := model.GetPublicKey(masterPrivate) + require.NoError(t, err) + userPublic, err := model.GetPublicKey(userPrivate) + require.NoError(t, err) + + t.Logf("master public key: %s", masterPublic) + t.Logf("user public key: %s", userPublic) + + ev := &model.Event{ + Event: nostr.Event{ + Kind: model.CustomIONKindAttestation, + CreatedAt: 1, + Tags: model.Tags{{model.TagAttestationName, userPublic, "", model.CustomIONAttestationKindActive + ":1"}}, + }, + } + require.NoError(t, ev.SignWithAlg(masterPrivate, model.SignAlgEDDSA, model.KeyAlgCurve25519)) + require.NoError(t, db.AcceptEvents(context.TODO(), ev)) + + meta := &model.Event{ + Event: nostr.Event{ + Kind: nostr.KindProfileMetadata, + CreatedAt: 2, + Tags: model.Tags{{model.CustomIONTagOnBehalfOf, masterPublic}}, + }, + } + require.NoError(t, meta.SignWithAlg(userPrivate, model.SignAlgEDDSA, model.KeyAlgCurve25519)) + require.NoError(t, db.AcceptEvents(context.TODO(), meta)) + + require.NoError(t, db.AcceptEvents(context.Background(), + &model.Event{ + Event: nostr.Event{ + ID: "t10id1", + Kind: nostr.KindFollowList, + PubKey: "t10pk1", + CreatedAt: 3, + Tags: model.Tags{ + {"p", masterPublic}, + }, + }, + }, + &model.Event{ + Event: nostr.Event{ + ID: "t10id2", + Kind: nostr.KindFollowList, + PubKey: "t10pk2", + CreatedAt: 4, + Tags: model.Tags{ + {"p", masterPublic}, + }, + }, + }, + &model.Event{ + Event: nostr.Event{ + ID: "t10id3", + Kind: nostr.KindFollowList, + PubKey: "t10pk3", + CreatedAt: 5, + Tags: model.Tags{ + {"p", masterPublic}, + }, + }, + }, + )) + events := helperSelectEvents(t, db, model.Filter{ + Authors: []string{masterPublic}, + Search: "include:dependencies:kind0>kind6400+kind3+group+p", + }) + require.Len(t, events, 3) // Attestation, Profile metadata, follower count. + for i, kind := range []int{nostr.KindProfileMetadata, model.CustomIONKindAttestation, model.KindDVMCountResponse} { + require.Equalf(t, kind, events[i].Kind, "event %d: %v", i, events[i]) + } + require.Equal(t, "3", events[2].Content) + }) }) } From f7fc136a41420002bccd783cef4c6b7cfadf336e Mon Sep 17 00:00:00 2001 From: Dionysos <75300347+ice-dionysos@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:31:41 +0100 Subject: [PATCH 2/2] use KindJobNostrEventCount --- database/query/query.go | 2 +- model/model.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/database/query/query.go b/database/query/query.go index 8916136..1e2329c 100644 --- a/database/query/query.go +++ b/database/query/query.go @@ -216,7 +216,7 @@ func (db *dbClient) eventTransform(event *databaseEvent) *databaseEvent { case model.KindDVMCountResponse: var ev databaseEvent - ev.Kind = model.KindDVMCountRequest + ev.Kind = model.KindJobNostrEventCount ev.CreatedAt = event.CreatedAt ev.Content = event.Dtag ev.Tags = append(event.Tags, model.Tag{"param", "relay", db.relayURL}) diff --git a/model/model.go b/model/model.go index b78d97b..5a28201 100644 --- a/model/model.go +++ b/model/model.go @@ -42,7 +42,6 @@ const ( CustomIONKindAttestation = 10_100 CustomIONKindRelayListMetadata = 20_002 - KindDVMCountRequest = 5400 KindDVMCountResponse = 6400 )