Skip to content

Commit

Permalink
Merge pull request #5472 from oasisprotocol/ptrus/fix/parametrs-chang…
Browse files Browse the repository at this point in the history
…e-pp

governance: Fix PrettyPrint for ChangeParametersProposal
  • Loading branch information
ptrus authored Nov 25, 2023
2 parents 6a44aee + 6becf92 commit 4ea9010
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions .changelog/5472.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
governance: Fix pretty priting of `ChangeParametersProposal`
29 changes: 19 additions & 10 deletions go/governance/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package api
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"reflect"
Expand All @@ -23,10 +24,6 @@ import (
// ModuleName is a unique module name for the governance backend.
const ModuleName = "governance"

// ProposalContentInvalidText is the textual representation of an invalid
// ProposalContent.
const ProposalContentInvalidText = "(invalid)"

var (
// ErrInvalidArgument is the error returned on malformed argument(s).
ErrInvalidArgument = errors.New(ModuleName, 1, "governance: invalid argument")
Expand Down Expand Up @@ -124,15 +121,17 @@ func (p *ProposalContent) Equals(other *ProposalContent) bool {
// PrettyPrint writes a pretty-printed representation of ProposalContent to the
// given writer.
func (p ProposalContent) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
switch {
case p.Upgrade != nil && p.CancelUpgrade == nil:
if p.Upgrade != nil {
fmt.Fprintf(w, "%sUpgrade:\n", prefix)
p.Upgrade.PrettyPrint(ctx, prefix+" ", w)
case p.CancelUpgrade != nil && p.Upgrade == nil:
}
if p.CancelUpgrade != nil {
fmt.Fprintf(w, "%sCancel Upgrade:\n", prefix)
p.CancelUpgrade.PrettyPrint(ctx, prefix+" ", w)
default:
fmt.Fprintf(w, "%s%s\n", prefix, ProposalContentInvalidText)
}
if p.ChangeParameters != nil {
fmt.Fprintf(w, "%sChange Parameters:\n", prefix)
p.ChangeParameters.PrettyPrint(ctx, prefix+" ", w)
}
}

Expand Down Expand Up @@ -233,8 +232,18 @@ func (p *ChangeParametersProposal) Equals(other *ChangeParametersProposal) bool
// PrettyPrint writes a pretty-printed representation of ChangeParametersProposal to the given
// writer.
func (p *ChangeParametersProposal) PrettyPrint(_ context.Context, prefix string, w io.Writer) {
var changes map[string]interface{}
if err := cbor.Unmarshal(p.Changes, &changes); err != nil {
fmt.Fprintf(w, "%s <error: %s>\n", prefix, err)
fmt.Fprintf(w, "%s <malformed: %s>\n", prefix, base64.StdEncoding.EncodeToString(p.Changes))
return
}
fmt.Fprintf(w, "%sModule: %s\n", prefix, p.Module)
fmt.Fprintf(w, "%sChanges: %v\n", prefix, p.Changes)
fmt.Fprintf(w, "%sChanges: \n", prefix)
for param, value := range changes {
fmt.Fprintf(w, "%s - Parameter: %s\n", prefix, param)
fmt.Fprintf(w, "%s Value: %v\n", prefix, value)
}
}

// PrettyType returns a representation of ChangeParametersProposal that can be used for pretty
Expand Down
12 changes: 7 additions & 5 deletions go/governance/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,18 @@ func TestProposalContentPrettyPrint(t *testing.T) {
},
},
{
expRegex: ProposalContentInvalidText,
expRegex: "",
p: &ProposalContent{},
},
{
expRegex: ProposalContentInvalidText,
expRegex: "^Change Parameters:",
p: &ProposalContent{
Upgrade: &UpgradeProposal{
Descriptor: upgrade.Descriptor{Handler: "test"},
ChangeParameters: &ChangeParametersProposal{
Module: "test-module",
Changes: cbor.Marshal(map[string]string{
"test-parameter": "test-value",
}),
},
CancelUpgrade: &CancelUpgradeProposal{ProposalID: 42},
},
},
} {
Expand Down

0 comments on commit 4ea9010

Please sign in to comment.