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): Autodetect chain context if none passed #317

Merged
merged 1 commit into from
Nov 15, 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
26 changes: 22 additions & 4 deletions cmd/network/set_chain_context.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
package network

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please group these correctly.

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

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

var setChainContextCmd = &cobra.Command{
Use: "set-chain-context <name> <chain-context>",
Use: "set-chain-context <name> [chain-context]",
Short: "Sets the chain context of the given network",
Args: cobra.ExactArgs(2),
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
name, chainContext := args[0], args[1]
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.
}

net.ChainContext = chainContext
if len(args) >= 2 {
net.ChainContext = args[1]
} 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)
Expand Down
10 changes: 8 additions & 2 deletions docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name> <chain-context>`.
`network set-chain-context <name> [chain-context]`.

:::caution

Expand All @@ -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 omit the `[chain-context]`
argument. This is especially useful for Localnet, where the chain context
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

Expand Down
1 change: 1 addition & 0 deletions examples/network/set-chain-context.in.static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis network set-chain-context mainnet_local
Empty file.
Loading