-
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.
- Loading branch information
1 parent
a24c936
commit 37141f4
Showing
8 changed files
with
86 additions
and
21 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,4 @@ MONGO_DB_NAME= | |
MONGO_DB_API_PORT= | ||
API_PORT= | ||
LOG_LEVEL= | ||
BEACON_NODE_URL= |
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ func main() { | |
s := api.NewApi( | ||
config.Port, | ||
config.MongoDBURI, | ||
config.BeaconNodeURL, | ||
) | ||
|
||
s.Start() | ||
|
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,65 @@ | ||
package validation | ||
|
||
import "github.com/dappnode/validator-monitoring/listener/internal/api/types" | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"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 | ||
func GetActiveValidators(requestsDecoded []types.SignatureRequestDecoded, beaconNodeUrl string) ([]types.SignatureRequestDecoded, error) { | ||
if len(requestsDecoded) == 0 { | ||
return nil, fmt.Errorf("no validators to check") | ||
} | ||
|
||
// Prepare the request body | ||
ids := make([]string, 0, len(requestsDecoded)) | ||
for _, req := range requestsDecoded { | ||
ids = append(ids, req.DecodedPayload.Pubkey) | ||
} | ||
requestBody := struct { | ||
Ids []string `json:"ids"` | ||
Statuses []string `json:"statuses"` | ||
}{ | ||
Ids: ids, | ||
Statuses: []string{"active_ongoing"}, | ||
} | ||
|
||
// Serialize the request body to JSON | ||
jsonData, err := json.Marshal(requestBody) | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshaling request body: %w", err) | ||
} | ||
|
||
// Configure HTTP client with timeout | ||
client := &http.Client{Timeout: 10 * time.Second} | ||
url := fmt.Sprintf("%s/eth/v1/beacon/states/head/validators", beaconNodeUrl) | ||
resp, err := client.Post(url, "application/json", bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error making API call to %s: %w", url, err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Check the HTTP response status before reading the body | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("API call to %s returned status %d", url, resp.StatusCode) | ||
} | ||
|
||
// Read and log the response body | ||
responseData, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading response data: %w", err) | ||
} | ||
|
||
// Assuming the server returns a list of active validators in the format you expect | ||
var activeValidators []types.SignatureRequestDecoded | ||
if err := json.Unmarshal(responseData, &activeValidators); err != nil { | ||
return nil, fmt.Errorf("error unmarshaling response data: %w", err) | ||
} | ||
|
||
return activeValidators, nil | ||
} |
Empty file.
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