diff --git a/listener/internal/api/handlers/postNewSignature.go b/listener/internal/api/handlers/postSignatures.go similarity index 86% rename from listener/internal/api/handlers/postNewSignature.go rename to listener/internal/api/handlers/postSignatures.go index e9f8779..aaaa8f6 100644 --- a/listener/internal/api/handlers/postNewSignature.go +++ b/listener/internal/api/handlers/postSignatures.go @@ -8,15 +8,30 @@ import ( "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" + "github.com/gorilla/mux" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) -func PostNewSignature(w http.ResponseWriter, r *http.Request, dbCollection *mongo.Collection, beaconNodeUrls map[types.Network]string) { - logger.Debug("Received new POST '/newSignature' request") +func PostSignatures(w http.ResponseWriter, r *http.Request, dbCollection *mongo.Collection, beaconNodeUrls map[types.Network]string) { + logger.Debug("Received new POST '/signatures' request") var requests []types.SignatureRequest + // Get network from URL + vars := mux.Vars(r) + networkVar, ok := vars["network"] + if !ok { + respondError(w, http.StatusBadRequest, "Invalid URL, missing network") + return + } + network := types.Network(networkVar) + beaconNodeUrl, ok := beaconNodeUrls[network] + if !ok { + respondError(w, http.StatusBadRequest, "Invalid network") + return + } + // Parse and validate request body if err := json.NewDecoder(r.Body).Decode(&requests); err != nil { logger.Error("Failed to decode request body: " + err.Error()) @@ -31,14 +46,6 @@ func PostNewSignature(w http.ResponseWriter, r *http.Request, dbCollection *mong return } - // Check network validity - network := requestsValidatedAndDecoded[0].Network - beaconNodeUrl, ok := beaconNodeUrls[network] - if !ok { - respondError(w, http.StatusBadRequest, "Invalid network") - return - } - // Get active validators pubkeys := getPubkeys(requestsValidatedAndDecoded) validatorsStatusMap, err := validation.GetValidatorsStatus(pubkeys, beaconNodeUrl) @@ -56,7 +63,7 @@ func PostNewSignature(w http.ResponseWriter, r *http.Request, dbCollection *mong } // Insert valid signatures into MongoDB - if err := insertSignaturesIntoDB(validSignatures, dbCollection); err != nil { + if err := insertSignaturesIntoDB(validSignatures, network, dbCollection); err != nil { logger.Error("Failed to insert signatures into MongoDB: " + err.Error()) respondError(w, http.StatusInternalServerError, "Failed to insert signatures into MongoDB") return @@ -98,12 +105,12 @@ func filterAndVerifySignatures(requests []types.SignatureRequestDecoded, validat return validSignatures } -func insertSignaturesIntoDB(signatures []types.SignatureRequestDecodedWithStatus, dbCollection *mongo.Collection) error { +func insertSignaturesIntoDB(signatures []types.SignatureRequestDecodedWithStatus, network types.Network, dbCollection *mongo.Collection) error { for _, req := range signatures { filter := bson.M{ "pubkey": req.Pubkey, "tag": req.Tag, - "network": req.Network, + "network": network, } update := bson.M{ "$setOnInsert": bson.M{"status": req.Status}, // do not update status if already exists diff --git a/listener/internal/api/routes/routes.go b/listener/internal/api/routes/routes.go index 40e1c9a..f7c7838 100644 --- a/listener/internal/api/routes/routes.go +++ b/listener/internal/api/routes/routes.go @@ -15,8 +15,8 @@ func SetupRouter(dbCollection *mongo.Collection, beaconNodeUrls map[types.Networ // Define routes r.HandleFunc("/", handlers.GetHealthCheck).Methods(http.MethodGet) // closure function to inject dbCollection into the handler - r.HandleFunc("/newSignature", func(w http.ResponseWriter, r *http.Request) { - handlers.PostNewSignature(w, r, dbCollection, beaconNodeUrls) + r.HandleFunc("/signatures", func(w http.ResponseWriter, r *http.Request) { + handlers.PostSignatures(w, r, dbCollection, beaconNodeUrls) }).Methods(http.MethodPost) // Middlewares diff --git a/listener/internal/api/types/types.go b/listener/internal/api/types/types.go index bc665c3..29f362c 100644 --- a/listener/internal/api/types/types.go +++ b/listener/internal/api/types/types.go @@ -26,11 +26,10 @@ const ( ) type SignatureRequest struct { - Payload string `json:"payload"` - Pubkey string `json:"pubkey"` - Signature string `json:"signature"` - Network Network `json:"network"` - Tag string `json:"tag"` + Payload string `json:"payload"` + Pubkey string `json:"pubkey"` + Signature string `json:"signature"` + Tag string `json:"tag"` } type DecodedPayload struct { diff --git a/listener/internal/api/validation/validateAndDecodeRequests.go b/listener/internal/api/validation/validateAndDecodeRequests.go index 6de991e..490b0a3 100644 --- a/listener/internal/api/validation/validateAndDecodeRequests.go +++ b/listener/internal/api/validation/validateAndDecodeRequests.go @@ -31,7 +31,6 @@ func ValidateAndDecodeRequests(requests []types.SignatureRequest) ([]types.Signa Payload: req.Payload, Pubkey: req.Pubkey, Signature: req.Signature, - Network: req.Network, Tag: req.Tag, }, }) @@ -43,7 +42,7 @@ func ValidateAndDecodeRequests(requests []types.SignatureRequest) ([]types.Signa // TODO: validate network and tag against enums func isValidCodedRequest(req *types.SignatureRequest) bool { // Check for any empty required fields - if req.Network == "" || req.Tag == "" || req.Signature == "" || req.Payload == "" || req.Pubkey == "" { + if req.Tag == "" || req.Signature == "" || req.Payload == "" || req.Pubkey == "" { logger.Debug("Received Invalid Request: One or more required fields are empty.") return false } diff --git a/listener/internal/api/validation/validateAndDecodeRequests_test.go b/listener/internal/api/validation/validateAndDecodeRequests_test.go index ad1444b..05b45ff 100644 --- a/listener/internal/api/validation/validateAndDecodeRequests_test.go +++ b/listener/internal/api/validation/validateAndDecodeRequests_test.go @@ -39,42 +39,36 @@ func TestValidateAndDecodeRequests(t *testing.T) { Payload: validEncodedPayload, Pubkey: validBlsPubkey, Signature: "0x" + repeatString("a", 192), // valid signature - Network: "mainnet", Tag: "tag1", }, { // Missing fields Payload: "", Pubkey: "", Signature: "", - Network: "", Tag: "", }, { // Invalid signature format Payload: validEncodedPayload, Pubkey: validBlsPubkey, Signature: "bad_signature", - Network: "mainnet", Tag: "tag2", }, { // Old timestamp Payload: oldEncodedPayload, Pubkey: validBlsPubkey, Signature: "0x" + repeatString("a", 192), - Network: "mainnet", Tag: "tag3", }, { // Invalid type Payload: invalidTypePayload, Pubkey: validBlsPubkey, Signature: "0x" + repeatString("a", 192), - Network: "mainnet", Tag: "tag4", }, { // Invalid BLS public key Payload: validEncodedPayload, Pubkey: invalidBlsPubkey, Signature: "0x" + repeatString("a", 192), - Network: "mainnet", Tag: "tag5", }, } diff --git a/listener/internal/api/validation/verifySignature_test.go b/listener/internal/api/validation/verifySignature_test.go index efcc120..d23dd2f 100644 --- a/listener/internal/api/validation/verifySignature_test.go +++ b/listener/internal/api/validation/verifySignature_test.go @@ -43,7 +43,6 @@ func TestVerifySignature(t *testing.T) { Pubkey: publicKey.SerializeToHexStr(), Payload: base64.StdEncoding.EncodeToString(messageBytes), Signature: signature.SerializeToHexStr(), - Network: "mainnet", Tag: "solo"}, }, Status: types.Active, @@ -89,7 +88,6 @@ func TestVerifySignatureError(t *testing.T) { Pubkey: badPublicKey, Payload: base64.StdEncoding.EncodeToString(payloadBytes), Signature: "clearlyInvalidSignature", - Network: "mainnet", Tag: "solo", }, },