Skip to content

Commit

Permalink
ICIP-3000: Communities. ICIP-4000: settings. (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles authored Dec 24, 2024
1 parent 33852f0 commit 78867d2
Show file tree
Hide file tree
Showing 22 changed files with 4,648 additions and 244 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ NIPs | latest commit hash implemented | comments
[96](https://github.com/nostr-protocol/nips/blob/master/96.md) | [4e73e94d417f16fa3451e58ef921cb3b512c6f8e](https://github.com/ice-blockchain/subzero/commit/130bac5adedf6563fe8d8e869f7e46b4cfb414e0)|
[98](https://github.com/nostr-protocol/nips/blob/master/98.md) | [ae0fd96907d0767f07fb54ca1de9f197c600cb27](https://github.com/ice-blockchain/subzero/commit/130bac5adedf6563fe8d8e869f7e46b4cfb414e0)|


Data vending machive | latest commit hash implemented | comments
--- | --- | ---
[Nostr Event Count](https://github.com/nostr-protocol/data-vending-machines/blob/master/kinds/5400.md) | [d7bd5cc](https://github.com/nostr-protocol/data-vending-machines/commit/d7bd5cc4b3a45b75edbee4134c38b3e56b7eef31)
4 changes: 3 additions & 1 deletion database/query/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CREATE TABLE IF NOT EXISTS events
key_alg text not null DEFAULT '',
content text not null,
d_tag text not null DEFAULT '',
h_tag text not null UNIQUE,
reference_id text references events (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
tags text not null DEFAULT '[]',
hidden integer not null default 0
Expand Down Expand Up @@ -234,7 +235,7 @@ create trigger if not exists trigger_events_before_insert_unwind_repost
when new.kind in (6, 16)
begin
insert into events
(kind, created_at, system_created_at, id, pubkey, master_pubkey, sig, content, tags, d_tag, hidden)
(kind, created_at, system_created_at, id, pubkey, master_pubkey, sig, content, tags, d_tag, h_tag, hidden)
select
json_extract(b, '$.kind'),
0,
Expand All @@ -246,6 +247,7 @@ select
'',
json_extract(b, '$.tags'),
'',
json_extract(b, '$.id'),
1
from
(select NEW.content as b)
Expand Down
7 changes: 7 additions & 0 deletions database/query/DDL_alter_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- SPDX-License-Identifier: ice License 1.0

ALTER TABLE events ADD COLUMN h_tag text not null;
--------
UPDATE events SET h_tag = id WHERE h_tag = NULL;
--------
CREATE UNIQUE INDEX IF NOT EXISTS uix_events_h_tag ON events(h_tag);
34 changes: 33 additions & 1 deletion database/query/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type (
var (
//go:embed DDL.sql
ddl string

//go:embed DDL_alter_events.sql
ddlAlterEvents string
)

func init() {
Expand Down Expand Up @@ -109,6 +112,8 @@ func openDatabase(target string, runDDL bool) *dbClient {
out = "master_pubkey"
case "dtag":
out = "d_tag"
case "htag":
out = "h_tag"
default:
out = n
}
Expand All @@ -117,14 +122,41 @@ func openDatabase(target string, runDDL bool) *dbClient {
})

if runDDL {
tx := client.MustBegin()
defer tx.Rollback()
for _, statement := range strings.Split(ddl, "--------") {
client.MustExec(statement)
tx.MustExec(statement)
}
if err := client.alterEventsTable(tx); err != nil {
panic(err)
}
tx.Commit()
}

return client
}

func (db *dbClient) alterEventsTable(tx *sqlx.Tx) error {
sqlQuery := "SELECT exists (select name from pragma_table_info('events') WHERE name = $1);"
res, err := tx.Queryx(sqlQuery, "h_tag")
if err != nil {
return err
}
var exists int
if res.Next() {
if err = res.Scan(&exists); err != nil {
return err
}
}
if exists == 0 {
for _, statement := range strings.Split(ddlAlterEvents, "--------") {
tx.MustExec(statement)
}
}

return nil
}

func (db *dbClient) WithRelayURL(relayURL string) *dbClient {
db.relayURL = relayURL

Expand Down
41 changes: 37 additions & 4 deletions database/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const (
var (
ErrUnexpectedRowsAffected = errors.New("unexpected rows affected")
ErrAttestationUpdateRejected = errors.New("attestation update rejected")

errEventIteratorInterrupted = errors.New("interrupted")
errEventIteratorInterrupted = errors.New("interrupted")

notifyExpiredEvents func(ctx context.Context, events ...*model.Event) error
)
Expand All @@ -39,6 +38,7 @@ type databaseEvent struct {
KeyAlg string
MasterPubKey string
Dtag string
Htag string
}

type databaseBatchRequest struct {
Expand Down Expand Up @@ -68,6 +68,7 @@ func (req *databaseBatchRequest) Save(e *model.Event) error {
SigAlg: sigAlg,
KeyAlg: keyAlg,
Dtag: e.Tags.GetD(),
Htag: e.GetHTag(),
})

return nil
Expand Down Expand Up @@ -97,6 +98,15 @@ func (db *dbClient) AcceptEvents(ctx context.Context, events ...*model.Event) er
}

if events[i].Kind == nostr.KindDeletion {
communityFilters, err := db.prepareCommunityDeleteFilters(ctx, events[i])
if err != nil {
return err
}
if len(communityFilters) > 0 {
req.Delete = append(req.Delete, communityFilters...)

continue
}
if err := req.Remove(events[i]); err != nil {
return err
}
Expand Down Expand Up @@ -139,9 +149,9 @@ func (db *dbClient) deleteEvents(ctx context.Context, filters []databaseFilterDe

func (db *dbClient) saveEvents(ctx context.Context, events []databaseEvent) error {
const stmt = `insert into events
(kind, created_at, system_created_at, id, pubkey, master_pubkey, sig, sig_alg, key_alg, content, tags, d_tag, reference_id)
(kind, created_at, system_created_at, id, pubkey, master_pubkey, sig, sig_alg, key_alg, content, tags, d_tag, h_tag, reference_id)
values
(:kind, :created_at, :system_created_at, :id, :pubkey, :master_pubkey, :sig, :sig_alg, :key_alg, :content, :jtags, :d_tag, :reference_id)
(:kind, :created_at, :system_created_at, :id, :pubkey, :master_pubkey, :sig, :sig_alg, :key_alg, :content, :jtags, :d_tag, :h_tag, :reference_id)
on conflict do update set
id = excluded.id,
kind = excluded.kind,
Expand All @@ -155,6 +165,7 @@ on conflict do update set
content = excluded.content,
tags = excluded.tags,
d_tag = excluded.d_tag,
h_tag = excluded.h_tag,
reference_id = excluded.reference_id,
hidden = 0
`
Expand Down Expand Up @@ -422,6 +433,7 @@ with eventsmain as (
e.sig,
e.content,
e.d_tag,
e.h_tag,
tags as jtags
from
events e
Expand Down Expand Up @@ -494,3 +506,24 @@ func (db *dbClient) deleteExpiredEvents(ctx context.Context) error {
}
return err
}

func (db *dbClient) prepareCommunityDeleteFilters(ctx context.Context, incomingEvent *model.Event) (filters []databaseFilterDelete, err error) {
var ids []string
for _, eTag := range incomingEvent.Tags.GetAll([]string{"e"}) {
if eTag.Key() == "e" {
ids = append(ids, eTag.Value())
}
}
filters = make([]databaseFilterDelete, 0)
for ev := range db.SelectEvents(ctx, model.Filter{IDs: ids}) {
if hTag := ev.GetTag("h"); hTag == nil {
continue
}
filters = append(filters, databaseFilterDelete{
Author: ev.GetMasterPublicKey(),
IDs: []string{ev.GetID()},
})
}

return filters, nil
}
5 changes: 3 additions & 2 deletions database/query/query_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ func TestQueryFuzzInsertEvents(t *testing.T) {

t.Run("Explain", func(t *testing.T) {
const sql = `explain query plan insert into events
(kind, created_at, system_created_at, id, pubkey, master_pubkey, sig, sig_alg, key_alg, content, tags, d_tag, reference_id)
(kind, created_at, system_created_at, id, pubkey, master_pubkey, sig, sig_alg, key_alg, content, tags, d_tag, h_tag, reference_id)
values
(:kind, :created_at, :system_created_at, :id, :pubkey, :master_pubkey, :sig, :sig_alg, :key_alg, :content, :jtags, :d_tag, :reference_id)
(:kind, :created_at, :system_created_at, :id, :pubkey, :master_pubkey, :sig, :sig_alg, :key_alg, :content, :jtags, :d_tag, :h_tag, :reference_id)
on conflict do update set
id = excluded.id,
kind = excluded.kind,
Expand All @@ -288,6 +288,7 @@ func TestQueryFuzzInsertEvents(t *testing.T) {
content = excluded.content,
tags = excluded.tags,
d_tag = excluded.d_tag,
h_tag = excluded.h_tag,
reference_id = excluded.reference_id,
hidden = 0
`
Expand Down
3 changes: 3 additions & 0 deletions database/query/query_where_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ select
w.WriteString(`cast(f.value as text) as content,`)
}
w.WriteString(`json_array(json_object('kinds', json_array(:` + (filterID + "fkind") + `),:` + (filterID + "ftagname") + `,json_array(f.reference_id))) as d_tag,
h_tag,
case when
f.kind = 7 then
json_array(
Expand Down Expand Up @@ -484,6 +485,7 @@ select
e.sig,
e.content,
e.d_tag,
e.h_tag,
tags as jtags
from
events e
Expand Down Expand Up @@ -560,6 +562,7 @@ select
'' as sig,
'' as content,
'' as d_tag,
'' as h_tag,
'[]' as jtags
from
events e
Expand Down
3 changes: 2 additions & 1 deletion dvm/dvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/ice-blockchain/subzero/cfg"
"github.com/ice-blockchain/subzero/model"
"github.com/ice-blockchain/subzero/validation"
)

type (
Expand Down Expand Up @@ -91,7 +92,7 @@ func (d *dvm) AcceptJob(ctx context.Context, event *model.Event) error {
return nil
}

if err := event.Validate(); err != nil {
if err := validation.Validate(ctx, event); err != nil {
return errors.Wrapf(err, "wrong dvm job: %v", event)
}

Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.24
github.com/mitchellh/mapstructure v1.5.0
github.com/mxschmitt/golang-combinations v1.2.0
github.com/nbd-wtf/go-nostr v0.44.1
github.com/nbd-wtf/go-nostr v0.45.0
github.com/puzpuzpuz/xsync/v3 v3.4.0
github.com/quic-go/quic-go v0.48.2
github.com/quic-go/webtransport-go v0.8.1-0.20241018022711-4ac2c9250e66
Expand All @@ -34,7 +34,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
github.com/syndtr/goleveldb v1.0.0
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/u2takey/ffmpeg-go v0.5.0
github.com/xssnick/tonutils-go v1.10.2
github.com/xssnick/tonutils-storage v0.6.5
Expand All @@ -48,6 +48,7 @@ require (
atomicgo.dev/schedule v0.1.0 // indirect
github.com/aws/aws-sdk-go v1.55.5 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/btcutil v1.1.6 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/bytedance/sonic v1.12.6 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
Expand Down Expand Up @@ -127,7 +128,7 @@ require (
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.28.0 // indirect
google.golang.org/protobuf v1.36.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 78867d2

Please sign in to comment.