Skip to content

Commit

Permalink
chore(build): respond to gosec comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMustermann2 committed Mar 4, 2024
1 parent 715557b commit 8258c46
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 112 deletions.
10 changes: 6 additions & 4 deletions x/dogfood/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
k.ClearPendingConsensusAddrs(ctx)
// finally, perform the actual operations of vote power changes.
operations := k.GetPendingOperations(ctx)
id, _ := k.GetValidatorSetID(ctx, ctx.BlockHeight())
id, _ := k.getValidatorSetID(ctx, ctx.BlockHeight())
if len(operations.GetList()) == 0 {
// there is no validator set change, so we just increment the block height
// and retain the same val set id mapping.
k.SetValidatorSetID(ctx, ctx.BlockHeight()+1, id)
k.setValidatorSetID(ctx, ctx.BlockHeight()+1, id)
return []abci.ValidatorUpdate{}
}
res := make([]abci.ValidatorUpdate, 0, len(operations.GetList()))
Expand All @@ -46,7 +46,8 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
ctx, operation.OperatorAddress,
)
if err != nil {
panic(err)
// this should never happen, but if it does, we just skip the operation.
continue
}
res = append(res, abci.ValidatorUpdate{
PubKey: operation.PubKey,
Expand All @@ -58,7 +59,8 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
Power: 0,
})
case types.KeyOpUnspecified:
panic("unspecified operation type")
// this should never happen, but if it does, we just skip the operation.
continue
}
}
// call via wrapper function so that validator info is stored.
Expand Down
20 changes: 12 additions & 8 deletions x/dogfood/keeper/opt_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func (k Keeper) GetOptOutsToFinish(
ctx sdk.Context, epoch int64,
) [][]byte {
store := ctx.KVStore(k.storeKey)
key := types.OptOutsToFinishKey(epoch)
// the epochs module validates at genesis that epoch is non-negative.
key, _ := types.OptOutsToFinishKey(epoch)
bz := store.Get(key)
if bz == nil {
return [][]byte{}
Expand All @@ -39,7 +40,7 @@ func (k Keeper) setOptOutsToFinish(
ctx sdk.Context, epoch int64, addrs types.AccountAddresses,
) {
store := ctx.KVStore(k.storeKey)
key := types.OptOutsToFinishKey(epoch)
key, _ := types.OptOutsToFinishKey(epoch)
bz, err := addrs.Marshal()
if err != nil {
panic(err)
Expand All @@ -61,7 +62,7 @@ func (k Keeper) RemoveOptOutToFinish(ctx sdk.Context, epoch int64, addr sdk.AccA
// finished at the end of the provided epoch.
func (k Keeper) ClearOptOutsToFinish(ctx sdk.Context, epoch int64) {
store := ctx.KVStore(k.storeKey)
key := types.OptOutsToFinishKey(epoch)
key, _ := types.OptOutsToFinishKey(epoch)
store.Delete(key)
}

Expand All @@ -71,7 +72,8 @@ func (k Keeper) SetOperatorOptOutFinishEpoch(
) {
store := ctx.KVStore(k.storeKey)
key := types.OperatorOptOutFinishEpochKey(operatorAddr)
bz := sdk.Uint64ToBigEndian(uint64(epoch))
uepoch, _ := types.SafeInt64ToUint64(epoch)
bz := sdk.Uint64ToBigEndian(uepoch)
store.Set(key, bz)
}

Expand All @@ -86,7 +88,9 @@ func (k Keeper) GetOperatorOptOutFinishEpoch(
if bz == nil {
return -1
}
return int64(sdk.BigEndianToUint64(bz))
// max int64 is 9 quintillion, and max uint64 is double of that.
// it is too far in the future to be a concern.
return int64(sdk.BigEndianToUint64(bz)) // #nosec G701 // see above.
}

// DeleteOperatorOptOutFinishEpoch deletes the epoch at which an operator's opt out will be
Expand Down Expand Up @@ -115,7 +119,7 @@ func (k Keeper) GetConsensusAddrsToPrune(
ctx sdk.Context, epoch int64,
) [][]byte {
store := ctx.KVStore(k.storeKey)
key := types.ConsensusAddrsToPruneKey(epoch)
key, _ := types.ConsensusAddrsToPruneKey(epoch)
bz := store.Get(key)
if bz == nil {
return [][]byte{}
Expand Down Expand Up @@ -143,7 +147,7 @@ func (k Keeper) DeleteConsensusAddrToPrune(
// epoch.
func (k Keeper) ClearConsensusAddrsToPrune(ctx sdk.Context, epoch int64) {
store := ctx.KVStore(k.storeKey)
key := types.ConsensusAddrsToPruneKey(epoch)
key, _ := types.ConsensusAddrsToPruneKey(epoch)
store.Delete(key)
}

Expand All @@ -153,7 +157,7 @@ func (k Keeper) setConsensusAddrsToPrune(
ctx sdk.Context, epoch int64, addrs types.ConsensusAddresses,
) {
store := ctx.KVStore(k.storeKey)
key := types.ConsensusAddrsToPruneKey(epoch)
key, _ := types.ConsensusAddrsToPruneKey(epoch)
bz, err := addrs.Marshal()
if err != nil {
panic(err)
Expand Down
28 changes: 11 additions & 17 deletions x/dogfood/keeper/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k Keeper) GetPendingOperations(ctx sdk.Context) types.Operations {
}
var operations types.Operations
if err := operations.Unmarshal(bz); err != nil {
panic(err)
return types.Operations{}
}
return operations
}
Expand All @@ -38,10 +38,7 @@ func (k Keeper) ClearPendingOperations(ctx sdk.Context) {
// SetPendingOptOuts sets the pending opt-outs to be applied at the end of the block.
func (k Keeper) SetPendingOptOuts(ctx sdk.Context, addrs types.AccountAddresses) {
store := ctx.KVStore(k.storeKey)
bz, err := addrs.Marshal()
if err != nil {
panic(err)
}
bz := k.cdc.MustMarshal(&addrs)
store.Set(types.PendingOptOutsKey(), bz)
}

Expand All @@ -54,7 +51,7 @@ func (k Keeper) GetPendingOptOuts(ctx sdk.Context) types.AccountAddresses {
}
var addrs types.AccountAddresses
if err := addrs.Unmarshal(bz); err != nil {
panic(err)
return types.AccountAddresses{}
}
return addrs
}
Expand All @@ -69,10 +66,7 @@ func (k Keeper) ClearPendingOptOuts(ctx sdk.Context) {
// block.
func (k Keeper) SetPendingConsensusAddrs(ctx sdk.Context, addrs types.ConsensusAddresses) {
store := ctx.KVStore(k.storeKey)
bz, err := addrs.Marshal()
if err != nil {
panic(err)
}
bz := k.cdc.MustMarshal(&addrs)
store.Set(types.PendingConsensusAddrsKey(), bz)
}

Expand All @@ -86,7 +80,7 @@ func (k Keeper) GetPendingConsensusAddrs(ctx sdk.Context) types.ConsensusAddress
}
var addrs types.ConsensusAddresses
if err := addrs.Unmarshal(bz); err != nil {
panic(err)
return types.ConsensusAddresses{}
}
return addrs
}
Expand All @@ -100,12 +94,12 @@ func (k Keeper) ClearPendingConsensusAddrs(ctx sdk.Context) {

// SetPendingUndelegations sets the pending undelegations to be released at the end of the
// block.
func (k Keeper) SetPendingUndelegations(ctx sdk.Context, undelegations types.UndelegationRecordKeys) {
func (k Keeper) SetPendingUndelegations(
ctx sdk.Context,
undelegations types.UndelegationRecordKeys,
) {
store := ctx.KVStore(k.storeKey)
bz, err := undelegations.Marshal()
if err != nil {
panic(err)
}
bz := k.cdc.MustMarshal(&undelegations)
store.Set(types.PendingUndelegationsKey(), bz)
}

Expand All @@ -119,7 +113,7 @@ func (k Keeper) GetPendingUndelegations(ctx sdk.Context) types.UndelegationRecor
}
var undelegations types.UndelegationRecordKeys
if err := undelegations.Unmarshal(bz); err != nil {
panic(err)
return types.UndelegationRecordKeys{}
}
return undelegations
}
Expand Down
8 changes: 4 additions & 4 deletions x/dogfood/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k Keeper) GetUnbondingCompletionEpoch(
// goes into effect at the beginning of epoch 6. the information
// should be held for 7 epochs, so it should be deleted at the
// beginning of epoch 13 or the end of epoch 12.
return epochInfo.CurrentEpoch + int64(unbondingEpochs)
return epochInfo.CurrentEpoch + int64(unbondingEpochs) // #nosec G701
}

// AppendUndelegationsToMature stores that the undelegation with recordKey should be
Expand All @@ -75,7 +75,7 @@ func (k Keeper) GetUndelegationsToMature(
ctx sdk.Context, epoch int64,
) [][]byte {
store := ctx.KVStore(k.storeKey)
key := types.UnbondingReleaseMaturityKey(epoch)
key, _ := types.UnbondingReleaseMaturityKey(epoch)
bz := store.Get(key)
if bz == nil {
return [][]byte{}
Expand All @@ -94,7 +94,7 @@ func (k Keeper) ClearUndelegationsToMature(
ctx sdk.Context, epoch int64,
) {
store := ctx.KVStore(k.storeKey)
key := types.UnbondingReleaseMaturityKey(epoch)
key, _ := types.UnbondingReleaseMaturityKey(epoch)
store.Delete(key)
}

Expand All @@ -104,7 +104,7 @@ func (k Keeper) setUndelegationsToMature(
ctx sdk.Context, epoch int64, undelegationRecords types.UndelegationRecordKeys,
) {
store := ctx.KVStore(k.storeKey)
key := types.UnbondingReleaseMaturityKey(epoch)
key, _ := types.UnbondingReleaseMaturityKey(epoch)
val, err := undelegationRecords.Marshal()
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 8258c46

Please sign in to comment.