From 2c2f91965d2027585ebe585f6b1b5181914efca2 Mon Sep 17 00:00:00 2001 From: pablomendezroyo <41727368+pablomendezroyo@users.noreply.github.com> Date: Wed, 15 May 2024 11:48:31 +0200 Subject: [PATCH] redo mongo db standard (#38) --- listener/internal/api/api.go | 2 +- .../internal/api/handlers/postNewSignature.go | 32 ++++++++++--------- .../{connection.go => GetMongoDbClient.go} | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) rename listener/internal/mongodb/{connection.go => GetMongoDbClient.go} (96%) diff --git a/listener/internal/api/api.go b/listener/internal/api/api.go index de0ab52..75c194e 100644 --- a/listener/internal/api/api.go +++ b/listener/internal/api/api.go @@ -37,7 +37,7 @@ func (s *httpApi) Start() { var err error // connect to the MongoDB server - dbClient, err := mongodb.ConnectMongoDB(s.dbUri) + dbClient, err := mongodb.GetMongoDbClient(s.dbUri) if err != nil { logger.Fatal("Failed to connect to MongoDB: " + err.Error()) } diff --git a/listener/internal/api/handlers/postNewSignature.go b/listener/internal/api/handlers/postNewSignature.go index f337eef..3447d38 100644 --- a/listener/internal/api/handlers/postNewSignature.go +++ b/listener/internal/api/handlers/postNewSignature.go @@ -4,13 +4,13 @@ import ( "context" "encoding/json" "net/http" - "sync" "github.com/dappnode/validator-monitoring/listener/internal/api/types" "github.com/dappnode/validator-monitoring/listener/internal/api/validation" "github.com/dappnode/validator-monitoring/listener/internal/logger" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) // Posting a new singature consists in the following steps: @@ -77,26 +77,28 @@ func PostNewSignature(w http.ResponseWriter, r *http.Request, dbCollection *mong return } - // Iterate over all active validators and validate and insert the signature - dbMutex := new(sync.Mutex) // Mutex for database operations + // Iterate over all valid signatures and insert the signature for _, req := range validSignatures { - dbMutex.Lock() - // Do we really need to lock the db insertions? - // Insert into MongoDB if signature is valid - _, err = dbCollection.InsertOne(context.TODO(), bson.M{ - "platform": req.DecodedPayload.Platform, - "timestamp": req.DecodedPayload.Timestamp, - "pubkey": req.Pubkey, - "signature": req.Signature, - "network": req.Network, - "tag": req.Tag, - }) + filter := bson.M{ + "pubkey": req.Pubkey, + "tag": req.Tag, + "network": req.Network, + } + update := bson.M{ + "$push": bson.M{ + "entries": bson.M{ + "payload": req.Payload, + "signature": req.Signature, + }, + }, + } + options := options.Update().SetUpsert(true) + _, err := dbCollection.UpdateOne(context.TODO(), filter, update, options) if err != nil { logger.Error("Failed to insert signature into MongoDB: " + err.Error()) continue } logger.Debug("New Signature " + req.Signature + " inserted into MongoDB") - dbMutex.Unlock() } respondOK(w, "Finished processing signatures") diff --git a/listener/internal/mongodb/connection.go b/listener/internal/mongodb/GetMongoDbClient.go similarity index 96% rename from listener/internal/mongodb/connection.go rename to listener/internal/mongodb/GetMongoDbClient.go index ed1d739..209a294 100644 --- a/listener/internal/mongodb/connection.go +++ b/listener/internal/mongodb/GetMongoDbClient.go @@ -10,7 +10,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -func ConnectMongoDB(uri string) (*mongo.Client, error) { +func GetMongoDbClient(uri string) (*mongo.Client, error) { // The URI includes the credentials var client *mongo.Client var err error // Declare err here to ensure it's accessible outside the loop