Group Slashing Protection History Packages Idiomatically (#9873)

* rename

* gaz

* gaz

* Gaz

* rename

* edit

* gaz

* gaz

* build

* fix

* build

* fix up

* fix

* gaz

* cli import export

* gaz

* flag

* rev

* comm

* package renames

* radek
This commit is contained in:
Raul Jordan 2021-11-09 11:49:28 -05:00 committed by GitHub
parent 6e731bdedd
commit 0fb91437fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 128 additions and 409 deletions

View File

@ -1,17 +1,44 @@
load("@prysm//tools/go:def.bzl", "go_library")
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["slashing-protection.go"],
srcs = [
"export.go",
"import.go",
"log.go",
"slashing-protection.go",
],
importpath = "github.com/prysmaticlabs/prysm/cmd/validator/slashing-protection",
visibility = ["//visibility:public"],
deps = [
"//cmd:go_default_library",
"//cmd/validator/flags:go_default_library",
"//config/features:go_default_library",
"//io/file:go_default_library",
"//runtime/tos:go_default_library",
"//validator/slashing-protection:go_default_library",
"//validator/accounts/userprompt:go_default_library",
"//validator/db/kv:go_default_library",
"//validator/slashing-protection-history:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["import_export_test.go"],
embed = [":go_default_library"],
deps = [
"//cmd:go_default_library",
"//cmd/validator/flags:go_default_library",
"//io/file:go_default_library",
"//testing/assert:go_default_library",
"//testing/require:go_default_library",
"//validator/db/kv:go_default_library",
"//validator/db/testing:go_default_library",
"//validator/slashing-protection-history/format:go_default_library",
"//validator/testing:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
)

View File

@ -1,4 +1,4 @@
package slashingprotection
package historycmd
import (
"encoding/json"
@ -11,7 +11,7 @@ import (
"github.com/prysmaticlabs/prysm/io/file"
"github.com/prysmaticlabs/prysm/validator/accounts/userprompt"
"github.com/prysmaticlabs/prysm/validator/db/kv"
export "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format"
slashingprotection "github.com/prysmaticlabs/prysm/validator/slashing-protection-history"
"github.com/urfave/cli/v2"
)
@ -19,7 +19,7 @@ const (
jsonExportFileName = "slashing_protection.json"
)
// ExportSlashingProtectionJSONCli extracts a validator's slashing protection
// Extracts a validator's slashing protection
// history from their database and formats it into an EIP-3076 standard JSON
// file via a CLI entrypoint to make it easy to migrate machines or Ethereum consensus clients.
//
@ -29,7 +29,7 @@ const (
// 3. Call the function which actually exports the data from
// from the validator's db into an EIP standard slashing protection format
// 4. Format and save the JSON file to a user's specified output directory.
func ExportSlashingProtectionJSONCli(cliCtx *cli.Context) error {
func exportSlashingProtectionJSON(cliCtx *cli.Context) error {
log.Info(
"This command exports your validator's attestation and proposal history into " +
"a file that can then be imported into any other Prysm setup across computers",
@ -63,7 +63,7 @@ func ExportSlashingProtectionJSONCli(cliCtx *cli.Context) error {
log.WithError(err).Errorf("Could not close validator DB")
}
}()
eipJSON, err := export.ExportStandardProtectionJSON(cliCtx.Context, validatorDB)
eipJSON, err := slashingprotection.ExportStandardProtectionJSON(cliCtx.Context, validatorDB)
if err != nil {
return errors.Wrap(err, "could not export slashing protection history")
}
@ -98,7 +98,7 @@ func ExportSlashingProtectionJSONCli(cliCtx *cli.Context) error {
}
log.Infof(
"Successfully wrote %s. You can import this file using Prysm's "+
"validator slashing-protection import command in another machine",
"validator slashing-protection-history import command in another machine",
outputFilePath,
)
return nil

View File

@ -1,4 +1,4 @@
package slashingprotection
package historycmd
import (
"bytes"
@ -10,11 +10,11 @@ import (
"github.com/prysmaticlabs/prysm/io/file"
"github.com/prysmaticlabs/prysm/validator/accounts/userprompt"
"github.com/prysmaticlabs/prysm/validator/db/kv"
slashingProtectionFormat "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format"
slashingprotection "github.com/prysmaticlabs/prysm/validator/slashing-protection-history"
"github.com/urfave/cli/v2"
)
// ImportSlashingProtectionCLI reads an input slashing protection EIP-3076
// Reads an input slashing protection EIP-3076
// standard JSON file and attempts to insert its data into our validator DB.
//
// Steps:
@ -23,7 +23,7 @@ import (
// 3. Read the JSON file from user input.
// 4. Call the function which actually imports the data from
// from the standard slashing protection JSON file into our database.
func ImportSlashingProtectionCLI(cliCtx *cli.Context) error {
func importSlashingProtectionJSON(cliCtx *cli.Context) error {
var err error
dataDir := cliCtx.String(cmd.DataDirFlag.Name)
if !cliCtx.IsSet(cmd.DataDirFlag.Name) {
@ -71,7 +71,7 @@ func ImportSlashingProtectionCLI(cliCtx *cli.Context) error {
}
log.Infof("Starting import of slashing protection file %s", protectionFilePath)
buf := bytes.NewBuffer(enc)
if err := slashingProtectionFormat.ImportStandardProtectionJSON(
if err := slashingprotection.ImportStandardProtectionJSON(
cliCtx.Context, valDB, buf,
); err != nil {
return err

View File

@ -1,4 +1,4 @@
package slashingprotection
package historycmd
import (
"encoding/json"
@ -14,7 +14,7 @@ import (
"github.com/prysmaticlabs/prysm/testing/require"
"github.com/prysmaticlabs/prysm/validator/db/kv"
dbTest "github.com/prysmaticlabs/prysm/validator/db/testing"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
mocks "github.com/prysmaticlabs/prysm/validator/testing"
"github.com/urfave/cli/v2"
)
@ -66,11 +66,11 @@ func TestImportExportSlashingProtectionCli_RoundTrip(t *testing.T) {
cliCtx := setupCliCtx(t, dbPath, protectionFilePath, outputPath)
// We import the slashing protection history file via CLI.
err = ImportSlashingProtectionCLI(cliCtx)
err = importSlashingProtectionJSON(cliCtx)
require.NoError(t, err)
// We export the slashing protection history file via CLI.
err = ExportSlashingProtectionJSONCli(cliCtx)
err = exportSlashingProtectionJSON(cliCtx)
require.NoError(t, err)
// Attempt to read the exported file from the output directory.
@ -142,11 +142,11 @@ func TestImportExportSlashingProtectionCli_EmptyData(t *testing.T) {
cliCtx := setupCliCtx(t, dbPath, protectionFilePath, outputPath)
// We import the slashing protection history file via CLI.
err = ImportSlashingProtectionCLI(cliCtx)
err = importSlashingProtectionJSON(cliCtx)
require.NoError(t, err)
// We export the slashing protection history file via CLI.
err = ExportSlashingProtectionJSONCli(cliCtx)
err = exportSlashingProtectionJSON(cliCtx)
require.NoError(t, err)
// Attempt to read the exported file from the output directory.

View File

@ -0,0 +1,5 @@
package historycmd
import "github.com/sirupsen/logrus"
var log = logrus.WithField("prefix", "historycmd")

View File

@ -1,19 +1,18 @@
package slashing_protection
package historycmd
import (
"github.com/prysmaticlabs/prysm/cmd"
"github.com/prysmaticlabs/prysm/cmd/validator/flags"
"github.com/prysmaticlabs/prysm/config/features"
"github.com/prysmaticlabs/prysm/runtime/tos"
slashingprotection "github.com/prysmaticlabs/prysm/validator/slashing-protection"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// Commands for slashing protection.
var Commands = &cli.Command{
Name: "slashing-protection",
Category: "slashing-protection",
Name: "slashing-protection-history",
Category: "slashing-protection-history",
Usage: "defines commands for interacting your validator's slashing protection history",
Subcommands: []*cli.Command{
{
@ -31,7 +30,7 @@ var Commands = &cli.Command{
},
Action: func(cliCtx *cli.Context) error {
features.ConfigureValidator(cliCtx)
if err := slashingprotection.ExportSlashingProtectionJSONCli(cliCtx); err != nil {
if err := exportSlashingProtectionJSON(cliCtx); err != nil {
logrus.Fatalf("Could not export slashing protection file: %v", err)
}
return nil
@ -56,7 +55,7 @@ var Commands = &cli.Command{
},
Action: func(cliCtx *cli.Context) error {
features.ConfigureValidator(cliCtx)
err := slashingprotection.ImportSlashingProtectionCLI(cliCtx)
err := importSlashingProtectionJSON(cliCtx)
if err != nil {
logrus.Fatalf("Could not import slashing protection cli: %v", err)
}

View File

@ -98,7 +98,7 @@ var (
Usage: "Disable max-cover algorithm when selecting attestations for proposer",
}
enableSlashingProtectionPruning = &cli.BoolFlag{
Name: "enable-slashing-protection-pruning",
Name: "enable-slashing-protection-history-pruning",
Usage: "Enables the pruning of the validator client's slashing protection database",
}
disableOptimizedBalanceUpdate = &cli.BoolFlag{

View File

@ -7,7 +7,10 @@ go_library(
"prompt.go",
],
importpath = "github.com/prysmaticlabs/prysm/validator/accounts/userprompt",
visibility = ["//validator:__subpackages__"],
visibility = [
"//cmd:__subpackages__",
"//validator:__subpackages__",
],
deps = [
"//cmd/validator/flags:go_default_library",
"//io/file:go_default_library",

View File

@ -128,7 +128,7 @@ go_test(
"//validator/graffiti:go_default_library",
"//validator/keymanager/derived:go_default_library",
"//validator/keymanager/remote:go_default_library",
"//validator/slashing-protection/local/standard-protection-format:go_default_library",
"//validator/slashing-protection-history:go_default_library",
"//validator/testing:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_pkg_errors//:go_default_library",

View File

@ -15,7 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
"github.com/prysmaticlabs/prysm/testing/require"
"github.com/prysmaticlabs/prysm/testing/util"
interchangeformat "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history"
)
type eip3076TestCase struct {
@ -88,7 +88,7 @@ func TestEIP3076SpecTests(t *testing.T) {
validator, _, _, _ := setup(t)
if tt.GenesisValidatorsRoot != "" {
r, err := interchangeformat.RootFromHex(tt.GenesisValidatorsRoot)
r, err := history.RootFromHex(tt.GenesisValidatorsRoot)
require.NoError(t, validator.db.SaveGenesisValidatorsRoot(context.Background(), r[:]))
require.NoError(t, err)
}
@ -100,7 +100,7 @@ func TestEIP3076SpecTests(t *testing.T) {
t.Fatal(err)
}
b := bytes.NewBuffer(interchangeBytes)
if err := interchangeformat.ImportStandardProtectionJSON(context.Background(), validator.db, b); err != nil {
if err := history.ImportStandardProtectionJSON(context.Background(), validator.db, b); err != nil {
if step.ShouldSucceed {
t.Fatal(err)
}
@ -110,9 +110,9 @@ func TestEIP3076SpecTests(t *testing.T) {
// This loops through a list of block signings to attempt after importing the interchange data above.
for _, sb := range step.Blocks {
bSlot, err := interchangeformat.SlotFromString(sb.Slot)
bSlot, err := history.SlotFromString(sb.Slot)
require.NoError(t, err)
pk, err := interchangeformat.PubKeyFromHex(sb.Pubkey)
pk, err := history.PubKeyFromHex(sb.Pubkey)
require.NoError(t, err)
b := util.NewBeaconBlock()
b.Block.Slot = bSlot
@ -134,11 +134,11 @@ func TestEIP3076SpecTests(t *testing.T) {
// This loops through a list of attestation signings to attempt after importing the interchange data above.
for _, sa := range step.Attestations {
target, err := interchangeformat.EpochFromString(sa.TargetEpoch)
target, err := history.EpochFromString(sa.TargetEpoch)
require.NoError(t, err)
source, err := interchangeformat.EpochFromString(sa.SourceEpoch)
source, err := history.EpochFromString(sa.SourceEpoch)
require.NoError(t, err)
pk, err := interchangeformat.PubKeyFromHex(sa.Pubkey)
pk, err := history.PubKeyFromHex(sa.Pubkey)
require.NoError(t, err)
ia := &ethpb.IndexedAttestation{
Data: &ethpb.AttestationData{

View File

@ -19,7 +19,10 @@ go_library(
"schema.go",
],
importpath = "github.com/prysmaticlabs/prysm/validator/db/kv",
visibility = ["//validator:__subpackages__"],
visibility = [
"//cmd:__subpackages__",
"//validator:__subpackages__",
],
deps = [
"//async/abool:go_default_library",
"//async/event:go_default_library",

View File

@ -4,7 +4,10 @@ go_library(
name = "go_default_library",
srcs = ["setup_db.go"],
importpath = "github.com/prysmaticlabs/prysm/validator/db/testing",
visibility = ["//validator:__subpackages__"],
visibility = [
"//cmd:__subpackages__",
"//validator:__subpackages__",
],
deps = [
"//validator/db/iface:go_default_library",
"//validator/db/kv:go_default_library",

View File

@ -44,7 +44,7 @@ go_library(
"//validator/keymanager:go_default_library",
"//validator/keymanager/derived:go_default_library",
"//validator/keymanager/imported:go_default_library",
"//validator/slashing-protection/local/standard-protection-format:go_default_library",
"//validator/slashing-protection-history:go_default_library",
"@com_github_fsnotify_fsnotify//:go_default_library",
"@com_github_golang_jwt_jwt//:go_default_library",
"@com_github_grpc_ecosystem_go_grpc_middleware//:go_default_library",
@ -104,7 +104,7 @@ go_test(
"//validator/keymanager:go_default_library",
"//validator/keymanager/derived:go_default_library",
"//validator/keymanager/imported:go_default_library",
"//validator/slashing-protection/local/standard-protection-format/format:go_default_library",
"//validator/slashing-protection-history/format:go_default_library",
"//validator/testing:go_default_library",
"@com_github_golang_jwt_jwt//:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",

View File

@ -8,7 +8,7 @@ import (
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client"
slashing "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format"
slashing "github.com/prysmaticlabs/prysm/validator/slashing-protection-history"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"

View File

@ -12,7 +12,7 @@ import (
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
"github.com/prysmaticlabs/prysm/validator/db/kv"
"github.com/prysmaticlabs/prysm/validator/keymanager"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
mocks "github.com/prysmaticlabs/prysm/validator/testing"
)

View File

@ -3,13 +3,17 @@ load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"doc.go",
"export.go",
"helpers.go",
"import.go",
"log.go",
],
importpath = "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format",
visibility = ["//validator:__subpackages__"],
importpath = "github.com/prysmaticlabs/prysm/validator/slashing-protection-history",
visibility = [
"//cmd:__subpackages__",
"//validator:__subpackages__",
],
deps = [
"//config/params:go_default_library",
"//encoding/bytesutil:go_default_library",
@ -18,7 +22,7 @@ go_library(
"//proto/prysm/v1alpha1/slashings:go_default_library",
"//validator/db:go_default_library",
"//validator/db/kv:go_default_library",
"//validator/slashing-protection/local/standard-protection-format/format:go_default_library",
"//validator/slashing-protection-history/format:go_default_library",
"@com_github_k0kubun_go_ansi//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
@ -42,7 +46,7 @@ go_test(
"//testing/require:go_default_library",
"//validator/db/kv:go_default_library",
"//validator/db/testing:go_default_library",
"//validator/slashing-protection/local/standard-protection-format/format:go_default_library",
"//validator/slashing-protection-history/format:go_default_library",
"//validator/testing:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",

View File

@ -0,0 +1,4 @@
// Package history defines methods to parse, import, and export slashing protection data
// from a standard JSON file according to EIP-3076 https://eips.ethereum.org/EIPS/eip-3076. This format
// is critical to allow safe interoperability between Ethereum consensus clients.
package history

View File

@ -1,4 +1,4 @@
package interchangeformat
package history
import (
"bytes"
@ -11,7 +11,7 @@ import (
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/monitoring/progress"
"github.com/prysmaticlabs/prysm/validator/db"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
)
// ExportStandardProtectionJSON extracts all slashing protection data from a validator database

View File

@ -1,4 +1,4 @@
package interchangeformat
package history
import (
"context"
@ -9,7 +9,7 @@ import (
"github.com/prysmaticlabs/prysm/testing/assert"
"github.com/prysmaticlabs/prysm/testing/require"
dbtest "github.com/prysmaticlabs/prysm/validator/db/testing"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
)
func TestExportStandardProtectionJSON_EmptyGenesisRoot(t *testing.T) {

View File

@ -3,6 +3,6 @@ load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["format.go"],
importpath = "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format",
visibility = ["//validator:__subpackages__"],
importpath = "github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format",
visibility = ["//visibility:public"],
)

View File

@ -1,6 +1,3 @@
// Package format defines methods to parse, import, and export slashing protection data
// from a standard JSON file according to EIP-3076 https://eips.ethereum.org/EIPS/eip-3076. This format
// is critical to allow safe interoperability between Ethereum consensus clients.
package format
// InterchangeFormatVersion specified by https://eips.ethereum.org/EIPS/eip-3076.

View File

@ -1,4 +1,4 @@
package interchangeformat
package history
import (
"encoding/hex"

View File

@ -1,4 +1,4 @@
package interchangeformat
package history
import (
"bytes"
@ -15,7 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/slashings"
"github.com/prysmaticlabs/prysm/validator/db"
"github.com/prysmaticlabs/prysm/validator/db/kv"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
)
// ImportStandardProtectionJSON takes in EIP-3076 compliant JSON file used for slashing protection

View File

@ -1,4 +1,4 @@
package interchangeformat
package history
import (
"bytes"
@ -15,7 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/testing/require"
"github.com/prysmaticlabs/prysm/validator/db/kv"
dbtest "github.com/prysmaticlabs/prysm/validator/db/testing"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
valtest "github.com/prysmaticlabs/prysm/validator/testing"
logTest "github.com/sirupsen/logrus/hooks/test"
)

View File

@ -0,0 +1,5 @@
package history
import "github.com/sirupsen/logrus"
var log = logrus.WithField("prefix", "slashing-protection-history")

View File

@ -1,4 +1,4 @@
package interchangeformat_test
package history_test
import (
"bytes"
@ -12,8 +12,8 @@ import (
"github.com/prysmaticlabs/prysm/testing/require"
"github.com/prysmaticlabs/prysm/validator/db/kv"
dbtest "github.com/prysmaticlabs/prysm/validator/db/testing"
protectionFormat "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
slashtest "github.com/prysmaticlabs/prysm/validator/testing"
)
@ -37,12 +37,12 @@ func TestImportExport_RoundTrip(t *testing.T) {
buf := bytes.NewBuffer(blob)
// Next, we attempt to import it into our validator database.
err = protectionFormat.ImportStandardProtectionJSON(ctx, validatorDB, buf)
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
require.NoError(t, err)
// Next up, we export our slashing protection database into the EIP standard file.
// Next, we attempt to import it into our validator database.
eipStandard, err := protectionFormat.ExportStandardProtectionJSON(ctx, validatorDB)
eipStandard, err := history.ExportStandardProtectionJSON(ctx, validatorDB)
require.NoError(t, err)
// We compare the metadata fields from import to export.
@ -111,12 +111,12 @@ func TestImportExport_RoundTrip_SkippedAttestationEpochs(t *testing.T) {
buf := bytes.NewBuffer(blob)
// Next, we attempt to import it into our validator database.
err = protectionFormat.ImportStandardProtectionJSON(ctx, validatorDB, buf)
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
require.NoError(t, err)
// Next up, we export our slashing protection database into the EIP standard file.
// Next, we attempt to import it into our validator database.
eipStandard, err := protectionFormat.ExportStandardProtectionJSON(ctx, validatorDB)
eipStandard, err := history.ExportStandardProtectionJSON(ctx, validatorDB)
require.NoError(t, err)
// We compare the metadata fields from import to export.
@ -148,7 +148,7 @@ func TestImportInterchangeData_OK(t *testing.T) {
buf := bytes.NewBuffer(blob)
// Next, we attempt to import it into our validator database.
err = protectionFormat.ImportStandardProtectionJSON(ctx, validatorDB, buf)
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
require.NoError(t, err)
// Next, we attempt to retrieve the attesting and proposals histories from our database and
@ -256,7 +256,7 @@ func TestImportInterchangeData_OK_SavesBlacklistedPublicKeys(t *testing.T) {
buf := bytes.NewBuffer(blob)
// Next, we attempt to import it into our validator database.
err = protectionFormat.ImportStandardProtectionJSON(ctx, validatorDB, buf)
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
require.NoError(t, err)
// Assert the three slashable keys in the imported JSON were saved to the database.
@ -299,7 +299,7 @@ func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
// Next, we attempt to import it into our validator database and check that
// we obtain an error during the import process.
err = protectionFormat.ImportStandardProtectionJSON(ctx, validatorDB, buf)
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
assert.NotNil(t, err)
// Next, we attempt to retrieve the attesting and proposals histories from our database and

View File

@ -1,64 +0,0 @@
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"cli_export.go",
"cli_import.go",
"external.go",
"log.go",
"slasher_client.go",
],
importpath = "github.com/prysmaticlabs/prysm/validator/slashing-protection",
visibility = [
"//cmd/validator:__subpackages__",
"//validator:__subpackages__",
],
deps = [
"//api/grpc:go_default_library",
"//cmd:go_default_library",
"//cmd/validator/flags:go_default_library",
"//io/file:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//validator/accounts/userprompt:go_default_library",
"//validator/db/kv:go_default_library",
"//validator/slashing-protection/local/standard-protection-format:go_default_library",
"@com_github_grpc_ecosystem_go_grpc_middleware//:go_default_library",
"@com_github_grpc_ecosystem_go_grpc_middleware//retry:go_default_library",
"@com_github_grpc_ecosystem_go_grpc_middleware//tracing/opentracing:go_default_library",
"@com_github_grpc_ecosystem_go_grpc_prometheus//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@io_opencensus_go//plugin/ocgrpc:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//connectivity:go_default_library",
"@org_golang_google_grpc//credentials:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = [
"cli_import_export_test.go",
"external_test.go",
"slasher_client_test.go",
],
embed = [":go_default_library"],
deps = [
"//cmd:go_default_library",
"//cmd/validator/flags:go_default_library",
"//config/params:go_default_library",
"//encoding/bytesutil:go_default_library",
"//io/file:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//testing/assert:go_default_library",
"//testing/require:go_default_library",
"//validator/db/kv:go_default_library",
"//validator/db/testing:go_default_library",
"//validator/slashing-protection/local/standard-protection-format/format:go_default_library",
"//validator/testing:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@org_golang_google_grpc//metadata:go_default_library",
],
)

View File

@ -1,35 +0,0 @@
package slashingprotection
import (
"context"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
// CheckBlockSafety for blocks before submitting them to the node.
func (s *Service) CheckBlockSafety(ctx context.Context, blockHeader *ethpb.SignedBeaconBlockHeader) bool {
ps, err := s.slasherClient.IsSlashableBlock(ctx, blockHeader)
if err != nil {
log.Errorf("External slashing block protection returned an error: %v", err)
return false
}
if ps != nil && len(ps.ProposerSlashings) != 0 {
log.Warn("External slashing proposal protection found the block to be slashable")
return false
}
return true
}
// CheckAttestationSafety for attestations before submitting them to the node.
func (s *Service) CheckAttestationSafety(ctx context.Context, attestation *ethpb.IndexedAttestation) bool {
as, err := s.slasherClient.IsSlashableAttestation(ctx, attestation)
if err != nil {
log.Errorf("External slashing attestation protection returned an error: %v", err)
return false
}
if as != nil && len(as.AttesterSlashings) != 0 {
log.Warnf("External slashing attestation protection found the attestation to be slashable: %v", as)
return false
}
return true
}

View File

@ -1,50 +0,0 @@
package slashingprotection
import (
"context"
"testing"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/testing/assert"
mockSlasher "github.com/prysmaticlabs/prysm/validator/testing"
)
func TestService_VerifyAttestation(t *testing.T) {
s := &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: true}}
att := &eth.IndexedAttestation{
AttestingIndices: []uint64{1, 2},
Data: &eth.AttestationData{
Slot: 5,
CommitteeIndex: 2,
BeaconBlockRoot: []byte("great block"),
Source: &eth.Checkpoint{
Epoch: 4,
Root: []byte("good source"),
},
Target: &eth.Checkpoint{
Epoch: 10,
Root: []byte("good target"),
},
},
}
assert.Equal(t, false, s.CheckAttestationSafety(context.Background(), att), "Expected verify attestation to fail verification")
s = &Service{slasherClient: mockSlasher.MockSlasher{SlashAttestation: false}}
assert.Equal(t, true, s.CheckAttestationSafety(context.Background(), att), "Expected verify attestation to pass verification")
}
func TestService_VerifyBlock(t *testing.T) {
s := &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: true}}
blk := &eth.BeaconBlockHeader{
Slot: 0,
ProposerIndex: 0,
ParentRoot: bytesutil.PadTo([]byte("parent"), 32),
StateRoot: bytesutil.PadTo([]byte("state"), 32),
BodyRoot: bytesutil.PadTo([]byte("body"), 32),
}
sblk := &eth.SignedBeaconBlockHeader{Header: blk, Signature: params.BeaconConfig().EmptySignature[:]}
assert.Equal(t, false, s.CheckBlockSafety(context.Background(), sblk), "Expected verify block to fail verification")
s = &Service{slasherClient: mockSlasher.MockSlasher{SlashBlock: false}}
assert.Equal(t, true, s.CheckBlockSafety(context.Background(), sblk), "Expected verify block to pass verification")
}

View File

@ -1,9 +0,0 @@
load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["protector.go"],
importpath = "github.com/prysmaticlabs/prysm/validator/slashing-protection/iface",
visibility = ["//validator:__subpackages__"],
deps = ["//proto/prysm/v1alpha1:go_default_library"],
)

View File

@ -1,14 +0,0 @@
package iface
import (
"context"
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
// Protector interface defines the methods of the service that provides slashing protection.
type Protector interface {
CheckAttestationSafety(ctx context.Context, attestation *eth.IndexedAttestation) bool
CheckBlockSafety(ctx context.Context, blockHeader *eth.SignedBeaconBlockHeader) bool
Status() error
}

View File

@ -1,5 +0,0 @@
package interchangeformat
import "github.com/sirupsen/logrus"
var log = logrus.WithField("prefix", "slashing-protection-format")

View File

@ -1,5 +0,0 @@
package slashingprotection
import "github.com/sirupsen/logrus"
var log = logrus.WithField("prefix", "slashingprotection")

View File

@ -1,129 +0,0 @@
package slashingprotection
import (
"context"
"errors"
"fmt"
"strings"
"time"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
grpcutil "github.com/prysmaticlabs/prysm/api/grpc"
ethsl "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
)
// Service represents a service to manage the validator
// ￿slashing protection.
type Service struct {
cfg *Config
ctx context.Context
cancel context.CancelFunc
conn *grpc.ClientConn
grpcHeaders []string
slasherClient ethsl.SlasherClient
}
// Config for the validator service.
type Config struct {
Endpoint string
CertFlag string
GrpcMaxCallRecvMsgSizeFlag int
GrpcRetriesFlag uint
GrpcRetryDelay time.Duration
GrpcHeadersFlag string
}
// NewService creates a new validator service for the service
// registry.
func NewService(ctx context.Context, cfg *Config) (*Service, error) {
ctx, cancel := context.WithCancel(ctx)
return &Service{
cfg: cfg,
ctx: ctx,
cancel: cancel,
grpcHeaders: strings.Split(cfg.GrpcHeadersFlag, ","),
}, nil
}
// Start the slasher protection service and grpc client.
func (s *Service) Start() {
if s.cfg.Endpoint != "" {
s.slasherClient = s.startSlasherClient()
}
}
func (s *Service) startSlasherClient() ethsl.SlasherClient {
var dialOpt grpc.DialOption
if s.cfg.CertFlag != "" {
creds, err := credentials.NewClientTLSFromFile(s.cfg.CertFlag, "")
if err != nil {
log.Errorf("Could not get valid slasher credentials: %v", err)
return nil
}
dialOpt = grpc.WithTransportCredentials(creds)
} else {
dialOpt = grpc.WithInsecure()
log.Warn("You are using an insecure slasher gRPC connection! Please provide a certificate and key to use a secure connection.")
}
s.ctx = grpcutil.AppendHeaders(s.ctx, s.grpcHeaders)
opts := []grpc.DialOption{
dialOpt,
grpc.WithDefaultCallOptions(
grpc_retry.WithMax(s.cfg.GrpcRetriesFlag),
grpc_retry.WithBackoff(grpc_retry.BackoffLinear(s.cfg.GrpcRetryDelay)),
),
grpc.WithStatsHandler(&ocgrpc.ClientHandler{}),
grpc.WithStreamInterceptor(middleware.ChainStreamClient(
grpc_opentracing.StreamClientInterceptor(),
grpc_prometheus.StreamClientInterceptor,
grpc_retry.StreamClientInterceptor(),
)),
grpc.WithUnaryInterceptor(middleware.ChainUnaryClient(
grpc_opentracing.UnaryClientInterceptor(),
grpc_prometheus.UnaryClientInterceptor,
grpc_retry.UnaryClientInterceptor(),
grpcutil.LogRequests,
)),
}
conn, err := grpc.DialContext(s.ctx, s.cfg.Endpoint, opts...)
if err != nil {
log.Errorf("Could not dial slasher endpoint: %s, %v", s.cfg.Endpoint, err)
return nil
}
log.Debug("Successfully started slasher gRPC connection")
s.conn = conn
return ethsl.NewSlasherClient(s.conn)
}
// Stop the validator service.
func (s *Service) Stop() error {
s.cancel()
log.Info("Stopping slashing protection service")
if s.conn != nil {
return s.conn.Close()
}
return nil
}
// Status checks if the connection to slasher server is ready,
// returns error otherwise.
func (s *Service) Status() error {
if s.conn == nil {
return errors.New("no connection to slasher RPC")
}
if s.conn.GetState() != connectivity.Ready {
return fmt.Errorf("can`t connect to slasher server at: %v connection status: %v ", s.cfg.Endpoint, s.conn.GetState())
}
return nil
}

View File

@ -1,23 +0,0 @@
package slashingprotection
import (
"context"
"testing"
"github.com/prysmaticlabs/prysm/testing/assert"
"github.com/prysmaticlabs/prysm/testing/require"
"google.golang.org/grpc/metadata"
)
func TestGrpcHeaders(t *testing.T) {
s := &Service{
cfg: &Config{},
ctx: context.Background(),
grpcHeaders: []string{"first=value1", "second=value2"},
}
s.startSlasherClient()
md, _ := metadata.FromOutgoingContext(s.ctx)
require.Equal(t, 2, md.Len(), "MetadataV0 contains wrong number of values")
assert.Equal(t, "value1", md.Get("first")[0])
assert.Equal(t, "value2", md.Get("second")[0])
}

View File

@ -10,7 +10,10 @@ go_library(
"protection_history.go",
],
importpath = "github.com/prysmaticlabs/prysm/validator/testing",
visibility = ["//validator:__subpackages__"],
visibility = [
"//cmd:__subpackages__",
"//validator:__subpackages__",
],
deps = [
"//config/params:go_default_library",
"//crypto/bls:go_default_library",
@ -18,7 +21,7 @@ go_library(
"//encoding/bytesutil:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//validator/db/kv:go_default_library",
"//validator/slashing-protection/local/standard-protection-format/format:go_default_library",
"//validator/slashing-protection-history/format:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",

View File

@ -9,7 +9,7 @@ import (
"github.com/prysmaticlabs/prysm/crypto/rand"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/validator/db/kv"
"github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format"
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
)
// MockSlashingProtectionJSON creates a mock, full slashing protection JSON struct