Skip to content

Commit

Permalink
moved state irrelevant methods to types; renaming and cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed May 3, 2024
1 parent e5c4c1f commit 77acc5b
Show file tree
Hide file tree
Showing 14 changed files with 446 additions and 412 deletions.
3 changes: 1 addition & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
### Refactor

* [2032](https://github.com/zeta-chain/node/pull/2032) - improve some general structure of the ZetaClient codebase

* [2110](https://github.com/zeta-chain/node/pull/2110) - move non-query rate limiter logic to zetaclient side and code refactor.

## v16.0.0

Expand Down Expand Up @@ -57,7 +57,6 @@
* [2013](https://github.com/zeta-chain/node/pull/2013) - rename `GasPriceVoter` message to `VoteGasPrice`
* [2059](https://github.com/zeta-chain/node/pull/2059) - Remove unused params from all functions in zetanode
* [2076](https://github.com/zeta-chain/node/pull/2076) - automatically deposit native zeta to an address if it doesn't exist on ZEVM.
* [2110](https://github.com/zeta-chain/node/pull/2110) - move non-query rate limiter logic to zetaclient side and code refactor.
* [2071](https://github.com/zeta-chain/node/pull/2071) - Modify chains struct to add all chain related information

### Features
Expand Down
1 change: 0 additions & 1 deletion cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
e2etests.TestUpdateBytecodeZRC20Name,
e2etests.TestUpdateBytecodeConnectorName,
e2etests.TestDepositEtherLiquidityCapName,
e2etests.TestRateLimiterName,

// TestMigrateChainSupportName tests EVM chain migration. Currently this test doesn't work with Anvil because pre-EIP1559 txs are not supported
// See issue below for details
Expand Down
26 changes: 24 additions & 2 deletions testutil/sample/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -50,10 +51,20 @@ func RateLimiterFlags() types.RateLimiterFlags {
}
}

func AssetRate() *types.AssetRate {
// CustomRateLimiterFlags creates a custom rate limiter flags with the given parameters
func CustomRateLimiterFlags(enabled bool, window int64, rate math.Uint, conversions []types.Conversion) types.RateLimiterFlags {
return types.RateLimiterFlags{
Enabled: enabled,
Window: window,
Rate: rate,
Conversions: conversions,
}
}

func AssetRate() types.AssetRate {
r := Rand()

return &types.AssetRate{
return types.AssetRate{
ChainId: r.Int63(),
Asset: EthAddress().Hex(),
Decimals: uint32(r.Uint64()),
Expand All @@ -62,6 +73,17 @@ func AssetRate() *types.AssetRate {
}
}

// CustomAssetRate creates a custom asset rate with the given parameters
func CustomAssetRate(chainID int64, asset string, decimals uint32, coinType coin.CoinType, rate sdk.Dec) types.AssetRate {
return types.AssetRate{
ChainId: chainID,
Asset: strings.ToLower(asset),
Decimals: decimals,
CoinType: coinType,
Rate: rate,
}
}

func OutTxTracker(t *testing.T, index string) types.OutTxTracker {
r := newRandFromStringSeed(t, index)

Expand Down
105 changes: 14 additions & 91 deletions x/crosschain/keeper/grpc_query_cctx_rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package keeper
import (
"context"
"sort"
"strings"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/pkg/coin"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -66,8 +64,11 @@ func (k Keeper) RateLimiterInput(c context.Context, req *types.QueryRateLimiterI

// get foreign chains and conversion rates of foreign coins
chains := k.zetaObserverKeeper.GetSupportedForeignChains(ctx)
assetRates := k.GetRateLimiterAssetRateList(ctx)
gasAssetRateMap, erc20AssetRateMap := BuildAssetRateMapFromList(assetRates)
_, assetRates, found := k.GetRateLimiterAssetRateList(ctx)
if !found {
return nil, status.Error(codes.Internal, "asset rates not found")
}
gasAssetRateMap, erc20AssetRateMap := types.BuildAssetRateMapFromList(assetRates)

// query pending nonces of each foreign chain and get the lowest height of the pending cctxs
lowestPendingCctxHeight := int64(0)
Expand Down Expand Up @@ -129,7 +130,7 @@ func (k Keeper) RateLimiterInput(c context.Context, req *types.QueryRateLimiterI

// sum up past cctxs' value within window
if inWindow && isPast {
pastCctxsValue = pastCctxsValue.Add(ConvertCctxValue(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap))
pastCctxsValue = pastCctxsValue.Add(types.ConvertCctxValue(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap))
}

// add cctx to corresponding list
Expand All @@ -140,7 +141,7 @@ func (k Keeper) RateLimiterInput(c context.Context, req *types.QueryRateLimiterI
} else {
cctxsPending = append(cctxsPending, cctx)
// sum up non-past pending cctxs' value
pendingCctxsValue = pendingCctxsValue.Add(ConvertCctxValue(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap))
pendingCctxsValue = pendingCctxsValue.Add(types.ConvertCctxValue(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap))
}
}
}
Expand Down Expand Up @@ -191,7 +192,7 @@ func (k Keeper) ListPendingCctxWithinRateLimit(c context.Context, req *types.Que

// check rate limit flags to decide if we should apply rate limit
applyLimit := true
rateLimitFlags, found := k.GetRateLimiterFlags(ctx)
rateLimitFlags, assetRates, found := k.GetRateLimiterAssetRateList(ctx)
if !found || !rateLimitFlags.Enabled {
applyLimit = false
}
Expand Down Expand Up @@ -231,19 +232,10 @@ func (k Keeper) ListPendingCctxWithinRateLimit(c context.Context, req *types.Que
leftWindowBoundary = 0
}

// get the conversion rates for all foreign coins
var gasAssetRateMap map[int64]*types.AssetRate
var erc20AssetRateMap map[int64]map[string]*types.AssetRate
var blockLimitInAzeta sdkmath.Int
var windowLimitInAzeta sdkmath.Int
if applyLimit {
assetRates := k.GetRateLimiterAssetRateList(ctx)
gasAssetRateMap, erc20AssetRateMap = BuildAssetRateMapFromList(assetRates)

// initiate block limit and window limit in azeta
blockLimitInAzeta = sdkmath.NewIntFromBigInt(rateLimitFlags.Rate.BigInt())
windowLimitInAzeta = blockLimitInAzeta.Mul(sdkmath.NewInt(rateLimitFlags.Window))
}
// initiate block limit and window limit in azeta; build asset rate maps
blockLimitInAzeta := sdkmath.NewIntFromBigInt(rateLimitFlags.Rate.BigInt())
windowLimitInAzeta := blockLimitInAzeta.Mul(sdkmath.NewInt(rateLimitFlags.Window))
gasAssetRateMap, erc20AssetRateMap := types.BuildAssetRateMapFromList(assetRates)

// the criteria to stop adding cctxs to the rpc response
maxCCTXsReached := func(cctxs []*types.CrossChainTx) bool {
Expand Down Expand Up @@ -322,7 +314,7 @@ func (k Keeper) ListPendingCctxWithinRateLimit(c context.Context, req *types.Que
break
}
// skip the cctx if rate limit is exceeded but still accumulate the total withdraw value
if inWindow && rateLimitExceeded(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap, &totalWithdrawInAzeta, withdrawLimitInAzeta) {
if inWindow && types.RateLimitExceeded(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap, &totalWithdrawInAzeta, withdrawLimitInAzeta) {
limitExceeded = true
continue
}
Expand Down Expand Up @@ -355,7 +347,7 @@ func (k Keeper) ListPendingCctxWithinRateLimit(c context.Context, req *types.Que
}

// skip the cctx if rate limit is exceeded but still accumulate the total withdraw value
if rateLimitExceeded(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap, &totalWithdrawInAzeta, withdrawLimitInAzeta) {
if types.RateLimitExceeded(chain.ChainId, cctx, gasAssetRateMap, erc20AssetRateMap, &totalWithdrawInAzeta, withdrawLimitInAzeta) {
limitExceeded = true
continue
}
Expand Down Expand Up @@ -388,72 +380,3 @@ func (k Keeper) ListPendingCctxWithinRateLimit(c context.Context, req *types.Que
RateLimitExceeded: limitExceeded,
}, nil
}

// ConvertCctxValue converts the value of the cctx to azeta using given conversion rates
func ConvertCctxValue(
chainID int64,
cctx *types.CrossChainTx,
gasAssetRateMap map[int64]*types.AssetRate,
erc20AssetRateMap map[int64]map[string]*types.AssetRate,
) sdkmath.Int {
var rate sdk.Dec
var decimals uint32
switch cctx.InboundTxParams.CoinType {
case coin.CoinType_Zeta:
// no conversion needed for ZETA
return sdk.NewIntFromBigInt(cctx.GetCurrentOutTxParam().Amount.BigInt())
case coin.CoinType_Gas:
assetRate, found := gasAssetRateMap[chainID]
if !found {
// skip if no rate found for gas coin on this chainID
return sdk.NewInt(0)
}
rate = assetRate.Rate
decimals = assetRate.Decimals
case coin.CoinType_ERC20:
// get the ERC20 coin rate
_, found := erc20AssetRateMap[chainID]
if !found {
// skip if no rate found for this chainID
return sdk.NewInt(0)
}
assetRate := erc20AssetRateMap[chainID][strings.ToLower(cctx.InboundTxParams.Asset)]
rate = assetRate.Rate
decimals = assetRate.Decimals
default:
// skip CoinType_Cmd
return sdk.NewInt(0)
}
// should not happen, return 0 to skip if it happens
if rate.IsNil() || rate.LTE(sdk.NewDec(0)) {
return sdkmath.NewInt(0)
}

// the whole coin amounts of zeta and zrc20
// given decimals = 6, the amount will be 10^6 = 1000000
oneZeta := coin.AzetaPerZeta()
oneZrc20 := sdk.NewDec(10).Power(uint64(decimals))

// convert cctx asset amount into azeta amount
// given amountCctx = 2000000, rate = 0.8, decimals = 6
// amountCctxDec: 2000000 * 0.8 = 1600000.0
// amountAzetaDec: 1600000.0 * 10e18 / 10e6 = 1600000000000000000.0
amountCctxDec := sdk.NewDecFromBigInt(cctx.GetCurrentOutTxParam().Amount.BigInt())
amountAzetaDec := amountCctxDec.Mul(rate).Mul(oneZeta).Quo(oneZrc20)
return amountAzetaDec.TruncateInt()
}

// rateLimitExceeded accumulates the cctx value and then checks if the rate limit is exceeded
// returns true if the rate limit is exceeded
func rateLimitExceeded(
chainID int64,
cctx *types.CrossChainTx,
gasAssetRateMap map[int64]*types.AssetRate,
erc20AssetRateMap map[int64]map[string]*types.AssetRate,
currentCctxValue *sdkmath.Int,
withdrawLimitInAzeta sdkmath.Int,
) bool {
amountZeta := ConvertCctxValue(chainID, cctx, gasAssetRateMap, erc20AssetRateMap)
*currentCctxValue = currentCctxValue.Add(amountZeta)
return currentCctxValue.GT(withdrawLimitInAzeta)
}
Loading

0 comments on commit 77acc5b

Please sign in to comment.