github.com/prysmaticlabs/prysm@v1.4.4/validator/slashing-protection/cli_import.go (about)

     1  package slashingprotection
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/prysmaticlabs/prysm/cmd/validator/flags"
     9  	"github.com/prysmaticlabs/prysm/shared/cmd"
    10  	"github.com/prysmaticlabs/prysm/shared/fileutil"
    11  	"github.com/prysmaticlabs/prysm/validator/accounts/prompt"
    12  	"github.com/prysmaticlabs/prysm/validator/db/kv"
    13  	slashingProtectionFormat "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format"
    14  	"github.com/urfave/cli/v2"
    15  )
    16  
    17  // ImportSlashingProtectionCLI reads an input slashing protection EIP-3076
    18  // standard JSON file and attempts to insert its data into our validator DB.
    19  //
    20  // Steps:
    21  // 1. Parse a path to the validator's datadir from the CLI context.
    22  // 2. Open the validator database.
    23  // 3. Read the JSON file from user input.
    24  // 4. Call the function which actually imports the data from
    25  // from the standard slashing protection JSON file into our database.
    26  func ImportSlashingProtectionCLI(cliCtx *cli.Context) error {
    27  	var err error
    28  	dataDir := cliCtx.String(cmd.DataDirFlag.Name)
    29  	if !cliCtx.IsSet(cmd.DataDirFlag.Name) {
    30  		dataDir, err = prompt.InputDirectory(cliCtx, prompt.DataDirDirPromptText, cmd.DataDirFlag)
    31  		if err != nil {
    32  			return err
    33  		}
    34  	}
    35  	// ensure that the validator.db is found under the specified dir or its subdirectories
    36  	found, _, err := fileutil.RecursiveFileFind(kv.ProtectionDbFileName, dataDir)
    37  	if err != nil {
    38  		return errors.Wrapf(err, "err finding validator database at path %s", dataDir)
    39  	}
    40  	if !found {
    41  		return errors.Wrapf(err, "err finding validator database at path %s", dataDir)
    42  	}
    43  
    44  	valDB, err := kv.NewKVStore(cliCtx.Context, dataDir, &kv.Config{})
    45  	if err != nil {
    46  		return errors.Wrapf(err, "could not access validator database at path: %s", dataDir)
    47  	}
    48  	defer func() {
    49  		if err := valDB.Close(); err != nil {
    50  			log.WithError(err).Errorf("Could not close validator DB")
    51  		}
    52  	}()
    53  	protectionFilePath, err := prompt.InputDirectory(cliCtx, prompt.SlashingProtectionJSONPromptText, flags.SlashingProtectionJSONFileFlag)
    54  	if err != nil {
    55  		return errors.Wrap(err, "could not get slashing protection json file")
    56  	}
    57  	if protectionFilePath == "" {
    58  		return fmt.Errorf(
    59  			"no path to a slashing_protection.json file specified, please retry or "+
    60  				"you can also specify it with the %s flag",
    61  			flags.SlashingProtectionJSONFileFlag.Name,
    62  		)
    63  	}
    64  	enc, err := fileutil.ReadFileAsBytes(protectionFilePath)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	buf := bytes.NewBuffer(enc)
    69  	if err := slashingProtectionFormat.ImportStandardProtectionJSON(
    70  		cliCtx.Context, valDB, buf,
    71  	); err != nil {
    72  		return err
    73  	}
    74  	log.Info("Slashing protection JSON successfully imported")
    75  	return nil
    76  }