Skip to content

Commit

Permalink
feat: oasis paratime show parameters
Browse files Browse the repository at this point in the history
Similar to oasis network show parameters. Prints ROFL staking thresholds for
now.
  • Loading branch information
matevz committed Nov 18, 2024
1 parent 94fb1f4 commit f3e06e5
Show file tree
Hide file tree
Showing 9 changed files with 484 additions and 238 deletions.
63 changes: 63 additions & 0 deletions cmd/common/json.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package common

import (
"context"
"encoding/json"
"fmt"
"strings"
"unicode/utf8"

"github.com/oasisprotocol/oasis-core/go/common"
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
Expand Down Expand Up @@ -98,3 +105,59 @@ func JSONMarshalUniversalValue(v interface{}) []byte {
cobra.CheckErr(err)
return vJSON
}

// PrettyPrint transforms generic JSON-formatted data into a pretty-printed string.
// For types implementing consensusPretty.PrettyPrinter, it uses the custom pretty printer.
// For other types, it does basic JSON indentation and cleanup of common delimiters.
func PrettyPrint(npa *NPASelection, prefix string, blob interface{}) string {
ret := ""
switch rtx := blob.(type) {
case consensusPretty.PrettyPrinter:
// Signed or unsigned consensus or runtime transaction.
var ns common.Namespace
if npa.ParaTime != nil {
ns = npa.ParaTime.Namespace()
}
sigCtx := signature.RichContext{
RuntimeID: ns,
ChainContext: npa.Network.ChainContext,
Base: types.SignatureContextBase,
}
ctx := context.Background()
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenSymbol, npa.Network.Denomination.Symbol)
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenValueExponent, npa.Network.Denomination.Decimals)
if npa.ParaTime != nil {
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
}
ctx = context.WithValue(ctx, signature.ContextKeySigContext, &sigCtx)
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, GenAccountNames())

// Set up chain context for signature verification during pretty-printing.
coreSignature.UnsafeResetChainContext()
coreSignature.SetChainContext(npa.Network.ChainContext)
var pp strings.Builder
rtx.PrettyPrint(ctx, prefix, &pp)
ret = pp.String()
default:
pp, err := PrettyJSONMarshal(blob)
cobra.CheckErr(err)

out := string(pp)
out = strings.ReplaceAll(out, "{", "")
out = strings.ReplaceAll(out, "}", "")
out = strings.ReplaceAll(out, "[", "")
out = strings.ReplaceAll(out, "]", "")
out = strings.ReplaceAll(out, ",", "")
out = strings.ReplaceAll(out, "\"", "")

for _, line := range strings.Split(out, "\n") {
line = strings.TrimRight(line, " \n")
if len(line) == 0 {
continue
}
ret += line + "\n"
}
}

return ret
}
27 changes: 2 additions & 25 deletions cmd/common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
Expand Down Expand Up @@ -352,31 +351,9 @@ func SignParaTimeTransaction(

// PrintTransactionRaw prints the transaction which can be either signed or unsigned.
func PrintTransactionRaw(npa *NPASelection, tx interface{}) {
switch rtx := tx.(type) {
switch tx.(type) {
case consensusPretty.PrettyPrinter:
// Signed or unsigned consensus or runtime transaction.
var ns common.Namespace
if npa.ParaTime != nil {
ns = npa.ParaTime.Namespace()
}
sigCtx := signature.RichContext{
RuntimeID: ns,
ChainContext: npa.Network.ChainContext,
Base: types.SignatureContextBase,
}
ctx := context.Background()
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenSymbol, npa.Network.Denomination.Symbol)
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenValueExponent, npa.Network.Denomination.Decimals)
if npa.ParaTime != nil {
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
}
ctx = context.WithValue(ctx, signature.ContextKeySigContext, &sigCtx)
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, GenAccountNames())

// Set up chain context for signature verification during pretty-printing.
coreSignature.UnsafeResetChainContext()
coreSignature.SetChainContext(npa.Network.ChainContext)
rtx.PrettyPrint(ctx, "", os.Stdout)
fmt.Print(PrettyPrint(npa, "", tx))
default:
fmt.Printf("[unsupported transaction type: %T]\n", tx)
}
Expand Down
30 changes: 3 additions & 27 deletions cmd/network/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ var showCmd = &cobra.Command{
}
return
case selParameters:
showParameters(ctx, height, consensusConn)
showParameters(ctx, npa, height, consensusConn)
return

default:
Expand Down Expand Up @@ -390,31 +390,7 @@ func showNativeToken(ctx context.Context, height int64, npa *common.NPASelection
}
}

func PrettifyFromJSON(blob interface{}) string {
pp, err := json.MarshalIndent(blob, "", " ")
cobra.CheckErr(err)

out := string(pp)
out = strings.ReplaceAll(out, "{", "")
out = strings.ReplaceAll(out, "}", "")
out = strings.ReplaceAll(out, "[", "")
out = strings.ReplaceAll(out, "]", "")
out = strings.ReplaceAll(out, ",", "")
out = strings.ReplaceAll(out, "\"", "")

ret := ""
for _, line := range strings.Split(out, "\n") {
line = strings.TrimRight(line, " \n")
if len(line) == 0 {
continue
}
ret += line + "\n"
}

return ret
}

func showParameters(ctx context.Context, height int64, cons consensus.ClientBackend) {
func showParameters(ctx context.Context, npa *common.NPASelection, height int64, cons consensus.ClientBackend) {
checkErr := func(what string, err error) {
if err != nil {
cobra.CheckErr(fmt.Errorf("%s: %w", what, err))
Expand Down Expand Up @@ -456,7 +432,7 @@ func showParameters(ctx context.Context, height int64, cons consensus.ClientBack
doc[name] = params
} else {
fmt.Printf("=== %s PARAMETERS ===\n", strings.ToUpper(name))
out := PrettifyFromJSON(params)
out := common.PrettyPrint(npa, " ", params)
fmt.Printf("%s\n", out)
}
}
Expand Down
Loading

0 comments on commit f3e06e5

Please sign in to comment.