Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
batphonghan committed Feb 14, 2024
1 parent b51dd77 commit 553d715
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 35 deletions.
2 changes: 1 addition & 1 deletion shared/services/config/stadernode-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (cfg *StaderNodeConfig) GetMerkleProofApi() string {
}

func (cfg *StaderNodeConfig) GetNodeDiversityApi() string {

Check warning on line 327 in shared/services/config/stadernode-config.go

View workflow job for this annotation

GitHub Actions / build

var-naming: method GetNodeDiversityApi should be GetNodeDiversityAPI (revive)
return cfg.baseStaderBackendUrl[cfg.Network.Value.(config.Network)] + "/nodeDiversity"
return cfg.baseStaderBackendUrl[cfg.Network.Value.(config.Network)] + "/saveNodeDiversity"
}

func (cfg *StaderNodeConfig) GetTxWatchUrl() string {
Expand Down
24 changes: 12 additions & 12 deletions shared/services/ec-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"io"
"math"
"math/big"
"net/http"
"strings"
"time"

Expand All @@ -39,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 @@ -537,19 +537,19 @@ func (p *ExecutionClientManager) Version() (string, error) {
url = p.primaryEcUrl
}

method := "POST"

payload := strings.NewReader(`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}`)

client := &http.Client{}
req, err := http.NewRequest(method, url, payload)

if err != nil {
return "", err
payload := struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []string `json:"params"`
Id int64 `json:"id"`

Check warning on line 544 in shared/services/ec-manager.go

View workflow job for this annotation

GitHub Actions / build

var-naming: struct field Id should be ID (revive)
}{
Jsonrpc: "2.0",
Method: "web3_clientVersion",
Params: []string{},
Id: 1,
}
req.Header.Add("Content-Type", "application/json")

res, err := client.Do(req)
res, err := net.MakePostRequest(url, payload)
if err != nil {
return "", err
}
Expand Down
12 changes: 7 additions & 5 deletions shared/types/stader-backend/node-diversity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ type NodeDiversityRequest struct {
}

type NodeDiversity struct {
ExecutionClient string `json:"executionClient"`
ConsensusClient string `json:"consensusClient"`
NodeAddress string `json:"nodeAddress"`
NodePublicKey string `json:"nodePublicKey"`
Relays string `json:"relays"`
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 {
Expand Down
39 changes: 33 additions & 6 deletions stader/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/stader-labs/stader-node/shared/utils/stdr"
"github.com/stader-labs/stader-node/shared/utils/validator"
"github.com/stader-labs/stader-node/stader-lib/node"
stader_lib "github.com/stader-labs/stader-node/stader-lib/stader"
eth2types "github.com/wealdtech/go-eth2-types/v2"

"github.com/fatih/color"
Expand Down Expand Up @@ -413,7 +414,7 @@ func run(c *cli.Context) error {
for {
infoLog.Printlnf("Running the node diversity tracker daemon")

message, err := makesNodeDiversityMessage(ec, bc, w, cfg)
message, err := makesNodeDiversityMessage(ec, bc, pnr, w, cfg)
if err != nil {
errorLog.Printlnf("Error makesNodeDiversityMessage %+v", err)
continue
Expand Down Expand Up @@ -451,6 +452,7 @@ func run(c *cli.Context) error {
func makesNodeDiversityMessage(
ec *services.ExecutionClientManager,
bc *services.BeaconClientManager,
pnr *stader_lib.PermissionlessNodeRegistryContractManager,
w *wallet.Wallet,
cfg *config.StaderConfig,
) (*stader_backend.NodeDiversity, error) {
Expand Down Expand Up @@ -491,18 +493,43 @@ func makesNodeDiversityMessage(
relayString = strings.Join(relayNames, ",")
}

operatorId, err := node.GetOperatorId(pnr, nodeAccount.Address, nil)

Check warning on line 496 in stader/node/node.go

View workflow job for this annotation

GitHub Actions / build

var-naming: var operatorId should be operatorID (revive)
if err != nil {
return nil, err
}

totalValidatorKeys, err := node.GetTotalValidatorKeys(pnr, operatorId, nil)
if err != nil {
return nil, err
}

//fmt.Printf("Get total non terminal validator keys\n")

Check failure on line 506 in stader/node/node.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)
totalNonTerminalValidatorKeys, err := node.GetTotalNonTerminalValidatorKeys(pnr, nodeAccount.Address, totalValidatorKeys, nil)
if err != nil {
return nil, err
}

// Get the new validator client according to the settings file
selectedConsensusClientConfig, err := cfg.GetSelectedConsensusClientConfig()
if err != nil {
return nil, err
}

message := stader_backend.NodeDiversity{
ExecutionClient: ecVersion,
ConsensusClient: bcNodeVersion.Version,
NodeAddress: nodeAccount.Address.String(),
NodePublicKey: nodePublicKey,
Relays: relayString,
ExecutionClient: ecVersion,
ConsensusClient: bcNodeVersion.Version,
ValidatorClient: selectedConsensusClientConfig.GetName(),
NodeAddress: nodeAccount.Address.String(),
TotalNonTerminalKeys: totalNonTerminalValidatorKeys,
NodePublicKey: nodePublicKey,
Relays: relayString,
}

return &message, nil
}

func makesNodeDiversityRequest(msg *stader_backend.NodeDiversity, privateKey *ecdsa.PrivateKey) (*stader_backend.NodeDiversityRequest, error) {
msg.TotalNonTerminalKeys = 1
msgBytes, err := json.Marshal(msg)
if err != nil {
return nil, err
Expand Down
39 changes: 28 additions & 11 deletions stader/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"testing"

"github.com/ethereum/go-ethereum/accounts"
Expand All @@ -14,8 +15,9 @@ import (
)

var (
ExecutionClient = "Nethermind/v1.25.2+78c7bf5f/linux-x64/dotnet8.0.1"
ConsensusClient = "Lighthouse/v4.6.0-rc.0-2e8e160/x86_64-linux"
ExecutionClient = "besu/v1.25.2+78c7bf5f/linux-x64/dotnet8.0.1"
ConsensusClient = "teku/v4.6.0-rc.0-2e8e160/x86_64-linux"
ValidatorClient = "prysm"

PrvFake = "f7d400ec4062274059f531413e03a938fd837e3a07692338ab78dfd93d1e21e1"
)
Expand All @@ -35,11 +37,13 @@ func TestVerifySignature(t *testing.T) {

pubkeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
req, err := makesNodeDiversityRequest(&stader_backend.NodeDiversity{
ExecutionClient: ExecutionClient,
ConsensusClient: ConsensusClient,
NodeAddress: crypto.PubkeyToAddress(*publicKeyECDSA).String(),
NodePublicKey: hex.EncodeToString(pubkeyBytes),
Relays: "ultrasound,aestus",
ExecutionClient: ExecutionClient,
ConsensusClient: ConsensusClient,
ValidatorClient: ValidatorClient,
TotalNonTerminalKeys: 1,
NodeAddress: crypto.PubkeyToAddress(*publicKeyECDSA).String(),
NodePublicKey: hex.EncodeToString(pubkeyBytes),
Relays: "ultrasound,aestus",
}, privateKey)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -73,10 +77,13 @@ func TestVerifySignatureFailed(t *testing.T) {
}

msg := stader_backend.NodeDiversity{
ExecutionClient: ExecutionClient,
ConsensusClient: ConsensusClient,
NodeAddress: crypto.PubkeyToAddress(*publicKeyECDSA).String(),
NodePublicKey: hex.EncodeToString(publickeyBytes),
ExecutionClient: ExecutionClient,
ConsensusClient: ConsensusClient,
ValidatorClient: ValidatorClient,
TotalNonTerminalKeys: 10,
Relays: "ultrasound,aestus",
NodeAddress: crypto.PubkeyToAddress(*publicKeyECDSA).String(),
NodePublicKey: hex.EncodeToString(publickeyBytes),
}
req, err := makesNodeDiversityRequest(&msg, privateKeyFake)
if err != nil {
Expand Down Expand Up @@ -104,11 +111,21 @@ func verifySignature(t *testing.T, msg *stader_backend.NodeDiversity, signEncode
t.Error(err)
}

if msg.ValidatorClient != ValidatorClient {
t.Error(err)
}

if msg.TotalNonTerminalKeys != 10 {
t.Error(err)
}

msgBytes, err := json.Marshal(msg)
if err != nil {
t.Error(err)
}

fmt.Printf("[%s]", msgBytes)
fmt.Printf("[%s]", signEncoded)
msgHashed := accounts.TextHash(msgBytes)

decodePubkey, err := hex.DecodeString(msg.NodePublicKey)
Expand Down

0 comments on commit 553d715

Please sign in to comment.