github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/gov/keeper/keeper.go (about) 1 package keeper 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 8 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types" 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/exported" 11 12 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 13 ) 14 15 // Keeper defines the governance module Keeper 16 type Keeper struct { 17 // The reference to the Paramstore to get and set gov specific params 18 paramSpace types.ParamSubspace 19 20 // The SupplyKeeper to reduce the supply of the network 21 supplyKeeper types.SupplyKeeper 22 23 // The reference to the DelegationSet and ValidatorSet to get information about validators and delegators 24 sk types.StakingKeeper 25 26 // The (unexposed) keys used to access the stores from the Context. 27 storeKey sdk.StoreKey 28 29 // The codec codec for binary encoding/decoding. 30 cdc *codec.Codec 31 32 // Proposal router 33 router types.Router 34 } 35 36 // NewKeeper returns a governance keeper. It handles: 37 // - submitting governance proposals 38 // - depositing funds into proposals, and activating upon sufficient funds being deposited 39 // - users voting on proposals, with weight proportional to stake in the system 40 // - and tallying the result of the vote. 41 // 42 // CONTRACT: the parameter Subspace must have the param key table already initialized 43 func NewKeeper( 44 cdc *codec.Codec, key sdk.StoreKey, paramSpace types.ParamSubspace, 45 supplyKeeper types.SupplyKeeper, sk types.StakingKeeper, rtr types.Router, 46 ) Keeper { 47 48 // ensure governance module account is set 49 if addr := supplyKeeper.GetModuleAddress(types.ModuleName); addr == nil { 50 panic(fmt.Sprintf("%s module account has not been set", types.ModuleName)) 51 } 52 53 // It is vital to seal the governance proposal router here as to not allow 54 // further handlers to be registered after the keeper is created since this 55 // could create invalid or non-deterministic behavior. 56 rtr.Seal() 57 58 return Keeper{ 59 storeKey: key, 60 paramSpace: paramSpace, 61 supplyKeeper: supplyKeeper, 62 sk: sk, 63 cdc: cdc, 64 router: rtr, 65 } 66 } 67 68 // Logger returns a module-specific logger. 69 func (keeper Keeper) Logger(ctx sdk.Context) log.Logger { 70 return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) 71 } 72 73 // Router returns the gov Keeper's Router 74 func (keeper Keeper) Router() types.Router { 75 return keeper.router 76 } 77 78 // GetGovernanceAccount returns the governance ModuleAccount 79 func (keeper Keeper) GetGovernanceAccount(ctx sdk.Context) exported.ModuleAccountI { 80 return keeper.supplyKeeper.GetModuleAccount(ctx, types.ModuleName) 81 } 82 83 // ProposalQueues 84 85 // InsertActiveProposalQueue inserts a ProposalID into the active proposal queue at endTime 86 func (keeper Keeper) InsertActiveProposalQueue(ctx sdk.Context, proposalID uint64, endTime time.Time) { 87 store := ctx.KVStore(keeper.storeKey) 88 bz := types.GetProposalIDBytes(proposalID) 89 store.Set(types.ActiveProposalQueueKey(proposalID, endTime), bz) 90 } 91 92 // RemoveFromActiveProposalQueue removes a proposalID from the Active Proposal Queue 93 func (keeper Keeper) RemoveFromActiveProposalQueue(ctx sdk.Context, proposalID uint64, endTime time.Time) { 94 store := ctx.KVStore(keeper.storeKey) 95 store.Delete(types.ActiveProposalQueueKey(proposalID, endTime)) 96 } 97 98 // InsertInactiveProposalQueue Inserts a ProposalID into the inactive proposal queue at endTime 99 func (keeper Keeper) InsertInactiveProposalQueue(ctx sdk.Context, proposalID uint64, endTime time.Time) { 100 store := ctx.KVStore(keeper.storeKey) 101 bz := types.GetProposalIDBytes(proposalID) 102 store.Set(types.InactiveProposalQueueKey(proposalID, endTime), bz) 103 } 104 105 // RemoveFromInactiveProposalQueue removes a proposalID from the Inactive Proposal Queue 106 func (keeper Keeper) RemoveFromInactiveProposalQueue(ctx sdk.Context, proposalID uint64, endTime time.Time) { 107 store := ctx.KVStore(keeper.storeKey) 108 store.Delete(types.InactiveProposalQueueKey(proposalID, endTime)) 109 } 110 111 // Iterators 112 113 // IterateActiveProposalsQueue iterates over the proposals in the active proposal queue 114 // and performs a callback function 115 func (keeper Keeper) IterateActiveProposalsQueue(ctx sdk.Context, endTime time.Time, cb func(proposal types.Proposal) (stop bool)) { 116 iterator := keeper.ActiveProposalQueueIterator(ctx, endTime) 117 118 defer iterator.Close() 119 for ; iterator.Valid(); iterator.Next() { 120 proposalID, _ := types.SplitActiveProposalQueueKey(iterator.Key()) 121 proposal, found := keeper.GetProposal(ctx, proposalID) 122 if !found { 123 panic(fmt.Sprintf("proposal %d does not exist", proposalID)) 124 } 125 126 if cb(proposal) { 127 break 128 } 129 } 130 } 131 132 // IterateInactiveProposalsQueue iterates over the proposals in the inactive proposal queue 133 // and performs a callback function 134 func (keeper Keeper) IterateInactiveProposalsQueue(ctx sdk.Context, endTime time.Time, cb func(proposal types.Proposal) (stop bool)) { 135 iterator := keeper.InactiveProposalQueueIterator(ctx, endTime) 136 137 defer iterator.Close() 138 for ; iterator.Valid(); iterator.Next() { 139 proposalID, _ := types.SplitInactiveProposalQueueKey(iterator.Key()) 140 proposal, found := keeper.GetProposal(ctx, proposalID) 141 if !found { 142 panic(fmt.Sprintf("proposal %d does not exist", proposalID)) 143 } 144 145 if cb(proposal) { 146 break 147 } 148 } 149 } 150 151 // ActiveProposalQueueIterator returns an sdk.Iterator for all the proposals in the Active Queue that expire by endTime 152 func (keeper Keeper) ActiveProposalQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { 153 store := ctx.KVStore(keeper.storeKey) 154 return store.Iterator(types.ActiveProposalQueuePrefix, sdk.PrefixEndBytes(types.ActiveProposalByTimeKey(endTime))) 155 } 156 157 // InactiveProposalQueueIterator returns an sdk.Iterator for all the proposals in the Inactive Queue that expire by endTime 158 func (keeper Keeper) InactiveProposalQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { 159 store := ctx.KVStore(keeper.storeKey) 160 return store.Iterator(types.InactiveProposalQueuePrefix, sdk.PrefixEndBytes(types.InactiveProposalByTimeKey(endTime))) 161 }