Skip to content

Commit

Permalink
setupDB: Add isSlashingProtectionMinimal argument.
Browse files Browse the repository at this point in the history
The feature addition is located in `validator/node/node_test.go`.
The rest of this commit consists in minimal slashing protection testing.
  • Loading branch information
nalepae committed Dec 22, 2023
1 parent dd0cd05 commit 1dfec9a
Show file tree
Hide file tree
Showing 11 changed files with 1,615 additions and 1,378 deletions.
16 changes: 14 additions & 2 deletions cmd/validator/slashing-protection/import_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func setupCliCtx(
return cli.NewContext(&app, set, nil)
}

// TestImportExportSlashingProtectionCli_RoundTrip imports a EIP-3076 interchange format JSON file,
// and exports it back to disk. It then compare the exported file to the original file.
// This test is only suitable for complete slashing protection history database, since minimal
// 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")
Expand All @@ -59,7 +64,8 @@ func TestImportExportSlashingProtectionCli_RoundTrip(t *testing.T) {
require.NoError(t, err)

// We create a CLI context with the required values, such as the database datadir and output directory.
validatorDB := dbTest.SetupDB(t, pubKeys)
isSlashingProtectionMinimal := false
validatorDB := dbTest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
dbPath := validatorDB.DatabasePath()
require.NoError(t, validatorDB.Close())
cliCtx := setupCliCtx(t, dbPath, protectionFilePath, outputPath)
Expand Down Expand Up @@ -108,6 +114,11 @@ func TestImportExportSlashingProtectionCli_RoundTrip(t *testing.T) {
}
}

// TestImportExportSlashingProtectionCli_EmptyData imports a EIP-3076 interchange format JSON file,
// and exports it back to disk. It then compare the exported file to the original file.
// This test is only suitable for complete slashing protection history database, since minimal
// 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_EmptyData(t *testing.T) {
numValidators := 10
outputPath := filepath.Join(t.TempDir(), "slashing-exports")
Expand Down Expand Up @@ -135,7 +146,8 @@ func TestImportExportSlashingProtectionCli_EmptyData(t *testing.T) {
require.NoError(t, err)

// We create a CLI context with the required values, such as the database datadir and output directory.
validatorDB := dbTest.SetupDB(t, pubKeys)
isSlashingProtectionMinimal := false
validatorDB := dbTest.SetupDB(t, pubKeys, isSlashingProtectionMinimal)
dbPath := validatorDB.DatabasePath()
require.NoError(t, validatorDB.Close())
cliCtx := setupCliCtx(t, dbPath, protectionFilePath, outputPath)
Expand Down
52 changes: 28 additions & 24 deletions validator/client/propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func setup(t *testing.T) (*validator, *mocks, bls.SecretKey, func()) {
func setupWithKey(t *testing.T, validatorKey bls.SecretKey) (*validator, *mocks, bls.SecretKey, func()) {
var pubKey [fieldparams.BLSPubkeyLength]byte
copy(pubKey[:], validatorKey.PublicKey().Marshal())
valDB := testing2.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey})
valDB := testing2.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey}, false)
ctrl := gomock.NewController(t)
m := &mocks{
validatorClient: validatormock.NewMockValidatorClient(ctrl),
Expand Down Expand Up @@ -955,28 +955,32 @@ func TestGetGraffiti_Ok(t *testing.T) {
}

func TestGetGraffitiOrdered_Ok(t *testing.T) {
pubKey := [fieldparams.BLSPubkeyLength]byte{'a'}
valDB := testing2.SetupDB(t, [][fieldparams.BLSPubkeyLength]byte{pubKey})
ctrl := gomock.NewController(t)
m := &mocks{
validatorClient: validatormock.NewMockValidatorClient(ctrl),
}
m.validatorClient.EXPECT().
ValidatorIndex(gomock.Any(), &ethpb.ValidatorIndexRequest{PublicKey: pubKey[:]}).
Times(5).
Return(&ethpb.ValidatorIndexResponse{Index: 2}, nil)

v := &validator{
db: valDB,
validatorClient: m.validatorClient,
graffitiStruct: &graffiti.Graffiti{
Ordered: []string{"a", "b", "c"},
Default: "d",
},
}
for _, want := range [][]byte{bytesutil.PadTo([]byte{'a'}, 32), bytesutil.PadTo([]byte{'b'}, 32), bytesutil.PadTo([]byte{'c'}, 32), bytesutil.PadTo([]byte{'d'}, 32), bytesutil.PadTo([]byte{'d'}, 32)} {
got, err := v.getGraffiti(context.Background(), pubKey)
require.NoError(t, err)
require.DeepEqual(t, want, got)
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)
ctrl := gomock.NewController(t)
m := &mocks{
validatorClient: validatormock.NewMockValidatorClient(ctrl),
}
m.validatorClient.EXPECT().
ValidatorIndex(gomock.Any(), &ethpb.ValidatorIndexRequest{PublicKey: pubKey[:]}).
Times(5).
Return(&ethpb.ValidatorIndexResponse{Index: 2}, nil)

v := &validator{
db: valDB,
validatorClient: m.validatorClient,
graffitiStruct: &graffiti.Graffiti{
Ordered: []string{"a", "b", "c"},
Default: "d",
},
}
for _, want := range [][]byte{bytesutil.PadTo([]byte{'a'}, 32), bytesutil.PadTo([]byte{'b'}, 32), bytesutil.PadTo([]byte{'c'}, 32), bytesutil.PadTo([]byte{'d'}, 32), bytesutil.PadTo([]byte{'d'}, 32)} {
got, err := v.getGraffiti(context.Background(), pubKey)
require.NoError(t, err)
require.DeepEqual(t, want, got)
}
})
}
}
Loading

0 comments on commit 1dfec9a

Please sign in to comment.