Skip to content

Commit

Permalink
Merge pull request #186 from stader-labs/node-diver-metrics
Browse files Browse the repository at this point in the history
Node diver metrics
  • Loading branch information
batphonghan authored Feb 16, 2024
2 parents b4ed663 + 95269ca commit f8cdc8f
Show file tree
Hide file tree
Showing 18 changed files with 518 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
with:
go-version: 1.19

- name: Test with the Go CLI
run: go test ./...

- name: Golangci
uses: golangci/golangci-lint-action@v3
with:
Expand Down
10 changes: 10 additions & 0 deletions shared/services/bc-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ func (m *BeaconClientManager) GetSyncStatus() (beacon.SyncStatus, error) {
return result.(beacon.SyncStatus), nil
}

func (m *BeaconClientManager) GetNodeVersion() (beacon.NodeVersion, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetNodeVersion()
})
if err != nil {
return beacon.NodeVersion{}, err
}
return result.(beacon.NodeVersion), nil
}

// Get the Beacon configuration
func (m *BeaconClientManager) GetEth2Config() (beacon.Eth2Config, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
Expand Down
5 changes: 5 additions & 0 deletions shared/services/beacon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type AttestationInfo struct {
CommitteeIndex uint64
}

type NodeVersion struct {
Version string
}

// Beacon client type
type BeaconClientType int

Expand Down Expand Up @@ -133,6 +137,7 @@ const (
type Client interface {
GetClientType() (BeaconClientType, error)
GetSyncStatus() (SyncStatus, error)
GetNodeVersion() (NodeVersion, error)
GetEth2Config() (Eth2Config, error)
GetEth2DepositContract() (Eth2DepositContract, error)
GetAttestations(blockId string) ([]AttestationInfo, bool, error)
Expand Down
27 changes: 27 additions & 0 deletions shared/services/beacon/client/std-http-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
RequestContentType = "application/json"

RequestSyncStatusPath = "/eth/v1/node/syncing"
RequestNodeVersionPath = "/eth/v1/node/version"
RequestEth2ConfigPath = "/eth/v1/config/spec"
RequestEth2DepositContractMethod = "/eth/v1/config/deposit_contract"
RequestGenesisPath = "/eth/v1/beacon/genesis"
Expand Down Expand Up @@ -107,6 +108,17 @@ func (c *StandardHttpClient) GetSyncStatus() (beacon.SyncStatus, error) {

}

func (c *StandardHttpClient) GetNodeVersion() (beacon.NodeVersion, error) {
nodeVersion, err := c.getNodeVersion()
if err != nil {
return beacon.NodeVersion{}, err
}

return beacon.NodeVersion{
Version: nodeVersion.Data.Version,
}, nil
}

// Get the eth2 config
func (c *StandardHttpClient) GetEth2Config() (beacon.Eth2Config, error) {

Expand Down Expand Up @@ -572,6 +584,21 @@ func (c *StandardHttpClient) getSyncStatus() (SyncStatusResponse, error) {
return syncStatus, nil
}

func (c *StandardHttpClient) getNodeVersion() (NodeVersionResponse, error) {
responseBody, status, err := c.getRequest(RequestNodeVersionPath)
if err != nil {
return NodeVersionResponse{}, fmt.Errorf("Could not get node sync status: %w", err)
}
if status != http.StatusOK {
return NodeVersionResponse{}, fmt.Errorf("Could not get node sync status: HTTP status %d; response body: '%s'", status, string(responseBody))
}
var nodeVersion NodeVersionResponse
if err := json.Unmarshal(responseBody, &nodeVersion); err != nil {
return NodeVersionResponse{}, fmt.Errorf("Could not decode node sync status: %w", err)
}
return nodeVersion, nil
}

// Get the eth2 config
func (c *StandardHttpClient) getEth2Config() (Eth2ConfigResponse, error) {
responseBody, status, err := c.getRequest(RequestEth2ConfigPath)
Expand Down
7 changes: 7 additions & 0 deletions shared/services/beacon/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ type SyncStatusResponse struct {
SyncDistance uinteger `json:"sync_distance"`
} `json:"data"`
}

type NodeVersionResponse struct {
Data struct {
Version string `json:"version"`
} `json:"data"`
}

type Eth2ConfigResponse struct {
Data struct {
SecondsPerSlot uinteger `json:"SECONDS_PER_SLOT"`
Expand Down
4 changes: 4 additions & 0 deletions shared/services/config/stadernode-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ func (cfg *StaderNodeConfig) GetMerkleProofApi() string {
return cfg.baseStaderBackendUrl[cfg.Network.Value.(config.Network)] + "/merklesForElRewards/proofs/%s"
}

func (cfg *StaderNodeConfig) GetNodeDiversityApi() string {
return cfg.baseStaderBackendUrl[cfg.Network.Value.(config.Network)] + "/saveNodeDiversity"
}

func (cfg *StaderNodeConfig) GetTxWatchUrl() string {
return cfg.txWatchUrl[cfg.Network.Value.(config.Network)]
}
Expand Down
51 changes: 51 additions & 0 deletions shared/services/ec-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ package services

import (
"context"
"encoding/json"
"fmt"
"io"
"math"
"math/big"
"strings"
Expand All @@ -36,6 +38,7 @@ import (
"github.com/stader-labs/stader-node/shared/types/api"
cfgtypes "github.com/stader-labs/stader-node/shared/types/config"
"github.com/stader-labs/stader-node/shared/utils/log"
"github.com/stader-labs/stader-node/shared/utils/net"
)

// This is a proxy for multiple ETH clients, providing natural fallback support if one of them fails.
Expand Down Expand Up @@ -520,3 +523,51 @@ func (p *ExecutionClientManager) runFunction(function ecFunction) (interface{},
func (p *ExecutionClientManager) isDisconnected(err error) bool {
return strings.Contains(err.Error(), "dial tcp")
}

func (p *ExecutionClientManager) Version() (string, error) {
if !p.primaryReady && !p.fallbackReady {
return "", fmt.Errorf("EC not ready")
}

var url string

if p.primaryReady {
url = p.primaryEcUrl
} else {
url = p.fallbackEcUrl
}

payload := struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []string `json:"params"`
Id int64 `json:"id"`
}{
Jsonrpc: "2.0",
Method: "web3_clientVersion",
Params: []string{},
Id: 1,
}

res, err := net.MakePostRequest(url, payload)
if err != nil {
return "", err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}

response := struct {
Result string `json:"result"`
}{}

err = json.Unmarshal(body, &response)
if err != nil {
return "", err
}

return response.Result, nil
}
2 changes: 1 addition & 1 deletion shared/services/gas/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func handleEtherscanGasPrices(gasSuggestion etherscan.GasFeeSuggestion, priority

desiredPriceFloat, err := strconv.ParseFloat(desiredPrice, 64)
if err != nil {
fmt.Println("Not a valid gas price (%s), try again.", err.Error())
fmt.Printf("Not a valid gas price (%s), try again.\n", err.Error())
continue
}
if desiredPriceFloat <= 0 {
Expand Down
2 changes: 1 addition & 1 deletion shared/services/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func waitBeaconClientSynced(c *cli.Context, verbose bool, timeout int64) (bool,
// Check sync status
if syncStatus.Syncing {
if verbose {
log.Println("Eth 2.0 node syncing: %.2f%%\n", syncStatus.Progress*100)
log.Printf("Eth 2.0 node syncing: %.2f%%\n", syncStatus.Progress*100)
}
} else {
return true, nil
Expand Down
3 changes: 1 addition & 2 deletions shared/services/stader/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (c *Client) UpdatePrometheusConfiguration(settings map[string]string) error
}
err = os.Chmod(prometheusConfigPath, 0664)
if err != nil {
return fmt.Errorf("Could not set Prometheus config file permissions: %w", shellescape.Quote(prometheusConfigPath), err)
return fmt.Errorf("Could not set Prometheus config file permissions: %s: %w", shellescape.Quote(prometheusConfigPath), err)
}

return nil
Expand Down Expand Up @@ -795,7 +795,6 @@ func (c *Client) GetDockerContainerShutdownTime(container string) (time.Time, er
}

func (c *Client) UpdateGuardianConfiguration(contents []byte) error {

guardianTemplatePath, err := homedir.Expand(fmt.Sprintf("%s/%s/%s", c.configPath, templatesDir, GuardianFileTemplate))
if err != nil {
return fmt.Errorf("error expanding Guardian template path: %w", err)
Expand Down
27 changes: 27 additions & 0 deletions shared/services/wallet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package wallet
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -186,3 +187,29 @@ func (w *Wallet) getNodeDerivedKey(index uint) (*hdkeychain.ExtendedKey, string,
return key, derivationPath, nil

}

// Get the node hex encoding public key
func (w *Wallet) GetNodePubkey() (string, error) {

// Check wallet is initialized
if !w.IsInitialized() {
return "", errors.New("Wallet is not initialized")
}

// Get private key
privateKey, _, err := w.getNodePrivateKey()
if err != nil {
return "", err
}

// Get public key
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return "", errors.New("Could not get node public key")
}

publickeyBytes := crypto.FromECDSAPub(publicKeyECDSA)

return hex.EncodeToString(publickeyBytes), nil
}
21 changes: 21 additions & 0 deletions shared/types/stader-backend/node-diversity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stader_backend

type NodeDiversityRequest struct {
Signature string `json:"signature"`
Message *NodeDiversity `json:"message"`
}

type NodeDiversity struct {
ExecutionClient string `json:"executionClient"`
ConsensusClient string `json:"consensusClient"`
ValidatorClient string `json:"validatorClient"`
TotalNonTerminalKeys uint64 `json:"totalNonTerminalKeys"`
NodeAddress string `json:"nodeAddress"`
NodePublicKey string `json:"nodePublicKey"`
Relays string `json:"relays"`
}

type NodeDiversityResponseType struct {
Success bool `json:"success"`
Error string `json:"error"`
}
37 changes: 37 additions & 0 deletions shared/utils/stader/node-diversity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package stader

import (
"encoding/json"
"fmt"

"github.com/stader-labs/stader-node/shared/services"
stader_backend "github.com/stader-labs/stader-node/shared/types/stader-backend"
"github.com/stader-labs/stader-node/shared/utils/net"
"github.com/urfave/cli"
)

func SendNodeDiversityResponseType(
c *cli.Context,
request *stader_backend.NodeDiversityRequest,
) (*stader_backend.NodeDiversityResponseType, error) {
config, err := services.GetConfig(c)
if err != nil {
return nil, err
}

res, err := net.MakePostRequest(config.StaderNode.GetNodeDiversityApi(), request)
if err != nil {
return nil, fmt.Errorf("request to GetNodeDiversityApi %w", err)
}
defer res.Body.Close()

var resp stader_backend.NodeDiversityResponseType
err = json.NewDecoder(res.Body).Decode(&resp)

if err != nil {
return nil, fmt.Errorf("decode NodeDiversityResponseType %w", err)
}

return &resp, nil

}
4 changes: 2 additions & 2 deletions stader-cli/node/claim-sp-rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func ClaimSpRewards(c *cli.Context) error {
cycleIndexes = append(cycleIndexes, big.NewInt(cycleInfo.MerkleProofInfo.Cycle))
}

fmt.Println("Following are the unclaimed cycles, Please enter in a comma seperated string the cycles you want to claim rewards for:\n")
fmt.Println("Following are the unclaimed cycles, Please enter in a comma separated string the cycles you want to claim rewards for:")

fmt.Printf("%-18s%-14.30s%-14.10s%-10s\n", "Cycle Number", "Cycle Date", "ETH Rewards", "SD Rewards")
fmt.Printf("\n%-18s%-14.30s%-14.10s%-10s\n", "Cycle Number", "Cycle Date", "ETH Rewards", "SD Rewards")
cyclesToClaim := map[int64]bool{}
for {
for _, cycleInfo := range detailedCyclesInfo.DetailedCyclesInfo {
Expand Down
10 changes: 5 additions & 5 deletions stader-cli/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ func startService(
fmt.Printf("%sWarning: couldn't verify that the validator container can be safely restarted:\n\t%s\n", colorYellow, err.Error())
fmt.Println("If you are changing to a different ETH2 client, it may resubmit an attestation you have already submitted.")
fmt.Println("This will slash your validator!")
fmt.Println("To prevent slashing, you must wait 15 minutes from the time you stopped the clients before starting them again.\n")
fmt.Println("**If you did NOT change clients, you can safely ignore this warning.**\n")
fmt.Println("To prevent slashing, you must wait 15 minutes from the time you stopped the clients before starting them again.")
fmt.Println("**If you did NOT change clients, you can safely ignore this warning.**")
if !cliutils.Confirm(fmt.Sprintf("Press y when you understand the above warning, have waited, and are ready to start Stader:%s", colorReset)) {
fmt.Println("Cancelled.")
return nil
Expand Down Expand Up @@ -914,7 +914,7 @@ func pruneExecutionClient(c *cli.Context) error {
}

fmt.Println("This will shut down your main execution client and prune its database, freeing up disk space.")
fmt.Println("Once pruning is complete, your execution client will restart automatically.\n")
fmt.Println("Once pruning is complete, your execution client will restart automatically.")

if selectedEc == cfgtypes.ExecutionClient_Geth {
if cfg.UseFallbackClients.Value == false {
Expand Down Expand Up @@ -1488,7 +1488,7 @@ func exportEcData(c *cli.Context, targetDir string) error {

fmt.Println("This will export your execution client's chain data to an external directory, such as a portable hard drive.")
fmt.Println("If your execution client is running, it will be shut down.")
fmt.Println("Once the export is complete, your execution client will restart automatically.\n")
fmt.Println("Once the export is complete, your execution client will restart automatically.")

// Get the container prefix
prefix, err := getContainerPrefix(staderClient)
Expand Down Expand Up @@ -1606,7 +1606,7 @@ func importEcData(c *cli.Context, sourceDir string) error {

fmt.Println("This will import execution layer chain data that you previously exported into your execution client.")
fmt.Println("If your execution client is running, it will be shut down.")
fmt.Println("Once the import is complete, your execution client will restart automatically.\n")
fmt.Println("Once the import is complete, your execution client will restart automatically.")

// Get the volume to import into
executionContainerName := prefix + ExecutionContainerSuffix
Expand Down
4 changes: 2 additions & 2 deletions stader-cli/wallet/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func exportWallet(c *cli.Context) error {
// Check if stdout is interactive
stat, err := os.Stdout.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "An error occured while determining whether or not the output is a tty: %w\n"+
"Use \"stader-cli --secure-session wallet export\" to bypass.\n", err)
fmt.Fprintf(os.Stderr, "An error occured while determining whether or not the output is a tty: %s\n"+
"Use \"stader-cli --secure-session wallet export\" to bypass.\n", err.Error())
os.Exit(1)
}

Expand Down
Loading

0 comments on commit f8cdc8f

Please sign in to comment.