Skip to content

Commit

Permalink
A bit of cleanup based on goconst tool (#8529)
Browse files Browse the repository at this point in the history
* Make 1 occurence of 'foo', 'bar' and 'fizz!'

* Make 1 occurence of 'merkleizing list that is too large, over limit' string

* Limit password occurrences

* Make only 1 occurence of 'strongPass'

* Limit testMnemonic occurrences

* Limit expected epoch error messages

* Rename errors and use constant

* Update bazel dependencies

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
  • Loading branch information
drpaneas and rkapka committed Mar 2, 2021
1 parent b3dcbfe commit 46f6bd6
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 68 deletions.
4 changes: 3 additions & 1 deletion beacon-chain/rpc/beacon/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"google.golang.org/grpc/status"
)

const errEpoch = "Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d"

// ListValidatorAssignments retrieves the validator assignments for a given epoch,
// optional validator indices or public keys may be included to filter validator assignments.
func (bs *Server) ListValidatorAssignments(
Expand Down Expand Up @@ -45,7 +47,7 @@ func (bs *Server) ListValidatorAssignments(
if requestedEpoch > currentEpoch {
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
errEpoch,
currentEpoch,
requestedEpoch,
)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/beacon/assignments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestServer_ListAssignments_CannotRequestFutureEpoch(t *testing.T) {
GenesisTimeFetcher: &mock.ChainService{},
}

wanted := "Cannot retrieve information about an epoch in the future"
wanted := errNoEpochInfoError
_, err := bs.ListValidatorAssignments(
ctx,
&ethpb.ListValidatorAssignmentsRequest{
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/rpc/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (bs *Server) ListValidatorBalances(
if requestedEpoch > currentEpoch {
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
errEpoch,
currentEpoch,
requestedEpoch,
)
Expand Down Expand Up @@ -207,7 +207,7 @@ func (bs *Server) ListValidators(
if q.Epoch > currentEpoch {
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
errEpoch,
currentEpoch,
q.Epoch,
)
Expand Down Expand Up @@ -401,7 +401,7 @@ func (bs *Server) GetValidatorActiveSetChanges(
if requestedEpoch > currentEpoch {
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
errEpoch,
currentEpoch,
requestedEpoch,
)
Expand Down Expand Up @@ -775,7 +775,7 @@ func (bs *Server) GetIndividualVotes(
if req.Epoch > currentEpoch {
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
errEpoch,
currentEpoch,
req.Epoch,
)
Expand Down
12 changes: 8 additions & 4 deletions beacon-chain/rpc/beacon/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (
"github.com/prysmaticlabs/prysm/shared/timeutils"
)

const (
errNoEpochInfoError = "Cannot retrieve information about an epoch in the future"
)

func TestServer_GetValidatorActiveSetChanges_CannotRequestFutureEpoch(t *testing.T) {
beaconDB := dbTest.SetupDB(t)
ctx := context.Background()
Expand All @@ -46,7 +50,7 @@ func TestServer_GetValidatorActiveSetChanges_CannotRequestFutureEpoch(t *testing
BeaconDB: beaconDB,
}

wanted := "Cannot retrieve information about an epoch in the future"
wanted := errNoEpochInfoError
_, err = bs.GetValidatorActiveSetChanges(
ctx,
&ethpb.GetValidatorActiveSetChangesRequest{
Expand All @@ -73,7 +77,7 @@ func TestServer_ListValidatorBalances_CannotRequestFutureEpoch(t *testing.T) {
GenesisTimeFetcher: &mock.ChainService{},
}

wanted := "Cannot retrieve information about an epoch in the future"
wanted := errNoEpochInfoError
_, err = bs.ListValidatorBalances(
ctx,
&ethpb.ListValidatorBalancesRequest{
Expand Down Expand Up @@ -415,7 +419,7 @@ func TestServer_ListValidators_CannotRequestFutureEpoch(t *testing.T) {
},
}

wanted := "Cannot retrieve information about an epoch in the future"
wanted := errNoEpochInfoError
_, err = bs.ListValidators(
ctx,
&ethpb.ListValidatorsRequest{
Expand Down Expand Up @@ -1916,7 +1920,7 @@ func TestServer_GetIndividualVotes_RequestFutureSlot(t *testing.T) {
req := &ethpb.IndividualVotesRequest{
Epoch: helpers.SlotToEpoch(ds.GenesisTimeFetcher.CurrentSlot()) + 1,
}
wanted := "Cannot retrieve information about an epoch in the future"
wanted := errNoEpochInfoError
_, err := ds.GetIndividualVotes(context.Background(), req)
assert.ErrorContains(t, wanted, err)
}
Expand Down
8 changes: 4 additions & 4 deletions shared/htrutils/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

const merkleizingListLimitError = "merkleizing list that is too large, over limit"

func TestBitlistRoot(t *testing.T) {
hasher := hashutil.CustomSHA256Hasher()
capacity := uint64(10)
Expand Down Expand Up @@ -44,10 +46,9 @@ func TestBitwiseMerkleizeOverLimit(t *testing.T) {
}
count := uint64(2)
limit := uint64(1)
expected := "merkleizing list that is too large, over limit"

_, err := htrutils.BitwiseMerkleize(hasher, chunks, count, limit)
assert.ErrorContains(t, expected, err)
assert.ErrorContains(t, merkleizingListLimitError, err)
}

func TestBitwiseMerkleizeArrays(t *testing.T) {
Expand All @@ -73,10 +74,9 @@ func TestBitwiseMerkleizeArraysOverLimit(t *testing.T) {
}
count := uint64(2)
limit := uint64(1)
expected := "merkleizing list that is too large, over limit"

_, err := htrutils.BitwiseMerkleizeArrays(hasher, chunks, count, limit)
assert.ErrorContains(t, expected, err)
assert.ErrorContains(t, merkleizingListLimitError, err)
}

func TestPack(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions shared/mputil/multilock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,31 +312,31 @@ func TestSyncCondCompatibility(t *testing.T) {
var wg sync.WaitGroup
wg.Add(2)
cond := sync.NewCond(NewMultilock("A", "C"))

sharedRsc := "foo"
var testValues = [3]string{"foo", "bar", "fizz!"}
sharedRsc := testValues[0]

go func() {
cond.L.Lock()
for sharedRsc == "foo" {
for sharedRsc == testValues[0] {
cond.Wait()
}
sharedRsc = "fizz!"
sharedRsc = testValues[2]
cond.Broadcast()
cond.L.Unlock()
wg.Done()
}()

go func() {
cond.L.Lock()
sharedRsc = "bar"
sharedRsc = testValues[1]
cond.Broadcast()
for sharedRsc == "bar" {
for sharedRsc == testValues[1] {
cond.Wait()
}
cond.L.Unlock()
wg.Done()
}()

wg.Wait()
assert.Equal(t, "fizz!", sharedRsc)
assert.Equal(t, testValues[2], sharedRsc)
}
4 changes: 2 additions & 2 deletions tools/keystores/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
)

const password = "secretPassw0rd$1999"

type cliConfig struct {
keystoresPath string
password string
Expand Down Expand Up @@ -70,7 +72,6 @@ func setupRandomDir(t testing.TB) string {

func TestDecrypt(t *testing.T) {
keystoresDir := setupRandomDir(t)
password := "secretPassw0rd$1999"
keystore, privKey := createRandomKeystore(t, password)
// We write a random keystore to a keystores directory.
encodedKeystore, err := json.MarshalIndent(keystore, "", "\t")
Expand Down Expand Up @@ -109,7 +110,6 @@ func TestDecrypt(t *testing.T) {

func TestEncrypt(t *testing.T) {
keystoresDir := setupRandomDir(t)
password := "secretPassw0rd$1999"
keystoreFilePath := filepath.Join(keystoresDir, "keystore.json")
privKey, err := bls.RandKey()
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions validator/accounts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ go_test(
"//validator/keymanager/derived:go_default_library",
"//validator/keymanager/imported:go_default_library",
"//validator/keymanager/remote:go_default_library",
"//validator/testing:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_google_uuid//:go_default_library",
Expand Down
7 changes: 2 additions & 5 deletions validator/accounts/accounts_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"encoding/hex"
"encoding/json"
constant "github.com/prysmaticlabs/prysm/validator/testing"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -22,10 +23,6 @@ import (
"github.com/prysmaticlabs/prysm/validator/keymanager/derived"
)

var (
testMnemonic = "tumble turn jewel sudden social great water general cabin jacket bounce dry flip monster advance problem social half flee inform century chicken hard reason"
)

func TestBackupAccounts_Noninteractive_Derived(t *testing.T) {
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
// Specify the password locally to this file for convenience.
Expand Down Expand Up @@ -69,7 +66,7 @@ func TestBackupAccounts_Noninteractive_Derived(t *testing.T) {
// Create 2 accounts
derivedKM, ok := km.(*derived.Keymanager)
require.Equal(t, true, ok)
err = derivedKM.RecoverAccountsFromMnemonic(cliCtx.Context, testMnemonic, "", 2)
err = derivedKM.RecoverAccountsFromMnemonic(cliCtx.Context, constant.TestMnemonic, "", 2)
require.NoError(t, err)

// Obtain the public keys of the accounts we created
Expand Down
3 changes: 2 additions & 1 deletion validator/accounts/accounts_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts
import (
"context"
"fmt"
constant "github.com/prysmaticlabs/prysm/validator/testing"
"io/ioutil"
"math"
"os"
Expand Down Expand Up @@ -249,7 +250,7 @@ func TestListAccounts_DerivedKeymanager(t *testing.T) {
require.NoError(t, err)

numAccounts := 5
err = keymanager.RecoverAccountsFromMnemonic(cliCtx.Context, testMnemonic, "", numAccounts)
err = keymanager.RecoverAccountsFromMnemonic(cliCtx.Context, constant.TestMnemonic, "", numAccounts)
require.NoError(t, err)

rescueStdout := os.Stdout
Expand Down
8 changes: 4 additions & 4 deletions validator/client/wait_for_activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"fmt"
constant "github.com/prysmaticlabs/prysm/validator/testing"
"testing"
"time"

Expand Down Expand Up @@ -304,8 +305,7 @@ func TestWaitForActivation_AccountsChanged(t *testing.T) {
})

t.Run("Derived keymanager", func(t *testing.T) {
mnemonic := "tumble turn jewel sudden social great water general cabin jacket bounce dry flip monster advance problem social half flee inform century chicken hard reason"
seed := bip39.NewSeed(mnemonic, "")
seed := bip39.NewSeed(constant.TestMnemonic, "")
inactivePrivKey, err :=
util.PrivateKeyFromSeedAndPath(seed, fmt.Sprintf(derived.ValidatingKeyDerivationPathTemplate, 0))
require.NoError(t, err)
Expand All @@ -327,7 +327,7 @@ func TestWaitForActivation_AccountsChanged(t *testing.T) {
ListenForChanges: true,
})
require.NoError(t, err)
err = km.RecoverAccountsFromMnemonic(ctx, mnemonic, "", 1)
err = km.RecoverAccountsFromMnemonic(ctx, constant.TestMnemonic, "", 1)
require.NoError(t, err)
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
v := validator{
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestWaitForActivation_AccountsChanged(t *testing.T) {
go func() {
// We add the active key into the keymanager and simulate a key refresh.
time.Sleep(time.Second * 1)
err = km.RecoverAccountsFromMnemonic(ctx, mnemonic, "", 2)
err = km.RecoverAccountsFromMnemonic(ctx, constant.TestMnemonic, "", 2)
require.NoError(t, err)
channel <- struct{}{}
}()
Expand Down
1 change: 1 addition & 0 deletions validator/keymanager/derived/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ go_test(
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"//validator/accounts/testing:go_default_library",
"//validator/testing:go_default_library",
"@com_github_tyler_smith_go_bip39//:go_default_library",
"@com_github_wealdtech_go_eth2_util//:go_default_library",
],
Expand Down

0 comments on commit 46f6bd6

Please sign in to comment.