diff --git a/changelog/rose2221_fix-slashing-export-nested-db.md b/changelog/rose2221_fix-slashing-export-nested-db.md new file mode 100644 index 0000000000..ecddf0d818 --- /dev/null +++ b/changelog/rose2221_fix-slashing-export-nested-db.md @@ -0,0 +1,3 @@ +### Fixed + +- Fix `slashing-protection-history export` failing when `validator.db` is in a nested folder like `data/direct/`. (#14954) diff --git a/cmd/validator/slashing-protection/export.go b/cmd/validator/slashing-protection/export.go index 21280f79ef..3207f0545e 100644 --- a/cmd/validator/slashing-protection/export.go +++ b/cmd/validator/slashing-protection/export.go @@ -58,10 +58,11 @@ func exportSlashingProtectionJSON(cliCtx *cli.Context) error { } // Ensure that the database is found under the specified dir or its subdirectories + var matchPath string if isDatabaseMinimal { - found, _, err = file.RecursiveDirFind(filesystem.DatabaseDirName, dataDir) + found, matchPath, err = file.RecursiveDirFind(filesystem.DatabaseDirName, dataDir) } else { - found, _, err = file.RecursiveFileFind(kv.ProtectionDbFileName, dataDir) + found, matchPath, err = file.RecursiveFileFind(kv.ProtectionDbFileName, dataDir) } if err != nil { @@ -74,6 +75,12 @@ func exportSlashingProtectionJSON(cliCtx *cli.Context) error { databaseFileDir = filesystem.DatabaseDirName } return fmt.Errorf("%s (validator database) was not found at path %s, so nothing to export", databaseFileDir, dataDir) + } else { + if !isDatabaseMinimal { + matchPath = filepath.Dir(matchPath) // strip the file name + } + dataDir = matchPath + log.Infof("Found validator database at path %s", dataDir) } // Open the validator database. diff --git a/cmd/validator/slashing-protection/import.go b/cmd/validator/slashing-protection/import.go index 85dcb2988d..706ab07097 100644 --- a/cmd/validator/slashing-protection/import.go +++ b/cmd/validator/slashing-protection/import.go @@ -3,6 +3,7 @@ package historycmd import ( "bytes" "fmt" + "path/filepath" "github.com/OffchainLabs/prysm/v6/cmd" "github.com/OffchainLabs/prysm/v6/cmd/validator/flags" @@ -45,16 +46,29 @@ func importSlashingProtectionJSON(cliCtx *cli.Context) error { } // Ensure that the database is found under the specified directory or its subdirectories + var matchPath string if isDatabaseMinimal { - found, _, err = file.RecursiveDirFind(filesystem.DatabaseDirName, dataDir) + found, matchPath, err = file.RecursiveDirFind(filesystem.DatabaseDirName, dataDir) } else { - found, _, err = file.RecursiveFileFind(kv.ProtectionDbFileName, dataDir) + found, matchPath, err = file.RecursiveFileFind(kv.ProtectionDbFileName, dataDir) } if err != nil { return errors.Wrapf(err, "error finding validator database at path %s", dataDir) } - + if !found { + databaseFileDir := kv.ProtectionDbFileName + if isDatabaseMinimal { + databaseFileDir = filesystem.DatabaseDirName + } + return fmt.Errorf("%s (validator database) was not found at path %s, so nothing to export", databaseFileDir, dataDir) + } else { + if !isDatabaseMinimal { + matchPath = filepath.Dir(matchPath) // strip the file name + } + dataDir = matchPath + log.Infof("Found validator database at path %s", dataDir) + } message := "Found existing database inside of %s" if !found { message = "Did not find existing database inside of %s, creating a new one" diff --git a/cmd/validator/slashing-protection/import_export_test.go b/cmd/validator/slashing-protection/import_export_test.go index 3348c9b9ab..33b7b09174 100644 --- a/cmd/validator/slashing-protection/import_export_test.go +++ b/cmd/validator/slashing-protection/import_export_test.go @@ -3,9 +3,6 @@ package historycmd import ( "encoding/json" "flag" - "path/filepath" - "testing" - "github.com/OffchainLabs/prysm/v6/cmd" "github.com/OffchainLabs/prysm/v6/cmd/validator/flags" "github.com/OffchainLabs/prysm/v6/io/file" @@ -16,6 +13,8 @@ import ( "github.com/OffchainLabs/prysm/v6/validator/slashing-protection-history/format" mocks "github.com/OffchainLabs/prysm/v6/validator/testing" "github.com/urfave/cli/v2" + "path/filepath" + "testing" ) func setupCliCtx( @@ -41,76 +40,100 @@ func setupCliCtx( // slashing protection history database will keep only the latest signed block slot / attestations, // and thus will not be able to export the same data as the original file. func TestImportExportSlashingProtectionCli_RoundTrip(t *testing.T) { - numValidators := 10 - outputPath := filepath.Join(t.TempDir(), "slashing-exports") - err := file.MkdirAll(outputPath) - require.NoError(t, err) - protectionFileName := "slashing_history_import.json" - - // Create some mock slashing protection history. and JSON file - pubKeys, err := mocks.CreateRandomPubKeys(numValidators) - require.NoError(t, err) - attestingHistory, proposalHistory := mocks.MockAttestingAndProposalHistories(pubKeys) - require.NoError(t, err) - mockJSON, err := mocks.MockSlashingProtectionJSON(pubKeys, attestingHistory, proposalHistory) - require.NoError(t, err) - - // We JSON encode the protection file and save it to disk as a JSON file. - encoded, err := json.Marshal(mockJSON) - require.NoError(t, err) - - protectionFilePath := filepath.Join(outputPath, protectionFileName) - err = file.WriteFile(protectionFilePath, encoded) - require.NoError(t, err) - - // We create a CLI context with the required values, such as the database datadir and output directory. - isSlashingProtectionMinimal := false - validatorDB := dbTest.SetupDB(t, pubKeys, isSlashingProtectionMinimal) - dbPath := validatorDB.DatabasePath() - require.NoError(t, validatorDB.Close()) - cliCtx := setupCliCtx(t, dbPath, protectionFilePath, outputPath) - - // We import the slashing protection history file via CLI. - err = importSlashingProtectionJSON(cliCtx) - require.NoError(t, err) - - // We export the slashing protection history file via CLI. - err = exportSlashingProtectionJSON(cliCtx) - require.NoError(t, err) - - // Attempt to read the exported file from the output directory. - enc, err := file.ReadFileAsBytes(filepath.Join(outputPath, jsonExportFileName)) - require.NoError(t, err) - - receivedJSON := &format.EIPSlashingProtectionFormat{} - err = json.Unmarshal(enc, receivedJSON) - require.NoError(t, err) - - // We verify the parsed JSON file matches. Given there is no guarantee of order, - // we will have to carefully compare and sort values as needed. - // - // First, we compare basic data such as the MetadataV0 value in the JSON file. - require.DeepEqual(t, mockJSON.Metadata, receivedJSON.Metadata) - wantedHistoryByPublicKey := make(map[string]*format.ProtectionData) - for _, item := range mockJSON.Data { - wantedHistoryByPublicKey[item.Pubkey] = item + layouts := []struct { + name string + mutate func(t *testing.T) (string, string) + }{ + { + name: "flat", + mutate: func(t *testing.T) (string, string) { + dbPath := t.TempDir() + return dbPath, dbPath + }, + }, + { + name: "nested", + mutate: func(t *testing.T) (string, string) { + dbPath := t.TempDir() + nestedDir := filepath.Join(dbPath, "data", "direct") + return dbPath, nestedDir + }, + }, } + for _, tc := range layouts { + t.Run(tc.name, func(t *testing.T) { + numValidators := 10 + outputPath := filepath.Join(t.TempDir(), "slashing-exports") + err := file.MkdirAll(outputPath) + require.NoError(t, err) + protectionFileName := "slashing_history_import.json" - // Next, we compare all the data for each validator public key. - for _, item := range receivedJSON.Data { - wanted, ok := wantedHistoryByPublicKey[item.Pubkey] - require.Equal(t, true, ok) - wantedAttsByRoot := make(map[string]*format.SignedAttestation) - for _, att := range wanted.SignedAttestations { - wantedAttsByRoot[att.SigningRoot] = att - } - for _, att := range item.SignedAttestations { - wantedAtt, ok := wantedAttsByRoot[att.SigningRoot] - require.Equal(t, true, ok) - require.DeepEqual(t, wantedAtt, att) - } - require.Equal(t, len(wanted.SignedBlocks), len(item.SignedBlocks)) - require.DeepEqual(t, wanted.SignedBlocks, item.SignedBlocks) + // Create some mock slashing protection history. and JSON file + pubKeys, err := mocks.CreateRandomPubKeys(numValidators) + require.NoError(t, err) + attestingHistory, proposalHistory := mocks.MockAttestingAndProposalHistories(pubKeys) + require.NoError(t, err) + mockJSON, err := mocks.MockSlashingProtectionJSON(pubKeys, attestingHistory, proposalHistory) + require.NoError(t, err) + + // We JSON encode the protection file and save it to disk as a JSON file. + encoded, err := json.Marshal(mockJSON) + require.NoError(t, err) + + protectionFilePath := filepath.Join(outputPath, protectionFileName) + err = file.WriteFile(protectionFilePath, encoded) + require.NoError(t, err) + + // We create a CLI context with the required values, such as the database datadir and output directory. + isSlashingProtectionMinimal := false + dataDir, dataPath := tc.mutate(t) + validatorDB := dbTest.SetupDB(t, dataPath, pubKeys, isSlashingProtectionMinimal) + require.NoError(t, validatorDB.Close()) + cliCtx := setupCliCtx(t, dataDir, protectionFilePath, outputPath) + + // We import the slashing protection history file via CLI. + err = importSlashingProtectionJSON(cliCtx) + require.NoError(t, err) + + // We export the slashing protection history file via CLI. + err = exportSlashingProtectionJSON(cliCtx) + require.NoError(t, err) + + // Attempt to read the exported file from the output directory. + enc, err := file.ReadFileAsBytes(filepath.Join(outputPath, jsonExportFileName)) + require.NoError(t, err) + + receivedJSON := &format.EIPSlashingProtectionFormat{} + err = json.Unmarshal(enc, receivedJSON) + require.NoError(t, err) + + // We verify the parsed JSON file matches. Given there is no guarantee of order, + // we will have to carefully compare and sort values as needed. + // + // First, we compare basic data such as the MetadataV0 value in the JSON file. + require.DeepEqual(t, mockJSON.Metadata, receivedJSON.Metadata) + wantedHistoryByPublicKey := make(map[string]*format.ProtectionData) + for _, item := range mockJSON.Data { + wantedHistoryByPublicKey[item.Pubkey] = item + } + + // Next, we compare all the data for each validator public key. + for _, item := range receivedJSON.Data { + wanted, ok := wantedHistoryByPublicKey[item.Pubkey] + require.Equal(t, true, ok) + wantedAttsByRoot := make(map[string]*format.SignedAttestation) + for _, att := range wanted.SignedAttestations { + wantedAttsByRoot[att.SigningRoot] = att + } + for _, att := range item.SignedAttestations { + wantedAtt, ok := wantedAttsByRoot[att.SigningRoot] + require.Equal(t, true, ok) + require.DeepEqual(t, wantedAtt, att) + } + require.Equal(t, len(wanted.SignedBlocks), len(item.SignedBlocks)) + require.DeepEqual(t, wanted.SignedBlocks, item.SignedBlocks) + } + }) } } @@ -147,7 +170,7 @@ func TestImportExportSlashingProtectionCli_EmptyData(t *testing.T) { // We create a CLI context with the required values, such as the database datadir and output directory. isSlashingProtectionMinimal := false - validatorDB := dbTest.SetupDB(t, pubKeys, isSlashingProtectionMinimal) + validatorDB := dbTest.SetupDB(t, t.TempDir(), pubKeys, isSlashingProtectionMinimal) dbPath := validatorDB.DatabasePath() require.NoError(t, validatorDB.Close()) cliCtx := setupCliCtx(t, dbPath, protectionFilePath, outputPath) diff --git a/config/proposer/loader/loader_test.go b/config/proposer/loader/loader_test.go index 67b9613cba..190a3551b3 100644 --- a/config/proposer/loader/loader_test.go +++ b/config/proposer/loader/loader_test.go @@ -930,7 +930,7 @@ func TestProposerSettingsLoader(t *testing.T) { set.Bool(flags.EnableBuilderFlag.Name, true, "") } cliCtx := cli.NewContext(&app, set, nil) - validatorDB := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal) + validatorDB := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal) if tt.withdb != nil { err := tt.withdb(validatorDB) require.NoError(t, err) @@ -978,7 +978,7 @@ func Test_ProposerSettingsLoaderWithOnlyBuilder_DoesNotSaveInDB(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Bool(flags.EnableBuilderFlag.Name, true, "") cliCtx := cli.NewContext(&app, set, nil) - validatorDB := dbTest.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal) + validatorDB := dbTest.SetupDB(t, t.TempDir(), [][fieldparams.BLSPubkeyLength]byte{}, isSlashingProtectionMinimal) loader, err := NewProposerSettingsLoader( cliCtx, validatorDB, diff --git a/validator/client/propose_test.go b/validator/client/propose_test.go index 61ad1b1e22..ff6a83dcd3 100644 --- a/validator/client/propose_test.go +++ b/validator/client/propose_test.go @@ -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)) diff --git a/validator/client/validator_test.go b/validator/client/validator_test.go index 00ef966f39..0d899f7032 100644 --- a/validator/client/validator_test.go +++ b/validator/client/validator_test.go @@ -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 := ðpb.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 := ðpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}} resp := ðpb.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 := ðpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}} resp := ðpb.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 := ðpb.DoppelGangerRequest{ValidatorRequests: []*ethpb.DoppelGangerRequest_ValidatorRequest{}} resp := ðpb.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 := ðpb.DoppelGangerResponse{Responses: []*ethpb.DoppelGangerResponse_ValidatorResponse{}} req := ðpb.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" diff --git a/validator/db/migrate_test.go b/validator/db/migrate_test.go index f95b92de12..f560b30f52 100644 --- a/validator/db/migrate_test.go +++ b/validator/db/migrate_test.go @@ -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{} diff --git a/validator/db/testing/setup_db.go b/validator/db/testing/setup_db.go index 44e4de3e0e..323e24017a 100644 --- a/validator/db/testing/setup_db.go +++ b/validator/db/testing/setup_db.go @@ -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 { diff --git a/validator/rpc/handlers_keymanager_test.go b/validator/rpc/handlers_keymanager_test.go index d4f19901c4..13119efafc 100644 --- a/validator/rpc/handlers_keymanager_test.go +++ b/validator/rpc/handlers_keymanager_test.go @@ -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, diff --git a/validator/slashing-protection-history/export_test.go b/validator/slashing-protection-history/export_test.go index fc3bf973ce..8132522884 100644 --- a/validator/slashing-protection-history/export_test.go +++ b/validator/slashing-protection-history/export_test.go @@ -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]) diff --git a/validator/slashing-protection-history/round_trip_test.go b/validator/slashing-protection-history/round_trip_test.go index 405b2a5c8b..66f962549d 100644 --- a/validator/slashing-protection-history/round_trip_test.go +++ b/validator/slashing-protection-history/round_trip_test.go @@ -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.