Skip to content

feat(nexus)!: set rate limit during asset registration #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
16ab26a
ibc v3
milapsheth Oct 24, 2022
d92d0d3
update protos
milapsheth Oct 24, 2022
1e9ef26
update swagger
milapsheth Oct 24, 2022
414937f
update makefile
milapsheth Oct 24, 2022
9bac921
Merge branch 'main' into ibc-v3
milapsheth Oct 24, 2022
e459dbb
update docs
milapsheth Oct 25, 2022
772de32
feat(nexus)!: add rate limits to transfers
milapsheth Oct 26, 2022
1d1fbe6
Merge branch 'main' into ibc-rate-limit
milapsheth Oct 26, 2022
d5dd344
format proto
milapsheth Oct 26, 2022
ab0bfcc
Merge branch 'main' into ibc-rate-limit
milapsheth Oct 26, 2022
3f2e5cd
Merge branch 'main' into ibc-rate-limit
milapsheth Oct 26, 2022
de51656
chore: lint and add docs
milapsheth Oct 26, 2022
aefba2b
refactor rate limits
milapsheth Oct 27, 2022
704a111
Merge branch 'main' into ibc-rate-limit
milapsheth Oct 27, 2022
d46c248
add log
milapsheth Oct 27, 2022
77b7dcc
proto-gen
milapsheth Oct 27, 2022
172ffcf
address comments
milapsheth Oct 27, 2022
49c46f5
feat(nexus)!: set rate limit during asset registration
milapsheth Oct 27, 2022
f795a4e
remove helper
milapsheth Oct 27, 2022
1c481f7
Merge branch 'ibc-rate-limit' into register-rate-limit
milapsheth Oct 27, 2022
a75a613
address comments
milapsheth Oct 27, 2022
5e06219
Update x/nexus/keeper/chain.go
milapsheth Oct 27, 2022
53ffafa
use enum
milapsheth Oct 28, 2022
e92de88
addresses comments
milapsheth Oct 31, 2022
f484bbc
Merge branch 'main' into ibc-rate-limit
milapsheth Oct 31, 2022
1b24d8d
move TransferFlow to exported
milapsheth Oct 31, 2022
0e58c98
Merge branch 'ibc-rate-limit' into register-rate-limit
milapsheth Oct 31, 2022
2b1e78c
Merge branch 'main' into ibc-rate-limit
milapsheth Nov 3, 2022
dd93d2a
rename TransferFlow to TransferDirection
milapsheth Nov 3, 2022
6efae9e
rename TransferRate to TransferEpoch
milapsheth Nov 3, 2022
6eec981
revert error log
milapsheth Nov 3, 2022
fec60db
address comments
milapsheth Nov 3, 2022
d435370
Merge branch 'ibc-rate-limit' into register-rate-limit
milapsheth Nov 3, 2022
7c59208
refactor test utils
milapsheth Nov 3, 2022
c8c1d23
Merge branch 'ibc-rate-limit' into register-rate-limit
milapsheth Nov 3, 2022
3bfbfba
use max uint as default limit
milapsheth Nov 4, 2022
0c3600e
goimport
milapsheth Nov 4, 2022
919b711
Merge branch 'ibc-rate-limit' into register-rate-limit
milapsheth Nov 4, 2022
af66bbc
missing file
milapsheth Nov 4, 2022
2f4a952
add unit tests
milapsheth Nov 4, 2022
4da0578
Merge branch 'ibc-rate-limit' into register-rate-limit
milapsheth Nov 4, 2022
b3d1e42
Merge branch 'main' into register-rate-limit
milapsheth Nov 4, 2022
c3d20b3
fix test
milapsheth Nov 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions utils/int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/math"
)

var (
// MaxUint specifies the max sdk.Uint value
MaxUint = sdk.NewUintFromBigInt(math.MaxBig256)
)
22 changes: 22 additions & 0 deletions utils/int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils_test

import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"

"github.com/axelarnetwork/axelar-core/utils"
)

func TestMaxUint(t *testing.T) {
x := utils.MaxUint
assert.Equal(t, 256, x.BigInt().BitLen())
assert.Panics(t, func() { x.AddUint64(1) })
assert.Equal(t, 1, x.BigInt().Sign())

y := big.NewInt(1)
y.Lsh(y, 256)
y.Sub(y, big.NewInt(1))
assert.Equal(t, y, x.BigInt())
}
14 changes: 10 additions & 4 deletions x/axelarnet/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"fmt"
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"

"github.com/axelarnetwork/axelar-core/utils"
"github.com/axelarnetwork/axelar-core/utils/events"
"github.com/axelarnetwork/axelar-core/x/axelarnet/exported"
"github.com/axelarnetwork/axelar-core/x/axelarnet/types"
Expand All @@ -19,6 +21,10 @@ import (

var _ types.MsgServiceServer = msgServer{}

const (
defaultRateLimitWindow = 6 * time.Hour
)

type msgServer struct {
Keeper
nexus types.Nexus
Expand Down Expand Up @@ -237,12 +243,12 @@ func (s msgServer) AddCosmosBasedChain(c context.Context, req *types.AddCosmosBa

// register asset in chain state
for _, asset := range req.NativeAssets {
if err := s.nexus.RegisterAsset(ctx, chain, asset); err != nil {
if err := s.nexus.RegisterAsset(ctx, chain, asset, utils.MaxUint, defaultRateLimitWindow); err != nil {
return nil, err
}

// also register on axelarnet, it routes assets from cosmos chains to evm chains
if err := s.nexus.RegisterAsset(ctx, exported.Axelarnet, nexus.NewAsset(asset.Denom, false)); err != nil {
if err := s.nexus.RegisterAsset(ctx, exported.Axelarnet, nexus.NewAsset(asset.Denom, false), utils.MaxUint, defaultRateLimitWindow); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -272,14 +278,14 @@ func (s msgServer) RegisterAsset(c context.Context, req *types.RegisterAssetRequ
}

// register asset in chain state
err := s.nexus.RegisterAsset(ctx, chain, req.Asset)
err := s.nexus.RegisterAsset(ctx, chain, req.Asset, utils.MaxUint, defaultRateLimitWindow)
if err != nil {
return nil, err
}

// also register on axelarnet, it routes assets from cosmos chains to evm chains
// ignore the error in case above chain is axelarnet, or if the asset is already registered
_ = s.nexus.RegisterAsset(ctx, exported.Axelarnet, nexus.NewAsset(req.Asset.Denom, false))
_ = s.nexus.RegisterAsset(ctx, exported.Axelarnet, nexus.NewAsset(req.Asset.Denom, false), utils.MaxUint, defaultRateLimitWindow)

return &types.RegisterAssetResponse{}, nil
}
Expand Down
9 changes: 6 additions & 3 deletions x/axelarnet/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
mathRand "math/rand"
"strings"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -843,7 +844,9 @@ func TestAddCosmosBasedChain(t *testing.T) {
GetChainFunc: func(ctx sdk.Context, chain nexus.ChainName) (nexus.Chain, bool) { return nexus.Chain{}, false },
GetChainByNativeAssetFunc: func(ctx sdk.Context, asset string) (nexus.Chain, bool) { return nexus.Chain{}, false },
SetChainFunc: func(ctx sdk.Context, chain nexus.Chain) {},
RegisterAssetFunc: func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset) error { return nil },
RegisterAssetFunc: func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset, limit sdk.Uint, window time.Duration) error {
return nil
},
}
ibcK := keeper.NewIBCKeeper(k, &mock.IBCTransferKeeperMock{}, &mock.ChannelKeeperMock{})
server = keeper.NewMsgServerImpl(k, nexusK, &mock.BankKeeperMock{}, &mock.AccountKeeperMock{}, ibcK)
Expand Down Expand Up @@ -929,15 +932,15 @@ func TestAddCosmosBasedChain(t *testing.T) {

When("asset is already registered", func() {
req.NativeAssets = []nexus.Asset{{Denom: rand.Denom(3, 10), IsNativeAsset: true}}
nexusK.RegisterAssetFunc = func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset) error {
nexusK.RegisterAssetFunc = func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset, limit sdk.Uint, window time.Duration) error {
return fmt.Errorf("asset already registered")
}
}).
Then2(requestFails("asset already registered")),

When("asset is already registered on axelarnet", func() {
req.NativeAssets = []nexus.Asset{{Denom: rand.Denom(3, 10), IsNativeAsset: true}}
nexusK.RegisterAssetFunc = func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset) error {
nexusK.RegisterAssetFunc = func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset, limit sdk.Uint, window time.Duration) error {
if chain.Name == exported.Axelarnet.Name {
return fmt.Errorf("asset already registered")
} else {
Expand Down
4 changes: 3 additions & 1 deletion x/axelarnet/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -41,7 +43,7 @@ type Nexus interface {
GetChain(ctx sdk.Context, chain nexus.ChainName) (nexus.Chain, bool)
LinkAddresses(ctx sdk.Context, sender nexus.CrossChainAddress, recipient nexus.CrossChainAddress) error
IsAssetRegistered(ctx sdk.Context, chain nexus.Chain, denom string) bool
RegisterAsset(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset) error
RegisterAsset(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset, limit sdk.Uint, window time.Duration) error
GetRecipient(ctx sdk.Context, sender nexus.CrossChainAddress) (nexus.CrossChainAddress, bool)
SetChain(ctx sdk.Context, chain nexus.Chain)
GetTransferFees(ctx sdk.Context) sdk.Coins
Expand Down
45 changes: 29 additions & 16 deletions x/axelarnet/types/mock/expected_keepers.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/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/axelarnetwork/axelar-core/utils"
"github.com/axelarnetwork/axelar-core/utils/events"
"github.com/axelarnetwork/axelar-core/x/evm/types"
multisig "github.com/axelarnetwork/axelar-core/x/multisig/exported"
Expand Down Expand Up @@ -445,7 +446,7 @@ func (s msgServer) CreateDeployToken(c context.Context, req *types.CreateDeployT
return nil, err
}

if err = s.nexus.RegisterAsset(ctx, chain, nexus.NewAsset(req.Asset.Name, false)); err != nil {
if err = s.nexus.RegisterAsset(ctx, chain, nexus.NewAsset(req.Asset.Name, false), utils.MaxUint, types.DefaultRateLimitWindow); err != nil {
return nil, err
}

Expand Down
5 changes: 4 additions & 1 deletion x/evm/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
mathRand "math/rand"
"strings"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -1196,7 +1197,9 @@ func TestHandleMsgCreateDeployToken(t *testing.T) {
return c, ok
},
IsAssetRegisteredFunc: func(sdk.Context, nexus.Chain, string) bool { return true },
RegisterAssetFunc: func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset) error { return nil },
RegisterAssetFunc: func(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset, limit sdk.Uint, window time.Duration) error {
return nil
},
}
multisigKeeper = &mock.MultisigKeeperMock{
GetCurrentKeyIDFunc: func(ctx sdk.Context, chain nexus.ChainName) (multisig.KeyID, bool) {
Expand Down
4 changes: 3 additions & 1 deletion x/evm/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"time"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -103,7 +105,7 @@ type Nexus interface {
GetChains(ctx sdk.Context) []nexus.Chain
GetChain(ctx sdk.Context, chain nexus.ChainName) (nexus.Chain, bool)
IsAssetRegistered(ctx sdk.Context, chain nexus.Chain, denom string) bool
RegisterAsset(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset) error
RegisterAsset(ctx sdk.Context, chain nexus.Chain, asset nexus.Asset, limit sdk.Uint, window time.Duration) error
GetChainMaintainers(ctx sdk.Context, chain nexus.Chain) []sdk.ValAddress
IsChainActivated(ctx sdk.Context, chain nexus.Chain) bool
GetChainByNativeAsset(ctx sdk.Context, asset string) (chain nexus.Chain, ok bool)
Expand Down
45 changes: 29 additions & 16 deletions x/evm/types/mock/expected_keepers.go

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

Loading