Skip to content

Commit

Permalink
feat: Refactor configuration and cli of programs (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitsalis authored Dec 7, 2023
1 parent fd8fc88 commit 61d72a7
Show file tree
Hide file tree
Showing 48 changed files with 776 additions and 1,138 deletions.
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ RUN apk add bash curl jq
COPY --from=builder /go/src/github.com/babylonchain/btc-validator/build/vald /bin/vald
COPY --from=builder /go/src/github.com/babylonchain/btc-validator/build/valcli /bin/valcli
COPY --from=builder /go/src/github.com/babylonchain/btc-validator/build/covd /bin/covd
COPY --from=builder /go/src/github.com/babylonchain/btc-validator/build/covcli /bin/covcli
COPY --from=builder /go/src/github.com/babylonchain/btc-validator/build/eotsd /bin/eotsd
COPY --from=builder /go/src/github.com/babylonchain/btc-validator/build/eotscli /bin/eotscli

WORKDIR /home/btcvalidator
RUN chown -R btcvalidator /home/btcvalidator
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ $ make install
The above will produce the following binaries:

- `eotsd`: The daemon program for the EOTS manager.
- `eotcli`: The CLI tool for interacting with the EOTS daemon.
- `vald`: The daemon program for the btc-validator.
- `valcli`: The CLI tool for interacting with the btc-validator daemon.
- `covd`: The daemon program for the covenant emulator.

To build locally,

Expand All @@ -113,7 +113,7 @@ The above will lead to a build directory having the following structure:

```bash
$ ls build
├── eotcli
├── covd
├── eotsd
├── valcli
└── vald
Expand Down
10 changes: 1 addition & 9 deletions eotsmanager/config/db.go → config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,33 @@ import "fmt"

const (
DefaultBackend = "bbolt"
DefaultDBPath = "bbolt-eots.db"
DefaultDBName = "default"
)

type DatabaseConfig struct {
Backend string `long:"backend" description:"Possible database to choose as backend"`
Path string `long:"path" description:"The path that stores the database file"`
Name string `long:"name" description:"The name of the database"`
}

func NewDatabaseConfig(backend string, path string, name string) (*DatabaseConfig, error) {
func NewDatabaseConfig(backend string, name string) (*DatabaseConfig, error) {
// TODO: add more supported DB types, currently we only support bbolt
if backend != DefaultBackend {
return nil, fmt.Errorf("unsupported DB backend")
}

if path == "" {
return nil, fmt.Errorf("DB path should not be empty")
}

if name == "" {
return nil, fmt.Errorf("bucket name should not be empty")
}

return &DatabaseConfig{
Backend: backend,
Path: path,
Name: name,
}, nil
}

func DefaultDatabaseConfig() DatabaseConfig {
return DatabaseConfig{
Backend: DefaultBackend,
Path: DefaultDBPath,
Name: DefaultDBName,
}
}
65 changes: 0 additions & 65 deletions covenant/cmd/covcli/admin.go

This file was deleted.

25 changes: 0 additions & 25 deletions covenant/cmd/covcli/main.go

This file was deleted.

18 changes: 18 additions & 0 deletions covenant/cmd/covd/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "github.com/cosmos/cosmos-sdk/crypto/keyring"

const (
homeFlag = "home"
forceFlag = "force"
keyNameFlag = "key-name"
passphraseFlag = "passphrase"
hdPathFlag = "hd-path"
chainIdFlag = "chain-id"
keyringBackendFlag = "keyring-backend"

defaultChainID = "chain-test"
defaultKeyringBackend = keyring.BackendTest
defaultPassphrase = ""
defaultHdPath = ""
)
56 changes: 56 additions & 0 deletions covenant/cmd/covd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
covcfg "github.com/babylonchain/btc-validator/covenant/config"
"github.com/babylonchain/btc-validator/util"
"github.com/jessevdk/go-flags"
"github.com/urfave/cli"
"path/filepath"
)

var initCommand = cli.Command{
Name: "init",
Usage: "Initialize a covenant home directory.",
Flags: []cli.Flag{
cli.StringFlag{
Name: homeFlag,
Usage: "Path to where the home directory will be initialized",
Value: covcfg.DefaultCovenantDir,
},
cli.BoolFlag{
Name: forceFlag,
Usage: "Override existing configuration",
Required: false,
},
},
Action: initHome,
}

func initHome(c *cli.Context) error {
homePath, err := filepath.Abs(c.String(homeFlag))
if err != nil {
return err
}
// ensure the directory exists
homePath = util.CleanAndExpandPath(homePath)
force := c.Bool(forceFlag)

if util.FileExists(homePath) && !force {
return fmt.Errorf("home path %s already exists", homePath)
}

if err := util.MakeDirectory(homePath); err != nil {
return err
}
// Create log directory
logDir := covcfg.LogDir(homePath)
if err := util.MakeDirectory(logDir); err != nil {
return err
}

defaultConfig := covcfg.DefaultConfigWithHomePath(homePath)
fileParser := flags.NewParser(&defaultConfig, flags.Default)

return flags.NewIniParser(fileParser).WriteFile(covcfg.ConfigFile(homePath), flags.IniIncludeComments|flags.IniIncludeDefaults)
}
36 changes: 10 additions & 26 deletions covenant/cmd/covcli/new.go → covenant/cmd/covd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,20 @@ package main
import (
"encoding/json"
"fmt"

"github.com/babylonchain/babylon/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/urfave/cli"

"github.com/babylonchain/btc-validator/covenant"
covcfg "github.com/babylonchain/btc-validator/covenant/config"
)

const (
keyNameFlag = "key-name"
passphraseFlag = "passphrase"
hdPathFlag = "hd-path"
chainIdFlag = "chain-id"
keyringDirFlag = "keyring-dir"
keyringBackendFlag = "keyring-backend"
defaultChainID = "chain-test"
defaultKeyringBackend = keyring.BackendTest
defaultPassphrase = ""
defaultHdPath = ""
"github.com/urfave/cli"
)

type covenantKey struct {
Name string `json:"name"`
PublicKey string `json:"public-key"`
}

var createCovenant = cli.Command{
Name: "create-covenant",
ShortName: "cc",
var createKeyCommand = cli.Command{
Name: "create-key",
ShortName: "ck",
Usage: "Create a Covenant account in the keyring.",
Flags: []cli.Flag{
cli.StringFlag{
Expand Down Expand Up @@ -61,24 +45,24 @@ var createCovenant = cli.Command{
Value: defaultKeyringBackend,
},
cli.StringFlag{
Name: keyringDirFlag,
Usage: "The directory where the keyring is stored",
Name: homeFlag,
Usage: "The home directory for the covenant",
Value: covcfg.DefaultCovenantDir,
},
},
Action: createCovenantKey,
Action: createKey,
}

func createCovenantKey(ctx *cli.Context) error {
keyringDir := ctx.String(keyringDirFlag)
func createKey(ctx *cli.Context) error {
homePath := ctx.String(homeFlag)
chainID := ctx.String(chainIdFlag)
keyName := ctx.String(keyNameFlag)
backend := ctx.String(keyringBackendFlag)
passphrase := ctx.String(passphraseFlag)
hdPath := ctx.String(hdPathFlag)

keyPair, err := covenant.CreateCovenantKey(
keyringDir,
homePath,
chainID,
keyName,
backend,
Expand Down
4 changes: 2 additions & 2 deletions covenant/cmd/covd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

func fatal(err error) {
fmt.Fprintf(os.Stderr, "[covenant-emulator] %v\n", err)
fmt.Fprintf(os.Stderr, "[covd] %v\n", err)
os.Exit(1)
}

func main() {
app := cli.NewApp()
app.Name = "covd"
app.Usage = "Covenant Emulator Daemon (covd)."
app.Commands = append(app.Commands, startCovenant)
app.Commands = append(app.Commands, startCommand, initCommand, createKeyCommand)

if err := app.Run(os.Args); err != nil {
fatal(err)
Expand Down
Loading

0 comments on commit 61d72a7

Please sign in to comment.