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

[Phase 1]: Tunnel offchain relayer #1

Merged
merged 20 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Tests
on: pull_request

env:
GOPRIVATE: github.com/bandprotocol/private-chain

jobs:
falcon-test:
runs-on: ubuntu-latest
Expand All @@ -13,6 +16,11 @@ jobs:
with:
go-version: "1.22.3"

- name: Configure git for private modules
env:
TOKEN: ${{ secrets.SHIBA_TOKEN }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why shiba haha?

run: git config --global url."https://nkitlabs:${TOKEN}@github.com".insteadOf "https://github.com"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next time, better to use band bot account token for this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RogerKSI fix leoi2 mee kon notice leaw

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bandbot somehow is not working for this case. think can open PR later to remove it after public.


- name: Check go mod cache
uses: actions/cache@v4
with:
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
name: Golangci-lint
on: pull_request

env:
GOPRIVATE: github.com/bandprotocol/private-chain

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v5
with:
go-version: 1.22.3

- name: Configure git for private modules
env:
TOKEN: ${{ secrets.SHIBA_TOKEN }}
run: git config --global url."https://nkitlabs:${TOKEN}@github.com".insteadOf "https://github.com"

- uses: actions/checkout@v4

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
version: v1.61
args: --timeout=5m0s
6 changes: 1 addition & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ linters-settings:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/cometbft/cometbft) # comet
- prefix(github.com/cosmos) # cosmos org
- prefix(cosmossdk.io) # new modules
- prefix(github.com/cosmos/cosmos-sdk) # cosmos sdk
- prefix(github.com/bandprotocol) # band org
- prefix(github.com/bandprotocol/chain)
- prefix(github.com/bandprotocol/falcon)
gocritic:
disabled-checks:
- regexpMust
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ go.sum: go.mod

test:
@go test -mod=readonly ./...

test-coverage:
@go test -mod=readonly -coverprofile=coverage.out ./...
@go tool cover -html=coverage.out
106 changes: 106 additions & 0 deletions cmd/chains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/bandprotocol/falcon/relayer"
)

// chainsCmd returns a command that manages chain configurations.
func chainsCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "chains",
Aliases: []string{"ch"},
Short: "Manage chain configurations",
}

cmd.AddCommand(
chainsAddCmd(app),
chainsDeleteCmd(app),
chainsListCmd(app),
chainsShowCmd(app),
)

return cmd
}

// chainsListCmd returns a command that lists all chains that are currently configured.
// NOTE: this command only show the list of registered chainIDs, to see the configuration
// please see `chains show`.
func chainsListCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "Return list of chain names that are currently configured",
Args: withUsage(cobra.NoArgs),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s chains list
$ %s ch l`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
_ = app
return nil
},
}

return cmd
}

// chainsShowCmd returns a command that shows a chain's configuration data.
func chainsShowCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "show [chain_name]",
Aliases: []string{"s"},
Short: "Return a chain's configuration data",
Args: withUsage(cobra.ExactArgs(1)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s ch s eth
$ %s chains show eth --yaml`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
_ = app
return nil
},
}

return cmd
}

// chainsAddCmd returns a command that adds a new chain configuration.
func chainsAddCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "add [chain_type] [chain_name]",
Aliases: []string{"a"},
Short: "Add a new chain to the configuration file by passing a configuration file (-f)",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s ch a evm eth --file chains/eth.toml
$ %s chains add evm eth --file chains/eth.toml`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
_ = app
return nil
},
}

return cmd
}

// chainsDeleteCmd returns a command that deletes a chain configuration.
func chainsDeleteCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "delete [chain_name]",
Aliases: []string{"d"},
Short: "Remove chain from config based on chain_name",
Args: withUsage(cobra.ExactArgs(1)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s chains delete eth
$ %s ch d eth`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
_ = app
return nil
},
}

return cmd
}
81 changes: 81 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cmd

import (
"fmt"
"strings"

"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"

"github.com/bandprotocol/falcon/relayer"
)

// configCmd returns a command that manages global configuration file
func configCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Aliases: []string{"cfg"},
Short: "Manage global configuration file",
}

cmd.AddCommand(
configShowCmd(app),
configInitCmd(app),
)
return cmd
}

// configShowCmd returns the commands that prints current configuration
func configShowCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Aliases: []string{"s", "list", "l"},
Short: "Prints current configuration",
Args: withUsage(cobra.NoArgs),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s config show --home %s
$ %s cfg list`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
if app.Config == nil {
return fmt.Errorf("config does not exist: %s", app.HomePath)
}

b, err := toml.Marshal(app.Config)
if err != nil {
return err
}

fmt.Fprintln(cmd.OutOrStdout(), string(b))
return nil
},
}
return cmd
}

// configInitCmd returns the commands that for initializing an empty config at the --home location
func configInitCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Aliases: []string{"i"},
Short: "Create a default configuration at home directory path defined by --home",
Args: withUsage(cobra.NoArgs),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s config init --home %s
$ %s cfg i`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flagHome)
if err != nil {
return err
}

file, err := cmd.Flags().GetString(flagFile)
if err != nil {
return err
}

return app.InitConfigFile(home, file)
},
}
cmd.Flags().StringP(flagFile, "f", "", "fetch toml data from specified file")
return cmd
}
137 changes: 137 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package cmd_test

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/require"

"github.com/bandprotocol/falcon/internal/relayertest"
)

func TestShowConfigCmd(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
require.NoError(t, res.Err)

res = sys.RunWithInput(t, "config", "show")
require.NoError(t, res.Err)

actual := res.Stdout.String()
require.Equal(t, relayertest.DefaultCfgText+"\n", actual)
}

func TestShowConfigCmdNotInit(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "show")
require.ErrorContains(t, res.Err, "config does not exist:")
}

func TestInitCmdDefault(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
require.NoError(t, res.Err)

cfgPath := path.Join(sys.HomeDir, "config", "config.toml")
require.FileExists(t, cfgPath)

// read the file
actualBytes, err := os.ReadFile(cfgPath)
require.NoError(t, err)

require.Equal(t, relayertest.DefaultCfgText, string(actualBytes))
}

func TestInitCmdWithFileShortFlag(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
err := os.WriteFile(customCfgPath, []byte(relayertest.CustomCfgText), 0o600)
require.NoError(t, err)

res := sys.RunWithInput(t, "config", "init", "-f", customCfgPath)
require.NoError(t, res.Err)

cfgPath := path.Join(sys.HomeDir, "config", "config.toml")
require.FileExists(t, cfgPath)

// read the file
actualBytes, err := os.ReadFile(cfgPath)
require.NoError(t, err)

require.Equal(t, relayertest.CustomCfgText, string(actualBytes))
}

func TestInitCmdWithFileLongFlag(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
err := os.WriteFile(customCfgPath, []byte(relayertest.CustomCfgText), 0o600)
require.NoError(t, err)

res := sys.RunWithInput(t, "config", "init", "--file", customCfgPath)
require.NoError(t, res.Err)

cfgPath := path.Join(sys.HomeDir, "config", "config.toml")
require.FileExists(t, cfgPath)

actualBytes, err := os.ReadFile(cfgPath)
require.NoError(t, err)

require.Equal(t, relayertest.CustomCfgText, string(actualBytes))
}

func TestInitCmdWithFileTimeString(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
err := os.WriteFile(customCfgPath, []byte(relayertest.CustomCfgTextWithTimeStr), 0o600)
require.NoError(t, err)

res := sys.RunWithInput(t, "config", "init", "--file", customCfgPath)
require.NoError(t, res.Err)

cfgPath := path.Join(sys.HomeDir, "config", "config.toml")
require.FileExists(t, cfgPath)

actualBytes, err := os.ReadFile(cfgPath)
require.NoError(t, err)

require.Equal(t, relayertest.CustomCfgText, string(actualBytes))
}

func TestInitCmdInvalidFile(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
err := os.WriteFile(customCfgPath, []byte(`[band]][]]`), 0o600)
require.NoError(t, err)

res := sys.RunWithInput(t, "config", "init", "--file", customCfgPath)
require.ErrorContains(t, res.Err, "error toml: expected newline")
}

func TestInitCmdNoCustomFile(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
res := sys.RunWithInput(t, "config", "init", "--file", customCfgPath)
require.ErrorContains(t, res.Err, "no such file or directory")
}

func TestInitCmdAlreadyExist(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
require.NoError(t, res.Err)

cfgPath := path.Join(sys.HomeDir, "config", "config.toml")
require.FileExists(t, cfgPath)

res = sys.RunWithInput(t, "config", "init")
require.ErrorContains(t, res.Err, "config already exists")
}
Loading
Loading