forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
477 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
rpc "github.com/celestiaorg/celestia-node/api/rpc/client" | ||
) | ||
|
||
const ( | ||
// defaultRPCAddress is a default address to dial to | ||
defaultRPCAddress = "http://localhost:26658" | ||
authEnvKey = "CELESTIA_NODE_AUTH_TOKEN" //nolint:gosec | ||
) | ||
|
||
var ( | ||
requestURL string | ||
authTokenFlag string | ||
) | ||
|
||
func InitURLFlag() (*string, string, string, string) { | ||
return &requestURL, "url", defaultRPCAddress, "Request URL" | ||
} | ||
|
||
func InitAuthTokenFlag() (*string, string, string, string) { | ||
return &authTokenFlag, | ||
"token", | ||
"", | ||
"Authorization token (if not provided, the " + authEnvKey + " environment variable will be used)" | ||
} | ||
|
||
func InitClient(cmd *cobra.Command, _ []string) error { | ||
if authTokenFlag == "" { | ||
authTokenFlag = os.Getenv(authEnvKey) | ||
} | ||
|
||
client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.WithValue(cmd.Context(), rpcClientKey{}, client) | ||
cmd.SetContext(ctx) | ||
return nil | ||
} | ||
|
||
type rpcClientKey struct{} | ||
|
||
func ParseClientFromCtx(ctx context.Context) (*rpc.Client, error) { | ||
client, ok := ctx.Value(rpcClientKey{}).(*rpc.Client) | ||
if !ok { | ||
return nil, errors.New("rpc client was not set") | ||
} | ||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/celestiaorg/celestia-node/cmd/celestia/util" | ||
) | ||
|
||
func init() { | ||
Cmd.AddCommand(samplingStatsCmd) | ||
} | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "das [command]", | ||
Short: "Allows to interact with the Daser via JSON-RPC", | ||
Args: cobra.NoArgs, | ||
PersistentPreRunE: util.InitClient, | ||
} | ||
|
||
var samplingStatsCmd = &cobra.Command{ | ||
Use: "sampling-stats", | ||
Short: "Returns the current statistics over the DA sampling process", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := util.ParseClientFromCtx(cmd.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
stats, err := client.DAS.SamplingStats(cmd.Context()) | ||
return util.PrintOutput(stats, err, nil) | ||
}, | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.