Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supporting nip-09, accepting 1 filter per req message, using aggregation pipeline on req. #110

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (c *Config) LoadParameters(params *mpb.GetParametersResponse) error {
c.WebsocketServer.Limitation = &websocket.Limitation{
MaxMessageLength: params.Limitations.MaxMessageLength,
MaxSubscriptions: params.Limitations.MaxSubscriptions,
MaxFilters: params.Limitations.MaxFilters,
MaxSubidLength: params.Limitations.MaxSubidLength,
MinPowDifficulty: params.Limitations.MinPowDifficulty,
AuthRequired: params.Limitations.AuthRequired,
Expand Down
2 changes: 1 addition & 1 deletion delivery/websocket/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ type clientState struct {
challenge string
pubkey *string
isKnown *bool
subs map[string]filter.Filters
subs map[string]filter.Filter
*sync.RWMutex
}
1 change: 0 additions & 1 deletion delivery/websocket/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "net/url"
type Limitation struct {
MaxMessageLength int32 // todo?.
MaxSubscriptions int32
MaxFilters int32
MaxSubidLength int32
MinPowDifficulty int32
AuthRequired bool
Expand Down
60 changes: 56 additions & 4 deletions delivery/websocket/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"github.com/dezh-tech/immortal/infrastructure/redis"
"github.com/dezh-tech/immortal/pkg/logger"
"github.com/dezh-tech/immortal/pkg/utils"
"github.com/dezh-tech/immortal/types"
"github.com/dezh-tech/immortal/types/filter"
"github.com/dezh-tech/immortal/types/message"
"github.com/gorilla/websocket"
gredis "github.com/redis/go-redis/v9"
)

// handleEvent handles new incoming EVENT messages from client.
func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {
func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) { //nolint
s.mu.Lock()
defer s.mu.Unlock()
defer measureLatency(s.metrics.EventLatency)()
Expand Down Expand Up @@ -172,9 +174,59 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {
return
}

if !msg.Event.Kind.IsEphemeral() {
err := s.handler.HandleEvent(msg.Event)
if err != nil {
if !msg.Event.Kind.IsEphemeral() { //nolint
if msg.Event.Kind == types.KindEventDeletionRequest {
deleteFilterString := msg.Event.Tags.GetValue("filter")
kehiy marked this conversation as resolved.
Show resolved Hide resolved

deleteFilter, err := filter.Decode([]byte(deleteFilterString))
if err != nil {
okm := message.MakeOK(false,
msg.Event.ID,
fmt.Sprintf("error: parse deletion event: %s", deleteFilterString),
)

_ = conn.WriteMessage(1, okm)

status = invalidFail

return
}

// you can only delete events you own.
if len(deleteFilter.Authors) == 1 {
if deleteFilter.Authors[0] != msg.Event.PublicKey {
okm := message.MakeOK(false,
msg.Event.ID,
fmt.Sprintf(
"error: you can request to delete your events only: %s",
deleteFilter.Authors),
)

_ = conn.WriteMessage(1, okm)

status = invalidFail

return
}
} else {
okm := message.MakeOK(false,
msg.Event.ID,
fmt.Sprintf(
"error: you can request to delete your events only: %s",
deleteFilter.Authors),
)

_ = conn.WriteMessage(1, okm)

status = invalidFail

return
}

go s.handler.DeleteByFilter(deleteFilter) //nolint
}

if err := s.handler.HandleEvent(msg.Event); err != nil {
okm := message.MakeOK(false,
msg.Event.ID,
"error: can't write event to database.",
Expand Down
13 changes: 2 additions & 11 deletions delivery/websocket/req_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ func (s *Server) handleReq(conn *websocket.Conn, m message.Message) {
return
}

if len(msg.Filters) >= int(s.config.Limitation.MaxFilters) {
_ = conn.WriteMessage(1, message.MakeNotice(fmt.Sprintf("error: max limit of filters is: %d",
s.config.Limitation.MaxFilters)))

status = limitsFail

return
}

if len(msg.SubscriptionID) >= int(s.config.Limitation.MaxSubidLength) {
_ = conn.WriteMessage(1, message.MakeNotice(fmt.Sprintf("error: max limit of sub id is: %d",
s.config.Limitation.MaxSubidLength)))
Expand All @@ -82,7 +73,7 @@ func (s *Server) handleReq(conn *websocket.Conn, m message.Message) {
return
}

res, err := s.handler.HandleReq(msg.Filters)
res, err := s.handler.HandleReq(&msg.Filter)
if err != nil {
_ = conn.WriteMessage(1, message.MakeNotice(fmt.Sprintf("error: can't process REQ message: %s", err.Error())))
status = databaseFail
Expand All @@ -99,6 +90,6 @@ func (s *Server) handleReq(conn *websocket.Conn, m message.Message) {

client.Lock()
s.metrics.Subscriptions.Inc()
client.subs[msg.SubscriptionID] = msg.Filters
client.subs[msg.SubscriptionID] = msg.Filter
client.Unlock()
}
2 changes: 1 addition & 1 deletion delivery/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.conns[conn] = clientState{
pubkey: &pubkey,
isKnown: &known,
subs: make(map[string]filter.Filters),
subs: make(map[string]filter.Filter),
RWMutex: &sync.RWMutex{},
}

Expand Down
2 changes: 1 addition & 1 deletion documents/NIPs.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Immortal supported NIPs

- [X] **NIP-01**: Basic Protocol Flow Description
- [ ] **NIP-09**: Event Deletion Request
- [X] **NIP-09**: Event Deletion Request
- [X] **NIP-11**: Relay Information Document
- [X] **NIP-13**: Proof of Work
- [X] **NIP-40**: Expiration Timestamp
Expand Down
110 changes: 50 additions & 60 deletions infrastructure/grpc_client/gen/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions infrastructure/grpc_client/proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ service Parameters {

message Limitations {
int32 max_message_length = 1;
int32 max_subscriptions = 2;
int32 max_filters = 3;
int32 max_subscriptions = 2;
int32 max_subid_length = 4;
int32 min_pow_difficulty = 5;
bool auth_required = 6;
Expand Down
1 change: 0 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ devtools:
go install mvdan.cc/gofumpt@latest
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
go install github.com/pactus-project/protoc-gen-doc/cmd/[email protected]
go install github.com/bufbuild/buf/cmd/[email protected]

### Testing
Expand Down
Loading
Loading