Skip to content

Commit

Permalink
feat: allow protocol stakers and delegators to vote (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey authored Jun 20, 2022
1 parent 08a6bbc commit 3abc1c5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func NewSimApp(
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper))
govKeeper := govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
&stakingKeeper, nil, govRouter,
)

app.GovKeeper = *govKeeper.SetHooks(
Expand Down
6 changes: 5 additions & 1 deletion x/gov/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Keeper struct {
// The reference to the DelegationSet and ValidatorSet to get information about validators and delegators
sk types.StakingKeeper

// The reference to the ProtocolDelegationSet and ProtocolValidatorSet to get information about protocol validators and delegators
rk types.RegistryKeeper

// GovHooks
hooks types.GovHooks

Expand All @@ -45,7 +48,7 @@ type Keeper struct {
// CONTRACT: the parameter Subspace must have the param key table already initialized
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace types.ParamSubspace,
authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, sk types.StakingKeeper, rtr types.Router,
authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, sk types.StakingKeeper, rk types.RegistryKeeper, rtr types.Router,
) Keeper {

// ensure governance module account is set
Expand All @@ -64,6 +67,7 @@ func NewKeeper(
authKeeper: authKeeper,
bankKeeper: bankKeeper,
sk: sk,
rk: rk,
cdc: cdc,
router: rtr,
}
Expand Down
24 changes: 22 additions & 2 deletions x/gov/keeper/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal types.Proposal) (passes boo
return false
})

//
if keeper.rk != nil {
keeper.rk.IterateProtocolBonding(ctx, voter, func(poolId uint64, amount sdk.Int) (stop bool) {
for _, option := range vote.Options {
subPower := amount.ToDec().Mul(option.Weight)
results[option.Option] = results[option.Option].Add(subPower)
}

totalVotingPower = totalVotingPower.Add(amount.ToDec())

return false
})
}

keeper.deleteVote(ctx, vote.ProposalId, voter)
return false
})
Expand All @@ -89,14 +103,20 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal types.Proposal) (passes boo
tallyParams := keeper.GetTallyParams(ctx)
tallyResults = types.NewTallyResultFromMap(results)

totalProtocolBonded := sdk.ZeroInt()
if keeper.rk != nil {
totalProtocolBonded = keeper.rk.TotalProtocolBonding(ctx)
}
totalBonded := keeper.sk.TotalBondedTokens(ctx).Add(totalProtocolBonded)

// TODO: Upgrade the spec to cover all of these cases & remove pseudocode.
// If there is no staked coins, the proposal fails
if keeper.sk.TotalBondedTokens(ctx).IsZero() {
if totalBonded.IsZero() {
return false, false, tallyResults
}

// If there is not enough quorum of votes, the proposal fails
percentVoting := totalVotingPower.Quo(keeper.sk.TotalBondedTokens(ctx).ToDec())
percentVoting := totalVotingPower.Quo(totalBonded.ToDec())
if percentVoting.LT(tallyParams.Quorum) {
return false, true, tallyResults
}
Expand Down
6 changes: 6 additions & 0 deletions x/gov/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type StakingKeeper interface {
)
}

// RegistryKeeper expected registry keeper (Protocol Validator and Delegator sets) (noalias)
type RegistryKeeper interface {
IterateProtocolBonding(ctx sdk.Context, address sdk.AccAddress, fn func(poolId uint64, amount sdk.Int) (stop bool))
TotalProtocolBonding(ctx sdk.Context) sdk.Int
}

// AccountKeeper defines the expected account keeper (noalias)
type AccountKeeper interface {
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
Expand Down

0 comments on commit 3abc1c5

Please sign in to comment.