ExocoreNetwork / exocore

5 stars 9 forks source link

fix(delegation): don't error if no delegations #60

Closed MaxMustermann2 closed 1 month ago

MaxMustermann2 commented 1 month ago

If both the operator and its stakers exit the system, the TotalAmount and TotalShare are 0. In that event, it is not erroneous to return a value of 0 for the share and the amount, respectively.

Updates tests as well

Fixes #59

MaxMustermann2 commented 1 month ago

Test: simulate a panic locally.

diff --git a/x/operator/keeper/abci.go b/x/operator/keeper/abci.go
index 8b39970..3247714 100644
--- a/x/operator/keeper/abci.go
+++ b/x/operator/keeper/abci.go
@@ -119,7 +119,7 @@ func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Valida
    }
    for _, avs := range avsList {
        err = k.UpdateVotingPower(ctx, avs)
-       if err != nil {
+       if err != nil || ctx.BlockHeight() == 3959435 {
            panic(err)
        }
    }
MaxMustermann2 commented 1 month ago

I also simulated the real situation by undelegating first.

diff --git a/x/operator/keeper/abci.go b/x/operator/keeper/abci.go
index 8b39970..3e164ee 100644
--- a/x/operator/keeper/abci.go
+++ b/x/operator/keeper/abci.go
@@ -1,12 +1,16 @@
 package keeper

 import (
+   "math/big"
+
    sdkmath "cosmossdk.io/math"
    assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"
    delegationkeeper "github.com/ExocoreNetwork/exocore/x/delegation/keeper"
+   delegationtypes "github.com/ExocoreNetwork/exocore/x/delegation/types"
    operatortypes "github.com/ExocoreNetwork/exocore/x/operator/types"
    abci "github.com/cometbft/cometbft/abci/types"
    sdk "github.com/cosmos/cosmos-sdk/types"
+   "github.com/ethereum/go-ethereum/common"
 )

 // CalculateUSDValueForOperator calculates the total and self usd value for the
@@ -117,6 +121,33 @@ func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Valida
    if err != nil {
        panic(err)
    }
+   if ctx.BlockHeight() == 3959435 {
+       opAccAddr, _ := sdk.AccAddressFromBech32("exo1lzf5247us2xwhg29mutpx5gyq4aemwqgc08ev7")
+       y, _ := k.assetsKeeper.GetOperatorSpecifiedAssetInfo(
+           ctx, opAccAddr,
+           "0x83e6850591425e3c1e263c054f4466838b9bd9e4_0x9ce1",
+       )
+       ctx.Logger().Error("EndBlock", "before", ctx.BlockHeight(), "amount", y.TotalAmount, "share", y.TotalShare)
+       delegationParams := &delegationtypes.DelegationOrUndelegationParams{}
+       delegationParams.ClientChainLzID = uint64(40161)
+       delegationParams.LzNonce = 1
+       delegationParams.AssetsAddress = common.HexToAddress("0x83e6850591425e3c1e263c054f4466838b9bd9e4").Bytes()
+       delegationParams.StakerAddress = common.HexToAddress("0x6e5ee3e436539f46455b5174411942f520c1120e").Bytes()
+       amount, _ := big.NewInt(0).SetString("1000000000000000000000", 10)
+       delegationParams.OpAmount = sdkmath.NewIntFromBigInt(amount)
+       delegationParams.OperatorAddress = opAccAddr
+       if err := k.delegationKeeper.UndelegateFrom(ctx, delegationParams); err != nil {
+           panic(err)
+       }
+       x, err := k.assetsKeeper.GetOperatorSpecifiedAssetInfo(
+           ctx, opAccAddr,
+           "0x83e6850591425e3c1e263c054f4466838b9bd9e4_0x9ce1",
+       )
+       if err != nil {
+           panic(err)
+       }
+       ctx.Logger().Error("EndBlock", "undelegated", ctx.BlockHeight(), "amount", x.TotalAmount, "share", x.TotalShare)
+   }
    for _, avs := range avsList {
        err = k.UpdateVotingPower(ctx, avs)
        if err != nil {
diff --git a/x/operator/types/expected_keepers.go b/x/operator/types/expected_keepers.go
index 102ba46..f5db77c 100644
--- a/x/operator/types/expected_keepers.go
+++ b/x/operator/types/expected_keepers.go
@@ -76,6 +76,7 @@ type DelegationKeeper interface {
        ctx sdk.Context, isUndelegation bool, operator sdk.AccAddress,
        stakerID, assetID string, share sdkmath.LegacyDec,
    ) (removeToken sdkmath.Int, err error)
+   UndelegateFrom(ctx sdk.Context, params *delegationtype.DelegationOrUndelegationParams) error
 }

 type PriceChange struct {
cloud8little commented 1 month ago

Test passed with the commit: https://github.com/ExocoreNetwork/exocore/pull/60/commits/75036f15c225b6eeabad43d62b30eb30e8bfe178

the test chain successfully resume and start consensus from height 3980240 with the new binary, and checked the delegation information of the test staker, it is now zero.

delegation_infos:
  exo13hasr43vvq8v44xpzh0l6yuym4kca98f87j7ac:
    undelegatable_share: "0.000000000000000000"
    wait_undelegation_amount: "0"