Merge PR #2392: remove redundant sprintf calls, improve a doc comment, write test
* remove redundant sprintf calls, improve a doc comment, write test * One more super minor fix
This commit is contained in:
parent
9dafa3252d
commit
1a5d122c93
|
@ -38,7 +38,11 @@ type Keeper struct {
|
||||||
codespace sdk.CodespaceType
|
codespace sdk.CodespaceType
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGovernanceMapper returns a mapper that uses go-codec to (binary) encode and decode gov types.
|
// NewKeeper returns a governance keeper. It handles:
|
||||||
|
// - submitting governance proposals
|
||||||
|
// - depositing funds into proposals, and activating upon sufficient funds being deposited
|
||||||
|
// - users voting on proposals, with weight proportional to stake in the system
|
||||||
|
// - and tallying the result of the vote.
|
||||||
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ps params.Setter, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper {
|
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ps params.Setter, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper {
|
||||||
return Keeper{
|
return Keeper{
|
||||||
storeKey: key,
|
storeKey: key,
|
||||||
|
@ -51,11 +55,6 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ps params.Setter, ck bank.Kee
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the go-codec codec.
|
|
||||||
func (keeper Keeper) WireCodec() *codec.Codec {
|
|
||||||
return keeper.cdc
|
|
||||||
}
|
|
||||||
|
|
||||||
// =====================================================
|
// =====================================================
|
||||||
// Proposals
|
// Proposals
|
||||||
|
|
||||||
|
|
|
@ -192,8 +192,9 @@ func (pt ProposalKind) String() string {
|
||||||
func (pt ProposalKind) Format(s fmt.State, verb rune) {
|
func (pt ProposalKind) Format(s fmt.State, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 's':
|
case 's':
|
||||||
s.Write([]byte(fmt.Sprintf("%s", pt.String())))
|
s.Write([]byte(pt.String()))
|
||||||
default:
|
default:
|
||||||
|
// TODO: Do this conversion more directly
|
||||||
s.Write([]byte(fmt.Sprintf("%v", byte(pt))))
|
s.Write([]byte(fmt.Sprintf("%v", byte(pt))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -295,8 +296,9 @@ func (status ProposalStatus) String() string {
|
||||||
func (status ProposalStatus) Format(s fmt.State, verb rune) {
|
func (status ProposalStatus) Format(s fmt.State, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 's':
|
case 's':
|
||||||
s.Write([]byte(fmt.Sprintf("%s", status.String())))
|
s.Write([]byte(status.String()))
|
||||||
default:
|
default:
|
||||||
|
// TODO: Do this conversion more directly
|
||||||
s.Write([]byte(fmt.Sprintf("%v", byte(status))))
|
s.Write([]byte(fmt.Sprintf("%v", byte(status))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -322,11 +324,8 @@ func EmptyTallyResult() TallyResult {
|
||||||
|
|
||||||
// checks if two proposals are equal
|
// checks if two proposals are equal
|
||||||
func (resultA TallyResult) Equals(resultB TallyResult) bool {
|
func (resultA TallyResult) Equals(resultB TallyResult) bool {
|
||||||
if resultA.Yes.Equal(resultB.Yes) &&
|
return (resultA.Yes.Equal(resultB.Yes) &&
|
||||||
resultA.Abstain.Equal(resultB.Abstain) &&
|
resultA.Abstain.Equal(resultB.Abstain) &&
|
||||||
resultA.No.Equal(resultB.No) &&
|
resultA.No.Equal(resultB.No) &&
|
||||||
resultA.NoWithVeto.Equal(resultB.NoWithVeto) {
|
resultA.NoWithVeto.Equal(resultB.NoWithVeto))
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package gov
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProposalKind_Format(t *testing.T) {
|
||||||
|
typeText, _ := ProposalTypeFromString("Text")
|
||||||
|
tests := []struct {
|
||||||
|
pt ProposalKind
|
||||||
|
sprintFArgs string
|
||||||
|
expectedStringOutput string
|
||||||
|
}{
|
||||||
|
{typeText, "%s", "Text"},
|
||||||
|
{typeText, "%v", "1"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
got := fmt.Sprintf(tt.sprintFArgs, tt.pt)
|
||||||
|
require.Equal(t, tt.expectedStringOutput, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProposalStatus_Format(t *testing.T) {
|
||||||
|
statusDepositPeriod, _ := ProposalStatusFromString("DepositPeriod")
|
||||||
|
tests := []struct {
|
||||||
|
pt ProposalStatus
|
||||||
|
sprintFArgs string
|
||||||
|
expectedStringOutput string
|
||||||
|
}{
|
||||||
|
{statusDepositPeriod, "%s", "DepositPeriod"},
|
||||||
|
{statusDepositPeriod, "%v", "1"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
got := fmt.Sprintf(tt.sprintFArgs, tt.pt)
|
||||||
|
require.Equal(t, tt.expectedStringOutput, got)
|
||||||
|
}
|
||||||
|
}
|
|
@ -205,12 +205,12 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
|
||||||
var proposalID int64
|
var proposalID int64
|
||||||
err2 := keeper.cdc.UnmarshalJSON(req.Data, proposalID)
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, proposalID)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
|
return res, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
proposal := keeper.GetProposal(ctx, proposalID)
|
proposal := keeper.GetProposal(ctx, proposalID)
|
||||||
if proposal == nil {
|
if proposal == nil {
|
||||||
return []byte{}, ErrUnknownProposal(DefaultCodespace, proposalID)
|
return res, ErrUnknownProposal(DefaultCodespace, proposalID)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tallyResult TallyResult
|
var tallyResult TallyResult
|
||||||
|
|
Loading…
Reference in New Issue