Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/gov): add governors #73

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
b301574
tally prototype with governors
giunatale Sep 13, 2024
8cb99c2
add protos
giunatale Sep 13, 2024
41b042f
governor object started (copy from Validator)
giunatale Sep 13, 2024
d4a8c03
wip
giunatale Sep 13, 2024
96abd4c
wip
giunatale Sep 14, 2024
f9ad9d8
...
giunatale Sep 14, 2024
700703d
...
giunatale Sep 14, 2024
f6c3392
missing governor election
giunatale Sep 14, 2024
6951d84
not right, cannot iterate on all governors
giunatale Sep 14, 2024
1a8495c
remove percentage and try sorting governors by VP
giunatale Sep 14, 2024
a769118
add msgs
giunatale Sep 14, 2024
ca19aaf
update test mock keepers
giunatale Sep 14, 2024
be1db00
...
giunatale Sep 14, 2024
e1a2bbb
compiles
giunatale Sep 15, 2024
851c005
add query and cli
giunatale Sep 15, 2024
372bca2
allow governor status updates only once a month
giunatale Sep 15, 2024
4ea0dad
genesis
giunatale Sep 15, 2024
fedba97
fix e2e
giunatale Sep 15, 2024
68645f2
add governors vp invariant
giunatale Sep 15, 2024
201b112
iterate over correct values
giunatale Sep 16, 2024
e5c1284
format pass
giunatale Sep 18, 2024
6f37870
fix missing return
giunatale Sep 18, 2024
c0cdec9
cleanup protos
giunatale Sep 19, 2024
80f44c7
Merge branch 'main' into governors
giunatale Sep 24, 2024
afa8d07
fix merge mistake
giunatale Sep 24, 2024
58d6a83
fix merge mistakes
giunatale Sep 24, 2024
44d71e6
approx VP computation for quorum with governors
giunatale Sep 24, 2024
5767cca
fix error in keeper.GetGovernor
giunatale Sep 24, 2024
25d017a
protogen
giunatale Sep 24, 2024
671755c
add min bonding requirements for governors
giunatale Sep 24, 2024
86031e6
governance delegations invariant
giunatale Sep 24, 2024
3a9f71f
fmt pass
giunatale Sep 24, 2024
b8eac36
use math
giunatale Sep 25, 2024
9bcbd0f
remove iteration over map
giunatale Sep 25, 2024
8daaffb
more efficient
giunatale Sep 25, 2024
26ff811
fix
giunatale Sep 25, 2024
ec48fe5
remove ambiguity
giunatale Sep 25, 2024
180e1c1
...
giunatale Sep 25, 2024
e147827
various fixes
giunatale Sep 25, 2024
e0d3865
Merge remote-tracking branch 'origin/main' into governors
giunatale Sep 25, 2024
48f45be
fix error codes
giunatale Sep 25, 2024
0659b9d
add alternative for tallying governors VP
giunatale Sep 25, 2024
8418fed
remove aggregate VP for governors
giunatale Sep 25, 2024
7bd0a30
don't use nil
giunatale Sep 25, 2024
abe5fc2
remove maxGovernors param
giunatale Sep 25, 2024
8b79c43
tally results updated only once only per voter
giunatale Sep 25, 2024
1f8e5a9
...
giunatale Sep 25, 2024
9560c40
Merge branch 'governors' into governors-no-aggregate-vp
giunatale Sep 25, 2024
374c315
Merge remote-tracking branch 'origin/main' into governors
giunatale Sep 27, 2024
de5f46a
Merge branch 'governors' into governors-no-aggregate-vp
giunatale Sep 27, 2024
90f540c
Merge branch 'main' into governors
giunatale Oct 2, 2024
c5e5ce4
fmt
giunatale Oct 2, 2024
a0b0650
Merge branch 'governors' into governors-no-aggregate-vp
giunatale Oct 2, 2024
7a792a8
fix/refac: tally with governors (#2)
tbruyelle Oct 11, 2024
8f1448d
test: keeper governor funcs (#3)
tbruyelle Oct 15, 2024
3d4fc3f
Merge branch 'main' into governors-no-aggregate-vp
giunatale Jan 14, 2025
2713498
fix merge conflicts
giunatale Jan 14, 2025
8dee330
test(x/gov): add e2e tests for governors (#4)
tbruyelle Jan 16, 2025
f25a8a4
fix(x/gov): REST query governor URL
tbruyelle Jan 16, 2025
539ee0e
docs(x/gov): add governors ADR (#5)
giunatale Jan 20, 2025
1b03975
docs(x/gov): add governors documentation (#6)
giunatale Jan 23, 2025
e48e30b
fix: gov init genesis order (#7)
tbruyelle Jan 23, 2025
e3a22b3
perf(x/gov): bunch of tally improvements (#8)
tbruyelle Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add governors vp invariant
  • Loading branch information
giunatale committed Sep 15, 2024
commit 68645f27aef73e7d1c2c63c21b2b6354fd3054d4
22 changes: 19 additions & 3 deletions x/gov/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ func (k Keeper) GetGovernorValShares(ctx sdk.Context, governorAddr types.Governo
return share, true
}

// IterateGovernorValShares iterates over all governor validator shares
func (k Keeper) IterateGovernorValShares(ctx sdk.Context, governorAddr types.GovernorAddress, cb func(index int64, share v1.GovernorValShares) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ValidatorSharesByGovernorKey(governorAddr, []byte{}))
defer iterator.Close()

for i := int64(0); iterator.Valid(); iterator.Next() {
var share v1.GovernorValShares
k.cdc.MustUnmarshal(iterator.Value(), &share)
if cb(i, share) {
break
}
i++
}
}

// GetGovernorValSharesByValidator gets all governor validator shares for a specific validator
func (k Keeper) GetGovernorValSharesByValidator(ctx sdk.Context, validatorAddr sdk.ValAddress) []v1.GovernorValShares {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -185,9 +201,9 @@ func (k Keeper) DecreaseGovernorShares(ctx sdk.Context, governorAddr types.Gover
k.UpdateGovernorByPowerIndex(ctx, governor)
}

// UndelegateGovernor decreases all governor validator shares in the store
// UndelegateFromGovernor decreases all governor validator shares in the store
// and then removes the governor delegation for the given delegator
func (k Keeper) undelegateGovernor(ctx sdk.Context, delegatorAddr sdk.AccAddress) {
func (k Keeper) UndelegateFromGovernor(ctx sdk.Context, delegatorAddr sdk.AccAddress) {
delegation, found := k.GetGovernanceDelegation(ctx, delegatorAddr)
if !found {
return
Expand Down Expand Up @@ -217,7 +233,7 @@ func (k Keeper) DelegateToGovernor(ctx sdk.Context, delegatorAddr sdk.AccAddress
// RedelegateGovernor re-delegates all governor validator shares from one governor to another
func (k Keeper) RedelegateToGovernor(ctx sdk.Context, delegatorAddr sdk.AccAddress, dstGovernorAddr types.GovernorAddress) {
// undelegate from the source governor
k.undelegateGovernor(ctx, delegatorAddr)
k.UndelegateFromGovernor(ctx, delegatorAddr)
// delegate to the destination governor
k.DelegateToGovernor(ctx, delegatorAddr, dstGovernorAddr)
}
55 changes: 55 additions & 0 deletions x/gov/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// RegisterInvariants registers all governance invariants
func RegisterInvariants(ir sdk.InvariantRegistry, keeper *Keeper, bk types.BankKeeper) {
ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(keeper, bk))
ir.RegisterRoute(types.ModuleName, "governors-voting-power", GovernorsVotingPowerInvariant(keeper, keeper.sk))
}

// AllInvariants runs all invariants of the governance module
Expand Down Expand Up @@ -47,3 +48,57 @@ func ModuleAccountInvariant(keeper *Keeper, bk types.BankKeeper) sdk.Invariant {
balances, expectedDeposits)), broken
}
}

// GovernorsVotingPowerInvariant checks that the voting power of all governors
// is actually equal to the voting power resulting from the delegated validator shares
func GovernorsVotingPowerInvariant(keeper *Keeper, sk types.StakingKeeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
expectedVotingPower sdk.Dec
actualVotingPower sdk.Dec
broken bool
brokenGovernorAddr string
fail bool
invariantStr string
)

keeper.IterateGovernors(ctx, func(index int64, governor v1.GovernorI) bool {
expectedVotingPower = governor.GetVotingPower()
actualVotingPower = sdk.ZeroDec()
fail = false
keeper.IterateGovernorValShares(ctx, governor.GetAddress(), func(index int64, shares v1.GovernorValShares) bool {
validatorAddr, err := sdk.ValAddressFromBech32(shares.ValidatorAddress)
if err != nil {
invariantStr = sdk.FormatInvariant(types.ModuleName, "governor %s voting power",
fmt.Sprintf("failed to parse validator address %s: %v", shares.ValidatorAddress, err))
fail = true
return true
}
validator, found := sk.GetValidator(ctx, validatorAddr)
if !found {
invariantStr = sdk.FormatInvariant(types.ModuleName, "governor %s voting power",
fmt.Sprintf("validator %s not found", validatorAddr.String()))
fail = true
return true
}
vp := shares.Shares.MulInt(validator.GetBondedTokens()).Quo(validator.GetDelegatorShares())
actualVotingPower = actualVotingPower.Add(vp)
return false
})
broken = !expectedVotingPower.Equal(actualVotingPower)
if fail {
broken = true
}
if broken {
brokenGovernorAddr = governor.GetAddress().String()
}
return broken // break on first broken invariant
})
if !fail {
invariantStr = sdk.FormatInvariant(types.ModuleName, "governor %s voting power",
fmt.Sprintf("\texpected %s voting power: %s\n\tactual voting power: %s\n",
brokenGovernorAddr, expectedVotingPower, actualVotingPower))
}
return invariantStr, broken
}
}
2 changes: 1 addition & 1 deletion x/gov/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (k msgServer) UndelegateGovernor(goCtx context.Context, msg *v1.MsgUndelega
}

// Remove the delegation
k.undelegateGovernor(ctx, delAddr)
k.UndelegateFromGovernor(ctx, delAddr)

return &v1.MsgUndelegateGovernorResponse{}, nil
}
Expand Down