Skip to content

Commit

Permalink
rebase to develop and fix the issues from code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TimmyExogenous committed Nov 11, 2024
1 parent 35ad190 commit 57620d7
Show file tree
Hide file tree
Showing 11 changed files with 1,409 additions and 175 deletions.
7 changes: 0 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"bytes"
"encoding/binary"
"sort"
"strings"

Expand Down Expand Up @@ -211,9 +210,3 @@ func SortByPower(
}
return sortedOperatorAddrs, sortedPubKeys, sortedPowers
}

func EncodeHeightBytes(height uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, height)
return buf
}
6 changes: 2 additions & 4 deletions x/avs/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,8 @@ func (k Keeper) GetAVSEpochInfo(ctx sdk.Context, addr string) (*epochstypes.Epoc
return nil, err
}
avsInfo := avsInfoResp.Info
epochInfo, found := k.epochsKeeper.GetEpochInfo(ctx, avsInfo.EpochIdentifier)
if !found {
return nil, types.ErrEpochNotFound.Wrapf("epoch info not found %s", avsInfo.EpochIdentifier)
}
// Epoch information must be present because it is checked when setting AVS information.
epochInfo, _ := k.epochsKeeper.GetEpochInfo(ctx, avsInfo.EpochIdentifier)
return &epochInfo, nil
}

Expand Down
3 changes: 1 addition & 2 deletions x/avs/keeper/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper_test
import (
"strconv"

sdkmath "cosmossdk.io/math"
types "github.com/ExocoreNetwork/exocore/x/avs/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -18,7 +17,7 @@ func (suite *AVSTestSuite) TestTaskInfo() {
TaskResponsePeriod: 10000,
TaskChallengePeriod: 5000,
ThresholdPercentage: 60,
TaskTotalPower: sdk.Dec(sdkmath.ZeroInt()),
TaskTotalPower: sdk.ZeroDec(),
}
err := suite.App.AVSManagerKeeper.SetTaskInfo(suite.Ctx, info)
suite.NoError(err)
Expand Down
45 changes: 38 additions & 7 deletions x/dogfood/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}
defer k.ClearValidatorSetUpdateFlag(ctx)
logger := k.Logger(ctx)
chainIDWithoutRevision := avstypes.ChainIDWithoutRevision(ctx.ChainID())

// start by clearing the previous consensus keys for the chain.
Expand All @@ -31,7 +32,7 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
for _, undelegation := range undelegations.GetList() {
err := k.delegationKeeper.DecrementUndelegationHoldCount(ctx, undelegation)
if err != nil {
k.Logger(ctx).Error("error decrementing undelegation hold count", "error", err)
logger.Error("error decrementing undelegation hold count", "error", err)
}
k.ClearUndelegationMaturityEpoch(ctx, undelegation)
}
Expand All @@ -43,7 +44,7 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
ctx, addr, chainIDWithoutRevision,
)
if err != nil {
k.Logger(ctx).Error("error completing operator key removal", "error", err)
logger.Error("error completing operator key removal", "error", err)
}
}
k.ClearPendingOptOuts(ctx)
Expand Down Expand Up @@ -74,7 +75,7 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
pubKey, err := validator.ConsPubKey()
if err != nil {
// indicates an error in deserialization, and should never happen.
k.Logger(ctx).Error("error deserializing consensus public key", "error", err)
logger.Error("error deserializing consensus public key", "error", err)
continue
}
addressString := sdk.GetConsAddress(pubKey).String()
Expand All @@ -85,30 +86,33 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
ctx, operators, chainIDWithoutRevision,
)
if err != nil {
k.Logger(ctx).Error("error getting vote power for chain", "error", err)
logger.Error("error getting vote power for chain", "error", err)
return []abci.ValidatorUpdate{}
}
operators, keys, powers = utils.SortByPower(operators, keys, powers)
maxVals := k.GetMaxValidators(ctx)
k.Logger(ctx).Info("max validators", "maxVals", maxVals, "len(operators)", len(operators))
logger.Info("before loop", "maxVals", maxVals, "len(operators)", len(operators))
// the capacity of this list is twice the maximum number of validators.
// this is because we can have a maximum of maxVals validators, and we can also have
// a maximum of maxVals validators that are removed.
res := make([]keytypes.WrappedConsKeyWithPower, 0, maxVals*2)
for i := range operators {
logger.Debug("loop", i)
// #nosec G701 // ok on 64-bit systems.
if i >= int(maxVals) {
// we have reached the maximum number of validators, amongst all the validators.
// even if there are intersections with the previous validator set, this will
// only be reached if we exceed the threshold.
// if there are no intersections, this case is glaringly obvious.
logger.Debug("max validators reached", "i", i)
break
}
power := powers[i]
if power < 1 {
// we have reached the bottom of the rung.
// assumption is that negative vote power isn't provided by the module.
// the consensus engine will reject it anyway and panic.
logger.Debug("power less than 1", "i", i)
break
}
// find the previous power.
Expand All @@ -118,10 +122,24 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
if found {
// if the power has changed, queue an update. skip, otherwise.
if prevPower != power {
logger.Debug(
"power changed",
"i", i,
"operator", operators[i].String(),
"power", power,
"prevPower", prevPower,
)
res = append(res, keytypes.WrappedConsKeyWithPower{
Key: wrappedKey,
Power: power,
})
} else {
logger.Debug(
"power not changed",
"i", i,
"operator", operators[i].String(),
"power", power,
)
}
// remove the validator from the previous map, so that 0 power
// is not queued for it.
Expand All @@ -132,11 +150,21 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
Key: wrappedKey,
Power: power,
})
logger.Debug(
"new validator",
"i", i,
"operator", operators[i].String(),
"power", power,
)
}
// all powers, regardless of whether the key exists, are added to the total power.
totalPower = totalPower.Add(sdk.NewInt(power))
}
k.Logger(ctx).Info("total power", "totalPower", totalPower, "len(res)", len(res))
logger.Info(
"before removal",
"totalPower", totalPower,
"len(res)", len(res),
)
// the remaining validators in prevMap have been removed.
// we need to queue a change in power to 0 for them.
for _, validator := range prevList { // O(N)
Expand All @@ -153,7 +181,10 @@ func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
// so the previous power of these validators does not need to be subtracted.
}
}
k.Logger(ctx).Info("total power", "totalPower", totalPower, "len(res)", len(res))
logger.Info(
"after removal",
"len(res)", len(res),
)
// if there are any updates, set total power on lookup index.
if len(res) > 0 {
k.SetLastTotalPower(ctx, totalPower)
Expand Down
4 changes: 2 additions & 2 deletions x/dogfood/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (k Keeper) Hooks() types.DogfoodHooks {
return types.MultiDogfoodHooks{}
}

// MarkUpdateValidatorSetFlag marks the end of the epoch. It is called within the BeginBlocker to inform
// the module to apply the validator updates at the end of this block.
// MarkUpdateValidatorSetFlag marks that the validator set needs to be updated at the end of this block.
// Mostly, these updates occur in response to the epoch ending. In other cases, they are the result of slashing.
func (k Keeper) MarkUpdateValidatorSetFlag(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
key := types.ShouldUpdateValidatorSetByteKey()
Expand Down
1 change: 0 additions & 1 deletion x/operator/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
assetstype "github.com/ExocoreNetwork/exocore/x/assets/types"

keytypes "github.com/ExocoreNetwork/exocore/types/keys"
assetstype "github.com/ExocoreNetwork/exocore/x/assets/types"
avstypes "github.com/ExocoreNetwork/exocore/x/avs/types"
"github.com/ExocoreNetwork/exocore/x/operator/types"
tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
Expand Down
2 changes: 1 addition & 1 deletion x/operator/keeper/usd_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (k *Keeper) CalculateUSDValueForStaker(ctx sdk.Context, stakerID, avsAddr s
return sdkmath.LegacyDec{}, err
}
if optedUSDValues.ActiveUSDValue.IsZero() {
return sdkmath.LegacyZeroDec(), err
return sdkmath.LegacyZeroDec(), nil
}

// calculate the active voting power for staker
Expand Down
4 changes: 1 addition & 3 deletions x/operator/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/binary"
"math"

"github.com/ExocoreNetwork/exocore/utils"

"golang.org/x/xerrors"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -165,7 +163,7 @@ func KeyForVotingPowerSnapshot(avs common.Address, height int64) []byte {
return AppendMany(
avs.Bytes(),
// Append the height
utils.EncodeHeightBytes(uint64(height)),
sdk.Uint64ToBigEndian(uint64(height)),
)
}

Expand Down
Loading

0 comments on commit 57620d7

Please sign in to comment.