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

feat(cmd/rpc): Automatically detect running node for RPC requests #3246

Merged
merged 24 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3246253
feat(cmd/rpc): Automatically detect running node for RPC requests
samwisegamgee369 Mar 7, 2024
b0666bd
Merge branch 'celestiaorg:main' into gaurang.rpc-detect-node
mastergaurang94 Mar 7, 2024
95e2a25
Merge branch 'main' into gaurang.rpc-detect-node
mastergaurang94 Mar 8, 2024
68cff0d
merge
samwisegamgee369 Mar 14, 2024
ab279ad
Merge branch 'gaurang.rpc-detect-node' of https://github.com/masterga…
samwisegamgee369 Mar 14, 2024
9c552c0
initial comments
samwisegamgee369 Mar 14, 2024
9a201fe
all current comments
samwisegamgee369 Mar 14, 2024
e3e042a
lint
mastergaurang94 Mar 14, 2024
c92ac72
address comments + tests
mastergaurang94 Mar 14, 2024
59caa60
comment nit
mastergaurang94 Mar 14, 2024
e049e1a
Merge branch 'main' of https://github.com/mastergaurang94/celestia-no…
mastergaurang94 Mar 15, 2024
513e427
Merge branch 'main' of https://github.com/mastergaurang94/celestia-no…
mastergaurang94 Apr 9, 2024
bcc068a
feat(cmd/rpc): read address, port, and auth settings from config
mastergaurang94 Apr 9, 2024
9ab9fe0
Merge branch 'main' into gaurang.rpc-detect-node
mastergaurang94 Apr 11, 2024
bf4833b
allow request url flag to override config and refactor request url co…
mastergaurang94 Apr 11, 2024
b4903d3
Merge branch 'gaurang.rpc-detect-node' of https://github.com/masterga…
mastergaurang94 Apr 11, 2024
322a733
lint
mastergaurang94 Apr 11, 2024
5c7b75e
Merge branch 'main' into gaurang.rpc-detect-node
mastergaurang94 Apr 11, 2024
e9cc992
Merge branch 'main' of https://github.com/mastergaurang94/celestia-no…
mastergaurang94 Apr 16, 2024
7cb1c6b
update err msgs for clarity + get networks/types from nodebuilder
mastergaurang94 Apr 16, 2024
88dfaef
lint + space convert to tab
mastergaurang94 Apr 17, 2024
6402423
one more space
mastergaurang94 Apr 17, 2024
e7a27a3
remove redudant naming
mastergaurang94 Apr 17, 2024
b34da95
Merge branch 'main' into gaurang.rpc-detect-node
mastergaurang94 Apr 17, 2024
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
35 changes: 31 additions & 4 deletions cmd/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

rpc "github.com/celestiaorg/celestia-node/api/rpc/client"
"github.com/celestiaorg/celestia-node/api/rpc/perms"
"github.com/celestiaorg/celestia-node/nodebuilder"
nodemod "github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

const (
Expand Down Expand Up @@ -47,11 +49,11 @@ func RPCFlags() *flag.FlagSet {

func InitClient(cmd *cobra.Command, _ []string) error {
if authTokenFlag == "" {
storePath := ""
if !cmd.Flag(nodeStoreFlag).Changed {
return errors.New("cant get the access to the auth token: token/node-store flag was not specified")
storePath, err := getStorePath(cmd)
if err != nil {
return err
}
storePath = cmd.Flag(nodeStoreFlag).Value.String()

mastergaurang94 marked this conversation as resolved.
Show resolved Hide resolved
token, err := getToken(storePath)
if err != nil {
return fmt.Errorf("cant get the access to the auth token: %v", err)
Expand All @@ -69,6 +71,31 @@ func InitClient(cmd *cobra.Command, _ []string) error {
return nil
}

func getStorePath(cmd *cobra.Command) (string, error) {
// if node store flag is set, use it
if cmd.Flag(nodeStoreFlag).Changed {
return cmd.Flag(nodeStoreFlag).Value.String(), nil
}

// try to detect a running node by checking for a lock file
nodeTypes := []nodemod.Type{nodemod.Bridge, nodemod.Full, nodemod.Light}
defaultNetwork := []p2p.Network{p2p.Mainnet, p2p.Mocha, p2p.Arabica, p2p.Private}
for _, t := range nodeTypes {
for _, n := range defaultNetwork {
path, err := DefaultNodeStorePath(t.String(), n.String())
if err != nil {
return "", err
}

if nodebuilder.IsOpened(path) {
return path, nil
}
}
}
mastergaurang94 marked this conversation as resolved.
Show resolved Hide resolved

return "", errors.New("cant get the access to the auth token: token/node-store flag was not specified")
}

func getToken(path string) (string, error) {
if path == "" {
return "", errors.New("root directory was not specified")
Expand Down
11 changes: 11 additions & 0 deletions nodebuilder/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nodebuilder
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
Expand Down Expand Up @@ -150,6 +151,16 @@ type fsStore struct {
dirLock *flock.Flock // protects directory
}

// IsOpened checks if a directory is in use by checking if a lock file exists.
func IsOpened(path string) bool {
lockPath := lockPath(path)
if _, err := os.Stat(lockPath); errors.Is(err, os.ErrNotExist) {
return false
}
mastergaurang94 marked this conversation as resolved.
Show resolved Hide resolved

return true
}

func storePath(path string) (string, error) {
return homedir.Expand(filepath.Clean(path))
}
Expand Down