Skip to content

Commit

Permalink
feat(cmd/network): Autodetect chain context if none passed
Browse files Browse the repository at this point in the history
  • Loading branch information
amela committed Nov 13, 2024
1 parent 6329841 commit 6080e84
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
67 changes: 48 additions & 19 deletions cmd/network/set_chain_context.go
Original file line number Diff line number Diff line change
@@ -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 <name>",
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)
chainContext, err := conn.Consensus().GetChainContext(ctx)

Check failure on line 43 in cmd/network/set_chain_context.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "chainContext" shadows declaration at line 17 (govet)
cobra.CheckErr(err)
net.ChainContext = chainContext
cobra.CheckErr(net.Validate())
}

err := cfg.Save()
cobra.CheckErr(err)
},
}
)

var setChainContextCmd = &cobra.Command{
Use: "set-chain-context <name> <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)
}
9 changes: 8 additions & 1 deletion docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ documentation.

![code shell](../examples/network/04-list.out)

![code shell](../examples/network/05-set-chain-context.in)
![code shell](../examples/network/07-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/05-set-chain-context.in)


[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

Expand Down
2 changes: 1 addition & 1 deletion examples/network/05-set-chain-context.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
oasis network set-chain-context mainnet_local 01234513331133a715c7a150313877dF1d33e77a715c7a150313877dF1d33e77
oasis network set-chain-context mainnet_local
1 change: 1 addition & 0 deletions examples/network/07-set-chain-context-ctx.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis network set-chain-context mainnet_local --chain-context 01234513331133a715c7a150313877dF1d33e77a715c7a150313877dF1d33e77
Empty file.

0 comments on commit 6080e84

Please sign in to comment.