2021-04-09 19:13:59 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
|
|
"github.com/pkg/errors"
|
2021-08-18 21:24:01 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client"
|
2021-11-09 16:49:28 +00:00
|
|
|
slashing "github.com/prysmaticlabs/prysm/validator/slashing-protection-history"
|
2021-04-16 07:46:38 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2021-04-09 19:13:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ExportSlashingProtection handles the rpc call returning the json slashing history.
|
|
|
|
// The format of the export follows the EIP-3076 standard which makes it
|
2021-06-26 19:00:33 +00:00
|
|
|
// easy to migrate machines or Ethereum consensus clients.
|
2021-04-09 19:13:59 +00:00
|
|
|
//
|
|
|
|
// Steps:
|
|
|
|
// 1. Call the function which exports the data from
|
|
|
|
// the validator's db into an EIP standard slashing protection format.
|
|
|
|
// 2. Format and send JSON in the response.
|
|
|
|
func (s *Server) ExportSlashingProtection(ctx context.Context, _ *empty.Empty) (*pb.ExportSlashingProtectionResponse, error) {
|
|
|
|
if s.valDB == nil {
|
|
|
|
return nil, errors.New("err finding validator database at path")
|
|
|
|
}
|
|
|
|
|
|
|
|
eipJSON, err := slashing.ExportStandardProtectionJSON(ctx, s.valDB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not export slashing protection history")
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded, err := json.MarshalIndent(eipJSON, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not JSON marshal slashing protection history")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.ExportSlashingProtectionResponse{
|
|
|
|
File: string(encoded),
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportSlashingProtection reads an input slashing protection EIP-3076
|
|
|
|
// standard JSON string and inserts the data into validator DB.
|
|
|
|
//
|
|
|
|
// Read the JSON string passed through rpc, then call the func
|
|
|
|
// which actually imports the data from the JSON file into our database.
|
|
|
|
func (s *Server) ImportSlashingProtection(ctx context.Context, req *pb.ImportSlashingProtectionRequest) (*emptypb.Empty, error) {
|
|
|
|
if s.valDB == nil {
|
|
|
|
return nil, errors.New("err finding validator database at path")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.SlashingProtectionJson == "" {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty slashing_protection json specified")
|
|
|
|
}
|
|
|
|
enc := []byte(req.SlashingProtectionJson)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(enc)
|
|
|
|
if err := slashing.ImportStandardProtectionJSON(ctx, s.valDB, buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Info("Slashing protection JSON successfully imported")
|
|
|
|
return &empty.Empty{}, nil
|
|
|
|
}
|