Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bridge: migrate cmd/ to cobra #67

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .run/guardiand build only.run.xml
Expand Up @@ -3,7 +3,7 @@
<module name="wormhole" />
<working_directory value="$PROJECT_DIR$/bridge" />
<kind value="PACKAGE" />
<package value="github.com/certusone/wormhole/bridge/cmd/guardiand" />
<package value="github.com/certusone/wormhole/bridge" />
<directory value="$PROJECT_DIR$/" />
<option name="run" value="false" />
<method v="2" />
Expand Down
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -12,8 +12,10 @@ Repo overview:
- **[bridge/](bridge/)** — The guardian node which connects to both chains, observes lockups and submits VAAs.
Written in pure Go.

- **[cmd/guardiand](bridge/cmd/guardiand)** — Most of the business logic for cross-chain communication
lives here. Consists of multiple loosely coupled services communicating via Go channels.
- [cmd/](bridge/cmd/) - CLI entry point, deals with the mechanics of parsing command line flags and loading keys.
- **[pkg/processor](bridge/pkg/processor)** — Most of the business logic for cross-chain communication
lives here. Talks to multiple loosely coupled services communicating via Go channels.
- [pkg/p2p](bridge/pkg/p2p)** — libp2p-based gossip network.
- [pkg/devnet](bridge/pkg/devnet) — Constants and helper functions for the deterministic local devnet.
- [pkg/ethereum](bridge/pkg/ethereum) — Ethereum chain interface with auto-generated contract ABI.
Uses go-ethereum to directly connect to an Eth node.
Expand Down
2 changes: 1 addition & 1 deletion Tiltfile
Expand Up @@ -27,7 +27,7 @@ def build_bridge_yaml():
container = obj["spec"]["template"]["spec"]["containers"][0]
if container["name"] != "guardiand":
fail("container 0 is not guardiand")
container["command"] += ["-devNumGuardians", str(num_guardians)]
container["command"] += ["--devNumGuardians", str(num_guardians)]

return encode_yaml_stream(bridge_yaml)

Expand Down
7 changes: 3 additions & 4 deletions bridge/Dockerfile
Expand Up @@ -3,15 +3,14 @@ FROM golang:1.15.3

WORKDIR /app

ADD go.mod .
ADD go.sum .
ADD tools tools

RUN --mount=type=cache,target=/root/.cache --mount=type=cache,target=/go \
go build -mod=readonly -o /dlv github.com/go-delve/delve/cmd/dlv
cd tools/ && go build -mod=readonly -o /dlv github.com/go-delve/delve/cmd/dlv

ADD . .

RUN --mount=type=cache,target=/root/.cache --mount=type=cache,target=/go \
go build -gcflags="all=-N -l" -mod=readonly -o /guardiand github.com/certusone/wormhole/bridge/cmd/guardiand
go build -gcflags="all=-N -l" -mod=readonly -o /guardiand github.com/certusone/wormhole/bridge

ENTRYPOINT /guardiand
58 changes: 41 additions & 17 deletions bridge/cmd/guardiand/main.go → bridge/cmd/guardiand/bridge.go
@@ -1,8 +1,7 @@
package main
package guardiand

import (
"context"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
Expand All @@ -12,6 +11,7 @@ import (
eth_common "github.com/ethereum/go-ethereum/common"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/spf13/cobra"
"go.uber.org/zap"
"golang.org/x/sys/unix"

Expand All @@ -29,26 +29,45 @@ import (
)

var (
p2pNetworkID = flag.String("network", "/wormhole/dev", "P2P network identifier")
p2pPort = flag.Uint("port", 8999, "P2P UDP listener port")
p2pBootstrap = flag.String("bootstrap", "", "P2P bootstrap peers (comma-separated)")
p2pNetworkID *string
p2pPort *uint
p2pBootstrap *string

nodeKeyPath = flag.String("nodeKey", "", "Path to node key (will be generated if it doesn't exist)")
nodeKeyPath *string

ethRPC = flag.String("ethRPC", "", "Ethereum RPC URL")
ethContract = flag.String("ethContract", "", "Ethereum bridge contract address")
ethConfirmations = flag.Uint64("ethConfirmations", 15, "Ethereum confirmation count requirement")
ethRPC *string
ethContract *string
ethConfirmations *uint64

agentRPC = flag.String("agentRPC", "", "Solana agent sidecar gRPC address")
agentRPC *string

logLevel = flag.String("logLevel", "info", "Logging level (debug, info, warn, error, dpanic, panic, fatal)")
logLevel *string

unsafeDevMode = flag.Bool("unsafeDevMode", false, "Launch node in unsafe, deterministic devnet mode")
devNumGuardians = flag.Uint("devNumGuardians", 5, "Number of devnet guardians to include in guardian set")

nodeName = flag.String("nodeName", "", "Node name to announce in gossip heartbeats")
unsafeDevMode *bool
devNumGuardians *uint
nodeName *string
)

func init() {
p2pNetworkID = BridgeCmd.Flags().String("network", "/wormhole/dev", "P2P network identifier")
p2pPort = BridgeCmd.Flags().Uint("port", 8999, "P2P UDP listener port")
p2pBootstrap = BridgeCmd.Flags().String("bootstrap", "", "P2P bootstrap peers (comma-separated)")

nodeKeyPath = BridgeCmd.Flags().String("nodeKey", "", "Path to node key (will be generated if it doesn't exist)")

ethRPC = BridgeCmd.Flags().String("ethRPC", "", "Ethereum RPC URL")
ethContract = BridgeCmd.Flags().String("ethContract", "", "Ethereum bridge contract address")
ethConfirmations = BridgeCmd.Flags().Uint64("ethConfirmations", 15, "Ethereum confirmation count requirement")

agentRPC = BridgeCmd.Flags().String("agentRPC", "", "Solana agent sidecar gRPC address")

logLevel = BridgeCmd.Flags().String("logLevel", "info", "Logging level (debug, info, warn, error, dpanic, panic, fatal)")

unsafeDevMode = BridgeCmd.Flags().Bool("unsafeDevMode", false, "Launch node in unsafe, deterministic devnet mode")
devNumGuardians = BridgeCmd.Flags().Uint("devNumGuardians", 5, "Number of devnet guardians to include in guardian set")
nodeName = BridgeCmd.Flags().String("nodeName", "", "Node name to announce in gossip heartbeats")
}

var (
rootCtx context.Context
rootCtxCancel context.CancelFunc
Expand Down Expand Up @@ -82,9 +101,14 @@ func rootLoggerName() string {
}
}

func main() {
flag.Parse()
// BridgeCmd represents the bridge command
var BridgeCmd = &cobra.Command{
Use: "bridge",
Short: "Run the bridge server",
Run: runBridge,
}

func runBridge(cmd *cobra.Command, args []string) {
if *unsafeDevMode {
fmt.Print(devwarning)
}
Expand Down
2 changes: 1 addition & 1 deletion bridge/cmd/guardiand/nodekeys.go
@@ -1,4 +1,4 @@
package main
package guardiand

import (
"crypto/ecdsa"
Expand Down
62 changes: 62 additions & 0 deletions bridge/cmd/root.go
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"

"github.com/certusone/wormhole/bridge/cmd/guardiand"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "guardiand",
Short: "Wormhole bridge server",
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.guardiand.yaml)")
rootCmd.AddCommand(guardiand.BridgeCmd)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".guardiand" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".guardiand.yaml")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
8 changes: 5 additions & 3 deletions bridge/go.mod
Expand Up @@ -11,7 +11,6 @@ require (
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/ethereum/go-ethereum v1.9.23
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/go-delve/delve v1.5.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/golang/protobuf v1.4.3
github.com/golang/snappy v0.0.2 // indirect
Expand All @@ -36,12 +35,15 @@ require (
github.com/marten-seemann/qtls-go1-15 v0.1.1 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/miguelmota/go-ethereum-hdwallet v0.0.0-20200123000308-a60dcd172b4c
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.3.1
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/peterh/liner v1.2.0 // indirect
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/shirou/gopsutil v2.20.9+incompatible // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.3.2
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 // indirect
github.com/stretchr/testify v1.6.1
github.com/tyler-smith/go-bip39 v1.0.2 // indirect
Expand All @@ -50,8 +52,8 @@ require (
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee // indirect
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 // indirect
golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca // indirect
google.golang.org/genproto v0.0.0-20201015140912-32ed001d685c // indirect
golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca
google.golang.org/genproto v0.0.0-20201015140912-32ed001d685c
google.golang.org/grpc v1.33.0
google.golang.org/protobuf v1.25.0
)