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

Add oasis paratime show parameters to show ROFL staking thresholds #318

Merged
merged 4 commits into from
Nov 19, 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
4 changes: 2 additions & 2 deletions .github/workflows/ci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Set up Node.js 18
- name: Set up Node.js 20
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
- name: Set up Go
uses: actions/setup-go@v5
with:
Expand Down
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
Loading