Skip to content

Commit

Permalink
add invariants for metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 committed May 3, 2024
1 parent f21efb6 commit 25f2722
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
15 changes: 0 additions & 15 deletions x/fbridge/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, gs *types.GenesisState) error {
k.setVote(ctx, vote.ProposalId, sdk.MustAccAddressFromBech32(vote.Voter), vote.Option)
}

expectedRoleMeta := types.RoleMetadata{}
for _, pair := range gs.Roles {
k.SetRole(ctx, pair.Role, sdk.MustAccAddressFromBech32(pair.Address))
switch pair.Role {
case types.RoleGuardian:
expectedRoleMeta.Guardian++
case types.RoleOperator:
expectedRoleMeta.Operator++
case types.RoleJudge:
expectedRoleMeta.Judge++
}
}

if expectedRoleMeta.Guardian != gs.RoleMetadata.Guardian ||
expectedRoleMeta.Operator != gs.RoleMetadata.Operator ||
expectedRoleMeta.Judge != gs.RoleMetadata.Judge {
panic("role metadata does not match the number of roles")
}
k.SetRoleMetadata(ctx, gs.RoleMetadata)

Expand Down
51 changes: 31 additions & 20 deletions x/fbridge/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {

var total uint32 = 0
roleMeta := k.GetRoleMetadata(ctx)
previousRole := k.GetRole(ctx, sdk.MustAccAddressFromBech32(proposal.Target))
switch proposal.Role {
case types.RoleGuardian:
total = roleMeta.Guardian
Expand All @@ -40,31 +39,43 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
}

if types.CheckTrustLevelThreshold(uint64(total), uint64(voteYes), guardianTrustLevel) {
if proposal.Role == types.RoleEmpty {
k.DeleteRole(ctx, sdk.MustAccAddressFromBech32(proposal.Target))
} else {
k.SetRole(ctx, proposal.Role, sdk.MustAccAddressFromBech32(proposal.Target))
if err := k.UpdateRole(ctx, proposal.Role, sdk.MustAccAddressFromBech32(proposal.Target)); err != nil {
panic(err)
}

switch proposal.Role {
case types.RoleGuardian:
roleMeta.Guardian++
case types.RoleOperator:
roleMeta.Operator++
case types.RoleJudge:
roleMeta.Judge++
}
switch previousRole {
k.DeleteRoleProposal(ctx, proposal.Id)
}
}
}

// RegisterInvariants registers all staking invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k keeper.Keeper) {
ir.RegisterRoute(types.ModuleName, "role-metadata", RoleMeatadataInvariant(k))
}

func RoleMeatadataInvariant(k keeper.Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
actualRoleMeta := k.GetRoleMetadata(ctx)
expectedRoleMeta := types.RoleMetadata{}
for _, pair := range k.GetRolePairs(ctx) {
k.SetRole(ctx, pair.Role, sdk.MustAccAddressFromBech32(pair.Address))
switch pair.Role {
case types.RoleGuardian:
roleMeta.Guardian--
expectedRoleMeta.Guardian++
case types.RoleOperator:
roleMeta.Operator--
expectedRoleMeta.Operator++
case types.RoleJudge:
roleMeta.Judge--
expectedRoleMeta.Judge++
}
k.SetRoleMetadata(ctx, roleMeta)

k.DeleteRoleProposal(ctx, proposal.Id)
}

broken := expectedRoleMeta.Guardian != actualRoleMeta.Guardian ||
expectedRoleMeta.Operator != actualRoleMeta.Operator ||
expectedRoleMeta.Judge != actualRoleMeta.Judge

return sdk.FormatInvariant(types.ModuleName, "registered members and role metadata", fmt.Sprintf(
"Saved Role Metadata: %+v"+
"Calculated Role Metadata: %+v",
actualRoleMeta, expectedRoleMeta)), broken
}
}
2 changes: 1 addition & 1 deletion x/fbridge/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val

// RegisterInvariants does nothing, there are no invariants to enforce
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
_ = ir
RegisterInvariants(ir, am.keeper)
}

// Deprecated: Route does nothing.
Expand Down

0 comments on commit 25f2722

Please sign in to comment.