From 2123b114993ef9fce0667e6d68a5b979025dec42 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Thu, 20 Jul 2023 13:41:34 +1000 Subject: [PATCH] chore: moving the db functionality in valcli to vald (#21) --- Makefile | 5 ++++- cmd/valcli/daemoncmd.go | 33 +++++++++++++++++++++++++++++++++ cmd/vald/main.go | 6 ++++++ proto/validators.go | 23 +++++++++++++++++++++++ service/app.go | 31 ++++++++++++++----------------- service/client/rpcclient.go | 10 ++++++++++ service/rpcserver.go | 8 +++++++- 7 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 proto/validators.go diff --git a/Makefile b/Makefile index 3f16f4aac1f54..07b359a7d7e97 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,9 @@ all: build install build: BUILD_ARGS := $(build_args) -o $(BUILDDIR) +clean: + rm -rf $(BUILDDIR) + $(BUILD_TARGETS): go.sum $(BUILDDIR)/ go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./... @@ -45,7 +48,7 @@ build-docker: $(DOCKER) build --secret id=sshKey,src=${BBN_PRIV_DEPLOY_KEY} --tag babylonchain/btc-validator -f Dockerfile \ $(shell git rev-parse --show-toplevel) -.PHONY: build build-docker +.PHONY: build build-docker clean test: go test ./... diff --git a/cmd/valcli/daemoncmd.go b/cmd/valcli/daemoncmd.go index b3c84f9966dd9..3c43e27bd14bf 100644 --- a/cmd/valcli/daemoncmd.go +++ b/cmd/valcli/daemoncmd.go @@ -19,6 +19,7 @@ var daemonCommands = []cli.Command{ Subcommands: []cli.Command{ getDaemonInfoCmd, createValCmd, + lsValCmd, }, }, } @@ -102,3 +103,35 @@ func createValDaemon(ctx *cli.Context) error { return nil } + +var lsValCmd = cli.Command{ + Name: "list-validators", + ShortName: "ls", + Usage: "List validators stored in the database.", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: valdDaemonAddressFlag, + Usage: "Full address of the validator daemon in format tcp://:", + Value: defaultValdDaemonAddress, + }, + }, + Action: lsValDaemon, +} + +func lsValDaemon(ctx *cli.Context) error { + daemonAddress := ctx.String(valdDaemonAddressFlag) + rpcClient, cleanUp, err := dc.NewValidatorServiceGRpcClient(daemonAddress) + if err != nil { + return err + } + defer cleanUp() + + resp, err := rpcClient.QueryValidatorList(context.Background()) + if err != nil { + return err + } + + printRespJSON(resp) + + return nil +} diff --git a/cmd/vald/main.go b/cmd/vald/main.go index 6f515857a4e7f..f0653d79fd18a 100644 --- a/cmd/vald/main.go +++ b/cmd/vald/main.go @@ -44,6 +44,12 @@ func main() { cfgLogger.Errorf("failed to create validator app: %v", err) os.Exit(1) } + // start app event loop + // TODO: graceful shutdown + if err := valApp.Start(); err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } valServer := service.NewValidatorServer(cfg, cfgLogger, valApp, shutdownInterceptor) diff --git a/proto/validators.go b/proto/validators.go new file mode 100644 index 0000000000000..58b6be6ed0c03 --- /dev/null +++ b/proto/validators.go @@ -0,0 +1,23 @@ +package proto + +import ( + "fmt" + + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/schnorr" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" +) + +func (v *Validator) GetBabylonPK() *secp256k1.PubKey { + return &secp256k1.PubKey{ + Key: v.BabylonPk, + } +} + +func (v *Validator) MustGetBTCPK() *btcec.PublicKey { + btcPubKey, err := schnorr.ParsePubKey(v.BtcPk) + if err != nil { + panic(fmt.Errorf("failed to parse BTC PK: %w", err)) + } + return btcPubKey +} diff --git a/service/app.go b/service/app.go index bfea21415e16b..f0e6b386abe97 100644 --- a/service/app.go +++ b/service/app.go @@ -8,7 +8,6 @@ import ( bstypes "github.com/babylonchain/babylon/x/btcstaking/types" ftypes "github.com/babylonchain/babylon/x/finality/types" "github.com/btcsuite/btcd/btcec/v2" - "github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -260,12 +259,18 @@ func (app *ValidatorApp) CreateValidator(keyName string) (*CreateValidatorResult } } +func (app *ValidatorApp) ListValidators() ([]*proto.Validator, error) { + return app.vs.ListValidators() +} + func (app *ValidatorApp) GetValidator(pkBytes []byte) (*proto.Validator, error) { return app.vs.GetValidator(pkBytes) } func (app *ValidatorApp) handleCreateValidatorRequest(req *createValidatorRequest) (*createValidatorResponse, error) { + app.logger.Debug("handling CreateValidator request") + kr, err := val.NewKeyringControllerWithKeyring(app.kr, req.keyName) if err != nil { @@ -287,21 +292,18 @@ func (app *ValidatorApp) handleCreateValidatorRequest(req *createValidatorReques return nil, fmt.Errorf("failed to save validator: %w", err) } - btcPubKey, err := schnorr.ParsePubKey(validator.BtcPk) - - if err != nil { - app.logger.WithFields(logrus.Fields{ - "err": err, - }).Fatal("failed to parse created btc public key") - } + btcPubKey := validator.MustGetBTCPK() + babylonPubKey := validator.GetBabylonPK() - babylonPubKey := secp256k1.PubKey{ - Key: validator.BabylonPk, - } + app.logger.Info("successfully created validator") + app.logger.WithFields(logrus.Fields{ // TODO: use hex format + "btc_pub_key": btcPubKey, + "babylon_pub_key": babylonPubKey, + }).Debug("created validator") return &createValidatorResponse{ BtcValidatorPk: *btcPubKey, - BabylonValidatorPk: babylonPubKey, + BabylonValidatorPk: *babylonPubKey, }, nil } @@ -319,11 +321,6 @@ func (app *ValidatorApp) eventLoop() { continue } - app.logger.WithFields(logrus.Fields{ - "btc_pub_key": resp.BtcValidatorPk, - "babylon_pub_key": resp.BabylonValidatorPk, - }).Info("Successfully created validator") - req.successResponse <- resp case <-app.quit: return diff --git a/service/client/rpcclient.go b/service/client/rpcclient.go index 8e58730b39b99..432c9fd2a85d7 100644 --- a/service/client/rpcclient.go +++ b/service/client/rpcclient.go @@ -68,3 +68,13 @@ func (c *ValidatorServiceGRpcClient) CreateValidator(ctx context.Context, keyNam return res, nil } + +func (c *ValidatorServiceGRpcClient) QueryValidatorList(ctx context.Context) (*proto.QueryValidatorListResponse, error) { + req := &proto.QueryValidatorListRequest{} + res, err := c.client.QueryValidatorList(ctx, req) + if err != nil { + return nil, err + } + + return res, nil +} diff --git a/service/rpcserver.go b/service/rpcserver.go index 4ed3a91f9cbe3..a557509e816f8 100644 --- a/service/rpcserver.go +++ b/service/rpcserver.go @@ -174,5 +174,11 @@ func (r *rpcServer) QueryValidator(ctx context.Context, req *proto.QueryValidato // QueryValidatorList queries the information of a list of validators func (r *rpcServer) QueryValidatorList(ctx context.Context, req *proto.QueryValidatorListRequest) ( *proto.QueryValidatorListResponse, error) { - panic("implement me") + + vals, err := r.app.ListValidators() + if err != nil { + return nil, err + } + + return &proto.QueryValidatorListResponse{Validators: vals}, nil }