From a4d13e6833f258998b731828ba35d3fdac32bd95 Mon Sep 17 00:00:00 2001 From: leonz789 Date: Thu, 29 Aug 2024 21:07:18 +0800 Subject: [PATCH] lint --- x/assets/keeper/bank.go | 3 +-- x/delegation/keeper/abci.go | 5 ++--- x/oracle/keeper/native_token.go | 24 ++++++++++++------------ x/oracle/keeper/prices.go | 6 ++++-- x/oracle/types/errors.go | 14 ++++++++------ 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/x/assets/keeper/bank.go b/x/assets/keeper/bank.go index 23f064440..cec14d7a8 100644 --- a/x/assets/keeper/bank.go +++ b/x/assets/keeper/bank.go @@ -6,7 +6,6 @@ import ( errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" - "github.com/ExocoreNetwork/exocore/x/assets/types" assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -46,7 +45,7 @@ func (k Keeper) PerformDepositOrWithdraw(ctx sdk.Context, params *DepositWithdra return errorsmod.Wrapf(assetstypes.ErrInvalidOperationType, "the operation type is: %v", params.Action) } - if types.IsNativeToken(assetID) { + if assetstypes.IsNativeToken(assetID) { // TODO: we skip check for case like withdraw amount>withdrawable is fine since it will fail for later check and the state will be rollback actualOpAmount = k.UpdateNativeTokenByDepositOrWithdraw(ctx, assetID, hexutil.Encode(params.StakerAddress), params.OpAmount) } diff --git a/x/delegation/keeper/abci.go b/x/delegation/keeper/abci.go index aa7a7e8f7..c71960d60 100644 --- a/x/delegation/keeper/abci.go +++ b/x/delegation/keeper/abci.go @@ -45,9 +45,8 @@ func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Valida panic(err) } // and the other is the fact that it matures at the next block - err = k.StorePendingUndelegationRecord(ctx, recordKey, record) - //TODO: remove previous index with currentHeight for pendingUndelegationRecord - if err != nil { + if err = k.StorePendingUndelegationRecord(ctx, recordKey, record); err != nil { + // TODO: remove previous index with currentHeight for pendingUndelegationRecord panic(err) } continue diff --git a/x/oracle/keeper/native_token.go b/x/oracle/keeper/native_token.go index 7e7905d7c..473613fb7 100644 --- a/x/oracle/keeper/native_token.go +++ b/x/oracle/keeper/native_token.go @@ -206,19 +206,19 @@ func (k Keeper) GetNativeTokenPriceUSDForOperator(ctx sdk.Context, assetID strin store := ctx.KVStore(k.storeKey) key := types.NativeTokenOperatorKey(assetID, operatorAddr) - if value := store.Get(key); value == nil { + value := store.Get(key) + if value == nil { + return types.Price{}, types.ErrGetPriceAssetNotFound + } + operatorInfo := &types.OperatorInfo{} + k.cdc.MustUnmarshal(value, operatorInfo) + baseTokenUSDPrice, err := k.GetSpecifiedAssetsPrice(ctx, assetstypes.GetBaseTokenForNativeToken(assetID)) + if err != nil { return types.Price{}, types.ErrGetPriceAssetNotFound - } else { - operatorInfo := &types.OperatorInfo{} - k.cdc.MustUnmarshal(value, operatorInfo) - baseTokenUSDPrice, err := k.GetSpecifiedAssetsPrice(ctx, assetstypes.GetBaseTokenForNativeToken(assetID)) - if err != nil { - return types.Price{}, types.ErrGetPriceAssetNotFound - } - operatorPriceFloat := getLatestOperatorPriceFloat(operatorInfo) - baseTokenUSDPrice.Value = (baseTokenUSDPrice.Value.ToLegacyDec().Mul(operatorPriceFloat)).RoundInt() - return baseTokenUSDPrice, nil } + operatorPriceFloat := getLatestOperatorPriceFloat(operatorInfo) + baseTokenUSDPrice.Value = (baseTokenUSDPrice.Value.ToLegacyDec().Mul(operatorPriceFloat)).RoundInt() + return baseTokenUSDPrice, nil } func (k Keeper) GetStakerList(ctx sdk.Context, assetID string) types.StakerList { @@ -340,7 +340,7 @@ func parseBalanceChange(rawData []byte, sl types.StakerList) (map[string]int, er bitsExtracted := 0 stakerChange := 0 - for bitsExtracted < int(lenValue) { //0<8, offset: + for bitsExtracted < int(lenValue) { bitsLeft := 8 - bitOffset byteValue := changes[byteIndex] << bitOffset if (int(lenValue) - bitsExtracted) < bitsLeft { diff --git a/x/oracle/keeper/prices.go b/x/oracle/keeper/prices.go index 7658c5ffd..ef350c569 100644 --- a/x/oracle/keeper/prices.go +++ b/x/oracle/keeper/prices.go @@ -207,7 +207,10 @@ func (k Keeper) AppendPriceTR(ctx sdk.Context, tokenID uint64, priceTR types.Pri assetIDs := p.GetAssetIDsFromTokenID(tokenID) for _, assetID := range assetIDs { if assetstypes.IsNativeToken(assetID) { - k.UpdateNativeTokenByBalanceChange(ctx, assetID, []byte(priceTR.Price), roundID) + if err := k.UpdateNativeTokenByBalanceChange(ctx, assetID, []byte(priceTR.Price), roundID); err != nil { + // we just report this error in log to notify validators + k.Logger(ctx).Error(types.ErrUpdateNativeTokenVirtualPriceFail.Error(), "error", err) + } } } @@ -216,7 +219,6 @@ func (k Keeper) AppendPriceTR(ctx sdk.Context, tokenID uint64, priceTR types.Pri // GrowRoundID Increases roundID with the previous price func (k Keeper) GrowRoundID(ctx sdk.Context, tokenID uint64) (price string, roundID uint64) { - // logInfo := fmt.Sprintf("add new round with previous price under fail aggregation, tokenID:%d", tokenID) if pTR, ok := k.GetPriceTRLatest(ctx, tokenID); ok { pTR.RoundID++ k.AppendPriceTR(ctx, tokenID, pTR) diff --git a/x/oracle/types/errors.go b/x/oracle/types/errors.go index d45033ce4..0c3183507 100644 --- a/x/oracle/types/errors.go +++ b/x/oracle/types/errors.go @@ -13,14 +13,16 @@ const ( invalidParams getPriceFailedAssetNotFound getPriceFailedRoundNotFound + updateNativeTokenVirtualPriceFail ) // x/oracle module sentinel errors var ( - ErrInvalidMsg = sdkerrors.Register(ModuleName, invalidMsg, "invalid input create price") - ErrPriceProposalIgnored = sdkerrors.Register(ModuleName, priceProposalIgnored, "price proposal ignored") - ErrPriceProposalFormatInvalid = sdkerrors.Register(ModuleName, priceProposalFormatInvalid, "price proposal message format invalid") - ErrInvalidParams = sdkerrors.Register(ModuleName, invalidParams, "invalid params") - ErrGetPriceAssetNotFound = sdkerrors.Register(ModuleName, getPriceFailedAssetNotFound, "get price failed for asset not found") - ErrGetPriceRoundNotFound = sdkerrors.Register(ModuleName, getPriceFailedRoundNotFound, "get price failed for round not found") + ErrInvalidMsg = sdkerrors.Register(ModuleName, invalidMsg, "invalid input create price") + ErrPriceProposalIgnored = sdkerrors.Register(ModuleName, priceProposalIgnored, "price proposal ignored") + ErrPriceProposalFormatInvalid = sdkerrors.Register(ModuleName, priceProposalFormatInvalid, "price proposal message format invalid") + ErrInvalidParams = sdkerrors.Register(ModuleName, invalidParams, "invalid params") + ErrGetPriceAssetNotFound = sdkerrors.Register(ModuleName, getPriceFailedAssetNotFound, "get price failed for asset not found") + ErrGetPriceRoundNotFound = sdkerrors.Register(ModuleName, getPriceFailedRoundNotFound, "get price failed for round not found") + ErrUpdateNativeTokenVirtualPriceFail = sdkerrors.Register(ModuleName, updateNativeTokenVirtualPriceFail, "update native token balance change failed") )