From 359d466e5a51df258f419a28a6e30cd407f8b649 Mon Sep 17 00:00:00 2001 From: batphonghan Date: Thu, 15 Feb 2024 19:18:09 +0700 Subject: [PATCH] Refactoring --- shared/services/gas/gas.go | 2 +- shared/services/requirements.go | 2 +- shared/services/stader/client.go | 3 +-- stader-cli/node/claim-sp-rewards.go | 4 ++-- stader-cli/service/service.go | 6 +++--- stader-cli/wallet/export.go | 4 ++-- stader/node/node.go | 15 +++++++++++---- stader/node/node_test.go | 15 ++++++++++----- 8 files changed, 31 insertions(+), 20 deletions(-) diff --git a/shared/services/gas/gas.go b/shared/services/gas/gas.go index c1ffa08cf..53c5fc228 100644 --- a/shared/services/gas/gas.go +++ b/shared/services/gas/gas.go @@ -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 { diff --git a/shared/services/requirements.go b/shared/services/requirements.go index 2e4bdaf34..7dfe8dda1 100644 --- a/shared/services/requirements.go +++ b/shared/services/requirements.go @@ -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 diff --git a/shared/services/stader/client.go b/shared/services/stader/client.go index 6f361c0e5..b287e6e39 100644 --- a/shared/services/stader/client.go +++ b/shared/services/stader/client.go @@ -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 @@ -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) diff --git a/stader-cli/node/claim-sp-rewards.go b/stader-cli/node/claim-sp-rewards.go index 53623f240..53c8c0b4c 100644 --- a/stader-cli/node/claim-sp-rewards.go +++ b/stader-cli/node/claim-sp-rewards.go @@ -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 { diff --git a/stader-cli/service/service.go b/stader-cli/service/service.go index 8088f1c93..867a56788 100644 --- a/stader-cli/service/service.go +++ b/stader-cli/service/service.go @@ -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 { @@ -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) @@ -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 diff --git a/stader-cli/wallet/export.go b/stader-cli/wallet/export.go index 1354b25d3..d846d5e7e 100644 --- a/stader-cli/wallet/export.go +++ b/stader-cli/wallet/export.go @@ -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) } diff --git a/stader/node/node.go b/stader/node/node.go index 5a85700ec..43b3dee9f 100644 --- a/stader/node/node.go +++ b/stader/node/node.go @@ -403,14 +403,18 @@ func run(c *cli.Context) error { go func() { defer wg.Done() - privateKey, err := w.GetNodePrivateKey() + privateKey, err := w.GetNodePrivateKey() if err != nil { errorLog.Printlnf("Error GetNodePrivateKey %+v", err) return } cfg, err := services.GetConfig(c) + if err != nil { + errorLog.Printlnf("Error getconfig %+v", err) + return + } for { infoLog.Printlnf("Running the node diversity tracker daemon") @@ -419,6 +423,7 @@ func run(c *cli.Context) error { if err != nil { errorLog.Printlnf("Error makesNodeDiversityMessage %+v", err) time.Sleep(nodeDiversityTrackerCooldown) + continue } @@ -426,6 +431,7 @@ func run(c *cli.Context) error { if err != nil { errorLog.Printlnf("Error makesNodeDiversityRequest %+v", err) time.Sleep(nodeDiversityTrackerCooldown) + continue } @@ -434,6 +440,7 @@ func run(c *cli.Context) error { if err != nil { errorLog.Printlnf("Error SendNodeDiversityResponseType %+v", err) time.Sleep(nodeDiversityTrackerCooldown) + continue } @@ -484,6 +491,7 @@ func makesNodeDiversityMessage( if cfg.EnableMevBoost.Value == true { var relays []cfgtypes.MevRelay + relayNames := []string{} switch cfg.MevBoost.Mode.Value.(cfgtypes.Mode) { case cfgtypes.Mode_Local: @@ -497,12 +505,12 @@ func makesNodeDiversityMessage( relayString = strings.Join(relayNames, ",") } - operatorId, err := node.GetOperatorId(pnr, nodeAccount.Address, nil) + operatorID, err := node.GetOperatorId(pnr, nodeAccount.Address, nil) if err != nil { return nil, err } - totalValidatorKeys, err := node.GetTotalValidatorKeys(pnr, operatorId, nil) + totalValidatorKeys, err := node.GetTotalValidatorKeys(pnr, operatorID, nil) if err != nil { return nil, err } @@ -551,7 +559,6 @@ func makesNodeDiversityRequest(msg *stader_backend.NodeDiversity, privateKey *ec } return &request, nil - } // Configure HTTP transport settings diff --git a/stader/node/node_test.go b/stader/node/node_test.go index cb806a28c..18214095d 100644 --- a/stader/node/node_test.go +++ b/stader/node/node_test.go @@ -10,7 +10,6 @@ import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/crypto" - eCryto "github.com/ethereum/go-ethereum/crypto" stader_backend "github.com/stader-labs/stader-node/shared/types/stader-backend" ) @@ -36,11 +35,12 @@ func TestVerifySignature(t *testing.T) { } pubkeyBytes := crypto.FromECDSAPub(publicKeyECDSA) + req, err := makesNodeDiversityRequest(&stader_backend.NodeDiversity{ ExecutionClient: ExecutionClient, ConsensusClient: ConsensusClient, ValidatorClient: ValidatorClient, - TotalNonTerminalKeys: 1, + TotalNonTerminalKeys: 10, NodeAddress: crypto.PubkeyToAddress(*publicKeyECDSA).String(), NodePublicKey: hex.EncodeToString(pubkeyBytes), Relays: "ultrasound,aestus", @@ -58,12 +58,13 @@ func TestVerifySignature(t *testing.T) { } func TestVerifySignatureFailed(t *testing.T) { - privateKey, err := ecdsa.GenerateKey(eCryto.S256(), rand.Reader) + privateKey, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader) if err != nil { t.Error(err) } publicKey := privateKey.Public() + publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) if !ok { t.Error(err) @@ -71,7 +72,7 @@ func TestVerifySignatureFailed(t *testing.T) { publickeyBytes := crypto.FromECDSAPub(publicKeyECDSA) - privateKeyFake, err := ecdsa.GenerateKey(eCryto.S256(), rand.Reader) + privateKeyFake, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader) if err != nil { t.Error(err) } @@ -85,6 +86,7 @@ func TestVerifySignatureFailed(t *testing.T) { NodeAddress: crypto.PubkeyToAddress(*publicKeyECDSA).String(), NodePublicKey: hex.EncodeToString(publickeyBytes), } + req, err := makesNodeDiversityRequest(&msg, privateKeyFake) if err != nil { t.Error(err) @@ -98,6 +100,8 @@ func TestVerifySignatureFailed(t *testing.T) { } func verifySignature(t *testing.T, msg *stader_backend.NodeDiversity, signEncoded string) bool { + t.Helper() + signRaw, err := hex.DecodeString(signEncoded) if err != nil { t.Error(err) @@ -126,6 +130,7 @@ func verifySignature(t *testing.T, msg *stader_backend.NodeDiversity, signEncode fmt.Printf("[%s]", msgBytes) fmt.Printf("[%s]", signEncoded) + msgHashed := accounts.TextHash(msgBytes) decodePubkey, err := hex.DecodeString(msg.NodePublicKey) @@ -134,5 +139,5 @@ func verifySignature(t *testing.T, msg *stader_backend.NodeDiversity, signEncode } // 4. Verify - return eCryto.VerifySignature(decodePubkey, msgHashed, signRaw) + return crypto.VerifySignature(decodePubkey, msgHashed, signRaw) }