-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement get active validators (#12)
* Implement get active validators --------- Co-authored-by: Marketen <marcfont12@gmail.com>
- Loading branch information
1 parent
a24c936
commit 1c06eb4
Showing
12 changed files
with
261 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ MONGO_DB_NAME= | |
MONGO_DB_API_PORT= | ||
API_PORT= | ||
LOG_LEVEL= | ||
BEACON_NODE_URL= | ||
BYPASS_VALIDATORS_FILTERING= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,90 @@ | ||
package validation | ||
|
||
import "github.com/dappnode/validator-monitoring/listener/internal/api/types" | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
// ValidatePubkeysWithConsensusClient checks if the given pubkeys from the requests are from active validators | ||
// or not by making SINGLE API call to the consensus client. It returns an array of the active validators pubkeys. | ||
func GetActiveValidators(requestsDecoded []types.SignatureRequestDecoded) ([]types.SignatureRequestDecoded, error) { | ||
requestsActiveValidators := requestsDecoded | ||
// make api call: GET /eth/v1/beacon/states/{state_id}/validators?id=validator_pubkey1,validator_pubkey2,validator_pubkey3 | ||
"github.com/dappnode/validator-monitoring/listener/internal/api/types" | ||
) | ||
|
||
return requestsActiveValidators, nil | ||
// GetActiveValidators checks the active status of validators from a specific beacon node. | ||
// If bypass is true, it simply returns all decoded requests. | ||
func GetActiveValidators(requestsDecoded []types.SignatureRequestDecoded, beaconNodeUrl string, bypass bool) []types.SignatureRequestDecoded { | ||
|
||
if len(requestsDecoded) == 0 { | ||
fmt.Println("no requests to process") | ||
return nil | ||
} | ||
|
||
ids := make([]string, 0, len(requestsDecoded)) | ||
for _, req := range requestsDecoded { | ||
ids = append(ids, req.DecodedPayload.Pubkey) | ||
} | ||
|
||
if len(ids) == 0 { | ||
fmt.Println("no valid public keys for network ", beaconNodeUrl, " to query") | ||
return nil | ||
} | ||
|
||
// Serialize the request body to JSON | ||
// See https://ethereum.github.io/beacon-APIs/#/Beacon/postStateValidators | ||
jsonData, err := json.Marshal(struct { | ||
Ids []string `json:"ids"` | ||
Statuses []string `json:"statuses"` | ||
}{ | ||
Ids: ids, | ||
Statuses: []string{"active_ongoing"}, // Only interested in currently active validators | ||
}) | ||
if err != nil { | ||
fmt.Printf("error marshaling request data: %v\n", err) | ||
return nil | ||
} | ||
|
||
// Create HTTP client with timeout | ||
client := &http.Client{Timeout: 50 * time.Second} | ||
apiUrl := fmt.Sprintf("%s/eth/v1/beacon/states/head/validators", beaconNodeUrl) | ||
|
||
// Make API call | ||
resp, err := client.Post(apiUrl, "application/json", bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
fmt.Printf("error making API call to %s: %v\n", apiUrl, err) | ||
return nil | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Check the HTTP response status before reading the body | ||
if resp.StatusCode != http.StatusOK { | ||
fmt.Printf("unexpected response status: %d\n", resp.StatusCode) | ||
return nil | ||
} | ||
|
||
// Decode the API response directly into the ApiResponse struct | ||
var apiResponse types.ActiveValidatorsApiResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil { | ||
fmt.Printf("error decoding response data: %v\n", err) | ||
return nil | ||
} | ||
|
||
// Use a map to quickly lookup active validators | ||
activeValidatorMap := make(map[string]bool) | ||
for _, validator := range apiResponse.Data { | ||
activeValidatorMap[validator.Validator.Pubkey] = true | ||
} | ||
|
||
// Filter the list of decoded requests to include only those that are active | ||
var activeValidators []types.SignatureRequestDecoded | ||
for _, req := range requestsDecoded { | ||
if _, isActive := activeValidatorMap[req.DecodedPayload.Pubkey]; isActive { | ||
activeValidators = append(activeValidators, req) | ||
} | ||
} | ||
|
||
if bypass { | ||
return requestsDecoded // do not return the filtered list | ||
} else { | ||
return activeValidators // return the filtered list (default behaviour) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
listener/internal/api/validation/GetActiveValidators_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package validation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dappnode/validator-monitoring/listener/internal/api/types" | ||
) | ||
|
||
func TestGetActiveValidators(t *testing.T) { | ||
// Setup the input data | ||
beaconNodeUrls := map[string]string{ | ||
"holesky": "https://holeskyvals.53650f79ab75c6ff.dyndns.dappnode.io", | ||
} | ||
|
||
requestsDecoded := []types.SignatureRequestDecoded{ | ||
{ | ||
Network: "holesky", | ||
DecodedPayload: types.DecodedPayload{ | ||
Pubkey: "0xa685beb5a1f317f5a01ecd6dade42113aad945b2ab53fb1b356334ab441323e538feadd2889894b17f8fa2babe1989ca", | ||
}, | ||
}, | ||
{ | ||
Network: "holesky", | ||
DecodedPayload: types.DecodedPayload{ | ||
Pubkey: "0xab31efdd97f32087e96d3262f6fb84a4480411d391689be0dfc931fd8a5c16c3f51f10b127040b1cb65eb955f2b78a63", | ||
}, | ||
}, | ||
{ | ||
Network: "holesky", | ||
DecodedPayload: types.DecodedPayload{ | ||
Pubkey: "0xa24a030d7d8ca3c5e1f5824760d0f4157a7a89bcca6414377cca97e6e63445bef0e1b63761ee35a0fc46bb317e31b34b", | ||
}, | ||
}, | ||
} | ||
|
||
// Call the function. "bypass" is set to false, so the function will do expected behaviour and filter out inactive validators | ||
result := GetActiveValidators(requestsDecoded, beaconNodeUrls["holesky"], false) | ||
|
||
// You may need to mock the server's response or adjust the expected values here according to your actual setup | ||
expectedNumValidators := 3 // This should match the number of mock validators that are "active" | ||
if len(result) != expectedNumValidators { | ||
t.Errorf("Expected %d active validators, got %d", expectedNumValidators, len(result)) | ||
} | ||
} |
Oops, something went wrong.