Skip to content

Commit

Permalink
validate role metadata during initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 committed May 2, 2024
1 parent 38d0969 commit f21efb6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
38 changes: 38 additions & 0 deletions x/fbridge/keeper/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,44 @@ func (k Keeper) SetRoleMetadata(ctx sdk.Context, data types.RoleMetadata) {
store.Set(types.KeyRoleMetadata, bz)

Check warning on line 218 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L215-L218

Added lines #L215 - L218 were not covered by tests
}

func (k Keeper) UpdateRole(ctx sdk.Context, role types.Role, addr sdk.AccAddress) error {
previousRole := k.GetRole(ctx, addr)
if previousRole == role {
return sdkerrors.ErrUnauthorized.Wrap("target already has same role")

Check warning on line 224 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L221-L224

Added lines #L221 - L224 were not covered by tests
}

metadata := k.GetRoleMetadata(ctx)

Check warning on line 227 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L227

Added line #L227 was not covered by tests

switch previousRole {
case types.RoleGuardian:
metadata.Guardian--
case types.RoleOperator:
metadata.Operator--
case types.RoleJudge:
metadata.Judge--

Check warning on line 235 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L229-L235

Added lines #L229 - L235 were not covered by tests
}

if role == types.RoleEmpty {
k.DeleteRole(ctx, addr)
return nil
} else {
k.SetRole(ctx, role, addr)

Check warning on line 242 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L238-L242

Added lines #L238 - L242 were not covered by tests
}

switch role {
case types.RoleGuardian:
metadata.Guardian++
case types.RoleOperator:
metadata.Operator++
case types.RoleJudge:
metadata.Judge++

Check warning on line 251 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L245-L251

Added lines #L245 - L251 were not covered by tests
}

k.SetRoleMetadata(ctx, metadata)

Check warning on line 254 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L254

Added line #L254 was not covered by tests

return nil

Check warning on line 256 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L256

Added line #L256 was not covered by tests
}

func (k Keeper) GetRoleMetadata(ctx sdk.Context) types.RoleMetadata {
store := ctx.KVStore(k.storeKey)

Check warning on line 260 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L259-L260

Added lines #L259 - L260 were not covered by tests

Expand Down
17 changes: 16 additions & 1 deletion x/fbridge/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,26 @@ func (k Keeper) InitGenesis(ctx sdk.Context, gs *types.GenesisState) error {
k.setVote(ctx, vote.ProposalId, sdk.MustAccAddressFromBech32(vote.Voter), vote.Option)

Check warning on line 19 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L18-L19

Added lines #L18 - L19 were not covered by tests
}

k.SetRoleMetadata(ctx, gs.RoleMetadata)
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++

Check warning on line 31 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L22-L31

Added lines #L22 - L31 were not covered by tests
}
}

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")

Check warning on line 38 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L35-L38

Added lines #L35 - L38 were not covered by tests
}
k.SetRoleMetadata(ctx, gs.RoleMetadata)

Check warning on line 40 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L40

Added line #L40 was not covered by tests

// TODO: we initialize the appropriate genesis parameters whenever the feature is added

return nil
Expand Down

0 comments on commit f21efb6

Please sign in to comment.