Skip to content

Commit

Permalink
add signaturesAggr & signatures (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketen authored Apr 9, 2024
1 parent 3a6c663 commit 9e2551c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
4 changes: 3 additions & 1 deletion listener/src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func (s *httpApi) SetupRouter() *mux.Router {

// Endpoints
r.HandleFunc("/", s.handleRoot).Methods(http.MethodGet)
r.HandleFunc("/signature", s.handleSignature).Methods(http.MethodPost)
r.HandleFunc("/newSignature", s.handleSignature).Methods(http.MethodPost)
r.HandleFunc("/signaturesByValidator", s.handleGetSignatures).Methods(http.MethodPost)
r.HandleFunc("/signaturesByValidatorAggr", s.handleGetSignaturesAggr).Methods(http.MethodPost)

// Middlewares
// r.Use(corsmiddleware()))
Expand Down
103 changes: 103 additions & 0 deletions listener/src/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/dappnode/validator-monitoring/listener/src/logger"
"github.com/dappnode/validator-monitoring/listener/src/types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)

func (s *httpApi) handleRoot(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -83,6 +84,108 @@ func (s *httpApi) handleSignature(w http.ResponseWriter, r *http.Request) {

}

func (s *httpApi) handleGetSignatures(w http.ResponseWriter, r *http.Request) {
var req types.SignaturesRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
s.respondError(w, http.StatusBadRequest, "Invalid request body")
return
}

filter := bson.M{"pubkey": req.Pubkey, "network": req.Network}
cursor, err := s.dbCollection.Find(r.Context(), filter)
if err != nil {
s.respondError(w, http.StatusInternalServerError, "Error fetching signatures from MongoDB")
return
}
defer cursor.Close(r.Context())

var signatures []bson.M
if err = cursor.All(r.Context(), &signatures); err != nil {
s.respondError(w, http.StatusInternalServerError, "Error reading signatures from cursor")
return
}

s.respondOK(w, signatures)
}

// Endpoint that returns an aggregation of all signatures for a given pubkey and network in this format:
// [
//
// {
// "label": "example_label",
// "network": "stader",
// "pubkey": "0xb48c495c19082d892f38227bced89f7199f4e9b642bf94c7f2f1ccf29c0e6a6f54d653002513aa7cdb56c88368797ec",
// "signatures": [
// {
// "platform": "dappnode",
// "signature": "0xa8b00e7746a523346c5165dfa80ffafe52317c6fe6cdcfabd41886f9c8209b829266c5000597142b58dddbcc9c23cfd81315180acf18bb000db50d08293bc539e06a7c751d3d9dec89fb441b3ba6aefdeeff9cfed72fb41171173f22e2993e74",
// "timestamp": "185921877"
// },
// {
// "platform": "dappnode",
// "signature": "0xa8b00e7746a523346c5165dfa80ffafe52317c6fe6cdcfabd41886f9c8209b829266c5000597142b58dddbcc9c23cfd81315180acf18bb000db50d08293bc539e06a7c751d3d9dec89fb441b3ba6aefdeeff9cfed72fb41171173f22e2993e74",
// "timestamp": "185921877"
// }
// ]
// }
//
// ]
func (s *httpApi) handleGetSignaturesAggr(w http.ResponseWriter, r *http.Request) {
var req types.SignaturesRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
s.respondError(w, http.StatusBadRequest, "Invalid request body")
return
}

// Define the aggregation pipeline
// We should probably add pubkey to each signatures array element, so a 3rd party can easily verify the signature?
pipeline := []bson.M{
{
"$match": bson.M{
"pubkey": req.Pubkey,
"network": req.Network,
},
},
{
"$group": bson.M{
"_id": bson.M{"pubkey": "$pubkey", "network": "$network", "label": "$label"},
"signatures": bson.M{
"$push": bson.M{
"signature": "$signature",
"timestamp": "$timestamp",
"platform": "$platform",
},
},
},
},
{
"$project": bson.M{
"_id": 0,
"pubkey": "$_id.pubkey",
"network": "$_id.network",
"label": "$_id.label",
"signatures": 1,
},
},
}

cursor, err := s.dbCollection.Aggregate(r.Context(), pipeline, options.Aggregate())
if err != nil {
s.respondError(w, http.StatusInternalServerError, "Error aggregating signatures from MongoDB")
return
}
defer cursor.Close(r.Context())

var results []bson.M
if err := cursor.All(r.Context(), &results); err != nil {
s.respondError(w, http.StatusInternalServerError, "Error reading aggregation results")
return
}

// Respond with the aggregation results
s.respondOK(w, results)
}

// TODO: error handling
func (s *httpApi) respondError(w http.ResponseWriter, code int, message string) {
w.Header().Set("Content-Type", "application/json")
Expand Down
5 changes: 5 additions & 0 deletions listener/src/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ type HttpErrorResp struct {
Code int `json:"code"`
Message string `json:"message"`
}

type SignaturesRequest struct {
Pubkey string `json:"pubkey"`
Network string `json:"network"`
}

0 comments on commit 9e2551c

Please sign in to comment.