Skip to content

Commit

Permalink
Merge pull request #390 from jbowen93/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan authored Feb 22, 2022
2 parents 91e0253 + be85542 commit bf8c028
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Month, DD, YYYY
- [docker: Created `docker/` dir with `Dockerfile` and `entrypoint.sh` script](https://github.com/celestiaorg/celestia-node/pull/295) [@jbowen93](https://github.com/jbowen93)
- [chore(share): handle rows concurrently in GetSharesByNamespace #241](https://github.com/celestiaorg/celestia-node/pull/241) [@vgonkivs](https://github.com/vgonkivs)
- [ci: adding data race detector action](https://github.com/celestiaorg/celestia-node/pull/289) [@Bidon15](https://github.com/Bidon15)
- [node: add the cmdnode.HeadersFlags() to the Bridge Node's init and start commands #390](https://github.com/celestiaorg/celestia-node/pull/390) [@jbowen93](https://github.com/jbowen93)

### BUG FIXES

Expand All @@ -55,6 +56,7 @@ Month, DD, YYYY
- [ci: increase tokens ratio for dupl to fix false positive scenarios](https://github.com/celestiaorg/celestia-node/pull/314) [@Bidon15](https://github.com/Bidon15)
- [node: Wrap datastore with mutex to prevent data race](https://github.com/celestiaorg/celestia-node/pull/325) [@Bidon15](https://github.com/Bidon15)
- [ci: update Docker entrypoint.sh to use new `store.path` flag name](https://github.com/celestiaorg/celestia-node/pull/337) [@jbowen93](https://github.com/jbowen93)
- [docker: update docker/entrypoint.sh to use new `node.store` flag replacing `store.path` #390](https://github.com/celestiaorg/celestia-node/pull/390) [@jbowen93 ](https://github.com/jbowen93)

### MISCELLANEOUS

Expand Down
7 changes: 7 additions & 0 deletions cmd/celestia/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ func init() {
cmdnode.NodeFlags(node.Bridge),
cmdnode.P2PFlags(),
cmdnode.CoreFlags(),
cmdnode.TrustedHashFlags(),
cmdnode.MiscFlags(),
),
cmdnode.Start(
cmdnode.NodeFlags(node.Bridge),
cmdnode.P2PFlags(),
cmdnode.CoreFlags(),
cmdnode.TrustedHashFlags(),
cmdnode.MiscFlags(),
),
)
Expand Down Expand Up @@ -53,6 +55,11 @@ var bridgeCmd = &cobra.Command{
return err
}

err = cmdnode.ParseTrustedHashFlags(cmd, env)
if err != nil {
return err
}

err = cmdnode.ParseMiscFlags(cmd)
if err != nil {
return err
Expand Down
107 changes: 107 additions & 0 deletions cmd/celestia/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
)

func TestLight(t *testing.T) {
// Run the tests in a temporary directory
tmpDir, err := ioutil.TempDir("", "light")
require.NoError(t, err, "error creating a temporary test directory")
testDir, err := os.Getwd()
require.NoError(t, err, "error getting the current working directory")
err = os.Chdir(tmpDir)
require.NoError(t, err, "error changing to the temporary test directory")

t.Run("init", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"bridge",
"--node.store", ".celestia-light",
"init",
})
err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
require.NoError(t, err)
})

t.Cleanup(func() {
if err := os.Chdir(testDir); err != nil {
t.Error("error resetting:", err)
}
})

// TODO @jbowen93: Commented out until a dry-run option can be implemented
/*
t.Run("start", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"light",
"--node.store", ".celestia-light",
"start",
"--headers.trusted-peer",
"/ip4/192.167.10.6/tcp/2121/p2p/12D3KooWL8z3KARAYJcmExhDsGwKbjChKeGaJpFPENyADdxmEHzw",
"--headers.trusted-hash",
"54A8B66D2BEF13850D67C8D474E196BD7485FE5A79989E31B17169371B0A9C96",
})
err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
require.NoError(t, err)
})
*/
}

func TestBridge(t *testing.T) {
// Run the tests in a temporary directory
tmpDir, err := ioutil.TempDir("", "bridge")
require.NoError(t, err, "error creating a temporary test directory")
testDir, err := os.Getwd()
require.NoError(t, err, "error getting the current working directory")
err = os.Chdir(tmpDir)
require.NoError(t, err, "error changing to the temporary test directory")

t.Run("init", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"bridge",
"--node.store", ".celestia-bridge",
"init",
})
err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
require.NoError(t, err)
})

t.Cleanup(func() {
if err := os.Chdir(testDir); err != nil {
t.Error("error resetting:", err)
}
})

// TODO @jbowen93: Commented out until a dry-run option can be implemented
/*
t.Run("start", func(t *testing.T) {
output := &bytes.Buffer{}
rootCmd.SetOut(output)
rootCmd.SetArgs([]string{
"bridge",
"--node.store", ".celestia-bridge",
"start",
"--core.remote",
"tcp://192.167.10.2:26657",
"--headers.trusted-hash",
"54A8B66D2BEF13850D67C8D474E196BD7485FE5A79989E31B17169371B0A9C96",
})
err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
require.NoError(t, err)
})
*/
}
72 changes: 51 additions & 21 deletions cmd/flags_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,67 @@ var (
func HeadersFlags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.AddFlagSet(TrustedPeerFlags())
flags.AddFlagSet(TrustedHashFlags())

return flags
}

// ParseHeadersFlags parses Header package flags from the given cmd and applies values to Env.
func ParseHeadersFlags(cmd *cobra.Command, env *Env) error {
if err := ParseTrustedHashFlags(cmd, env); err != nil {
return err
}
if err := ParseTrustedPeerFlags(cmd, env); err != nil {
return err
}

return nil
}

// TrustedPeerFlags returns a set of flags related to configuring a `TrustedPeer`.
func TrustedPeerFlags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.String(
headersTrustedHashFlag,
headersTrustedPeersFlag,
"",
"Hex encoded header hash. Used to subjectively initialize header synchronization",
"Multiaddr of a reliable peer to fetch headers from. (Format: multiformats.io/multiaddr)",
)

flags.StringSlice(
headersTrustedPeersFlag,
make([]string, 0),
"Multiaddr of a reliable peer to fetch headers from. (Format: multiformats.io/multiaddr)",
return flags
}

// ParseTrustedPeerFlags parses Header package flags from the given cmd and applies values to Env.
func ParseTrustedPeerFlags(cmd *cobra.Command, env *Env) error {
tpeer := cmd.Flag(headersTrustedPeersFlag).Value.String()
if tpeer != "" {
_, err := multiaddr.NewMultiaddr(tpeer)
if err != nil {
return fmt.Errorf("cmd: while parsing '%s': %w", headersTrustedPeersFlag, err)
}

env.AddOptions(node.WithTrustedPeer(tpeer))
}

return nil
}

// TrustedHashFlags returns a set of flags related to configuring a `TrustedHash`.
func TrustedHashFlags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.String(
headersTrustedHashFlag,
"",
"Hex encoded header hash. Used to subjectively initialize header synchronization",
)

return flags
}

// ParseHeadersFlags parses Header package flags from the given cmd and applies values to Env.
func ParseHeadersFlags(cmd *cobra.Command, env *Env) error {
func ParseTrustedHashFlags(cmd *cobra.Command, env *Env) error {
hash := cmd.Flag(headersTrustedHashFlag).Value.String()
if hash != "" {
_, err := hex.DecodeString(hash)
Expand All @@ -47,19 +91,5 @@ func ParseHeadersFlags(cmd *cobra.Command, env *Env) error {
env.AddOptions(node.WithTrustedHash(hash))
}

tpeers, err := cmd.Flags().GetStringSlice(headersTrustedPeersFlag)
if err != nil {
return err
}
if len(tpeers) != 0 {
for _, tpeer := range tpeers {
_, err := multiaddr.NewMultiaddr(tpeer)
if err != nil {
return fmt.Errorf("cmd: while parsing '%s' with peer addr '%s': %w", headersTrustedPeersFlag, tpeer, err)
}
env.AddOptions(node.WithTrustedPeer(tpeer))
}
}

return nil
}
2 changes: 1 addition & 1 deletion docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

if [ "$1" = 'celestia' ]; then
./celestia "${NODE_TYPE}" --store.path /celestia-"${NODE_TYPE}" init
./celestia "${NODE_TYPE}" --node.store /celestia-"${NODE_TYPE}" init

exec ./"$@" "--"
fi
Expand Down

0 comments on commit bf8c028

Please sign in to comment.