Fix validator export failing when validator.db is in nested directory (#15351)

* files changed

* added changelog file

* added tests and formatting changes

* fixed bugs

* fixed formatting
This commit is contained in:
Rose Jethani
2025-05-30 20:23:19 +05:30
committed by GitHub
parent 711984d942
commit 3300866572
12 changed files with 162 additions and 115 deletions

View File

@@ -81,7 +81,7 @@ func setup(t *testing.T, isSlashingProtectionMinimal bool) (*validator, *mocks,
func setupWithKey(t *testing.T, validatorKey bls.SecretKey, isSlashingProtectionMinimal bool) (*validator, *mocks, bls.SecretKey, func()) {
var pubKey [fieldparams.BLSPubkeyLength]byte
copy(pubKey[:], validatorKey.PublicKey().Marshal())
valDB := testing2.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey}, isSlashingProtectionMinimal)
valDB := testing2.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{pubKey}, isSlashingProtectionMinimal)
ctrl := gomock.NewController(t)
m := &mocks{
validatorClient: validatormock.NewMockValidatorClient(ctrl),
@@ -1105,7 +1105,7 @@ func TestGetGraffitiOrdered_Ok(t *testing.T) {
for _, isSlashingProtectionMinimal := range [...]bool{false, true} {
t.Run(fmt.Sprintf("SlashingProtectionMinimal:%v", isSlashingProtectionMinimal), func(t *testing.T) {
pubKey := [fieldparams.BLSPubkeyLength]byte{'a'}
valDB := testing2.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey}, isSlashingProtectionMinimal)
valDB := testing2.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{pubKey}, isSlashingProtectionMinimal)
ctrl := gomock.NewController(t)
m := &mocks{
validatorClient: validatormock.NewMockValidatorClient(ctrl),
@@ -1188,7 +1188,7 @@ func Test_validator_DeleteGraffiti(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := &validator{
db: testing2.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey}, false),
db: testing2.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{pubKey}, false),
proposerSettings: tt.proposerSettings,
}
err := v.DeleteGraffiti(context.Background(), pubKey)
@@ -1268,7 +1268,7 @@ func Test_validator_SetGraffiti(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := &validator{
db: testing2.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey}, false),
db: testing2.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{pubKey}, false),
proposerSettings: tt.proposerSettings,
}
err := v.SetGraffiti(context.Background(), pubKey, []byte(tt.graffiti))

View File

@@ -165,7 +165,7 @@ func TestWaitForChainStart_SetsGenesisInfo(t *testing.T) {
defer ctrl.Finish()
client := validatormock.NewMockValidatorClient(ctrl)
db := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
v := validator{
validatorClient: client,
db: db,
@@ -215,7 +215,7 @@ func TestWaitForChainStart_SetsGenesisInfo_IncorrectSecondTry(t *testing.T) {
defer ctrl.Finish()
client := validatormock.NewMockValidatorClient(ctrl)
db := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
v := validator{
validatorClient: client,
db: db,
@@ -925,7 +925,7 @@ func TestValidator_CheckDoppelGanger(t *testing.T) {
km := genMockKeymanager(t, 10)
keys, err := km.FetchValidatingPublicKeys(context.Background())
assert.NoError(t, err)
db := dbTest.SetupDB(t, keys, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), keys, isSlashingProtectionMinimal)
req := &ethpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}}
for _, k := range keys {
pkey := k
@@ -959,7 +959,7 @@ func TestValidator_CheckDoppelGanger(t *testing.T) {
km := genMockKeymanager(t, 10)
keys, err := km.FetchValidatingPublicKeys(context.Background())
assert.NoError(t, err)
db := dbTest.SetupDB(t, keys, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), keys, isSlashingProtectionMinimal)
req := &ethpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}}
resp := &ethpb.DoppelGangerResponse{Responses: []*ethpb.DoppelGangerResponse_ValidatorResponse{}}
for i, k := range keys {
@@ -1000,7 +1000,7 @@ func TestValidator_CheckDoppelGanger(t *testing.T) {
km := genMockKeymanager(t, 10)
keys, err := km.FetchValidatingPublicKeys(context.Background())
assert.NoError(t, err)
db := dbTest.SetupDB(t, keys, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), keys, isSlashingProtectionMinimal)
req := &ethpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}}
resp := &ethpb.DoppelGangerResponse{Responses: []*ethpb.DoppelGangerResponse_ValidatorResponse{}}
for i, k := range keys {
@@ -1039,7 +1039,7 @@ func TestValidator_CheckDoppelGanger(t *testing.T) {
km := genMockKeymanager(t, 10)
keys, err := km.FetchValidatingPublicKeys(context.Background())
assert.NoError(t, err)
db := dbTest.SetupDB(t, keys, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), keys, isSlashingProtectionMinimal)
req := &ethpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}}
resp := &ethpb.DoppelGangerResponse{Responses: []*ethpb.DoppelGangerResponse_ValidatorResponse{}}
attLimit := 5
@@ -1085,7 +1085,7 @@ func TestValidator_CheckDoppelGanger(t *testing.T) {
km := genMockKeymanager(t, 1)
keys, err := km.FetchValidatingPublicKeys(context.Background())
assert.NoError(t, err)
db := dbTest.SetupDB(t, keys, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), keys, isSlashingProtectionMinimal)
resp := &ethpb.DoppelGangerResponse{Responses: []*ethpb.DoppelGangerResponse_ValidatorResponse{}}
req := &ethpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}}
for _, k := range keys {
@@ -1123,7 +1123,7 @@ func TestValidatorAttestationsAreOrdered(t *testing.T) {
km := genMockKeymanager(t, 10)
keys, err := km.FetchValidatingPublicKeys(context.Background())
assert.NoError(t, err)
db := dbTest.SetupDB(t, keys, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), keys, isSlashingProtectionMinimal)
k := keys[0]
att := createAttestation(10, 14)
@@ -1303,7 +1303,7 @@ func TestValidator_WaitForKeymanagerInitialization_web3Signer(t *testing.T) {
for _, isSlashingProtectionMinimal := range [...]bool{false, true} {
t.Run(fmt.Sprintf("SlashingProtectionMinimal:%v", isSlashingProtectionMinimal), func(t *testing.T) {
ctx := context.Background()
db := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
root := make([]byte, 32)
copy(root[2:], "a")
err := db.SaveGenesisValidatorsRoot(ctx, root)
@@ -1336,7 +1336,7 @@ func TestValidator_WaitForKeymanagerInitialization_Web(t *testing.T) {
for _, isSlashingProtectionMinimal := range [...]bool{false, true} {
t.Run(fmt.Sprintf("SlashingProtectionMinimal:%v", isSlashingProtectionMinimal), func(t *testing.T) {
ctx := context.Background()
db := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
root := make([]byte, 32)
copy(root[2:], "a")
err := db.SaveGenesisValidatorsRoot(ctx, root)
@@ -1370,7 +1370,7 @@ func TestValidator_WaitForKeymanagerInitialization_Interop(t *testing.T) {
for _, isSlashingProtectionMinimal := range [...]bool{false, true} {
t.Run(fmt.Sprintf("SlashingProtectionMinimal:%v", isSlashingProtectionMinimal), func(t *testing.T) {
ctx := context.Background()
db := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
root := make([]byte, 32)
copy(root[2:], "a")
err := db.SaveGenesisValidatorsRoot(ctx, root)
@@ -1430,7 +1430,7 @@ func TestValidator_PushSettings(t *testing.T) {
for _, isSlashingProtectionMinimal := range [...]bool{false, true} {
ctrl := gomock.NewController(t)
ctx := context.Background()
db := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
db := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
client := validatormock.NewMockValidatorClient(ctrl)
nodeClient := validatormock.NewMockNodeClient(ctrl)
defaultFeeHex := "0x046Fb65722E7b2455043BFEBf6177F1D2e9738D9"

View File

@@ -26,7 +26,7 @@ func TestMigrateUp_NoDBFound(t *testing.T) {
// This, it is tested only for complete slashing protection database.
func TestMigrateUp_OK(t *testing.T) {
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, nil, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), nil, isSlashingProtectionMinimal)
dbPath := validatorDB.DatabasePath()
require.NoError(t, validatorDB.Close())
app := cli.App{}
@@ -52,7 +52,7 @@ func TestMigrateDown_NoDBFound(t *testing.T) {
// This, it is tested only for complete slashing protection database.
func TestMigrateDown_OK(t *testing.T) {
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, nil, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), nil, isSlashingProtectionMinimal)
dbPath := validatorDB.DatabasePath()
require.NoError(t, validatorDB.Close())
app := cli.App{}

View File

@@ -13,7 +13,7 @@ import (
// SetupDB instantiates and returns a DB instance for the validator client.
// The `minimal` flag indicates whether the DB should be instantiated with minimal, filesystem
// slashing protection database.
func SetupDB(t testing.TB, pubkeys [][fieldparams.BLSPubkeyLength]byte, minimal bool) iface.ValidatorDB {
func SetupDB(t testing.TB, dataPath string, pubkeys [][fieldparams.BLSPubkeyLength]byte, minimal bool) iface.ValidatorDB {
var (
db iface.ValidatorDB
err error
@@ -22,10 +22,10 @@ func SetupDB(t testing.TB, pubkeys [][fieldparams.BLSPubkeyLength]byte, minimal
// Create a new DB instance.
if minimal {
config := &filesystem.Config{PubKeys: pubkeys}
db, err = filesystem.NewStore(t.TempDir(), config)
db, err = filesystem.NewStore(dataPath, config)
} else {
config := &kv.Config{PubKeys: pubkeys}
db, err = kv.NewKVStore(context.Background(), t.TempDir(), config)
db, err = kv.NewKVStore(context.Background(), dataPath, config)
}
if err != nil {

View File

@@ -1110,7 +1110,7 @@ func TestServer_SetGasLimit(t *testing.T) {
m := &testutil.FakeValidator{}
err := m.SetProposerSettings(ctx, tt.proposerSettings)
require.NoError(t, err)
validatorDB := dbtest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
vs, err := client.NewValidatorService(ctx, &client.Config{
Validator: m,
DB: validatorDB,
@@ -1299,7 +1299,7 @@ func TestServer_DeleteGasLimit(t *testing.T) {
m := &testutil.FakeValidator{}
err := m.SetProposerSettings(ctx, tt.proposerSettings)
require.NoError(t, err)
validatorDB := dbtest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
vs, err := client.NewValidatorService(ctx, &client.Config{
Validator: m,
DB: validatorDB,
@@ -1777,7 +1777,7 @@ func TestServer_FeeRecipientByPubkey(t *testing.T) {
m := &testutil.FakeValidator{}
err := m.SetProposerSettings(ctx, tt.proposerSettings)
require.NoError(t, err)
validatorDB := dbtest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
// save a default here
vs, err := client.NewValidatorService(ctx, &client.Config{
@@ -1889,7 +1889,7 @@ func TestServer_DeleteFeeRecipientByPubkey(t *testing.T) {
m := &testutil.FakeValidator{}
err := m.SetProposerSettings(ctx, tt.proposerSettings)
require.NoError(t, err)
validatorDB := dbtest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal)
vs, err := client.NewValidatorService(ctx, &client.Config{
Validator: m,
DB: validatorDB,

View File

@@ -21,7 +21,7 @@ func TestExportStandardProtectionJSON_EmptyGenesisRoot(t *testing.T) {
pubKeys := [][fieldparams.BLSPubkeyLength]byte{
{1},
}
validatorDB := dbtest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), pubKeys, isSlashingProtectionMinimal)
_, err := ExportStandardProtectionJSON(ctx, validatorDB)
require.ErrorContains(t, "genesis validators root is empty", err)
genesisValidatorsRoot := [32]byte{1}
@@ -40,7 +40,7 @@ func Test_getSignedAttestationsByPubKey(t *testing.T) {
{1},
}
ctx := context.Background()
validatorDB := dbtest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), pubKeys, isSlashingProtectionMinimal)
// No attestation history stored should return empty.
signedAttestations, err := signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0])
@@ -98,7 +98,7 @@ func Test_getSignedAttestationsByPubKey(t *testing.T) {
ctx := context.Background()
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), pubKeys, isSlashingProtectionMinimal)
// No attestation history stored should return empty.
signedAttestations, err := signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0])
@@ -145,7 +145,7 @@ func Test_getSignedAttestationsByPubKey(t *testing.T) {
ctx := context.Background()
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), pubKeys, isSlashingProtectionMinimal)
// No attestation history stored should return empty.
signedAttestations, err := signedAttestationsByPubKey(ctx, validatorDB, pubKeys[0])
@@ -196,7 +196,7 @@ func Test_getSignedBlocksByPubKey(t *testing.T) {
{1},
}
ctx := context.Background()
validatorDB := dbtest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), pubKeys, isSlashingProtectionMinimal)
// No highest and/or lowest signed blocks will return empty.
signedBlocks, err := signedBlocksByPubKey(ctx, validatorDB, pubKeys[0])

View File

@@ -29,7 +29,7 @@ func TestImportExport_RoundTrip(t *testing.T) {
require.NoError(t, err)
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, publicKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), publicKeys, isSlashingProtectionMinimal)
// First we setup some mock attesting and proposal histories and create a mock
// standard slashing protection format JSON struct.
@@ -98,7 +98,7 @@ func TestImportExport_RoundTrip_SkippedAttestationEpochs(t *testing.T) {
require.NoError(t, err)
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), pubKeys, isSlashingProtectionMinimal)
wanted := &format.EIPSlashingProtectionFormat{
Metadata: struct {
InterchangeFormatVersion string `json:"interchange_format_version"`
@@ -164,7 +164,7 @@ func TestImportExport_FilterKeys(t *testing.T) {
require.NoError(t, err)
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, publicKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), publicKeys, isSlashingProtectionMinimal)
// First we setup some mock attesting and proposal histories and create a mock
// standard slashing protection format JSON struct.
@@ -209,7 +209,7 @@ func TestImportInterchangeData_OK(t *testing.T) {
require.NoError(t, err)
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, publicKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), publicKeys, isSlashingProtectionMinimal)
// First we setup some mock attesting and proposal histories and create a mock
// standard slashing protection format JSON struct.
@@ -279,7 +279,7 @@ func TestImportInterchangeData_OK_SavesBlacklistedPublicKeys(t *testing.T) {
require.NoError(t, err)
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, publicKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), publicKeys, isSlashingProtectionMinimal)
// First we setup some mock attesting and proposal histories and create a mock
// standard slashing protection format JSON struct.
@@ -374,7 +374,7 @@ func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
require.NoError(t, err)
isSlashingProtectionMinimal := false
validatorDB := dbtest.SetupDB(t, publicKeys, isSlashingProtectionMinimal)
validatorDB := dbtest.SetupDB(t, t.TempDir(), publicKeys, isSlashingProtectionMinimal)
// First we setup some mock attesting and proposal histories and create a mock
// standard slashing protection format JSON struct.