github.com/prysmaticlabs/prysm@v1.4.4/validator/rpc/slashing.go (about)

     1  package rpc
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  
     8  	"github.com/golang/protobuf/ptypes/empty"
     9  	"github.com/pkg/errors"
    10  	pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
    11  	slashing "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format"
    12  	"google.golang.org/grpc/codes"
    13  	"google.golang.org/grpc/status"
    14  	"google.golang.org/protobuf/types/known/emptypb"
    15  )
    16  
    17  // ExportSlashingProtection handles the rpc call returning the json slashing history.
    18  // The format of the export follows the EIP-3076 standard which makes it
    19  // easy to migrate machines or Ethereum consensus clients.
    20  //
    21  // Steps:
    22  // 1. Call the function which exports the data from
    23  //  the validator's db into an EIP standard slashing protection format.
    24  // 2. Format and send JSON in the response.
    25  func (s *Server) ExportSlashingProtection(ctx context.Context, _ *empty.Empty) (*pb.ExportSlashingProtectionResponse, error) {
    26  	if s.valDB == nil {
    27  		return nil, errors.New("err finding validator database at path")
    28  	}
    29  
    30  	eipJSON, err := slashing.ExportStandardProtectionJSON(ctx, s.valDB)
    31  	if err != nil {
    32  		return nil, errors.Wrap(err, "could not export slashing protection history")
    33  	}
    34  
    35  	encoded, err := json.MarshalIndent(eipJSON, "", "\t")
    36  	if err != nil {
    37  		return nil, errors.Wrap(err, "could not JSON marshal slashing protection history")
    38  	}
    39  
    40  	return &pb.ExportSlashingProtectionResponse{
    41  		File: string(encoded),
    42  	}, nil
    43  
    44  }
    45  
    46  // ImportSlashingProtection reads an input slashing protection EIP-3076
    47  // standard JSON string and inserts the data into validator DB.
    48  //
    49  // Read the JSON string passed through rpc, then call the func
    50  // which actually imports the data from the JSON file into our database.
    51  func (s *Server) ImportSlashingProtection(ctx context.Context, req *pb.ImportSlashingProtectionRequest) (*emptypb.Empty, error) {
    52  	if s.valDB == nil {
    53  		return nil, errors.New("err finding validator database at path")
    54  	}
    55  
    56  	if req.SlashingProtectionJson == "" {
    57  		return nil, status.Errorf(codes.InvalidArgument, "empty slashing_protection json specified")
    58  	}
    59  	enc := []byte(req.SlashingProtectionJson)
    60  
    61  	buf := bytes.NewBuffer(enc)
    62  	if err := slashing.ImportStandardProtectionJSON(ctx, s.valDB, buf); err != nil {
    63  		return nil, err
    64  	}
    65  	log.Info("Slashing protection JSON successfully imported")
    66  	return &empty.Empty{}, nil
    67  }