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(network): Move network info -> network show native-token and gas-costs #79

Merged
merged 1 commit into from
May 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/common/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ func GetNPASelection(cfg *cliConfig.Config) *NPASelection {
return &s
}

// PrettyPrintNetwork formats the network name and description, if one exists.
func (npa *NPASelection) PrettyPrintNetwork() (out string) {
out = npa.NetworkName
if len(npa.Network.Description) > 0 {
out += fmt.Sprintf(" (%s)", npa.Network.Description)
}
return
}

func init() {
SelectorFlags = flag.NewFlagSet("", flag.ContinueOnError)
SelectorFlags.StringVar(&selectedNetwork, "network", "", "explicitly set network to use")
Expand Down
5 changes: 1 addition & 4 deletions cmd/common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,7 @@ func PrintTransaction(npa *NPASelection, tx interface{}) {
PrintTransactionRaw(npa, tx)

fmt.Println()
fmt.Printf("Network: %s", npa.NetworkName)
if len(npa.Network.Description) > 0 {
fmt.Printf(" (%s)", npa.Network.Description)
}
fmt.Printf("Network: %s", npa.PrettyPrintNetwork())

fmt.Println()
if isRuntimeTx(tx) && npa.ParaTime != nil {
Expand Down
125 changes: 0 additions & 125 deletions cmd/network/info.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func init() {
Cmd.AddCommand(addLocalCmd)
Cmd.AddCommand(governanceProposalCmd)
Cmd.AddCommand(listCmd)
Cmd.AddCommand(infoCmd)
Cmd.AddCommand(rmCmd)
Cmd.AddCommand(setDefaultCmd)
Cmd.AddCommand(setRPCCmd)
Expand Down
122 changes: 112 additions & 10 deletions cmd/network/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
"github.com/oasisprotocol/oasis-core/go/staking/api/token"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
Expand All @@ -19,20 +22,22 @@ import (
cliConfig "github.com/oasisprotocol/cli/config"
)

type registrySelector int
type propertySelector int

const (
selInvalid registrySelector = iota
selInvalid propertySelector = iota
selEntities
selNodes
selRuntimes
selValidators
selNativeToken
selGasCosts
)

var showCmd = &cobra.Command{
Use: "show { <id> | entities | nodes | runtimes | validators }",
Short: "Show entry in the network's registry",
Long: "Show entry in the network's registry by ID or all entries of a specified kind",
Use: "show { <id> | entities | nodes | runtimes | validators | native-token | gas-costs }",
Short: "Show network properties",
Long: "Show network property stored in the registry, scheduler, genesis document or chain. Query by ID, hash or a specified kind.",
Args: cobra.ExactArgs(1),
Aliases: []string{"s"},
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -123,7 +128,7 @@ var showCmd = &cobra.Command{
}

// Probably don't need to bother querying the runtimes by address.
case registrySelector:
case propertySelector:
switch v {
case selEntities:
entities, err := registryConn.GetEntities(ctx, height)
Expand Down Expand Up @@ -153,8 +158,6 @@ var showCmd = &cobra.Command{
}
return
case selValidators:
// Yes, this is a scheduler query, not a registry query
// but this also is a reasonable place for this.
schedulerConn := consensusConn.Scheduler()
validators, err := schedulerConn.GetValidators(ctx, height)
cobra.CheckErr(err)
Expand All @@ -163,6 +166,22 @@ var showCmd = &cobra.Command{
cobra.CheckErr(err)
}
return
case selNativeToken:
stakingConn := consensusConn.Staking()
showNativeToken(ctx, height, npa, stakingConn)
return
case selGasCosts:
stakingConn := consensusConn.Staking()
consensusParams, err := stakingConn.ConsensusParameters(ctx, height)
cobra.CheckErr(err)

fmt.Printf("Gas costs for network %s:", npa.PrettyPrintNetwork())
fmt.Println()
for kind, cost := range consensusParams.GasCosts {
fmt.Printf(" - %-26s %d", kind+":", cost)
fmt.Println()
}
return
default:
// Should never happen.
}
Expand Down Expand Up @@ -196,7 +215,7 @@ func parseIdentifier(
return nil, fmt.Errorf("unrecognized id: '%s'", s)
}

func selectorFromString(s string) registrySelector {
func selectorFromString(s string) propertySelector {
switch strings.ToLower(strings.TrimSpace(s)) {
case "entities":
return selEntities
Expand All @@ -206,11 +225,94 @@ func selectorFromString(s string) registrySelector {
return selRuntimes
case "validators":
return selValidators
case "native-token":
return selNativeToken
case "gas-costs":
return selGasCosts
}
return selInvalid
}

func showNativeToken(ctx context.Context, height int64, npa *common.NPASelection, stakingConn staking.Backend) {
fmt.Printf("%-25s %s", "Network:", npa.PrettyPrintNetwork())
fmt.Println()

tokenSymbol, err := stakingConn.TokenSymbol(ctx)
cobra.CheckErr(err)
tokenValueExponent, err := stakingConn.TokenValueExponent(ctx)
cobra.CheckErr(err)

ctx = context.WithValue(
ctx,
consensusPretty.ContextKeyTokenSymbol,
tokenSymbol,
)
ctx = context.WithValue(
ctx,
consensusPretty.ContextKeyTokenValueExponent,
tokenValueExponent,
)

fmt.Printf("%-25s %s", "Token's ticker symbol:", tokenSymbol)
fmt.Println()
fmt.Printf("%-25s %d", "Token's base-10 exponent:", tokenValueExponent)
fmt.Println()

totalSupply, err := stakingConn.TotalSupply(ctx, height)
cobra.CheckErr(err)
fmt.Printf("%-25s ", "Total supply:")
token.PrettyPrintAmount(ctx, *totalSupply, os.Stdout)
fmt.Println()

commonPool, err := stakingConn.CommonPool(ctx, height)
cobra.CheckErr(err)
fmt.Printf("%-25s ", "Common pool:")
token.PrettyPrintAmount(ctx, *commonPool, os.Stdout)
fmt.Println()

lastBlockFees, err := stakingConn.LastBlockFees(ctx, height)
cobra.CheckErr(err)
fmt.Printf("%-25s ", "Last block fees:")
token.PrettyPrintAmount(ctx, *lastBlockFees, os.Stdout)
fmt.Println()

governanceDeposits, err := stakingConn.GovernanceDeposits(ctx, height)
cobra.CheckErr(err)
fmt.Printf("%-25s ", "Governance deposits:")
token.PrettyPrintAmount(ctx, *governanceDeposits, os.Stdout)
fmt.Println()

consensusParams, err := stakingConn.ConsensusParameters(ctx, height)
cobra.CheckErr(err)

fmt.Printf("%-25s %d epoch(s)", "Debonding interval:", consensusParams.DebondingInterval)
fmt.Println()

fmt.Println("\n=== STAKING THRESHOLDS ===")
thresholdsToQuery := []staking.ThresholdKind{
staking.KindEntity,
staking.KindNodeValidator,
staking.KindNodeCompute,
staking.KindNodeKeyManager,
staking.KindRuntimeCompute,
staking.KindRuntimeKeyManager,
}
for _, kind := range thresholdsToQuery {
threshold, err := stakingConn.Threshold(
ctx,
&staking.ThresholdQuery{
Kind: kind,
Height: height,
},
)
cobra.CheckErr(err)
fmt.Printf(" %-19s ", kind.String()+":")
token.PrettyPrintAmount(ctx, *threshold, os.Stdout)
fmt.Println()
}
}

func init() {
showCmd.Flags().AddFlagSet(common.SelectorNPFlags)
showCmd.Flags().AddFlagSet(common.SelectorNFlags)
showCmd.Flags().AddFlagSet(common.HeightFlag)
}