Skip to content

Commit

Permalink
perf: cache origin token amount during crosschain (#755)
Browse files Browse the repository at this point in the history
Co-authored-by: nulnut <[email protected]>
  • Loading branch information
zakir-code and nulnut authored Oct 18, 2024
1 parent 0a123f8 commit 6a7784a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
27 changes: 17 additions & 10 deletions x/crosschain/keeper/bridge_call_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ func (k Keeper) BridgeCallBaseCoin(
fxTarget *types.FxTarget,
originTokenAmount sdkmath.Int,
) (uint64, error) {
var cacheKey string
var nonce uint64
if fxTarget.IsIBC() {
if !coins.IsValid() || len(coins) != 1 {
return 0, sdkerrors.ErrInvalidCoins.Wrapf("ibc transfer with coins: %s", coins.String())
Expand All @@ -260,20 +262,25 @@ func (k Keeper) BridgeCallBaseCoin(
if err != nil {
return 0, err
}
sequence, err := k.IBCTransfer(ctx, from.Bytes(), toAddr, ibcCoin, fxTarget.IBCChannel, string(memo))
nonce, err = k.IBCTransfer(ctx, from.Bytes(), toAddr, ibcCoin, fxTarget.IBCChannel, string(memo))
if err != nil {
return 0, err
}
if originTokenAmount.IsPositive() {
ibcTransferKey := types.NewIBCTransferKey(fxTarget.IBCChannel, sequence)
if err = k.erc20Keeper.SetCache(ctx, ibcTransferKey); err != nil {
return 0, err
}
cacheKey = types.NewIBCTransferKey(fxTarget.IBCChannel, nonce)
} else {
var err error
if nonce, err = k.AddOutgoingBridgeCall(ctx, from, refund, coins, to, data, memo, 0); err != nil {
return 0, err
}
cacheKey = types.NewOriginTokenKey(k.moduleName, nonce)
}

if originTokenAmount.IsPositive() {
if err := k.erc20Keeper.SetCache(ctx, cacheKey, originTokenAmount); err != nil {
return 0, err
}
return sequence, nil
}
// todo record origin amount
return k.AddOutgoingBridgeCall(ctx, from, refund, coins, to, data, memo, 0)
return nonce, nil
}

func (k Keeper) CrosschainBaseCoin(
Expand Down Expand Up @@ -301,7 +308,7 @@ func (k Keeper) CrosschainBaseCoin(
}

if originToken {
return k.erc20Keeper.SetCache(ctx, cacheKey)
return k.erc20Keeper.SetCache(ctx, cacheKey, amount.Amount.Add(fee.Amount))
}
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions x/crosschain/mock/expected_keepers_mocks.go

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

3 changes: 2 additions & 1 deletion x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"

sdkmath "cosmossdk.io/math"
tmbytes "github.com/cometbft/cometbft/libs/bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand Down Expand Up @@ -46,7 +47,7 @@ type Erc20Keeper interface {
BaseCoinToEvm(ctx context.Context, holder common.Address, coin sdk.Coin) (string, error)

HasCache(ctx context.Context, key string) (bool, error)
SetCache(ctx context.Context, key string) error
SetCache(ctx context.Context, key string, amount sdkmath.Int) error
DeleteCache(ctx context.Context, key string) error

HasToken(ctx context.Context, token string) (bool, error)
Expand Down
6 changes: 3 additions & 3 deletions x/erc20/keeper/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package keeper
import (
"context"

"cosmossdk.io/collections"
sdkmath "cosmossdk.io/math"
)

func (k Keeper) HasCache(ctx context.Context, key string) (bool, error) {
return k.Cache.Has(ctx, key)
}

func (k Keeper) SetCache(ctx context.Context, key string) error {
return k.Cache.Set(ctx, key, collections.NoValue{})
func (k Keeper) SetCache(ctx context.Context, key string, amount sdkmath.Int) error {
return k.Cache.Set(ctx, key, amount)
}

func (k Keeper) DeleteCache(ctx context.Context, key string) error {
Expand Down
5 changes: 3 additions & 2 deletions x/erc20/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"cosmossdk.io/collections"
"cosmossdk.io/core/store"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -37,7 +38,7 @@ type Keeper struct {
BridgeToken collections.Map[collections.Pair[string, string], types.BridgeToken] // baseDenom -> BridgeToken
IBCToken collections.Map[collections.Pair[string, string], types.IBCToken] // baseDenom -> IBCToken
DenomIndex collections.Map[string, string] // bridgeDenom/erc20_contract/ibc_denom -> baseDenom
Cache collections.Map[string, collections.NoValue] // crosschain cache
Cache collections.Map[string, sdkmath.Int] // crosschain cache
}

// NewKeeper creates new instances of the erc20 Keeper
Expand Down Expand Up @@ -70,7 +71,7 @@ func NewKeeper(
BridgeToken: collections.NewMap(sb, types.BridgeTokenKey, "bridge_token", collections.PairKeyCodec(collections.StringKey, collections.StringKey), codec.CollValue[types.BridgeToken](cdc)),
IBCToken: collections.NewMap(sb, types.IBCTokenKey, "ibc_token", collections.PairKeyCodec(collections.StringKey, collections.StringKey), codec.CollValue[types.IBCToken](cdc)),
DenomIndex: collections.NewMap(sb, types.DenomIndexKey, "denom_index", collections.StringKey, collections.StringValue),
Cache: collections.NewMap(sb, types.CacheKey, "cache", collections.StringKey, collections.NoValue{}),
Cache: collections.NewMap(sb, types.CacheKey, "cache", collections.StringKey, sdk.IntValue),
}
schema, err := sb.Build()
if err != nil {
Expand Down

0 comments on commit 6a7784a

Please sign in to comment.