Skip to content

Commit da12da6

Browse files
committed
feat(cmd/network): Autodetect chain context if none passed
1 parent 6329841 commit da12da6

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

cmd/network/set_chain_context.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,59 @@
11
package network
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/spf13/cobra"
8+
flag "github.com/spf13/pflag"
9+
10+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
711

812
cliConfig "github.com/oasisprotocol/cli/config"
13+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
14+
)
15+
16+
var (
17+
chainContext string
18+
19+
setChainContextCmd = &cobra.Command{
20+
Use: "set-chain-context <name>",
21+
Short: "Sets the chain context of the given network",
22+
Args: cobra.ExactArgs(1),
23+
Run: func(cmd *cobra.Command, args []string) {
24+
cfg := cliConfig.Global()
25+
name := args[0]
26+
27+
net := cfg.Networks.All[name]
28+
if net == nil {
29+
cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name))
30+
return // To make staticcheck happy as it doesn't know CheckErr exits.
31+
}
32+
33+
if chainContext != "" {
34+
net.ChainContext = chainContext
35+
} else {
36+
// Connect to the network and query the chain context.
37+
network := config.Network{
38+
RPC: net.RPC,
39+
}
40+
ctx := context.Background()
41+
conn, err := connection.ConnectNoVerify(ctx, &network)
42+
cobra.CheckErr(err)
43+
chainCtx, err := conn.Consensus().GetChainContext(ctx)
44+
cobra.CheckErr(err)
45+
net.ChainContext = chainCtx
46+
cobra.CheckErr(net.Validate())
47+
}
48+
49+
err := cfg.Save()
50+
cobra.CheckErr(err)
51+
},
52+
}
953
)
1054

11-
var setChainContextCmd = &cobra.Command{
12-
Use: "set-chain-context <name> <chain-context>",
13-
Short: "Sets the chain context of the given network",
14-
Args: cobra.ExactArgs(2),
15-
Run: func(cmd *cobra.Command, args []string) {
16-
cfg := cliConfig.Global()
17-
name, chainContext := args[0], args[1]
18-
19-
net := cfg.Networks.All[name]
20-
if net == nil {
21-
cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name))
22-
return // To make staticcheck happy as it doesn't know CheckErr exits.
23-
}
24-
25-
net.ChainContext = chainContext
26-
27-
err := cfg.Save()
28-
cobra.CheckErr(err)
29-
},
55+
func init() {
56+
chainContextFlag := flag.NewFlagSet("", flag.ContinueOnError)
57+
chainContextFlag.StringVar(&chainContext, "chain-context", "", "set chain-context")
58+
setChainContextCmd.Flags().AddFlagSet(chainContextFlag)
3059
}

docs/network.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ You can also delete network in non-interactive mode format by passing the
9595
## Set Network Chain Context {#set-chain-context}
9696

9797
To change the chain context of a network, use
98-
`network set-chain-context <name> <chain-context>`.
98+
`network set-chain-context <name> [flags]`.
9999

100100
:::caution
101101

@@ -110,12 +110,18 @@ documentation.
110110

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

113-
![code shell](../examples/network/05-set-chain-context.in)
113+
![code shell](../examples/network/07-set-chain-context-ctx.in)
114114

115115
![code shell](../examples/network/06-list.in)
116116

117117
![code shell](../examples/network/06-list.out)
118118

119+
To automatically detect the chain context, simply run the command without the
120+
`chain-context` flag. This is especially useful for Localnet, where the chain
121+
context often changes each time you restart the `oasis-net-runner`.
122+
123+
![code shell](../examples/network/05-set-chain-context.in)
124+
119125
[Mainnet]: https://github.com/oasisprotocol/docs/blob/main/docs/node/mainnet/README.md
120126
[Testnet]: https://github.com/oasisprotocol/docs/blob/main/docs/node/testnet/README.md
121127

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
oasis network set-chain-context mainnet_local 01234513331133a715c7a150313877dF1d33e77a715c7a150313877dF1d33e77
1+
oasis network set-chain-context mainnet_local
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oasis network set-chain-context mainnet_local --chain-context 01234513331133a715c7a150313877dF1d33e77a715c7a150313877dF1d33e77

examples/network/07-set-chain-context-ctx.out

Whitespace-only changes.

0 commit comments

Comments
 (0)