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 420f953
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 23 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)
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 <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)
}
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> [flags]`.

:::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 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

Expand Down
1 change: 1 addition & 0 deletions examples/network/05-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
File renamed without changes.
1 change: 0 additions & 1 deletion examples/network/05-set-chain-context.in

This file was deleted.

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.
2 changes: 1 addition & 1 deletion examples/transaction/sign.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Method: staking.Transfer
Body:
To: oasis1qrydpazemvuwtnp3efm7vmfvg3tde044qg6cxwzx
Amount: 1.0 TEST
Nonce: 50
Nonce: 53
Fee:
Amount: 0.0 TEST
Gas limit: 1265
Expand Down

0 comments on commit 420f953

Please sign in to comment.