Skip to content

Commit

Permalink
go/staking/api: Update commission schedule rate (bound) pretty prints
Browse files Browse the repository at this point in the history
Pretty print rates and rate bounds as enumerated lists to enable easier
inspection of commission schedule (amendments) in combination with a
hardware-based signer plugin.
  • Loading branch information
tjanez committed Sep 11, 2020
1 parent 41375fd commit 6dac03c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changelog/3265.feature.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go/staking/api: Update commission schedule rate and rate bound pretty prints

Pretty print rates and rate bounds as enumerated lists to enable easier
inspection of commission schedule (amendments) in combination with a
hardware-based signer plugin.
18 changes: 11 additions & 7 deletions go/staking/api/commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ type CommissionRateStep struct {
// PrettyPrint writes a pretty-printed representation of CommissionRateStep to
// the given writer.
func (crs CommissionRateStep) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%s- Start: epoch %d\n", prefix, crs.Start)
indexInfix, emptyInfix := PrettyPrintCommissionScheduleIndexInfixes(ctx)

fmt.Fprintf(w, "%s Rate: %s\n", prefix, PrettyPrintCommissionRatePercentage(crs.Rate))
fmt.Fprintf(w, "%s%sstart: epoch %d\n", prefix, indexInfix, crs.Start)
fmt.Fprintf(w, "%s%srate: %s\n", prefix, emptyInfix, PrettyPrintCommissionRatePercentage(crs.Rate))
}

// PrettyType returns a representation of CommissionRateStep that can be used
Expand All @@ -77,10 +78,11 @@ type CommissionRateBoundStep struct {
// PrettyPrint writes a pretty-printed representation of CommissionRateBoundStep
// to the given writer.
func (crbs CommissionRateBoundStep) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%s- Start: epoch %d\n", prefix, crbs.Start)
indexInfix, emptyInfix := PrettyPrintCommissionScheduleIndexInfixes(ctx)

fmt.Fprintf(w, "%s Minimum Rate: %s\n", prefix, PrettyPrintCommissionRatePercentage(crbs.RateMin))
fmt.Fprintf(w, "%s Maximum Rate: %s\n", prefix, PrettyPrintCommissionRatePercentage(crbs.RateMax))
fmt.Fprintf(w, "%s%sstart: epoch %d\n", prefix, indexInfix, crbs.Start)
fmt.Fprintf(w, "%s%sminimum rate: %s\n", prefix, emptyInfix, PrettyPrintCommissionRatePercentage(crbs.RateMin))
fmt.Fprintf(w, "%s%smaximum rate: %s\n", prefix, emptyInfix, PrettyPrintCommissionRatePercentage(crbs.RateMax))
}

// PrettyType returns a representation of CommissionRateBoundStep that can be
Expand All @@ -105,7 +107,8 @@ func (cs CommissionSchedule) PrettyPrint(ctx context.Context, prefix string, w i
fmt.Fprintf(w, "%sRates: (none)\n", prefix)
} else {
fmt.Fprintf(w, "%sRates:\n", prefix)
for _, rate := range cs.Rates {
for i, rate := range cs.Rates {
ctx = context.WithValue(ctx, prettyprint.ContextKeyCommissionScheduleIndex, i)
rate.PrettyPrint(ctx, prefix+" ", w)
}
}
Expand All @@ -114,7 +117,8 @@ func (cs CommissionSchedule) PrettyPrint(ctx context.Context, prefix string, w i
fmt.Fprintf(w, "%sRate Bounds: (none)\n", prefix)
} else {
fmt.Fprintf(w, "%sRate Bounds:\n", prefix)
for _, rateBound := range cs.Bounds {
for i, rateBound := range cs.Bounds {
ctx = context.WithValue(ctx, prettyprint.ContextKeyCommissionScheduleIndex, i)
rateBound.PrettyPrint(ctx, prefix+" ", w)
}
}
Expand Down
88 changes: 88 additions & 0 deletions go/staking/api/commission_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package api

import (
"bytes"
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
epochtime "github.com/oasisprotocol/oasis-core/go/epochtime/api"
)
Expand Down Expand Up @@ -770,3 +773,88 @@ func TestCommissionSchedule(t *testing.T) {
require.Equal(t, epochtime.EpochTime(10), cs.Rates[0].Start, "prune 10 rates start")
require.Equal(t, epochtime.EpochTime(10), cs.Bounds[0].Start, "prune 10 bounds start")
}

func TestPrettyPrintCommissionRateStep(t *testing.T) {
require := require.New(t)

for _, t := range []struct {
expectedPPrint string
rateStart epochtime.EpochTime
rateNumerator *quantity.Quantity
index int
}{
{
"" +
"(1) start: epoch 10\n" +
" rate: 0.0%\n",
epochtime.EpochTime(10), quantity.NewFromUint64(0), 0,
},
{
"" +
"(11) start: epoch 20\n" +
" rate: 50.0%\n",
epochtime.EpochTime(20), quantity.NewFromUint64(50_000), 10,
},
{
"" +
"(101) start: epoch 100\n" +
" rate: 100.0%\n",
epochtime.EpochTime(100), quantity.NewFromUint64(100_000), 100,
},
} {
rateStep := CommissionRateStep{
Start: t.rateStart,
Rate: *t.rateNumerator,
}
var b bytes.Buffer
ctx := context.WithValue(context.Background(), prettyprint.ContextKeyCommissionScheduleIndex, t.index)
rateStep.PrettyPrint(ctx, "", &b)
pPrint := b.String()
require.Equal(t.expectedPPrint, pPrint, "obtained pretty print didn't match expected value")
}
}

func TestPrettyPrintCommissionRateBoundStep(t *testing.T) {
require := require.New(t)

for _, t := range []struct {
expectedPPrint string
rateStart epochtime.EpochTime
rateMinNumerator *quantity.Quantity
rateMaxNumerator *quantity.Quantity
index int
}{
{
"" +
"(1) start: epoch 10\n" +
" minimum rate: 0.0%\n" +
" maximum rate: 20.0%\n",
epochtime.EpochTime(10), quantity.NewFromUint64(0), quantity.NewFromUint64(20_000), 0,
},
{
"" +
"(11) start: epoch 20\n" +
" minimum rate: 40.0%\n" +
" maximum rate: 60.0%\n",
epochtime.EpochTime(20), quantity.NewFromUint64(40_000), quantity.NewFromUint64(60_000), 10,
},
{
"" +
"(101) start: epoch 100\n" +
" minimum rate: 0.0%\n" +
" maximum rate: 100.0%\n",
epochtime.EpochTime(100), quantity.NewFromUint64(0), quantity.NewFromUint64(100_000), 100,
},
} {
rateStep := CommissionRateBoundStep{
Start: t.rateStart,
RateMin: *t.rateMinNumerator,
RateMax: *t.rateMaxNumerator,
}
var b bytes.Buffer
ctx := context.WithValue(context.Background(), prettyprint.ContextKeyCommissionScheduleIndex, t.index)
rateStep.PrettyPrint(ctx, "", &b)
pPrint := b.String()
require.Equal(t.expectedPPrint, pPrint, "obtained pretty print didn't match expected value")
}
}

0 comments on commit 6dac03c

Please sign in to comment.