Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement network as a query parameter and update endpoint from /newSignature to /signatures #53

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions listener/internal/api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions listener/internal/api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand Down
2 changes: 0 additions & 2 deletions listener/internal/api/validation/verifySignature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -89,7 +88,6 @@ func TestVerifySignatureError(t *testing.T) {
Pubkey: badPublicKey,
Payload: base64.StdEncoding.EncodeToString(payloadBytes),
Signature: "clearlyInvalidSignature",
Network: "mainnet",
Tag: "solo",
},
},
Expand Down
Loading