From f9338bfdbe279c4c5a6a4cb6f02fd94502953864 Mon Sep 17 00:00:00 2001 From: MaxMustermann2 <82761650+MaxMustermann2@users.noreply.github.com> Date: Fri, 11 Oct 2024 03:38:11 +0000 Subject: [PATCH] fix: error duplicate asset registration --- precompiles/assets/tx.go | 13 ++--------- x/assets/keeper/client_chain_asset.go | 32 ++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/precompiles/assets/tx.go b/precompiles/assets/tx.go index 4a220fb57..fd5073ac8 100644 --- a/precompiles/assets/tx.go +++ b/precompiles/assets/tx.go @@ -189,18 +189,9 @@ func (p Precompile) UpdateToken( return nil, err } - // check that the asset being updated actually exists _, assetID := assetstypes.GetStakerIDAndAssetIDFromStr(uint64(clientChainID), "", hexAssetAddr) - assetInfo, err := p.assetsKeeper.GetStakingAssetInfo(ctx, assetID) - if err != nil { - // fails if asset does not exist with ErrNoClientChainAssetKey - return nil, err - } - - // finally, execute the update - assetInfo.AssetBasicInfo.MetaInfo = metadata - - if err := p.assetsKeeper.SetStakingAssetInfo(ctx, assetInfo); err != nil { + // this verifies the existence of the asset and returns an error if it doesn't exist + if err := p.assetsKeeper.UpdateStakingAssetMetaInfo(ctx, assetID, metadata); err != nil { return nil, err } diff --git a/x/assets/keeper/client_chain_asset.go b/x/assets/keeper/client_chain_asset.go index 5fd3cb924..4aa9c1fcf 100644 --- a/x/assets/keeper/client_chain_asset.go +++ b/x/assets/keeper/client_chain_asset.go @@ -35,8 +35,9 @@ func (k Keeper) UpdateStakingAssetTotalAmount(ctx sdk.Context, assetID string, c } // SetStakingAssetInfo todo: Temporarily use clientChainAssetAddr+'_'+LayerZeroChainID as the key. -// It provides a function to register the client chain assets supported by exoCore.It's called by genesis configuration now,however it will be called by the governance in the future -// The caller is responsible for ensuring that no such asset already exists (if a new asset is being created) +// It provides a function to register the client chain assets supported by Exocore. +// New assets may be registered via ExocoreGateway, which uses precompiles to call this function. +// If an asset with the provided assetID already exists, it will return an error. func (k Keeper) SetStakingAssetInfo(ctx sdk.Context, info *assetstype.StakingAssetInfo) (err error) { if info.AssetBasicInfo.Decimals > assetstype.MaxDecimal { return errorsmod.Wrapf(assetstype.ErrInvalidInputParameter, "the decimal is greater than the MaxDecimal,decimal:%v,MaxDecimal:%v", info.AssetBasicInfo.Decimals, assetstype.MaxDecimal) @@ -45,19 +46,39 @@ func (k Keeper) SetStakingAssetInfo(ctx sdk.Context, info *assetstype.StakingAss return errorsmod.Wrapf(assetstype.ErrInvalidInputParameter, "the total staking amount is negative, StakingTotalAmount:%v", info.StakingTotalAmount) } store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo) - // key := common.HexToAddress(incentive.Contract) - bz := k.cdc.MustMarshal(info) - _, assetID := assetstype.GetStakerIDAndAssetIDFromStr(info.AssetBasicInfo.LayerZeroChainID, "", info.AssetBasicInfo.Address) + if k.IsStakingAsset(ctx, assetID) { + return assetstype.ErrInvalidInputParameter.Wrapf( + "the asset has already been registered,assetID:%v,LayerZeroChainID:%v,ClientChainAssetAddr:%v", + assetID, info.AssetBasicInfo.LayerZeroChainID, info.AssetBasicInfo.Address, + ) + } + bz := k.cdc.MustMarshal(info) store.Set([]byte(assetID), bz) return nil } +// IsStakingAsset checks if the assetID is a staking asset. func (k Keeper) IsStakingAsset(ctx sdk.Context, assetID string) bool { store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo) return store.Has([]byte(assetID)) } +// UpdateStakingAssetMetaInfo updates the meta information stored against a provided assetID. +// If the assetID does not exist, it returns an error. +func (k Keeper) UpdateStakingAssetMetaInfo(ctx sdk.Context, assetID string, metainfo string) error { + info, err := k.GetStakingAssetInfo(ctx, assetID) + if err != nil { + return err + } + store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo) + info.AssetBasicInfo.MetaInfo = metainfo + bz := k.cdc.MustMarshal(info) + store.Set([]byte(assetID), bz) + return nil +} + +// GetStakingAssetInfo returns the asset information stored against a provided assetID. func (k Keeper) GetStakingAssetInfo(ctx sdk.Context, assetID string) (info *assetstype.StakingAssetInfo, err error) { store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo) value := store.Get([]byte(assetID)) @@ -70,6 +91,7 @@ func (k Keeper) GetStakingAssetInfo(ctx sdk.Context, assetID string) (info *asse return &ret, nil } +// GetAssetsDecimal returns the decimal of all the provided assets. func (k Keeper) GetAssetsDecimal(ctx sdk.Context, assets map[string]interface{}) (decimals map[string]uint32, err error) { if assets == nil { return nil, errorsmod.Wrap(assetstype.ErrInputPointerIsNil, "assets is nil")