New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export Slashing Protection History Via CLI #8040
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
470fb8f
include cli entrypoint for history exports
rauljordan 6434528
builds properly
rauljordan 058c903
test to confirm we export the data as expected
rauljordan 699993e
abstract helpers properly
rauljordan 5f83c1e
full test suite
rauljordan dfac237
Merge branch 'feature/slashing-interchange' into export-history-cli
rauljordan 1b337c2
gaz
rauljordan 437e545
Merge branch 'export-history-cli' of github.com:prysmaticlabs/prysm i…
rauljordan ab219e4
better errors
rauljordan 349fb61
Merge branch 'feature/slashing-interchange' into export-history-cli
rauljordan fd70d97
marshal ident
rauljordan 779d216
Merge branch 'export-history-cli' of github.com:prysmaticlabs/prysm i…
rauljordan b458e14
Merge branch 'feature/slashing-interchange' into export-history-cli
rauljordan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package slashingprotection | ||
|
||
import ( | ||
"encoding/json" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/shared/cmd" | ||
"github.com/prysmaticlabs/prysm/shared/fileutil" | ||
"github.com/prysmaticlabs/prysm/validator/db/kv" | ||
"github.com/prysmaticlabs/prysm/validator/flags" | ||
export "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
const ( | ||
jsonExportFileName = "slashing_protection.json" | ||
) | ||
|
||
// ExportSlashingProtectionJSONCli 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 eth2 clients. | ||
// | ||
// Steps: | ||
// 1. Parse a path to the validator's datadir from the CLI context. | ||
// 2. Open the validator database file. | ||
// 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 { | ||
datadir := cliCtx.String(cmd.DataDirFlag.Name) | ||
validatorDB, err := kv.NewKVStore(datadir, nil) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not open database at %s", datadir) | ||
} | ||
eipJSON, err := export.ExportStandardProtectionJSON(cliCtx.Context, validatorDB) | ||
if err != nil { | ||
return errors.Wrap(err, "could not export slashing protection history") | ||
} | ||
outputDir := cliCtx.String(flags.SlashingProtectionExportDirFlag.Name) | ||
exists, err := fileutil.HasDir(outputDir) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not check if output directory %s already exists", outputDir) | ||
} | ||
if !exists { | ||
if err := fileutil.MkdirAll(outputDir); err != nil { | ||
return errors.Wrapf(err, "could not create output directory %s", outputDir) | ||
} | ||
} | ||
outputFilePath := filepath.Join(outputDir, jsonExportFileName) | ||
encoded, err := json.MarshalIndent(eipJSON, "", "\t") | ||
if err != nil { | ||
return errors.Wrap(err, "could not JSON marshal slashing protection history") | ||
} | ||
return fileutil.WriteFile(outputFilePath, encoded) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package slashingprotection | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/shared/cmd" | ||
"github.com/prysmaticlabs/prysm/shared/fileutil" | ||
"github.com/prysmaticlabs/prysm/shared/testutil/assert" | ||
"github.com/prysmaticlabs/prysm/shared/testutil/require" | ||
dbTest "github.com/prysmaticlabs/prysm/validator/db/testing" | ||
"github.com/prysmaticlabs/prysm/validator/flags" | ||
protectionFormat "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format" | ||
mocks "github.com/prysmaticlabs/prysm/validator/testing" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func setupCliCtx( | ||
tb testing.TB, | ||
dbPath string, | ||
outputDir string, | ||
) *cli.Context { | ||
app := cli.App{} | ||
set := flag.NewFlagSet("test", 0) | ||
set.String(cmd.DataDirFlag.Name, dbPath, "") | ||
set.String(flags.SlashingProtectionExportDirFlag.Name, outputDir, "") | ||
assert.NoError(tb, set.Set(cmd.DataDirFlag.Name, dbPath)) | ||
assert.NoError(tb, set.Set(flags.SlashingProtectionExportDirFlag.Name, outputDir)) | ||
return cli.NewContext(&app, set, nil) | ||
} | ||
|
||
func TestExportSlashingProtectionCli(t *testing.T) { | ||
ctx := context.Background() | ||
numValidators := 10 | ||
|
||
// Create some mock slashing protection history. and JSON file | ||
pubKeys, err := mocks.CreateRandomPubKeys(numValidators) | ||
require.NoError(t, err) | ||
attestingHistory, proposalHistory, err := mocks.MockAttestingAndProposalHistories(numValidators) | ||
require.NoError(t, err) | ||
mockJSON, err := mocks.MockSlashingProtectionJSON(pubKeys, attestingHistory, proposalHistory) | ||
require.NoError(t, err) | ||
|
||
// We JSON encode the protection file and import it into our database. | ||
encoded, err := json.Marshal(mockJSON) | ||
require.NoError(t, err) | ||
buf := bytes.NewBuffer(encoded) | ||
|
||
validatorDB := dbTest.SetupDB(t, pubKeys) | ||
err = protectionFormat.ImportStandardProtectionJSON(ctx, validatorDB, buf) | ||
require.NoError(t, err) | ||
require.NoError(t, validatorDB.Close()) | ||
|
||
// We export our slashing protection history by creating a CLI context | ||
// with the required values, such as the database datadir and output directory. | ||
dbPath := validatorDB.DatabasePath() | ||
outputPath := filepath.Join(os.TempDir(), "slashing-exports") | ||
cliCtx := setupCliCtx(t, dbPath, outputPath) | ||
err = ExportSlashingProtectionJSONCli(cliCtx) | ||
require.NoError(t, err) | ||
|
||
// Attempt to read the exported file from the output directory. | ||
enc, err := fileutil.ReadFileAsBytes(filepath.Join(outputPath, jsonExportFileName)) | ||
require.NoError(t, err) | ||
|
||
receivedJSON := &protectionFormat.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 Metadata value in the JSON file. | ||
require.DeepEqual(t, mockJSON.Metadata, receivedJSON.Metadata) | ||
wantedHistoryByPublicKey := make(map[string]*protectionFormat.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) | ||
require.Equal(t, len(wanted.SignedBlocks), len(item.SignedBlocks)) | ||
require.Equal(t, len(wanted.SignedAttestations), len(item.SignedAttestations)) | ||
require.DeepEqual(t, wanted.SignedBlocks, item.SignedBlocks) | ||
require.DeepEqual(t, wanted.SignedAttestations, item.SignedAttestations) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package slashingprotection | ||
|
||
import ( | ||
"github.com/prysmaticlabs/prysm/shared/cmd" | ||
"github.com/prysmaticlabs/prysm/shared/featureconfig" | ||
"github.com/prysmaticlabs/prysm/shared/tos" | ||
"github.com/prysmaticlabs/prysm/validator/flags" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// Commands for slashing protection. | ||
var Commands = &cli.Command{ | ||
Name: "slashing-protection", | ||
Category: "slashing-protection", | ||
Usage: "defines commands for interacting your validator's slashing protection history", | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "export", | ||
Description: `exports your validator slashing protection history into an EIP-3076 compliant JSON`, | ||
Flags: cmd.WrapFlags([]cli.Flag{ | ||
cmd.DataDirFlag, | ||
flags.SlashingProtectionExportDirFlag, | ||
}), | ||
Before: func(cliCtx *cli.Context) error { | ||
if err := cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags); err != nil { | ||
return err | ||
} | ||
return tos.VerifyTosAcceptedOrPrompt(cliCtx) | ||
}, | ||
Action: func(cliCtx *cli.Context) error { | ||
featureconfig.ConfigureValidator(cliCtx) | ||
return ExportSlashingProtectionJSONCli(cliCtx) | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do other clients call it this as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no standard for it