mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-30 01:26:32 +00:00
refactored kind server responses
This commit is contained in:
parent
77f3e0314d
commit
ffbfe76b42
@ -17,17 +17,20 @@ import (
|
|||||||
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
||||||
if len(message) != 2 {
|
if len(message) != 2 {
|
||||||
fmt.Println("Invalid EVENT message format")
|
fmt.Println("Invalid EVENT message format")
|
||||||
|
response.SendNotice(ws, "", "Invalid EVENT message format")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
eventData, ok := message[1].(map[string]interface{})
|
eventData, ok := message[1].(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Println("Invalid event data format")
|
fmt.Println("Invalid event data format")
|
||||||
|
response.SendNotice(ws, "", "Invalid event data format")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
eventBytes, err := json.Marshal(eventData)
|
eventBytes, err := json.Marshal(eventData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error marshaling event data:", err)
|
fmt.Println("Error marshaling event data:", err)
|
||||||
|
response.SendNotice(ws, "", "Error marshaling event data")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,54 +38,33 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
|||||||
err = json.Unmarshal(eventBytes, &evt)
|
err = json.Unmarshal(eventBytes, &evt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error unmarshaling event data:", err)
|
fmt.Println("Error unmarshaling event data:", err)
|
||||||
|
response.SendNotice(ws, "", "Error unmarshaling event data")
|
||||||
return
|
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)
|
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) {
|
if !utils.CheckSignature(evt) {
|
||||||
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
|
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
collection := db.GetCollection(evt.Kind)
|
collection := db.GetCollection(evt.Kind)
|
||||||
|
|
||||||
rateLimiter := utils.GetRateLimiter()
|
rateLimiter := utils.GetRateLimiter()
|
||||||
sizeLimiter := utils.GetSizeLimiter()
|
sizeLimiter := utils.GetSizeLimiter()
|
||||||
var category string
|
|
||||||
switch {
|
category := determineCategory(evt.Kind)
|
||||||
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {
|
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {
|
||||||
response.SendOK(ws, evt.ID, false, msg)
|
response.SendOK(ws, evt.ID, false, msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
eventSize := len(eventBytes) // Calculate event size
|
|
||||||
if allowed, msg := sizeLimiter.AllowSize(evt.Kind, eventSize); !allowed {
|
if allowed, msg := sizeLimiter.AllowSize(evt.Kind, eventSize); !allowed {
|
||||||
response.SendOK(ws, evt.ID, false, msg)
|
response.SendOK(ws, evt.ID, false, msg)
|
||||||
return
|
return
|
||||||
@ -93,15 +75,15 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventB
|
|||||||
case evt.Kind == 0:
|
case evt.Kind == 0:
|
||||||
err = kinds.HandleKind0(ctx, evt, collection, ws)
|
err = kinds.HandleKind0(ctx, evt, collection, ws)
|
||||||
case evt.Kind == 1:
|
case evt.Kind == 1:
|
||||||
err = kinds.HandleKind1(ctx, evt, collection)
|
err = kinds.HandleKind1(ctx, evt, collection, ws)
|
||||||
case evt.Kind == 2:
|
case evt.Kind == 2:
|
||||||
err = kinds.HandleKind2Deprecated(ctx, evt, ws)
|
err = kinds.HandleKind2(ctx, evt, ws)
|
||||||
case evt.Kind == 3:
|
case evt.Kind == 3:
|
||||||
err = kinds.HandleReplaceableKind(ctx, evt, collection, ws)
|
err = kinds.HandleReplaceableKind(ctx, evt, collection, ws)
|
||||||
case evt.Kind >= 4 && evt.Kind < 45:
|
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:
|
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:
|
case evt.Kind >= 10000 && evt.Kind < 20000:
|
||||||
err = kinds.HandleReplaceableKind(ctx, evt, collection, ws)
|
err = kinds.HandleReplaceableKind(ctx, evt, collection, ws)
|
||||||
case evt.Kind >= 20000 && evt.Kind < 30000:
|
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, "")
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error {
|
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}
|
filter := bson.M{"pubkey": evt.PubKey}
|
||||||
var existingEvent relay.Event
|
var existingEvent relay.Event
|
||||||
err := collection.FindOne(ctx, filter).Decode(&existingEvent)
|
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)
|
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 err != mongo.ErrNoDocuments {
|
||||||
if existingEvent.CreatedAt >= evt.CreatedAt {
|
if existingEvent.CreatedAt >= evt.CreatedAt {
|
||||||
// If the existing event is newer or the same, respond with a NOTICE
|
response.SendNotice(ws, evt.PubKey, "blocked: a newer kind 0 event already exists for this pubkey")
|
||||||
response.SendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey")
|
response.SendOK(ws, evt.ID, false, "blocked: a newer kind 0 event already exists for this pubkey")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the existing event if it has the same pubkey
|
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"id": evt.ID,
|
"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)
|
_, err = collection.UpdateOne(ctx, filter, update, opts)
|
||||||
if err != nil {
|
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)
|
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)
|
fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
|
// kinds/kind1.go
|
||||||
package kinds
|
package kinds
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"grain/relay/handlers/response"
|
||||||
relay "grain/relay/types"
|
relay "grain/relay/types"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleKind1(ctx context.Context, evt relay.Event, collection *mongo.Collection) error {
|
func HandleKind1(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error {
|
||||||
// Insert event into MongoDB
|
|
||||||
_, err := collection.InsertOne(ctx, evt)
|
_, err := collection.InsertOne(ctx, evt)
|
||||||
if err != nil {
|
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)
|
return fmt.Errorf("error inserting event into MongoDB: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Inserted event kind 1 into MongoDB:", evt.ID)
|
fmt.Println("Inserted event kind 1 into MongoDB:", evt.ID)
|
||||||
|
response.SendOK(ws, evt.ID, true, "")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,12 @@ import (
|
|||||||
"golang.org/x/net/websocket"
|
"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")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -3,17 +3,21 @@ package kinds
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"grain/relay/handlers/response"
|
||||||
relay "grain/relay/types"
|
relay "grain/relay/types"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"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)
|
_, err := collection.InsertOne(ctx, evt)
|
||||||
if err != nil {
|
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)
|
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)
|
fmt.Printf("Inserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID)
|
||||||
|
response.SendOK(ws, evt.ID, true, "")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,8 @@ func HandleReplaceableKind(ctx context.Context, evt relay.Event, collection *mon
|
|||||||
|
|
||||||
if err != mongo.ErrNoDocuments {
|
if err != mongo.ErrNoDocuments {
|
||||||
if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -33,9 +34,11 @@ func HandleReplaceableKind(ctx context.Context, evt relay.Event, collection *mon
|
|||||||
}
|
}
|
||||||
_, err = collection.UpdateOne(ctx, filter, update, opts)
|
_, err = collection.UpdateOne(ctx, filter, update, opts)
|
||||||
if err != nil {
|
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)
|
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)
|
fmt.Printf("Upserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID)
|
||||||
|
response.SendOK(ws, evt.ID, true, "")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,8 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co
|
|||||||
|
|
||||||
if err != mongo.ErrNoDocuments {
|
if err != mongo.ErrNoDocuments {
|
||||||
if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,9 +41,11 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co
|
|||||||
}
|
}
|
||||||
_, err = collection.UpdateOne(ctx, filter, update, opts)
|
_, err = collection.UpdateOne(ctx, filter, update, opts)
|
||||||
if err != nil {
|
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)
|
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)
|
fmt.Printf("Upserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID)
|
||||||
|
response.SendOK(ws, evt.ID, true, "")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
func HandleUnknownKind(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error {
|
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
|
// 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 as there's no error in the process, just that the event is not accepted
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user