Skip to content

Commit

Permalink
added root tallyroute
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 committed Feb 3, 2021
1 parent 4833ed5 commit 940d2cf
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 35 deletions.
5 changes: 3 additions & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ func NewSimApp(
&stakingKeeper, govRouter, tallyRouter,
)

// Be sure to Seal() tallyrouter after registering all routes
tallyRouter.AddRoute(stakingtally.TallyRoute, stakingtally.NewStakingTallyHandler(app.GovKeeper, app.StakingKeeper)).Seal()
// TODO: Make requirement to set a Root Tally Route, panic or something if not set
tallyRouter.AddRoute(govtypes.RootTallyRoute, stakingtally.NewStakingTallyHandler(app.GovKeeper, app.StakingKeeper))
tallyRouter.AddRoute(stakingtally.TallyRoute, stakingtally.NewStakingTallyHandler(app.GovKeeper, app.StakingKeeper))

// Create Transfer Keepers
app.TransferKeeper = ibctransferkeeper.NewKeeper(
Expand Down
3 changes: 1 addition & 2 deletions x/distribution/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand Down Expand Up @@ -42,7 +41,7 @@ func (csp *CommunityPoolSpendProposal) ProposalType() string { return ProposalTy

// TallyRoute returns the tally route of the strategy that should weight votes
// on a community pool spend proposal.
func (csp *CommunityPoolSpendProposal) TallyRoute() string { return stakingtally.TallyRoute }
func (csp *CommunityPoolSpendProposal) TallyRoute() string { return govtypes.RootTallyRoute }

// ValidateBasic runs basic stateless validity checks
func (csp *CommunityPoolSpendProposal) ValidateBasic() error {
Expand Down
9 changes: 4 additions & 5 deletions x/gov/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/staking"
)
Expand All @@ -32,7 +31,7 @@ func TestTickExpiredDepositPeriod(t *testing.T) {
inactiveQueue.Close()

newProposalMsg, err := types.NewMsgSubmitProposal(
types.ContentFromProposalType("test", "test", types.ProposalTypeText, stakingtally.TallyRoute),
types.ContentFromProposalType("test", "test", types.ProposalTypeText, types.RootTallyRoute),
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 5)},
addrs[0],
)
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
inactiveQueue.Close()

newProposalMsg, err := types.NewMsgSubmitProposal(
types.ContentFromProposalType("test", "test", types.ProposalTypeText, stakingtally.TallyRoute),
types.ContentFromProposalType("test", "test", types.ProposalTypeText, types.RootTallyRoute),
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 5)},
addrs[0],
)
Expand All @@ -107,7 +106,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
inactiveQueue.Close()

newProposalMsg2, err := types.NewMsgSubmitProposal(
types.ContentFromProposalType("test2", "test2", types.ProposalTypeText, stakingtally.TallyRoute),
types.ContentFromProposalType("test2", "test2", types.ProposalTypeText, types.RootTallyRoute),
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 5)},
addrs[0],
)
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestTickPassedDepositPeriod(t *testing.T) {
activeQueue.Close()

newProposalMsg, err := types.NewMsgSubmitProposal(
types.ContentFromProposalType("test2", "test2", types.ProposalTypeText, stakingtally.TallyRoute),
types.ContentFromProposalType("test2", "test2", types.ProposalTypeText, types.RootTallyRoute),
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 5)},
addrs[0],
)
Expand Down
5 changes: 2 additions & 3 deletions x/gov/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtestutil "github.com/cosmos/cosmos-sdk/x/gov/client/testutil"
"github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand All @@ -46,7 +45,7 @@ func (s *IntegrationTestSuite) SetupSuite() {

// create a proposal with deposit
_, err = govtestutil.MsgSubmitProposal(val.ClientCtx, val.Address.String(),
"Text Proposal 1", "Where is the title!?", types.ProposalTypeText, stakingtally.TallyRoute,
"Text Proposal 1", "Where is the title!?", types.ProposalTypeText, types.RootTallyRoute,
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens).String()))
s.Require().NoError(err)
_, err = s.network.WaitForHeight(1)
Expand All @@ -58,7 +57,7 @@ func (s *IntegrationTestSuite) SetupSuite() {

// create a proposal without deposit
_, err = govtestutil.MsgSubmitProposal(val.ClientCtx, val.Address.String(),
"Text Proposal 2", "Where is the title!?", types.ProposalTypeText, stakingtally.TallyRoute)
"Text Proposal 2", "Where is the title!?", types.ProposalTypeText, types.RootTallyRoute)
s.Require().NoError(err)
_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
Expand Down
5 changes: 2 additions & 3 deletions x/gov/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtestutil "github.com/cosmos/cosmos-sdk/x/gov/client/testutil"
"github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand All @@ -42,7 +41,7 @@ func (s *IntegrationTestSuite) SetupSuite() {

// create a proposal with deposit
_, err = govtestutil.MsgSubmitProposal(val.ClientCtx, val.Address.String(),
"Text Proposal 1", "Where is the title!?", types.ProposalTypeText, stakingtally.TallyRoute,
"Text Proposal 1", "Where is the title!?", types.ProposalTypeText, types.RootTallyRoute,
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens).String()))
s.Require().NoError(err)
_, err = s.network.WaitForHeight(1)
Expand All @@ -54,7 +53,7 @@ func (s *IntegrationTestSuite) SetupSuite() {

// create a proposal without deposit
_, err = govtestutil.MsgSubmitProposal(val.ClientCtx, val.Address.String(),
"Text Proposal 2", "Where is the title!?", types.ProposalTypeText, stakingtally.TallyRoute)
"Text Proposal 2", "Where is the title!?", types.ProposalTypeText, types.RootTallyRoute)
s.Require().NoError(err)
_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
Expand Down
3 changes: 3 additions & 0 deletions x/gov/keeper/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ import (

func (k Keeper) Tally(ctx sdk.Context, proposal types.Proposal) (passes bool, burnDeposits bool, tallyResults types.TallyResult) {
tally := k.TallyRouter().GetRoute(proposal.TallyRoute())
if tally == nil {
tally = k.TallyRouter().GetRoute(types.RootTallyRoute)
}
return tally(ctx, proposal)
}
3 changes: 1 addition & 2 deletions x/gov/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand All @@ -27,7 +26,7 @@ func TestDecodeStore(t *testing.T) {
dec := simulation.NewDecodeStore(cdc)

endTime := time.Now().UTC()
content := types.ContentFromProposalType("test", "test", types.ProposalTypeText, stakingtally.TallyRoute)
content := types.ContentFromProposalType("test", "test", types.ProposalTypeText, types.RootTallyRoute)
proposal, err := types.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour))
require.NoError(t, err)

Expand Down
5 changes: 5 additions & 0 deletions x/gov/types/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Governance message types and routes
const (
RootTallyRoute = "root"
)

// TallyStrategy defines a function that takes takes a proposal at the end of the Voting period and returns the result
type TallyStrategy func(ctx sdk.Context, proposal Proposal) (passes bool, burnDeposits bool, tallyResults TallyResult)

Expand Down
12 changes: 1 addition & 11 deletions x/gov/types/tally_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type TallyRouter interface {
AddRoute(r string, h TallyStrategy) (rtr TallyRouter)
HasRoute(r string) bool
GetRoute(path string) (h TallyStrategy)
Seal()
}

type tallyrouter struct {
Expand All @@ -30,15 +29,6 @@ func NewTallyRouter() TallyRouter {
}
}

// Seal seals the router which prohibits any subsequent route handlers to be
// added. Seal will panic if called more than once.
func (rtr *tallyrouter) Seal() {
if rtr.sealed {
panic("router already sealed")
}
rtr.sealed = true
}

// AddRoute adds a governance handler for a given path. It returns the Router
// so AddRoute calls can be linked. It will panic if the router is sealed.
func (rtr *tallyrouter) AddRoute(path string, t TallyStrategy) TallyRouter {
Expand All @@ -65,7 +55,7 @@ func (rtr *tallyrouter) HasRoute(path string) bool {
// GetRoute returns a Handler for a given path.
func (rtr *tallyrouter) GetRoute(path string) TallyStrategy {
if !rtr.HasRoute(path) {
panic(fmt.Sprintf("route \"%s\" does not exist", path))
return nil
}

return rtr.routes[path]
Expand Down
3 changes: 1 addition & 2 deletions x/ibc/core/02-client/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
"github.com/cosmos/cosmos-sdk/x/ibc/core/exported"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (cup *ClientUpdateProposal) ProposalRoute() string { return RouterKey }
func (cup *ClientUpdateProposal) ProposalType() string { return ProposalTypeClientUpdate }

// TallyRoute returns the tally route of a client update proposal.
func (cup *ClientUpdateProposal) TallyRoute() string { return stakingtally.TallyRoute }
func (cup *ClientUpdateProposal) TallyRoute() string { return govtypes.RootTallyRoute }

// ValidateBasic runs basic stateless validity checks
func (cup *ClientUpdateProposal) ValidateBasic() error {
Expand Down
3 changes: 1 addition & 2 deletions x/params/types/proposal/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

yaml "gopkg.in/yaml.v2"

stakingtally "github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func (pcp *ParameterChangeProposal) ProposalRoute() string { return RouterKey }
func (pcp *ParameterChangeProposal) ProposalType() string { return ProposalTypeChange }

// TallyRoute returns the tally route of a parameter change proposal.
func (pcp *ParameterChangeProposal) TallyRoute() string { return stakingtally.TallyRoute }
func (pcp *ParameterChangeProposal) TallyRoute() string { return govtypes.RootTallyRoute }

// ValidateBasic validates the parameter change proposal
func (pcp *ParameterChangeProposal) ValidateBasic() error {
Expand Down
5 changes: 2 additions & 3 deletions x/upgrade/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
stakingtally "github.com/cosmos/cosmos-sdk/x/gov/stakingtally"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand Down Expand Up @@ -32,7 +31,7 @@ func (sup *SoftwareUpgradeProposal) GetTitle() string { return sup.Title }
func (sup *SoftwareUpgradeProposal) GetDescription() string { return sup.Description }
func (sup *SoftwareUpgradeProposal) ProposalRoute() string { return RouterKey }
func (sup *SoftwareUpgradeProposal) ProposalType() string { return ProposalTypeSoftwareUpgrade }
func (sup *SoftwareUpgradeProposal) TallyRoute() string { return stakingtally.TallyRoute }
func (sup *SoftwareUpgradeProposal) TallyRoute() string { return gov.RootTallyRoute }
func (sup *SoftwareUpgradeProposal) ValidateBasic() error {
if err := sup.Plan.ValidateBasic(); err != nil {
return err
Expand Down Expand Up @@ -65,7 +64,7 @@ func (csup *CancelSoftwareUpgradeProposal) ProposalRoute() string { return Rout
func (csup *CancelSoftwareUpgradeProposal) ProposalType() string {
return ProposalTypeCancelSoftwareUpgrade
}
func (csup *CancelSoftwareUpgradeProposal) TallyRoute() string { return stakingtally.TallyRoute }
func (csup *CancelSoftwareUpgradeProposal) TallyRoute() string { return gov.RootTallyRoute }
func (csup *CancelSoftwareUpgradeProposal) ValidateBasic() error {
return gov.ValidateAbstract(csup)
}
Expand Down

0 comments on commit 940d2cf

Please sign in to comment.