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/network/add): Make the chain context optional #321

Merged
merged 1 commit into from
Nov 18, 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
34 changes: 27 additions & 7 deletions cmd/network/add.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
package network

import (
"context"

"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"

cliConfig "github.com/oasisprotocol/cli/config"
)

var addCmd = &cobra.Command{
Use: "add <name> <chain-context> <rpc-endpoint>",
Use: "add <name> <rpc-endpoint> [chain-context]",
Short: "Add a new network",
Args: cobra.ExactArgs(3),
Args: cobra.RangeArgs(2, 3),
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
name, chainContext, rpc := args[0], args[1], args[2]
name, rpc := args[0], args[1]

net := config.Network{
ChainContext: chainContext,
RPC: rpc,
}
// Validate initial network configuration early.
cobra.CheckErr(config.ValidateIdentifier(name))

net := config.Network{
RPC: rpc,
}

if len(args) >= 3 {
net.ChainContext = args[2]
} else {
// Connect to the network and query the chain context.
network := config.Network{
RPC: net.RPC,
}
ctx := context.Background()
conn, err := connection.ConnectNoVerify(ctx, &network)
cobra.CheckErr(err)
chainCtx, err := conn.Consensus().GetChainContext(ctx)
cobra.CheckErr(err)
net.ChainContext = chainCtx
cobra.CheckErr(net.Validate())
}

cobra.CheckErr(net.Validate())

// Ask user for some additional parameters.
Expand Down
17 changes: 12 additions & 5 deletions docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,29 @@ the current Mainnet and Testnet endpoints.

## Add a Network {#add}

Invoke `network add <name> <chain-context> <rpc-endpoint>` to add a new
Invoke `network add <name> <rpc-endpoint> [chain-context]` to add a new
endpoint with a specific chain domain separation context and a gRPC address.
This command is useful, if you want to connect to your own instance of the Oasis
node instead of relying on the public gRPC endpoints.

For TCP/IP endpoints, run:

![code shell](../examples/network/add-tcpip.in.static)
![code shell](../examples/network/add-tcpip-ctx.in.static)

![code](../examples/network/add-tcpip.out.static)
![code](../examples/network/add-tcpip-ctx.out.static)

For Unix sockets, use:

![code shell](../examples/network/add-unix.in.static)
![code shell](../examples/network/add-unix-ctx.in.static)

![code](../examples/network/add-unix-ctx.out.static)

To automatically detect the chain context, simply omit the `[chain-context]`
argument:

![code shell](../examples/network/add-tcpip.in.static)

![code](../examples/network/add-unix.out.static)
![code](../examples/network/add-tcpip.out.static)

## Add a Local Network {#add-local}

Expand Down
1 change: 1 addition & 0 deletions examples/network/add-tcpip-ctx.in.static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis network add testnet_alt testnet2.grpc.oasis.io:443 0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76
3 changes: 3 additions & 0 deletions examples/network/add-tcpip-ctx.out.static
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
? Description: Testnet alternative
? Denomination symbol: TEST
? Denomination decimal places: (9)
2 changes: 1 addition & 1 deletion examples/network/add-tcpip.in.static
Original file line number Diff line number Diff line change
@@ -1 +1 @@
oasis network add testnet_alt 0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76 testnet2.grpc.oasis.io:443
oasis network add testnet_alt testnet2.grpc.oasis.io:443
1 change: 1 addition & 0 deletions examples/network/add-unix-ctx.in.static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis network add testnet_local unix:/node_testnet/data/internal.sock 0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76
1 change: 0 additions & 1 deletion examples/network/add-unix.in.static

This file was deleted.

Loading