github.com/KiraCore/sekai@v0.3.43/x/gov/keeper/councilor.go (about) 1 package keeper 2 3 import ( 4 "github.com/cosmos/cosmos-sdk/store/prefix" 5 sdk "github.com/cosmos/cosmos-sdk/types" 6 7 "github.com/KiraCore/sekai/x/gov/types" 8 ) 9 10 var ( 11 CouncilorsKey = []byte{0x21} // Councilors key prefix. 12 CouncilorsByMonikerKey = []byte{0x22} // Councilors by moniker prefix. 13 ) 14 15 func GetCouncilorKey(address sdk.AccAddress) []byte { 16 return append(CouncilorsKey, address.Bytes()...) 17 } 18 19 func (k Keeper) SaveCouncilor(ctx sdk.Context, councilor types.Councilor) { 20 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), CouncilorIdentityRegistryPrefix) 21 22 bz := k.cdc.MustMarshal(&councilor) 23 24 councilorKey := GetCouncilorKey(councilor.Address) 25 26 prefixStore.Set(councilorKey, bz) 27 } 28 29 func (k Keeper) DeleteCouncilor(ctx sdk.Context, councilor types.Councilor) { 30 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), CouncilorIdentityRegistryPrefix) 31 councilorKey := GetCouncilorKey(councilor.Address) 32 prefixStore.Delete(councilorKey) 33 } 34 35 func (k Keeper) GetCouncilor(ctx sdk.Context, address sdk.AccAddress) (types.Councilor, bool) { 36 return k.getCouncilorByKey(ctx, GetCouncilorKey(address)) 37 } 38 39 func (k Keeper) GetCouncilorByMoniker(ctx sdk.Context, moniker string) (types.Councilor, bool) { 40 addresses := k.GetAddressesByIdRecordKey(ctx, "moniker", moniker) 41 42 if len(addresses) != 1 { 43 return types.Councilor{}, false 44 } 45 46 return k.GetCouncilor(ctx, addresses[0]) 47 } 48 49 func (k Keeper) getCouncilorByKey(ctx sdk.Context, key []byte) (types.Councilor, bool) { 50 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), CouncilorIdentityRegistryPrefix) 51 52 bz := prefixStore.Get(key) 53 if bz == nil { 54 return types.Councilor{}, false 55 } 56 57 var councilor types.Councilor 58 k.cdc.MustUnmarshal(bz, &councilor) 59 return councilor, true 60 } 61 62 func (k Keeper) GetAllCouncilors(ctx sdk.Context) []types.Councilor { 63 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), CouncilorIdentityRegistryPrefix) 64 65 iterator := prefixStore.Iterator(nil, nil) 66 defer iterator.Close() 67 68 councilors := []types.Councilor{} 69 for ; iterator.Valid(); iterator.Next() { 70 var councilor types.Councilor 71 k.cdc.MustUnmarshal(iterator.Value(), &councilor) 72 councilors = append(councilors, councilor) 73 } 74 75 return councilors 76 } 77 78 func (k Keeper) OnCouncilorAct(ctx sdk.Context, addr sdk.AccAddress) { 79 councilor, found := k.GetCouncilor(ctx, addr) 80 if !found { 81 return 82 } 83 if councilor.Status == types.CouncilorActive { 84 councilor.AbstentionCounter = 0 85 councilor.Rank++ 86 k.SaveCouncilor(ctx, councilor) 87 } 88 } 89 90 func (k Keeper) OnCouncilorAbsent(ctx sdk.Context, addr sdk.AccAddress) { 91 councilor, found := k.GetCouncilor(ctx, addr) 92 if !found { 93 return 94 } 95 if councilor.Status == types.CouncilorActive { 96 properties := k.GetNetworkProperties(ctx) 97 // increase `abstention counter` by `1` 98 councilor.AbstentionCounter++ 99 // decrease rank by `abstention_rank_decrease_amount` 100 if councilor.Rank > int64(properties.AbstentionRankDecreaseAmount) { 101 councilor.Rank -= int64(properties.AbstentionRankDecreaseAmount) 102 } else { 103 councilor.Rank = 0 104 } 105 106 // Counselor **consecutively** did NOT voted on the proposal he had permission to vote more than `max_abstention` number of times while having `active` status 107 // - change status to `inactive` 108 // - set `rank` to `0` 109 if councilor.AbstentionCounter >= int64(properties.MaxAbstention) { 110 councilor.Status = types.CouncilorInactive 111 councilor.Rank = 0 112 } 113 114 k.SaveCouncilor(ctx, councilor) 115 } 116 } 117 118 func (k Keeper) OnCouncilorJail(ctx sdk.Context, addr sdk.AccAddress) { 119 councilor, found := k.GetCouncilor(ctx, addr) 120 if !found { 121 return 122 } 123 councilor.Status = types.CouncilorJailed 124 councilor.Rank = 0 125 k.SaveCouncilor(ctx, councilor) 126 } 127 128 func (k Keeper) ResetWholeCouncilorRank(ctx sdk.Context) { 129 councilors := k.GetAllCouncilors(ctx) 130 for _, councilor := range councilors { 131 councilor.Status = types.CouncilorActive 132 councilor.Rank = 0 133 councilor.AbstentionCounter = 0 134 k.SaveCouncilor(ctx, councilor) 135 } 136 }