Skip to content

Commit

Permalink
redo mongo db standard (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo authored May 15, 2024
1 parent ecbf1e4 commit 2c2f919
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion listener/internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
32 changes: 17 additions & 15 deletions listener/internal/api/handlers/postNewSignature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c2f919

Please sign in to comment.