Skip to content

Commit

Permalink
chore: moving the db functionality in valcli to vald (ethereum-optimi…
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis authored Jul 20, 2023
1 parent 729171e commit 2123b11
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 19 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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) ./...

Expand All @@ -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 ./...
Expand Down
33 changes: 33 additions & 0 deletions cmd/valcli/daemoncmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var daemonCommands = []cli.Command{
Subcommands: []cli.Command{
getDaemonInfoCmd,
createValCmd,
lsValCmd,
},
},
}
Expand Down Expand Up @@ -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://<host>:<port>",
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
}
6 changes: 6 additions & 0 deletions cmd/vald/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions proto/validators.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 14 additions & 17 deletions service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions service/client/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion service/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 2123b11

Please sign in to comment.