Skip to content

Commit

Permalink
resolve the missed issues identified by code review, such as adding s…
Browse files Browse the repository at this point in the history
…ome comments and refine the naming
  • Loading branch information
TimmyExogenous committed Mar 18, 2024
1 parent 361d0fa commit ddaf1a2
Show file tree
Hide file tree
Showing 34 changed files with 462 additions and 397 deletions.
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,9 @@ func NewExocoreApp(
// set exoCore staking keepers
app.AssetsKeeper = assetsKeeper.NewKeeper(keys[assetsTypes.StoreKey], appCodec)
app.DepositKeeper = depositKeeper.NewKeeper(keys[depositTypes.StoreKey], appCodec, app.AssetsKeeper)
app.OperatorKeeper = operatorKeeper.NewKeeper(keys[operatorTypes.StoreKey], appCodec, app.AssetsKeeper, operatorTypes.MockOracle{}, operatorTypes.MockAVS{}, delegationTypes.VirtualISlashKeeper{})
app.OperatorKeeper = operatorKeeper.NewKeeper(keys[operatorTypes.StoreKey], appCodec, app.AssetsKeeper, operatorTypes.MockOracle{}, operatorTypes.MockAvs{}, delegationTypes.VirtualSlashKeeper{})
// todo: need to replace the virtual keepers with actual keepers after they have been implemented
app.DelegationKeeper = delegationKeeper.NewKeeper(keys[depositTypes.StoreKey], appCodec, app.AssetsKeeper, app.DepositKeeper, delegationTypes.VirtualISlashKeeper{}, &app.OperatorKeeper)
app.DelegationKeeper = delegationKeeper.NewKeeper(keys[depositTypes.StoreKey], appCodec, app.AssetsKeeper, delegationTypes.VirtualSlashKeeper{}, &app.OperatorKeeper)
app.OperatorKeeper.RegisterExpectDelegationInterface(&app.DelegationKeeper)

app.WithdrawKeeper = *withdrawKeeper.NewKeeper(appCodec, keys[withdrawTypes.StoreKey], app.AssetsKeeper, app.DepositKeeper)
Expand Down
2 changes: 1 addition & 1 deletion proto/exocore/operator/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ option go_package = "github.com/ExocoreNetwork/exocore/x/operator/types";

// QueryOperatorInfoReq is the request to obtain the operator information.
message GetOperatorInfoReq {
// operator_addr is the operator address.
// operator_addr is the operator address,its type should be a sdk.AccAddress
string operator_addr = 1
[(cosmos_proto.scalar) = "cosmos.AddressString"];
}
Expand Down
30 changes: 13 additions & 17 deletions proto/exocore/operator/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ message RegisterOperatorReq {
// RegisterOperatorResponse is the response to a register operator request.
message RegisterOperatorResponse{}

// OptInToChainIDRequest defines the OptInToChainID request.
message OptInToChainIDRequest {
// OptInToCosmosChainRequest defines the OptInToCosmosChain request.
message OptInToCosmosChainRequest {
option (cosmos.msg.v1.signer) = "address";
// address is the operator address
string address = 1;
Expand All @@ -127,21 +127,21 @@ message OptInToChainIDRequest {
string public_key = 3;
}

// OptInToChainIDResponse defines the OptInToChainID response.
message OptInToChainIDResponse {
// OptInToCosmosChainResponse defines the OptInToCosmosChain response.
message OptInToCosmosChainResponse {
}

// InitiateOptOutFromChainIDRequest defines the InitiateOptOutFromChainID request.
message InitiateOptOutFromChainIDRequest {
// InitiateOptOutFromCosmosChainRequest defines the InitiateOptOutFromCosmosChain request.
message InitiateOptOutFromCosmosChainRequest {
option (cosmos.msg.v1.signer) = "address";
// address is the operator address
string address = 1;
// chain_id is the identifier for the chain that wants to opt out.
string chain_id = 2;
}

// InitiateOptOutFromChainIDResponse defines the InitiateOptOutFromChainID response.
message InitiateOptOutFromChainIDResponse {
// InitiateOptOutFromCosmosChainResponse defines the InitiateOptOutFromCosmosChain response.
message InitiateOptOutFromCosmosChainResponse {
}

// Msg defines the operator Msg service.
Expand All @@ -151,18 +151,14 @@ service Msg {
rpc RegisterOperator(RegisterOperatorReq) returns (RegisterOperatorResponse);

// add services for dogfood
// OptInToChainID acts as opt in method for an operator to
// OptInToCosmosChain acts as opt in method for an operator to
// start validatring on a chain. The operator must sign the request with
// the key with which they registered in the system.
rpc OptInToChainID(OptInToChainIDRequest) returns (OptInToChainIDResponse) {};
// InitiateOptOutFromChainID is a method with which an operator can initiate
rpc OptInToCosmosChain(OptInToCosmosChainRequest) returns (OptInToCosmosChainResponse) {};
// InitiateOptOutFromCosmosChain is a method with which an operator can initiate
// the opt out process from a chain. The operator must sign the request with
// the key with which they registered in the system. The opt-out process takes
// as long as the chain's unbonding period to complete, plus some loose change
// for message relaying across chains.
rpc InitiateOptOutFromChainID(InitiateOptOutFromChainIDRequest) returns (InitiateOptOutFromChainIDResponse) {};
}




rpc InitiateOptOutFromCosmosChain(InitiateOptOutFromCosmosChainRequest) returns (InitiateOptOutFromCosmosChainResponse) {};
}
8 changes: 4 additions & 4 deletions testutil/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func Commit(ctx sdk.Context, app *app.ExocoreApp, t time.Duration, vs *tmtypes.V
return ctx.WithBlockHeader(header), nil
}

// CommitAndCreateNewCtx commits a block at a given time creating a Ctx with the current settings
// CommitAndCreateNewCtx commits a block at a given time creating a ctx with the current settings
// This is useful to keep test settings that could be affected by EndBlockers, e.g.
// setting a baseFee == 0 and expecting this condition to continue after commit
func CommitAndCreateNewCtx(ctx sdk.Context, app *app.ExocoreApp, t time.Duration, vs *tmtypes.ValidatorSet, isUncachedCtx bool) (sdk.Context, error) {
func CommitAndCreateNewCtx(ctx sdk.Context, app *app.ExocoreApp, t time.Duration, vs *tmtypes.ValidatorSet, useUncachedCtx bool) (sdk.Context, error) {
header, err := commit(ctx, app, t, vs)
if err != nil {
return ctx, err
Expand All @@ -45,7 +45,7 @@ func CommitAndCreateNewCtx(ctx sdk.Context, app *app.ExocoreApp, t time.Duration
// but resets other context fields
// GasMeter is set as InfiniteGasMeter
var newCtx sdk.Context
if isUncachedCtx {
if useUncachedCtx {
newCtx = app.BaseApp.NewUncachedContext(false, header)
} else {
newCtx = app.BaseApp.NewContext(false, header)
Expand Down Expand Up @@ -228,7 +228,7 @@ func commit(ctx sdk.Context, app *app.ExocoreApp, t time.Duration, vs *tmtypes.V
return header, nil
}

// checkTxBytes encodes a transaction and calls checkTx on the App.
// checkTxBytes encodes a transaction and calls checkTx on the app.
func checkTxBytes(app *app.ExocoreApp, txEncoder sdk.TxEncoder, tx sdk.Tx) (abci.ResponseCheckTx, error) {
bz, err := txEncoder(tx)
if err != nil {
Expand Down
16 changes: 0 additions & 16 deletions x/assets/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"

errorsmod "cosmossdk.io/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -140,17 +138,3 @@ func ParseJoinedStoreKey(key []byte, number int) (keys []string, err error) {
}
return stringList, nil
}

// add for dogfood
func OperatorSnapshotKey(operatorAddr sdk.AccAddress, height uint64) []byte {
base := []byte{prefixOperatorSnapshot}
base = append(base, operatorAddr.Bytes()...)
base = append(base, sdk.Uint64ToBigEndian(height)...)
return base
}

func OperatorLastSnapshotHeightKey(operatorAddr sdk.AccAddress) []byte {
base := []byte{prefixOperatorLastSnapshotHeight}
base = append(base, operatorAddr.Bytes()...)
return base
}
4 changes: 1 addition & 3 deletions x/assets/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions x/delegation/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Valida
// todo: don't think about freezing the operator in current implementation
/* if k.slashKeeper.IsOperatorFrozen(ctx, operatorAccAddress) {
// reSet the completed height if the operator is frozen
record.CompleteBlockNumber = k.expectedOperatorInterface.GetUnbondingExpirationBlockNumber(ctx, operatorAccAddress, record.BlockNumber)
record.CompleteBlockNumber = k.operatorKeeper.GetUnbondingExpirationBlockNumber(ctx, operatorAccAddress, record.BlockNumber)
if record.CompleteBlockNumber <= uint64(ctx.BlockHeight()) {
panic(fmt.Sprintf("the reset completedHeight isn't in future,setHeight:%v,curHeight:%v", record.CompleteBlockNumber, ctx.BlockHeight()))
}
Expand Down Expand Up @@ -51,12 +51,6 @@ func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Valida
}
continue
}
// operator opt out: since operators can not immediately withdraw their funds, that is,
// even operator funds are not immediately available, operator opt out does not require
// any special handling here. if an operator undelegates before they opt out, the undelegation
// will be processed normally. if they undelegate after they opt out, the undelegation will
// be released at the same time as opt out completion, provided there are no other chains that
// the operator is still active on. the same applies to delegators too.
// TODO(mike): ensure that operator is required to perform self delegation to match above.

// calculate the actual canUndelegated asset amount
Expand Down
15 changes: 7 additions & 8 deletions x/delegation/keeper/cross_chain_tx_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ type DelegationOrUndelegationParams struct {
// DelegateTo : It doesn't need to check the active status of the operator in middlewares when delegating assets to the operator. This is because it adds assets to the operator's amount. But it needs to check if operator has been slashed or frozen.
func (k *Keeper) DelegateTo(ctx sdk.Context, params *DelegationOrUndelegationParams) error {
// check if the delegatedTo address is an operator
if !k.expectedOperatorInterface.IsOperator(ctx, params.OperatorAddress) {
return errorsmod.Wrap(delegationtype.ErrOperatorNotExist, fmt.Sprintf("input opreatorAddr is:%s", params.OperatorAddress))
if !k.operatorKeeper.IsOperator(ctx, params.OperatorAddress) {
return errorsmod.Wrap(delegationtype.ErrOperatorNotExist, fmt.Sprintf("input operatorAddr is:%s", params.OperatorAddress))
}

// check if the operator has been slashed or frozen
Expand Down Expand Up @@ -167,7 +167,7 @@ func (k *Keeper) DelegateTo(ctx sdk.Context, params *DelegationOrUndelegationPar
return err
}
// call operator module to bond the increased assets to the opted-in AVS
err = k.expectedOperatorInterface.UpdateOptedInAssetsState(ctx, stakerID, assetID, params.OperatorAddress.String(), params.OpAmount)
err = k.operatorKeeper.UpdateOptedInAssetsState(ctx, stakerID, assetID, params.OperatorAddress.String(), params.OpAmount)
if err != nil {
return err
}
Expand All @@ -182,7 +182,7 @@ func (k *Keeper) DelegateTo(ctx sdk.Context, params *DelegationOrUndelegationPar
// So we use two steps to handle the undelegation. Fist,record the undelegation request and the corresponding exit time which needs to be obtained from the operator opt-in module. Then,we handle the record when the exit time has expired.
func (k *Keeper) UndelegateFrom(ctx sdk.Context, params *DelegationOrUndelegationParams) error {
// check if the UndelegatedFrom address is an operator
if !k.expectedOperatorInterface.IsOperator(ctx, params.OperatorAddress) {
if !k.operatorKeeper.IsOperator(ctx, params.OperatorAddress) {
return delegationtype.ErrOperatorNotExist
}
if params.OpAmount.IsNegative() {
Expand Down Expand Up @@ -210,7 +210,7 @@ func (k *Keeper) UndelegateFrom(ctx sdk.Context, params *DelegationOrUndelegatio
Amount: params.OpAmount,
ActualCompletedAmount: sdkmath.NewInt(0),
}
r.CompleteBlockNumber = k.expectedOperatorInterface.GetUnbondingExpirationBlockNumber(ctx, params.OperatorAddress, r.BlockNumber)
r.CompleteBlockNumber = k.operatorKeeper.GetUnbondingExpirationBlockNumber(ctx, params.OperatorAddress, r.BlockNumber)
err = k.SetUndelegationRecords(ctx, []*delegationtype.UndelegationRecord{r})
if err != nil {
return err
Expand Down Expand Up @@ -248,14 +248,13 @@ func (k *Keeper) UndelegateFrom(ctx sdk.Context, params *DelegationOrUndelegatio
}

// call operator module to decrease the state of opted-in assets
err = k.expectedOperatorInterface.UpdateOptedInAssetsState(ctx, stakerID, assetID, params.OperatorAddress.String(), params.OpAmount.Neg())
err = k.operatorKeeper.UpdateOptedInAssetsState(ctx, stakerID, assetID, params.OperatorAddress.String(), params.OpAmount.Neg())
if err != nil {
return err
}

// call the hooks registered by the other modules
k.Hooks().AfterUndelegationStarted(ctx, params.OperatorAddress, delegationtype.GetUndelegationRecordKey(r.LzTxNonce, r.TxHash, r.OperatorAddr))
return nil
return k.Hooks().AfterUndelegationStarted(ctx, params.OperatorAddress, delegationtype.GetUndelegationRecordKey(r.LzTxNonce, r.TxHash, r.OperatorAddr))
}

/*func (k *Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error {
Expand Down
2 changes: 1 addition & 1 deletion x/delegation/keeper/delegation_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (suite *DelegationTestSuite) TestDelegateTo() {
TxHash: common.HexToHash("0x24c4a315d757249c12a7a1d7b6fb96261d49deee26f06a3e1787d008b445c3ac"),
}
err = suite.App.DelegationKeeper.DelegateTo(suite.Ctx, delegationParams)
suite.EqualError(err, errorsmod.Wrap(delegationtype.ErrOperatorNotExist, fmt.Sprintf("input opreatorAddr is:%s", delegationParams.OperatorAddress)).Error())
suite.EqualError(err, errorsmod.Wrap(delegationtype.ErrOperatorNotExist, fmt.Sprintf("input operatorAddr is:%s", delegationParams.OperatorAddress)).Error())

registerReq := &operatortype.RegisterOperatorReq{
FromAddress: opAccAddr.String(),
Expand Down
6 changes: 4 additions & 2 deletions x/delegation/keeper/delegation_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func (k *Keeper) GetStakerDelegationTotalAmount(ctx sdk.Context, stakerID string
store := prefix.NewStore(ctx.KVStore(k.storeKey), delegationtype.KeyPrefixRestakerDelegationInfo)
var ret delegationtype.ValueField
prefixKey := assetstype.GetJoinedStoreKey(stakerID, assetID)
isExit := store.Has(prefixKey)
if !isExit {
if !store.Has(prefixKey) {
return sdkmath.Int{}, errorsmod.Wrap(delegationtype.ErrNoKeyInTheStore, fmt.Sprintf("GetStakerDelegationTotalAmount: key is %s", prefixKey))
}
value := store.Get(prefixKey)
Expand Down Expand Up @@ -147,6 +146,9 @@ func (k *Keeper) GetDelegationInfo(ctx sdk.Context, stakerID, assetID string) (*
}

// DelegationStateByOperatorAssets get the specified assets state delegated to the specified operator
// assetsFilter: assetID->nil, it's used to filter the specified assets
// the first return value is a nested map, its type is: stakerID->assetID->DelegationAmounts
// It means all delegation information related to the specified operator and filtered by the specified asset IDs
func (k *Keeper) DelegationStateByOperatorAssets(ctx sdk.Context, operatorAddr string, assetsFilter map[string]interface{}) (map[string]map[string]delegationtype.DelegationAmounts, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), delegationtype.KeyPrefixRestakerDelegationInfo)
iterator := sdk.KVStorePrefixIterator(store, nil)
Expand Down
29 changes: 12 additions & 17 deletions x/delegation/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package keeper
import (
"context"

"github.com/ExocoreNetwork/exocore/x/assets/keeper"
delegationtype "github.com/ExocoreNetwork/exocore/x/delegation/types"
depositkeeper "github.com/ExocoreNetwork/exocore/x/deposit/keeper"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,28 +16,25 @@ type Keeper struct {
cdc codec.BinaryCodec

// other keepers
assetsKeeper keeper.Keeper
depositKeeper depositkeeper.Keeper
slashKeeper delegationtype.ISlashKeeper
expectedOperatorInterface delegationtype.ExpectedOperatorInterface
hooks delegationtype.DelegationHooks
assetsKeeper delegationtype.AssetsKeeper
slashKeeper delegationtype.SlashKeeper
operatorKeeper delegationtype.OperatorKeeper
hooks delegationtype.DelegationHooks
}

func NewKeeper(
storeKey storetypes.StoreKey,
cdc codec.BinaryCodec,
assetsKeeper keeper.Keeper,
depositKeeper depositkeeper.Keeper,
slashKeeper delegationtype.ISlashKeeper,
operatorKeeper delegationtype.ExpectedOperatorInterface,
assetsKeeper delegationtype.AssetsKeeper,
slashKeeper delegationtype.SlashKeeper,
operatorKeeper delegationtype.OperatorKeeper,
) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
assetsKeeper: assetsKeeper,
depositKeeper: depositKeeper,
slashKeeper: slashKeeper,
expectedOperatorInterface: operatorKeeper,
storeKey: storeKey,
cdc: cdc,
assetsKeeper: assetsKeeper,
slashKeeper: slashKeeper,
operatorKeeper: operatorKeeper,
}
}

Expand Down
13 changes: 8 additions & 5 deletions x/delegation/keeper/un_delegation_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (k *Keeper) SetSingleUndelegationRecord(ctx sdk.Context, record *types.Unde
}

// StoreWaitCompleteRecord add it to handle the delay of completing undelegation caused by onHoldCount
func (k Keeper) StoreWaitCompleteRecord(ctx sdk.Context, singleRecKey []byte, record *types.UndelegationRecord) error {
// In the event that the undelegation is held by another module, this function is used within the EndBlocker to increment the scheduled completion block number by 1. Then the completion time of the undelegation will be delayed to the next block.
func (k *Keeper) StoreWaitCompleteRecord(ctx sdk.Context, singleRecKey []byte, record *types.UndelegationRecord) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWaitCompleteUndelegations)
waitCompleteKey := types.GetWaitCompleteRecordKey(record.CompleteBlockNumber, record.LzTxNonce)
store.Set(waitCompleteKey, singleRecKey)
Expand Down Expand Up @@ -151,14 +152,15 @@ func (k *Keeper) GetWaitCompleteUndelegationRecords(ctx sdk.Context, height uint
return k.GetUndelegationRecords(ctx, recordKeys, AllRecords)
}

func (k *Keeper) IncrementUndelegationHoldCount(ctx sdk.Context, recordKey []byte) {
func (k *Keeper) IncrementUndelegationHoldCount(ctx sdk.Context, recordKey []byte) error {
prev := k.GetUndelegationHoldCount(ctx, recordKey)
if prev == math.MaxUint64 {
panic("cannot increment undelegation hold count above max uint64")
return types.ErrCannotIncHoldCount
}
now := prev + 1
store := ctx.KVStore(k.storeKey)
store.Set(types.GetUndelegationOnHoldKey(recordKey), sdk.Uint64ToBigEndian(now))
return nil
}

func (k *Keeper) GetUndelegationHoldCount(ctx sdk.Context, recordKey []byte) uint64 {
Expand All @@ -167,12 +169,13 @@ func (k *Keeper) GetUndelegationHoldCount(ctx sdk.Context, recordKey []byte) uin
return sdk.BigEndianToUint64(bz)
}

func (k *Keeper) DecrementUndelegationHoldCount(ctx sdk.Context, recordKey []byte) {
func (k *Keeper) DecrementUndelegationHoldCount(ctx sdk.Context, recordKey []byte) error {
prev := k.GetUndelegationHoldCount(ctx, recordKey)
if prev == 0 {
panic("cannot decrement undelegation hold count below zero")
return types.ErrCannotDecHoldCount
}
now := prev - 1
store := ctx.KVStore(k.storeKey)
store.Set(types.GetUndelegationOnHoldKey(recordKey), sdk.Uint64ToBigEndian(now))
return nil
}
4 changes: 4 additions & 0 deletions x/delegation/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ var (
ErrNotSupportYet = errorsmod.Register(ModuleName, 9, "don't have supported it yet")

ErrDelegationAmountTooBig = errorsmod.Register(ModuleName, 10, "the delegation amount is bigger than the canWithdraw amount")

ErrCannotIncHoldCount = errorsmod.Register(ModuleName, 11, "cannot increment undelegation hold count above max uint64")

ErrCannotDecHoldCount = errorsmod.Register(ModuleName, 12, "cannot decrement undelegation hold count below zero")
)
Loading

0 comments on commit ddaf1a2

Please sign in to comment.