From e7a6eb6db9d4fb9f8da1b3e7bdb1aace9be0d0dc Mon Sep 17 00:00:00 2001 From: amela Date: Mon, 18 Nov 2024 09:42:05 +0100 Subject: [PATCH] feat(cmd/network/add): Make the chain context optional --- cmd/network/add.go | 34 +++++++++++++++---- docs/network.md | 17 +++++++--- examples/network/add-tcpip-ctx.in.static | 1 + examples/network/add-tcpip-ctx.out.static | 3 ++ examples/network/add-tcpip.in.static | 2 +- examples/network/add-unix-ctx.in.static | 1 + ...nix.out.static => add-unix-ctx.out.static} | 0 examples/network/add-unix.in.static | 1 - 8 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 examples/network/add-tcpip-ctx.in.static create mode 100644 examples/network/add-tcpip-ctx.out.static create mode 100644 examples/network/add-unix-ctx.in.static rename examples/network/{add-unix.out.static => add-unix-ctx.out.static} (100%) delete mode 100644 examples/network/add-unix.in.static diff --git a/cmd/network/add.go b/cmd/network/add.go index 54991ffd..8265eab2 100644 --- a/cmd/network/add.go +++ b/cmd/network/add.go @@ -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 ", + Use: "add [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. diff --git a/docs/network.md b/docs/network.md index 1f2ab9ff..ced835e4 100644 --- a/docs/network.md +++ b/docs/network.md @@ -32,22 +32,29 @@ the current Mainnet and Testnet endpoints. ## Add a Network {#add} -Invoke `network add ` to add a new +Invoke `network add [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} diff --git a/examples/network/add-tcpip-ctx.in.static b/examples/network/add-tcpip-ctx.in.static new file mode 100644 index 00000000..3c211729 --- /dev/null +++ b/examples/network/add-tcpip-ctx.in.static @@ -0,0 +1 @@ +oasis network add testnet_alt testnet2.grpc.oasis.io:443 0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76 diff --git a/examples/network/add-tcpip-ctx.out.static b/examples/network/add-tcpip-ctx.out.static new file mode 100644 index 00000000..112fcd4c --- /dev/null +++ b/examples/network/add-tcpip-ctx.out.static @@ -0,0 +1,3 @@ +? Description: Testnet alternative +? Denomination symbol: TEST +? Denomination decimal places: (9) diff --git a/examples/network/add-tcpip.in.static b/examples/network/add-tcpip.in.static index 62449d43..33e32476 100644 --- a/examples/network/add-tcpip.in.static +++ b/examples/network/add-tcpip.in.static @@ -1 +1 @@ -oasis network add testnet_alt 0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76 testnet2.grpc.oasis.io:443 +oasis network add testnet_alt testnet2.grpc.oasis.io:443 diff --git a/examples/network/add-unix-ctx.in.static b/examples/network/add-unix-ctx.in.static new file mode 100644 index 00000000..171d5a4f --- /dev/null +++ b/examples/network/add-unix-ctx.in.static @@ -0,0 +1 @@ +oasis network add testnet_local unix:/node_testnet/data/internal.sock 0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76 diff --git a/examples/network/add-unix.out.static b/examples/network/add-unix-ctx.out.static similarity index 100% rename from examples/network/add-unix.out.static rename to examples/network/add-unix-ctx.out.static diff --git a/examples/network/add-unix.in.static b/examples/network/add-unix.in.static deleted file mode 100644 index 51cf9dcd..00000000 --- a/examples/network/add-unix.in.static +++ /dev/null @@ -1 +0,0 @@ -oasis network add testnet_local 0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76 unix:/node_testnet/data/internal.sock