Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 committed May 3, 2024
1 parent 25f2722 commit 2de72c8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
80 changes: 40 additions & 40 deletions x/fbridge/keeper/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,44 @@ func (k Keeper) addVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress
return nil
}

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

metadata := k.GetRoleMetadata(ctx)

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

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

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

k.SetRoleMetadata(ctx, metadata)

return nil
}

func (k Keeper) IsValidRole(role types.Role) error {
switch role {
case types.RoleGuardian, types.RoleOperator, types.RoleJudge:
Expand Down Expand Up @@ -176,7 +214,7 @@ func (k Keeper) GetProposalVotes(ctx sdk.Context, proposalID uint64) []types.Vot
return votes
}

func (k Keeper) SetRole(ctx sdk.Context, role types.Role, addr sdk.AccAddress) {
func (k Keeper) setRole(ctx sdk.Context, role types.Role, addr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
bz := make([]byte, 4)
binary.BigEndian.PutUint32(bz, uint32(role))
Expand Down Expand Up @@ -207,7 +245,7 @@ func (k Keeper) GetRolePairs(ctx sdk.Context) []types.RolePair {
return pairs
}

func (k Keeper) DeleteRole(ctx sdk.Context, addr sdk.AccAddress) {
func (k Keeper) deleteRole(ctx sdk.Context, addr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.RoleKey(addr))
}
Expand All @@ -218,44 +256,6 @@ func (k Keeper) SetRoleMetadata(ctx sdk.Context, data types.RoleMetadata) {
store.Set(types.KeyRoleMetadata, bz)
}

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

metadata := k.GetRoleMetadata(ctx)

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

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

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

k.SetRoleMetadata(ctx, metadata)

return nil
}

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

Expand Down
2 changes: 1 addition & 1 deletion x/fbridge/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, gs *types.GenesisState) error {
}

for _, pair := range gs.Roles {
k.SetRole(ctx, pair.Role, sdk.MustAccAddressFromBech32(pair.Address))
k.setRole(ctx, pair.Role, sdk.MustAccAddressFromBech32(pair.Address))
}
k.SetRoleMetadata(ctx, gs.RoleMetadata)

Expand Down

0 comments on commit 2de72c8

Please sign in to comment.