Skip to content

Commit

Permalink
address the code comments left by the AI
Browse files Browse the repository at this point in the history
  • Loading branch information
TimmyExogenous committed Sep 10, 2024
1 parent c1ffb85 commit 5fbf81e
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 105 deletions.
2 changes: 1 addition & 1 deletion precompiles/assets/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (p Precompile) RegisterToken(
}

stakingAsset := &assetstypes.StakingAssetInfo{
AssetBasicInfo: &asset,
AssetBasicInfo: asset,
StakingTotalAmount: sdkmath.NewInt(0),
}

Expand Down
4 changes: 2 additions & 2 deletions proto/exocore/operator/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ message OperatorSlashState {
// it's corresponding to the kvStore `BytePrefixForOperatorAndChainIDToPrevConsKey`
message PrevConsKey {
// key is used for storing the previous consensus key,
// which is the combination of keystore prefix,chainID, and operator address.
// which is the combination of chainID and operator address.
string key = 1;
// consensus_key is the consensus key of the operator on the chain.
// the length of this key should be exactly 32 bytes, and must be enforced
Expand All @@ -112,7 +112,7 @@ message PrevConsKey {
// is in the process of unbonding their key for the given chainID.
// it's corresponding to the kvStore `BytePrefixForOperatorKeyRemovalForChainID`
message OperatorKeyRemoval {
// key is the combination of keystore prefix, operator address, and chainID.
// key is the combination of operator address and chainID.
string key = 1;
}

Expand Down
19 changes: 10 additions & 9 deletions x/assets/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"github.com/ExocoreNetwork/exocore/x/assets/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -15,14 +16,14 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
for i := range data.ClientChains {
info := data.ClientChains[i]
if err := k.SetClientChainInfo(ctx, &info); err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set client chain info"))
}
}
// client_chain_asset.go
for i := range data.Tokens {
info := data.Tokens[i]
if err := k.SetStakingAssetInfo(ctx, &info); err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set staking asset info"))
}
}
// staker_asset.go (deposits)
Expand All @@ -40,7 +41,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
if err := k.UpdateStakerAssetState(
ctx, stakerID, assetID, infoAsChange,
); err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set deposit info"))
}
}
}
Expand All @@ -52,7 +53,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
infoAsChange := types.DeltaOperatorSingleAsset(assetInfo.Info)
err := k.UpdateOperatorAssetState(ctx, accAddress, assetInfo.AssetID, infoAsChange)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set operator asset info"))
}
}
}
Expand All @@ -63,31 +64,31 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
res := types.GenesisState{}
params, err := k.GetParams(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get parameter").Error())
}
res.Params = *params

allClientChains, err := k.GetAllClientChainInfo(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all client chains").Error())
}
res.ClientChains = allClientChains

allAssets, err := k.GetAllStakingAssetsInfo(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all staking assets info").Error())
}
res.Tokens = allAssets

allDeposits, err := k.AllDeposits(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all deposits").Error())
}
res.Deposits = allDeposits

operatorAssets, err := k.AllOperatorAssets(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all assets info for the operators").Error())
}
res.OperatorAssets = operatorAssets
return &res
Expand Down
14 changes: 7 additions & 7 deletions x/delegation/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func (k Keeper) InitGenesis(
// init the state from the general exporting genesis file
err := k.SetAllDelegationStates(ctx, gs.DelegationStates)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all delegation states"))
}
err = k.SetAllStakerList(ctx, gs.StakersByOperator)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all staker list"))
}
err = k.SetUndelegationRecords(ctx, gs.Undelegations)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all undelegation records"))
}
return []abci.ValidatorUpdate{}
}
Expand All @@ -53,25 +53,25 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *delegationtype.GenesisState {
res := delegationtype.GenesisState{}
associations, err := k.GetAllAssociations(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all associations").Error())
}
res.Associations = associations

delegationStates, err := k.AllDelegationStates(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all delegation states").Error())
}
res.DelegationStates = delegationStates

stakerList, err := k.AllStakerList(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all staker list").Error())
}
res.StakersByOperator = stakerList

undelegations, err := k.AllUndelegations(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all undelegations").Error())
}
res.Undelegations = undelegations
return &res
Expand Down
8 changes: 4 additions & 4 deletions x/delegation/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ func ValidateIDAndOperator(stakerID, assetID, operator string) error {
// validate the operator address
if _, err := sdk.AccAddressFromBech32(operator); err != nil {
return xerrors.Errorf(
"invalid operator address for operator %s", operator,
"ValidateIDAndOperator: invalid operator address for operator %s", operator,
)
}
_, stakerClientChainID, err := assetstypes.ValidateID(stakerID, true, false)
if err != nil {
return xerrors.Errorf(
"invalid stakerID: %s",
"ValidateIDAndOperator: invalid stakerID: %s",
stakerID,
)
}
_, assetClientChainID, err := assetstypes.ValidateID(assetID, true, false)
if err != nil {
return xerrors.Errorf(
"invalid assetID: %s",
"ValidateIDAndOperator: invalid assetID: %s",
assetID,
)
}
if stakerClientChainID != assetClientChainID {
return xerrors.Errorf(
"the client chain layerZero IDs of the staker and asset are different, stakerID:%s, assetID:%s",
"ValidateIDAndOperator: the client chain layerZero IDs of the staker and asset are different, stakerID:%s, assetID:%s",
stakerID, assetID)
}
return nil
Expand Down
45 changes: 33 additions & 12 deletions x/operator/keeper/consensus_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -446,28 +447,38 @@ func (k *Keeper) SetAllPrevConsKeys(ctx sdk.Context, prevConsKeys []types.PrevCo
store := ctx.KVStore(k.storeKey)
for i := range prevConsKeys {
prevKey := prevConsKeys[i]
keyBytes, err := hexutil.Decode(prevKey.Key)
keys, err := assetstypes.ParseJoinedStoreKey([]byte(prevKey.Key), 2)
if err != nil {
return nil
return err
}
chainID := keys[0]
opAccAddr, err := sdk.AccAddressFromBech32(keys[1])
if err != nil {
return err
}
wrappedKey := types.NewWrappedConsKeyFromHex(prevKey.ConsensusKey)
bz := k.cdc.MustMarshal(wrappedKey.ToTmProtoKey())
store.Set(keyBytes, bz)

store.Set(types.KeyForChainIDAndOperatorToPrevConsKey(chainID, opAccAddr), bz)
}
return nil
}

func (k *Keeper) GetAllPrevConsKeys(ctx sdk.Context) ([]types.PrevConsKey, error) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.BytePrefixForOperatorAndChainIDToPrevConsKey})
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{types.BytePrefixForOperatorAndChainIDToPrevConsKey})
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

ret := make([]types.PrevConsKey, 0)
for ; iterator.Valid(); iterator.Next() {
var consKey tmprotocrypto.PublicKey
k.cdc.MustUnmarshal(iterator.Value(), &consKey)
chainID, operatorAddr, err := types.ParsePrevConsKey(iterator.Key())
if err != nil {
return nil, err
}
ret = append(ret, types.PrevConsKey{
Key: hexutil.Encode(iterator.Key()),
Key: string(assetstypes.GetJoinedStoreKey(chainID, operatorAddr.String())),
ConsensusKey: hexutil.Encode(consKey.GetEd25519()),
})
}
Expand All @@ -478,24 +489,34 @@ func (k *Keeper) SetAllOperatorKeyRemovals(ctx sdk.Context, operatorKeyRemoval [
store := ctx.KVStore(k.storeKey)
for i := range operatorKeyRemoval {
keyRemoval := operatorKeyRemoval[i]
keyBytes, err := hexutil.Decode(keyRemoval.Key)
keys, err := assetstypes.ParseJoinedStoreKey([]byte(keyRemoval.Key), 2)
if err != nil {
return nil
return err
}
chainID := keys[1]
opAccAddr, err := sdk.AccAddressFromBech32(keys[0])
if err != nil {
return err
}
store.Set(keyBytes, []byte{})
store.Set(types.KeyForOperatorKeyRemovalForChainID(opAccAddr, chainID), []byte{})
}
return nil
}

func (k *Keeper) GetAllOperatorKeyRemovals(ctx sdk.Context) ([]types.OperatorKeyRemoval, error) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.BytePrefixForOperatorKeyRemovalForChainID})
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{types.BytePrefixForOperatorKeyRemovalForChainID})
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

ret := make([]types.OperatorKeyRemoval, 0)
for ; iterator.Valid(); iterator.Next() {
operatorAddr, chainID, err := types.ParseKeyForOperatorKeyRemoval(iterator.Key())
if err != nil {
return nil, err
}

ret = append(ret, types.OperatorKeyRemoval{
Key: hexutil.Encode(iterator.Key()),
Key: string(assetstypes.GetJoinedStoreKey(operatorAddr.String(), chainID)),
})
}
return ret, nil
Expand Down
29 changes: 15 additions & 14 deletions x/operator/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"github.com/ExocoreNetwork/exocore/x/operator/types"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -13,7 +14,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) []abci.Va
op.OperatorInfo.EarningsAddr = op.OperatorAddress
}
if err := k.SetOperatorInfo(ctx, op.OperatorAddress, &op.OperatorInfo); err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set operator info"))
}
}
for _, record := range state.OperatorRecords {
Expand All @@ -29,27 +30,27 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) []abci.Va
// init the state from the general exporting genesis file
err := k.SetAllOptedInfo(ctx, state.OptStates)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all opted info"))
}
err = k.SetAllOperatorUSDValues(ctx, state.OperatorUSDValues)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all operator USD values"))
}
err = k.SetAllAVSUSDValues(ctx, state.AVSUSDValues)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all AVS USD values"))
}
err = k.SetAllSlashInfo(ctx, state.SlashStates)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all slash info"))
}
err = k.SetAllPrevConsKeys(ctx, state.PreConsKeys)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all previous consensus keys"))
}
err = k.SetAllOperatorKeyRemovals(ctx, state.OperatorKeyRemovals)
if err != nil {
panic(err)
panic(errorsmod.Wrap(err, "failed to set all key removals for operators"))
}
return []abci.ValidatorUpdate{}
}
Expand All @@ -60,43 +61,43 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {

operatorRecords, err := k.GetAllOperatorConsKeyRecords(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all consensus keys for operators").Error())
}
res.OperatorRecords = operatorRecords

optedInfos, err := k.GetAllOptedInfo(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all opted info").Error())
}
res.OptStates = optedInfos

allAVSUSDValues, err := k.GetAllAVSUSDValues(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all AVS USD values").Error())
}
res.AVSUSDValues = allAVSUSDValues

allOperatorUSDValues, err := k.GetAllOperatorUSDValues(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all operator USD values").Error())
}
res.OperatorUSDValues = allOperatorUSDValues

slashingInfos, err := k.GetAllSlashInfo(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to set all slashing info").Error())
}
res.SlashStates = slashingInfos

prevConsKeys, err := k.GetAllPrevConsKeys(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all previous consensus keys").Error())
}
res.PreConsKeys = prevConsKeys

operatorKeyRemovals, err := k.GetAllOperatorKeyRemovals(ctx)
if err != nil {
panic(err)
ctx.Logger().Error(errorsmod.Wrap(err, "failed to get all key removals for operators").Error())
}
res.OperatorKeyRemovals = operatorKeyRemovals

Expand Down
5 changes: 4 additions & 1 deletion x/operator/keeper/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func (k *Keeper) SetOperatorInfo(
ctx sdk.Context, addr string, info *operatortypes.OperatorInfo,
) (err error) {
// #nosec G703 // already validated in `ValidateBasic`
opAccAddr, _ := sdk.AccAddressFromBech32(addr)
opAccAddr, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return errorsmod.Wrap(err, "SetOperatorInfo: error occurred when parse acc address from Bech32")
}
// if already registered, this request should go to EditOperator.
if k.IsOperator(ctx, opAccAddr) {
return errorsmod.Wrap(
Expand Down
Loading

0 comments on commit 5fbf81e

Please sign in to comment.