Skip to content

Commit

Permalink
feat: Proto generation with buf, Babylon config file, and minor nitpi…
Browse files Browse the repository at this point in the history
…cks (#14)
  • Loading branch information
vitsalis authored Jul 18, 2023
1 parent 52ea27b commit b28e1ed
Show file tree
Hide file tree
Showing 28 changed files with 389 additions and 310 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
main
tmp/
build/

*.swp
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test-e2e:
proto-all: proto-gen

proto-gen:
sh ./valrpc/protocgen.sh
./proto/scripts/protocgen.sh

.PHONY: proto-gen

Expand Down
12 changes: 9 additions & 3 deletions bbnclient/bbncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package babylonclient
import (
"context"
"fmt"
"github.com/babylonchain/btc-validator/valcfg"

"github.com/babylonchain/babylon/types"
btcstakingtypes "github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/babylonchain/rpc-client/client"
bbncfg "github.com/babylonchain/rpc-client/config"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/sirupsen/logrus"
)
Expand All @@ -20,11 +20,17 @@ type BabylonController struct {
}

func NewBabylonController(
cfg *bbncfg.BabylonConfig,
cfg *valcfg.BBNConfig,
logger *logrus.Logger,
) (*BabylonController, error) {
babylonConfig := valcfg.BBNConfigToBabylonConfig(cfg)

// TODO should be validated earlier
if err := babylonConfig.Validate(); err != nil {
return nil, err
}
// create a Tendermint/Cosmos client for Babylon
rpcClient, err := client.New(cfg)
rpcClient, err := client.New(&babylonConfig)
if err != nil {
return nil, fmt.Errorf("unable to create Babylon rpc client: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/valcli/daemoncmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var daemonCommands = []cli.Command{
{
Name: "daemon",
ShortName: "dn",
Usage: "More advanced commands which require validator daemon to be running",
Usage: "More advanced commands which require validator daemon to be running.",
Category: "Daemon commands",
Subcommands: []cli.Command{
getDaemonInfo,
Expand All @@ -33,11 +33,11 @@ var (
var getDaemonInfo = cli.Command{
Name: "get-info",
ShortName: "gi",
Usage: "Get information of the running daemon",
Usage: "Get information of the running daemon.",
Flags: []cli.Flag{
cli.StringFlag{
Name: validatorDaemonAddressFlag,
Usage: "full address of the validator daemon in format tcp://<host>:<port>",
Usage: "Full address of the validator daemon in format tcp://<host>:<port>",
Value: defaultValidatorDaemonAddress,
},
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/valcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ const (
func main() {
app := cli.NewApp()
app.Name = "valcli"
app.Usage = "control plane for your BTC Validator Daemon (vald)"
app.Usage = "Control plane for the Bitcoin Validator Daemon (vald)."
app.Flags = []cli.Flag{
cli.StringFlag{
Name: dbTypeFlag,
Usage: "the type of the database",
Usage: "The type of the database",
Value: valcfg.DefaultBackend,
},
cli.StringFlag{
Name: dbPathFlag,
Usage: "the path of the database file",
Usage: "The path of the database file",
Value: valcfg.DefaultDBPath,
},
cli.StringFlag{
Name: dbNameFlag,
Usage: "the name of the database bucket",
Usage: "The name of the database bucket",
Value: valcfg.DefaultDBName,
},
}
Expand Down
42 changes: 15 additions & 27 deletions cmd/valcli/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ package main
import (
"context"
"fmt"
"os"
"path"

"github.com/cosmos/cosmos-sdk/client"
"github.com/urfave/cli"

"github.com/babylonchain/btc-validator/codec"
"github.com/babylonchain/btc-validator/proto"
dc "github.com/babylonchain/btc-validator/service/client"
"github.com/babylonchain/btc-validator/val"
"github.com/babylonchain/btc-validator/valcfg"
"github.com/babylonchain/btc-validator/valrpc"
)

const (
chainIdFlag = "chain-id"
keyringDirFlag = "keyring-dir"
keyringBackendFlag = "keyring-backend"
keyNameFlag = "key-name"
)

var (
defaultChainID = "test-chain"
defaultKeyringBackend = "test"
)
Expand All @@ -32,7 +27,7 @@ var validatorsCommands = []cli.Command{
{
Name: "validators",
ShortName: "vals",
Usage: "Control BTC validators.",
Usage: "Control Bitcoin validators.",
Category: "Validators",
Subcommands: []cli.Command{
createValidator, listValidators, importValidator, registerValidator,
Expand All @@ -43,26 +38,26 @@ var validatorsCommands = []cli.Command{
var createValidator = cli.Command{
Name: "create-validator",
ShortName: "cv",
Usage: "create a BTC validator object and save it in database",
Usage: "Create a Bitcoin validator object and save it in database.",
Flags: []cli.Flag{
cli.StringFlag{
Name: chainIdFlag,
Usage: "the chainID of the Babylonchain",
Usage: "The chainID of the Babylonchain",
Value: defaultChainID,
},
cli.StringFlag{
Name: keyNameFlag,
Usage: "the unique name of the validator key",
Usage: "The unique name of the validator key",
Required: true,
},
cli.StringFlag{
Name: keyringBackendFlag,
Usage: "select keyring's backend (os|file|test)",
Usage: "Select keyring's backend (os|file|test)",
Value: defaultKeyringBackend,
},
cli.StringFlag{
Name: keyringDirFlag,
Usage: "the directory where the keyring is stored",
Usage: "The directory where the keyring is stored",
},
},
Action: createVal,
Expand All @@ -84,7 +79,7 @@ func createVal(ctx *cli.Context) error {
}

if krController.KeyNameTaken() {
return fmt.Errorf("the key name is taken")
return fmt.Errorf("the key name %s is taken", krController.GetKeyName())
}

validator, err := krController.CreateBTCValidator()
Expand All @@ -104,7 +99,7 @@ func createVal(ctx *cli.Context) error {
return err
}

printRespJSON(&valrpc.CreateValidatorResponse{
printRespJSON(&proto.CreateValidatorResponse{
BabylonPk: validator.BabylonPk,
BtcPk: validator.BtcPk,
})
Expand All @@ -115,7 +110,7 @@ func createVal(ctx *cli.Context) error {
var listValidators = cli.Command{
Name: "list-validators",
ShortName: "ls",
Usage: "list validators stored in the database",
Usage: "List validators stored in the database.",
Action: lsVal,
}

Expand All @@ -133,15 +128,15 @@ func lsVal(ctx *cli.Context) error {
return err
}

printRespJSON(&valrpc.QueryValidatorListResponse{Validators: valList})
printRespJSON(&proto.QueryValidatorListResponse{Validators: valList})

return nil
}

var importValidator = cli.Command{
Name: "import-validator",
ShortName: "iv",
Usage: "import a BTC validator object with given BTC and Babylon addresses",
Usage: "Import a Bitcoin validator object with given Bitcoin and Babylon addresses.",
Flags: []cli.Flag{
// TODO: add flags
},
Expand All @@ -155,12 +150,12 @@ func importVal(ctx *cli.Context) error {
var registerValidator = cli.Command{
Name: "register-validator",
ShortName: "rv",
Usage: "register a created BTC validator to Babylon, requiring the validator daemon running",
Usage: "Register a created Bitcoin validator to Babylon, requiring the validator daemon running.",
UsageText: "register-validator [Babylon public key]",
Flags: []cli.Flag{
cli.StringFlag{
Name: validatorDaemonAddressFlag,
Usage: "full address of the validator daemon in format tcp://<host>:<port>",
Usage: "Full address of the validator daemon in format tcp://<host>:<port>",
Value: defaultValidatorDaemonAddress,
},
},
Expand Down Expand Up @@ -205,16 +200,9 @@ func getValStoreFromCtx(ctx *cli.Context) (*val.ValidatorStore, error) {
}

func createClientCtx(ctx *cli.Context) (client.Context, error) {
var err error
var homeDir string

dir := ctx.String(keyringDirFlag)
if dir == "" {
homeDir, err = os.UserHomeDir()
if err != nil {
return client.Context{}, err
}
dir = path.Join(homeDir, ".btc-validator")
dir = valcfg.DefaultValidatordDir
}

return client.Context{}.
Expand Down
4 changes: 1 addition & 3 deletions cmd/vald/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"

"github.com/babylonchain/rpc-client/config"
"github.com/jessevdk/go-flags"
"github.com/lightningnetwork/lnd/signal"

Expand Down Expand Up @@ -35,8 +34,7 @@ func main() {
}

// TODO: use default babylon config for now
bbnCfg := config.DefaultBabylonConfig()
bbnClient, err := babylonclient.NewBabylonController(&bbnCfg, cfgLogger)
bbnClient, err := babylonclient.NewBabylonController(cfg.BabylonConfig, cfgLogger)
if err != nil {
cfgLogger.Errorf("failed to create Babylon rpc client: %v", err)
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ require (
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cosmos/cosmos-sdk v0.47.2
github.com/cosmos/go-bip39 v1.0.0
github.com/gogo/protobuf v1.3.3
github.com/cosmos/gogoproto v1.4.10
github.com/golang/mock v1.6.0
github.com/jessevdk/go-flags v1.5.0
github.com/lightningnetwork/lnd v0.16.4-beta.rc1
github.com/sirupsen/logrus v1.9.3
github.com/strangelove-ventures/lens v0.5.2-0.20230418174104-735a2a0195eb
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
go.etcd.io/bbolt v1.3.7
Expand Down Expand Up @@ -78,7 +77,6 @@ require (
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.4.10 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
github.com/cosmos/ibc-go/v7 v7.0.1 // indirect
github.com/cosmos/ics23/go v0.9.1-0.20221207100636-b1abd8678aab // indirect
Expand Down Expand Up @@ -112,6 +110,7 @@ require (
github.com/go-stack/stack v1.8.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.3 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down Expand Up @@ -224,6 +223,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/strangelove-ventures/lens v0.5.2-0.20230418174104-735a2a0195eb // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/supranational/blst v0.3.8 // indirect
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions proto/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: v1
plugins:
- plugin: go
out: .
opt: paths=source_relative
- name: go-grpc
out: .
opt: paths=source_relative
13 changes: 13 additions & 0 deletions proto/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: gogo
repository: protobuf
commit: 4df00b267f944190a229ce3695781e99
digest: shake256:de60e0534d11dfd7a1817909618e847e15d4ab6f5cc6d71154a9735af651c7bda2232b33f3fb6a4daf23f64a3fe80270e99d42d77c551bb9a69ab5dc48ec2e04
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 8d7204855ec14631a499bd7393ce1970
digest: shake256:40bf4112960cad01281930beed85829910768e32e80e986791596853eccd42c0cbd9d96690b918f658020d2d427e16f8b6514e2ac7f4a10306fd32e77be44329
26 changes: 26 additions & 0 deletions proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: v1
name: buf.build/babylonchain/btc-validator
deps:
- buf.build/googleapis/googleapis:8d7204855ec14631a499bd7393ce1970
- buf.build/gogo/protobuf:b03c65ea87cdc3521ede29f62fe3ce239267c1bc
breaking:
use:
- FILE
lint:
use:
- DEFAULT
- COMMENTS
- FILE_LOWER_SNAKE_CASE
- COMMENT_MESSAGE
- COMMENT_ENUM_VALUE
- COMMENT_ENUM
- COMMENT_RPC
- COMMENT_ONEOF
except:
- UNARY_RPC
- COMMENT_FIELD
- SERVICE_SUFFIX
- PACKAGE_VERSION_SUFFIX
- RPC_REQUEST_STANDARD_NAME
- ENUM_VALUE_PREFIX
- ENUM_ZERO_VALUE_SUFFIX
8 changes: 8 additions & 0 deletions proto/scripts/protocgen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -eo pipefail

cd proto
buf mod update
buf generate .
cd ..
Loading

0 comments on commit b28e1ed

Please sign in to comment.