Skip to content

Commit 4d5c255

Browse files
authored
Merge pull request #318 from oasisprotocol/matevz/feature/rofl-staking-thresholds
Add `oasis paratime show parameters` to show ROFL staking thresholds
2 parents 7dd53bf + 54d661a commit 4d5c255

File tree

12 files changed

+521
-258
lines changed

12 files changed

+521
-258
lines changed

.github/workflows/ci-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ jobs:
3434
uses: actions/setup-python@v5
3535
with:
3636
python-version: '3.x'
37-
- name: Set up Node.js 18
37+
- name: Set up Node.js 20
3838
uses: actions/setup-node@v4
3939
with:
40-
node-version: 18
40+
node-version: 20
4141
- name: Set up Go
4242
uses: actions/setup-go@v5
4343
with:

cmd/common/json.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package common
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"strings"
78
"unicode/utf8"
89

10+
"github.com/oasisprotocol/oasis-core/go/common"
11+
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
12+
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
13+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
14+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
15+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
916
"github.com/spf13/cobra"
1017

1118
"github.com/oasisprotocol/oasis-core/go/common/cbor"
@@ -98,3 +105,59 @@ func JSONMarshalUniversalValue(v interface{}) []byte {
98105
cobra.CheckErr(err)
99106
return vJSON
100107
}
108+
109+
// PrettyPrint transforms generic JSON-formatted data into a pretty-printed string.
110+
// For types implementing consensusPretty.PrettyPrinter, it uses the custom pretty printer.
111+
// For other types, it does basic JSON indentation and cleanup of common delimiters.
112+
func PrettyPrint(npa *NPASelection, prefix string, blob interface{}) string {
113+
ret := ""
114+
switch rtx := blob.(type) {
115+
case consensusPretty.PrettyPrinter:
116+
// Signed or unsigned consensus or runtime transaction.
117+
var ns common.Namespace
118+
if npa.ParaTime != nil {
119+
ns = npa.ParaTime.Namespace()
120+
}
121+
sigCtx := signature.RichContext{
122+
RuntimeID: ns,
123+
ChainContext: npa.Network.ChainContext,
124+
Base: types.SignatureContextBase,
125+
}
126+
ctx := context.Background()
127+
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenSymbol, npa.Network.Denomination.Symbol)
128+
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenValueExponent, npa.Network.Denomination.Decimals)
129+
if npa.ParaTime != nil {
130+
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
131+
}
132+
ctx = context.WithValue(ctx, signature.ContextKeySigContext, &sigCtx)
133+
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, GenAccountNames())
134+
135+
// Set up chain context for signature verification during pretty-printing.
136+
coreSignature.UnsafeResetChainContext()
137+
coreSignature.SetChainContext(npa.Network.ChainContext)
138+
var pp strings.Builder
139+
rtx.PrettyPrint(ctx, prefix, &pp)
140+
ret = pp.String()
141+
default:
142+
pp, err := PrettyJSONMarshal(blob)
143+
cobra.CheckErr(err)
144+
145+
out := string(pp)
146+
out = strings.ReplaceAll(out, "{", "")
147+
out = strings.ReplaceAll(out, "}", "")
148+
out = strings.ReplaceAll(out, "[", "")
149+
out = strings.ReplaceAll(out, "]", "")
150+
out = strings.ReplaceAll(out, ",", "")
151+
out = strings.ReplaceAll(out, "\"", "")
152+
153+
for _, line := range strings.Split(out, "\n") {
154+
line = strings.TrimRight(line, " \n")
155+
if len(line) == 0 {
156+
continue
157+
}
158+
ret += line + "\n"
159+
}
160+
}
161+
162+
return ret
163+
}

cmd/common/transaction.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/spf13/cobra"
1111
flag "github.com/spf13/pflag"
1212

13-
"github.com/oasisprotocol/oasis-core/go/common"
1413
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1514
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
1615
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
@@ -352,31 +351,9 @@ func SignParaTimeTransaction(
352351

353352
// PrintTransactionRaw prints the transaction which can be either signed or unsigned.
354353
func PrintTransactionRaw(npa *NPASelection, tx interface{}) {
355-
switch rtx := tx.(type) {
354+
switch tx.(type) {
356355
case consensusPretty.PrettyPrinter:
357-
// Signed or unsigned consensus or runtime transaction.
358-
var ns common.Namespace
359-
if npa.ParaTime != nil {
360-
ns = npa.ParaTime.Namespace()
361-
}
362-
sigCtx := signature.RichContext{
363-
RuntimeID: ns,
364-
ChainContext: npa.Network.ChainContext,
365-
Base: types.SignatureContextBase,
366-
}
367-
ctx := context.Background()
368-
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenSymbol, npa.Network.Denomination.Symbol)
369-
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenValueExponent, npa.Network.Denomination.Decimals)
370-
if npa.ParaTime != nil {
371-
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
372-
}
373-
ctx = context.WithValue(ctx, signature.ContextKeySigContext, &sigCtx)
374-
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, GenAccountNames())
375-
376-
// Set up chain context for signature verification during pretty-printing.
377-
coreSignature.UnsafeResetChainContext()
378-
coreSignature.SetChainContext(npa.Network.ChainContext)
379-
rtx.PrettyPrint(ctx, "", os.Stdout)
356+
fmt.Print(PrettyPrint(npa, "", tx))
380357
default:
381358
fmt.Printf("[unsupported transaction type: %T]\n", tx)
382359
}

cmd/network/show.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ var showCmd = &cobra.Command{
253253
}
254254
return
255255
case selParameters:
256-
showParameters(ctx, height, consensusConn)
256+
showParameters(ctx, npa, height, consensusConn)
257257
return
258258

259259
default:
@@ -390,31 +390,7 @@ func showNativeToken(ctx context.Context, height int64, npa *common.NPASelection
390390
}
391391
}
392392

393-
func PrettifyFromJSON(blob interface{}) string {
394-
pp, err := json.MarshalIndent(blob, "", " ")
395-
cobra.CheckErr(err)
396-
397-
out := string(pp)
398-
out = strings.ReplaceAll(out, "{", "")
399-
out = strings.ReplaceAll(out, "}", "")
400-
out = strings.ReplaceAll(out, "[", "")
401-
out = strings.ReplaceAll(out, "]", "")
402-
out = strings.ReplaceAll(out, ",", "")
403-
out = strings.ReplaceAll(out, "\"", "")
404-
405-
ret := ""
406-
for _, line := range strings.Split(out, "\n") {
407-
line = strings.TrimRight(line, " \n")
408-
if len(line) == 0 {
409-
continue
410-
}
411-
ret += line + "\n"
412-
}
413-
414-
return ret
415-
}
416-
417-
func showParameters(ctx context.Context, height int64, cons consensus.ClientBackend) {
393+
func showParameters(ctx context.Context, npa *common.NPASelection, height int64, cons consensus.ClientBackend) {
418394
checkErr := func(what string, err error) {
419395
if err != nil {
420396
cobra.CheckErr(fmt.Errorf("%s: %w", what, err))
@@ -456,7 +432,7 @@ func showParameters(ctx context.Context, height int64, cons consensus.ClientBack
456432
doc[name] = params
457433
} else {
458434
fmt.Printf("=== %s PARAMETERS ===\n", strings.ToUpper(name))
459-
out := PrettifyFromJSON(params)
435+
out := common.PrettyPrint(npa, " ", params)
460436
fmt.Printf("%s\n", out)
461437
}
462438
}

0 commit comments

Comments
 (0)