From ffbfe76b4265c081588e5948a9f548c3440e9693 Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Sat, 27 Jul 2024 10:06:34 -0400 Subject: [PATCH] refactored kind server responses --- relay/handlers/event.go | 61 +++++++++---------- relay/handlers/kinds/kind0.go | 12 ++-- relay/handlers/kinds/kind1.go | 9 ++- relay/handlers/kinds/kind2.go | 7 ++- relay/handlers/kinds/regular.go | 8 ++- relay/handlers/kinds/replaceable.go | 5 +- relay/handlers/kinds/replaceableParameters.go | 5 +- relay/handlers/kinds/unknown.go | 2 +- 8 files changed, 62 insertions(+), 47 deletions(-) diff --git a/relay/handlers/event.go b/relay/handlers/event.go index a78e1b9..d037192 100644 --- a/relay/handlers/event.go +++ b/relay/handlers/event.go @@ -17,17 +17,20 @@ import ( func HandleEvent(ws *websocket.Conn, message []interface{}) { if len(message) != 2 { fmt.Println("Invalid EVENT message format") + response.SendNotice(ws, "", "Invalid EVENT message format") return } eventData, ok := message[1].(map[string]interface{}) if !ok { fmt.Println("Invalid event data format") + response.SendNotice(ws, "", "Invalid event data format") return } eventBytes, err := json.Marshal(eventData) if err != nil { fmt.Println("Error marshaling event data:", err) + response.SendNotice(ws, "", "Error marshaling event data") return } @@ -35,54 +38,33 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) { err = json.Unmarshal(eventBytes, &evt) if err != nil { fmt.Println("Error unmarshaling event data:", err) + response.SendNotice(ws, "", "Error unmarshaling event data") return } - HandleKind(context.TODO(), evt, ws, eventBytes) + eventSize := len(eventBytes) // Calculate event size + HandleKind(context.TODO(), evt, ws, eventSize) fmt.Println("Event processed:", evt.ID) } -func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventBytes []byte) { +func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventSize int) { if !utils.CheckSignature(evt) { response.SendOK(ws, evt.ID, false, "invalid: signature verification failed") return } collection := db.GetCollection(evt.Kind) - rateLimiter := utils.GetRateLimiter() sizeLimiter := utils.GetSizeLimiter() - var category string - switch { - case evt.Kind == 0: - category = "replaceable" - case evt.Kind == 1: - category = "regular" - case evt.Kind == 2: - category = "deprecated" - case evt.Kind == 3: - category = "replaceable" - case evt.Kind >= 4 && evt.Kind < 45: - category = "regular" - case evt.Kind >= 1000 && evt.Kind < 10000: - category = "regular" - case evt.Kind >= 10000 && evt.Kind < 20000: - category = "replaceable" - case evt.Kind >= 20000 && evt.Kind < 30000: - category = "ephemeral" - case evt.Kind >= 30000 && evt.Kind < 40000: - category = "parameterized_replaceable" - default: - category = "unknown" - } + + category := determineCategory(evt.Kind) if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed { response.SendOK(ws, evt.ID, false, msg) return } - eventSize := len(eventBytes) // Calculate event size if allowed, msg := sizeLimiter.AllowSize(evt.Kind, eventSize); !allowed { response.SendOK(ws, evt.ID, false, msg) return @@ -93,15 +75,15 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventB case evt.Kind == 0: err = kinds.HandleKind0(ctx, evt, collection, ws) case evt.Kind == 1: - err = kinds.HandleKind1(ctx, evt, collection) + err = kinds.HandleKind1(ctx, evt, collection, ws) case evt.Kind == 2: - err = kinds.HandleKind2Deprecated(ctx, evt, ws) + err = kinds.HandleKind2(ctx, evt, ws) case evt.Kind == 3: err = kinds.HandleReplaceableKind(ctx, evt, collection, ws) case evt.Kind >= 4 && evt.Kind < 45: - err = kinds.HandleRegularKind(ctx, evt, collection) + err = kinds.HandleRegularKind(ctx, evt, collection, ws) case evt.Kind >= 1000 && evt.Kind < 10000: - err = kinds.HandleRegularKind(ctx, evt, collection) + err = kinds.HandleRegularKind(ctx, evt, collection, ws) case evt.Kind >= 10000 && evt.Kind < 20000: err = kinds.HandleReplaceableKind(ctx, evt, collection, ws) case evt.Kind >= 20000 && evt.Kind < 30000: @@ -119,3 +101,20 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventB response.SendOK(ws, evt.ID, true, "") } + +func determineCategory(kind int) string { + switch { + case kind == 0, kind == 3, kind >= 10000 && kind < 20000: + return "replaceable" + case kind == 1, kind >= 4 && kind < 45, kind >= 1000 && kind < 10000: + return "regular" + case kind == 2: + return "deprecated" + case kind >= 20000 && kind < 30000: + return "ephemeral" + case kind >= 30000 && kind < 40000: + return "parameterized_replaceable" + default: + return "unknown" + } +} diff --git a/relay/handlers/kinds/kind0.go b/relay/handlers/kinds/kind0.go index 79c98da..8fd6401 100644 --- a/relay/handlers/kinds/kind0.go +++ b/relay/handlers/kinds/kind0.go @@ -13,7 +13,6 @@ import ( ) func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { - // Find the existing event with the same pubkey filter := bson.M{"pubkey": evt.PubKey} var existingEvent relay.Event err := collection.FindOne(ctx, filter).Decode(&existingEvent) @@ -21,16 +20,14 @@ func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collect return fmt.Errorf("error finding existing event: %v", err) } - // If an existing event is found, compare the created_at times if err != mongo.ErrNoDocuments { if existingEvent.CreatedAt >= evt.CreatedAt { - // If the existing event is newer or the same, respond with a NOTICE - response.SendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey") + response.SendNotice(ws, evt.PubKey, "blocked: a newer kind 0 event already exists for this pubkey") + response.SendOK(ws, evt.ID, false, "blocked: a newer kind 0 event already exists for this pubkey") return nil } } - // Replace the existing event if it has the same pubkey update := bson.M{ "$set": bson.M{ "id": evt.ID, @@ -42,12 +39,13 @@ func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collect }, } - opts := options.Update().SetUpsert(true) // Insert if not found + opts := options.Update().SetUpsert(true) _, err = collection.UpdateOne(ctx, filter, update, opts) if err != nil { + response.SendOK(ws, evt.ID, false, "error: could not connect to the database") return fmt.Errorf("error updating/inserting event kind 0 into MongoDB: %v", err) } - + response.SendOK(ws, evt.ID, true, "") fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID) return nil } diff --git a/relay/handlers/kinds/kind1.go b/relay/handlers/kinds/kind1.go index fb38790..78c3611 100644 --- a/relay/handlers/kinds/kind1.go +++ b/relay/handlers/kinds/kind1.go @@ -1,21 +1,24 @@ +// kinds/kind1.go package kinds import ( "context" "fmt" - + "grain/relay/handlers/response" relay "grain/relay/types" "go.mongodb.org/mongo-driver/mongo" + "golang.org/x/net/websocket" ) -func HandleKind1(ctx context.Context, evt relay.Event, collection *mongo.Collection) error { - // Insert event into MongoDB +func HandleKind1(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { _, err := collection.InsertOne(ctx, evt) if err != nil { + response.SendOK(ws, evt.ID, false, "error: could not connect to the database") return fmt.Errorf("error inserting event into MongoDB: %v", err) } fmt.Println("Inserted event kind 1 into MongoDB:", evt.ID) + response.SendOK(ws, evt.ID, true, "") return nil } diff --git a/relay/handlers/kinds/kind2.go b/relay/handlers/kinds/kind2.go index db33c86..a8c2869 100644 --- a/relay/handlers/kinds/kind2.go +++ b/relay/handlers/kinds/kind2.go @@ -8,7 +8,12 @@ import ( "golang.org/x/net/websocket" ) -func HandleKind2Deprecated(ctx context.Context, evt relay.Event, ws *websocket.Conn) error { +func HandleKind2(ctx context.Context, evt relay.Event, ws *websocket.Conn) error { + // Send a NOTICE message to inform the client about the deprecation response.SendNotice(ws, evt.PubKey, "kind 2 is deprecated, event not accepted to the relay, please use kind 10002 as defined in NIP-65") + + // Send an OK message to indicate the event was not accepted + response.SendOK(ws, evt.ID, false, "invalid: kind 2 is deprecated, use kind 10002 (NIP65)") + return nil } diff --git a/relay/handlers/kinds/regular.go b/relay/handlers/kinds/regular.go index 4035315..86c7d65 100644 --- a/relay/handlers/kinds/regular.go +++ b/relay/handlers/kinds/regular.go @@ -3,17 +3,21 @@ package kinds import ( "context" "fmt" + "grain/relay/handlers/response" relay "grain/relay/types" "go.mongodb.org/mongo-driver/mongo" + "golang.org/x/net/websocket" ) -func HandleRegularKind(ctx context.Context, evt relay.Event, collection *mongo.Collection) error { +func HandleRegularKind(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { _, err := collection.InsertOne(ctx, evt) if err != nil { + response.SendOK(ws, evt.ID, false, "error: could not connect to the database") return fmt.Errorf("error inserting event kind %d into MongoDB: %v", evt.Kind, err) } fmt.Printf("Inserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID) + response.SendOK(ws, evt.ID, true, "") return nil -} \ No newline at end of file +} diff --git a/relay/handlers/kinds/replaceable.go b/relay/handlers/kinds/replaceable.go index 0214956..e07bf54 100644 --- a/relay/handlers/kinds/replaceable.go +++ b/relay/handlers/kinds/replaceable.go @@ -22,7 +22,8 @@ func HandleReplaceableKind(ctx context.Context, evt relay.Event, collection *mon if err != mongo.ErrNoDocuments { if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) { - response.SendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey") + response.SendNotice(ws, evt.PubKey, "blocked: relay already has a newer event of the same kind with this pubkey") + response.SendOK(ws, evt.ID, false, "blocked: relay already has a newer event of the same kind with this pubkey") return nil } } @@ -33,9 +34,11 @@ func HandleReplaceableKind(ctx context.Context, evt relay.Event, collection *mon } _, err = collection.UpdateOne(ctx, filter, update, opts) if err != nil { + response.SendOK(ws, evt.ID, false, "error: could not connect to the database") return fmt.Errorf("error updating/inserting event kind %d into MongoDB: %v", evt.Kind, err) } fmt.Printf("Upserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID) + response.SendOK(ws, evt.ID, true, "") return nil } diff --git a/relay/handlers/kinds/replaceableParameters.go b/relay/handlers/kinds/replaceableParameters.go index 49be030..1b4c7c5 100644 --- a/relay/handlers/kinds/replaceableParameters.go +++ b/relay/handlers/kinds/replaceableParameters.go @@ -29,7 +29,8 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co if err != mongo.ErrNoDocuments { if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) { - response.SendNotice(ws, evt.PubKey, "relay already has a newer event for this pubkey and d tag") + response.SendNotice(ws, evt.PubKey, "blocked: relay already has a newer event for this pubkey and d tag") + response.SendOK(ws, evt.ID, false, "blocked: relay already has a newer event for this pubkey and d tag") return nil } } @@ -40,9 +41,11 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co } _, err = collection.UpdateOne(ctx, filter, update, opts) if err != nil { + response.SendOK(ws, evt.ID, false, "error: could not connect to the database") return fmt.Errorf("error updating/inserting event kind %d into MongoDB: %v", evt.Kind, err) } fmt.Printf("Upserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID) + response.SendOK(ws, evt.ID, true, "") return nil } diff --git a/relay/handlers/kinds/unknown.go b/relay/handlers/kinds/unknown.go index 79a2499..acb2e54 100644 --- a/relay/handlers/kinds/unknown.go +++ b/relay/handlers/kinds/unknown.go @@ -11,7 +11,7 @@ import ( func HandleUnknownKind(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { // Respond with an OK message indicating the event is not accepted - response.SendOK(ws, evt.ID, false, "kind is unknown and not accepted") + response.SendOK(ws, evt.ID, false, "invalid: kind is outside the ranges defined in NIP01") // Return nil as there's no error in the process, just that the event is not accepted return nil