Skip to content

Commit

Permalink
Add service/client/command
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Jul 19, 2023
1 parent 53903f5 commit 5dade00
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 155 deletions.
44 changes: 42 additions & 2 deletions cmd/valcli/daemoncmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var daemonCommands = []cli.Command{
Usage: "More advanced commands which require validator daemon to be running.",
Category: "Daemon commands",
Subcommands: []cli.Command{
getDaemonInfo,
getDaemonInfoCmd,
createValCmd,
},
},
}
Expand All @@ -30,7 +31,7 @@ var (
defaultValdDaemonAddress = "127.0.0.1:" + strconv.Itoa(valcfg.DefaultRPCPort)
)

var getDaemonInfo = cli.Command{
var getDaemonInfoCmd = cli.Command{
Name: "get-info",
ShortName: "gi",
Usage: "Get information of the running daemon.",
Expand Down Expand Up @@ -62,3 +63,42 @@ func getInfo(ctx *cli.Context) error {

return nil
}

var createValCmd = cli.Command{
Name: "create-validator",
ShortName: "cv",
Usage: "Get information of the running daemon.",
Flags: []cli.Flag{
cli.StringFlag{
Name: valdDaemonAddressFlag,
Usage: "Full address of the validator daemon in format tcp://<host>:<port>",
Value: defaultValdDaemonAddress,
},
cli.StringFlag{
Name: keyNameFlag,
Usage: "The unique name of the validator key",
Required: true,
},
},
Action: createValDaemon,
}

func createValDaemon(ctx *cli.Context) error {
daemonAddress := ctx.String(valdDaemonAddressFlag)
keyName := ctx.String(keyNameFlag)
client, cleanUp, err := dc.NewValidatorServiceGRpcClient(daemonAddress)
if err != nil {
return err
}
defer cleanUp()

info, err := client.CreateValidator(context.Background(), keyName)

if err != nil {
return err
}

printRespJSON(info)

return nil
}
5 changes: 3 additions & 2 deletions cmd/valcli/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/hex"
"fmt"

"github.com/urfave/cli"
Expand Down Expand Up @@ -105,8 +106,8 @@ func createVal(ctx *cli.Context) error {
}

printRespJSON(&proto.CreateValidatorResponse{
BabylonPk: validator.BabylonPk,
BtcPk: validator.BtcPk,
BabylonPk: hex.EncodeToString(validator.BabylonPk),
BtcPk: hex.EncodeToString(validator.BtcPk),
})

return err
Expand Down
305 changes: 158 additions & 147 deletions proto/validators.pb.go

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions proto/validators.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ message GetInfoResponse {
}

message CreateValidatorRequest {
// key_name is the identifier key in keyring
string key_name = 1;
}

message CreateValidatorResponse {
// babylon_pk is the Babylon secp256k1 PK of this BTC validator
bytes babylon_pk = 1;
string babylon_pk = 1;
// btc_pk is the BTC secp256k1 PK of the validator encoded in BIP-340 spec
bytes btc_pk = 2;
string btc_pk = 2;
}

message RegisterValidatorRequest {
Expand Down
4 changes: 3 additions & 1 deletion service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ func (app *ValidatorApp) eventLoop() {
btcPubKey, err := schnorr.ParsePubKey(validator.BtcPk)

if err != nil {
app.logger.Fatalf("failed to parse created btc public key: %w", err)
app.logger.WithFields(logrus.Fields{
"err": err,
}).Fatal("failed to parse created btc public key")
}

babylonPubKey := secp256k1.PubKey{
Expand Down
10 changes: 10 additions & 0 deletions service/client/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ func (c *ValidatorServiceGRpcClient) CommitPubRandList(ctx context.Context, bbnP

return res, nil
}

func (c *ValidatorServiceGRpcClient) CreateValidator(ctx context.Context, keyName string) (*proto.CreateValidatorResponse, error) {
req := &proto.CreateValidatorRequest{KeyName: keyName}
res, err := c.client.CreateValidator(ctx, req)
if err != nil {
return nil, err
}

return res, nil
}
16 changes: 15 additions & 1 deletion service/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package service

import (
"context"
"encoding/hex"
"fmt"
"sync"
"sync/atomic"

"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/lightningnetwork/lnd/signal"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
Expand Down Expand Up @@ -98,7 +100,19 @@ func (r *rpcServer) GetInfo(context.Context, *proto.GetInfoRequest) (*proto.GetI
// CreateValidator generates a validator object and saves it in the database
func (r *rpcServer) CreateValidator(ctx context.Context, req *proto.CreateValidatorRequest) (
*proto.CreateValidatorResponse, error) {
panic("implement me")
result, err := r.app.CreateValidator(req.KeyName)

if err != nil {
return nil, err
}

btcPk := schnorr.SerializePubKey(&result.BtcValidatorPk)

return &proto.CreateValidatorResponse{
BtcPk: hex.EncodeToString(btcPk),
BabylonPk: hex.EncodeToString(result.BabylonValidatorPk.Key),
}, nil

}

// RegisterValidator sends a transactions to Babylon to register a BTC validator
Expand Down

0 comments on commit 5dade00

Please sign in to comment.