From 15d3ba691684cecd13160e5e4c39c9ce971c868a Mon Sep 17 00:00:00 2001 From: amela Date: Wed, 13 Nov 2024 10:47:49 +0100 Subject: [PATCH] feat(cmd/network): Autodetect chain context if none passed --- cmd/network/set_chain_context.go | 67 +++++++++++++------ docs/network.md | 10 ++- examples/network/05-set-chain-context-ctx.in | 1 + ...ntext.out => 05-set-chain-context-ctx.out} | 0 examples/network/05-set-chain-context.in | 1 - examples/network/set-chain-context.in.static | 1 + examples/network/set-chain-context.out.static | 0 7 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 examples/network/05-set-chain-context-ctx.in rename examples/network/{05-set-chain-context.out => 05-set-chain-context-ctx.out} (100%) delete mode 100644 examples/network/05-set-chain-context.in create mode 100644 examples/network/set-chain-context.in.static create mode 100644 examples/network/set-chain-context.out.static diff --git a/cmd/network/set_chain_context.go b/cmd/network/set_chain_context.go index 78582aa7..83e260e1 100644 --- a/cmd/network/set_chain_context.go +++ b/cmd/network/set_chain_context.go @@ -1,30 +1,59 @@ package network import ( + "context" "fmt" "github.com/spf13/cobra" + flag "github.com/spf13/pflag" + + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" cliConfig "github.com/oasisprotocol/cli/config" + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" +) + +var ( + chainContext string + + setChainContextCmd = &cobra.Command{ + Use: "set-chain-context ", + Short: "Sets the chain context of the given network", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + cfg := cliConfig.Global() + name := args[0] + + net := cfg.Networks.All[name] + if net == nil { + cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name)) + return // To make staticcheck happy as it doesn't know CheckErr exits. + } + + if chainContext != "" { + net.ChainContext = chainContext + } 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()) + } + + err := cfg.Save() + cobra.CheckErr(err) + }, + } ) -var setChainContextCmd = &cobra.Command{ - Use: "set-chain-context ", - Short: "Sets the chain context of the given network", - Args: cobra.ExactArgs(2), - Run: func(cmd *cobra.Command, args []string) { - cfg := cliConfig.Global() - name, chainContext := args[0], args[1] - - net := cfg.Networks.All[name] - if net == nil { - cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name)) - return // To make staticcheck happy as it doesn't know CheckErr exits. - } - - net.ChainContext = chainContext - - err := cfg.Save() - cobra.CheckErr(err) - }, +func init() { + chainContextFlag := flag.NewFlagSet("", flag.ContinueOnError) + chainContextFlag.StringVar(&chainContext, "chain-context", "", "set chain-context") + setChainContextCmd.Flags().AddFlagSet(chainContextFlag) } diff --git a/docs/network.md b/docs/network.md index dd2a4e04..9e68a1fb 100644 --- a/docs/network.md +++ b/docs/network.md @@ -95,7 +95,7 @@ You can also delete network in non-interactive mode format by passing the ## Set Network Chain Context {#set-chain-context} To change the chain context of a network, use -`network set-chain-context `. +`network set-chain-context [flags]`. :::caution @@ -110,12 +110,18 @@ documentation. ![code shell](../examples/network/04-list.out) -![code shell](../examples/network/05-set-chain-context.in) +![code shell](../examples/network/05-set-chain-context-ctx.in) ![code shell](../examples/network/06-list.in) ![code shell](../examples/network/06-list.out) +To automatically detect the chain context, simply run the command without the +`chain-context` flag. This is especially useful for Localnet, where the chain +context often changes each time you restart the `oasis-net-runner`. + +![code shell](../examples/network/set-chain-context.in.static) + [Mainnet]: https://github.com/oasisprotocol/docs/blob/main/docs/node/mainnet/README.md [Testnet]: https://github.com/oasisprotocol/docs/blob/main/docs/node/testnet/README.md diff --git a/examples/network/05-set-chain-context-ctx.in b/examples/network/05-set-chain-context-ctx.in new file mode 100644 index 00000000..3f4a908a --- /dev/null +++ b/examples/network/05-set-chain-context-ctx.in @@ -0,0 +1 @@ +oasis network set-chain-context mainnet_local --chain-context 01234513331133a715c7a150313877dF1d33e77a715c7a150313877dF1d33e77 diff --git a/examples/network/05-set-chain-context.out b/examples/network/05-set-chain-context-ctx.out similarity index 100% rename from examples/network/05-set-chain-context.out rename to examples/network/05-set-chain-context-ctx.out diff --git a/examples/network/05-set-chain-context.in b/examples/network/05-set-chain-context.in deleted file mode 100644 index b78a28bc..00000000 --- a/examples/network/05-set-chain-context.in +++ /dev/null @@ -1 +0,0 @@ -oasis network set-chain-context mainnet_local 01234513331133a715c7a150313877dF1d33e77a715c7a150313877dF1d33e77 diff --git a/examples/network/set-chain-context.in.static b/examples/network/set-chain-context.in.static new file mode 100644 index 00000000..806def8e --- /dev/null +++ b/examples/network/set-chain-context.in.static @@ -0,0 +1 @@ +oasis network set-chain-context mainnet_local diff --git a/examples/network/set-chain-context.out.static b/examples/network/set-chain-context.out.static new file mode 100644 index 00000000..e69de29b