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

refactor(nip-01): accepting 1 filter per request. #100

Closed
wants to merge 5 commits into from
Closed
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 *kraken.GetConfigResponse) 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
40 changes: 19 additions & 21 deletions handler/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,31 @@ type filterQuery struct {
Limit uint32
}

func (h *Handler) HandleReq(fs filter.Filters) ([]event.Event, error) {
func (h *Handler) HandleReq(f *filter.Filter) ([]event.Event, error) {
ctx, cancel := context.WithTimeout(context.Background(), h.db.QueryTimeout)
defer cancel()

queryKinds := make(map[types.Kind][]filterQuery)

for _, f := range fs {
qf := filterQuery{
Tags: f.Tags,
Authors: f.Authors,
IDs: f.IDs,
Since: f.Since,
Until: f.Until,
Limit: f.Limit,
}
qf := filterQuery{
Tags: f.Tags,
Authors: f.Authors,
IDs: f.IDs,
Since: f.Since,
Until: f.Until,
Limit: f.Limit,
}

if len(f.Kinds) != 0 {
uniqueKinds := removeDuplicateKind(f.Kinds)
for _, k := range uniqueKinds {
queryKinds[k] = append(queryKinds[k], qf)
}
} else {
//! we query most requested kinds if there is no kind provided.
// FIX: any better way?
for _, k := range possibleKinds {
queryKinds[k] = append(queryKinds[k], qf)
}
if len(f.Kinds) != 0 {
uniqueKinds := removeDuplicateKind(f.Kinds)
for _, k := range uniqueKinds {
queryKinds[k] = append(queryKinds[k], qf)
}
} else {
//! we query most requested kinds if there is no kind provided.
// FIX: any better way?
for _, k := range possibleKinds {
queryKinds[k] = append(queryKinds[k], qf)
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/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 server/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
9 changes: 6 additions & 3 deletions server/websocket/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) { //nolint

expirationTag := msg.Event.Tags.GetValue("expiration")

if expirationTag != "" {
if expirationTag != "" &&
expirationTag != "0" &&
!msg.Event.Kind.IsEphemeral() {
expiration, err := strconv.ParseInt(expirationTag, 10, 64)
if err != nil {
okm := message.MakeOK(false,
Expand Down Expand Up @@ -266,7 +268,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) { //nolint
return
}

if !msg.Event.Kind.IsEphemeral() {
if !msg.Event.Kind.IsEphemeral() && expirationTag != "0" {
err := s.handler.HandleEvent(msg.Event)
if err != nil {
okm := message.MakeOK(false,
Expand All @@ -280,9 +282,10 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) { //nolint

return
}
_ = conn.WriteMessage(1, message.MakeOK(true, msg.Event.ID, ""))
}

_ = conn.WriteMessage(1, message.MakeOK(true, msg.Event.ID, ""))

_, err = s.redis.Client.BFAdd(qCtx, s.redis.BloomFilterName, eID[:]).Result()
if err != nil {
log.Printf("error: adding event to bloom filter.")
Expand Down
13 changes: 2 additions & 11 deletions server/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 server/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,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
16 changes: 6 additions & 10 deletions types/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (em Event) EncodeToJSON() ([]byte, error) {
// Req represents a NIP-01 REQ message.
type Req struct {
SubscriptionID string
filter.Filters
filter.Filter
}

func (Req) Type() string { return "REQ" }
Expand All @@ -140,21 +140,17 @@ func (rm *Req) DecodeFromJSON(data []byte) error {
}
}
rm.SubscriptionID = arr[1].Str
rm.Filters = make(filter.Filters, len(arr)-2)
f := 0
for i := 2; i < len(arr); i++ {
if err := easyjson.Unmarshal([]byte(arr[i].Raw), &rm.Filters[f]); err != nil {
return types.DecodeError{
Reason: fmt.Sprintf("REQ message: %s", err.Error()),
}
rm.Filter = filter.Filter{}
if err := easyjson.Unmarshal([]byte(arr[2].Raw), &rm.Filter); err != nil {
return types.DecodeError{
Reason: fmt.Sprintf("REQ message: %s", err.Error()),
}
f++
}

return nil
}

func (rm Req) EncodeToJSON() ([]byte, error) {
func (rm *Req) EncodeToJSON() ([]byte, error) {
return nil, nil
}

Expand Down
6 changes: 3 additions & 3 deletions types/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ var testCases = []testCase{
},
{
Name: "REQ message",
Message: []byte(`["REQ","million", {"kinds": [1]}, {"kinds": [30023 ], "#d": ["buteko", "batuke"]}]`),
Message: []byte(`["REQ","million", {"kinds": [30023 ], "#d": ["buteko", "batuke"]}]`),
ExpectedEnvelope: &message.Req{
SubscriptionID: "million",
Filters: filter.Filters{{Kinds: []types.Kind{1}}, {
Filter: filter.Filter{
Kinds: []types.Kind{30023},
Tags: map[string][]string{"d": {"buteko", "batuke"}},
}},
},
},
},
{
Expand Down
Loading