Skip to content

Commit

Permalink
test: update unit test for bridge call to fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Jan 8, 2025
1 parent fdf7355 commit 23c2920
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions precompiles/bank/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *TransferFromModuleToAccountMethod) RequiredGas() uint64 {

func (m *TransferFromModuleToAccountMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) {
accessControlKeeper := fxcontract.NewAccessControlKeeper(pretypes.NewVMCall(evm, contract), fxcontract.AccessControlAddress)
has, err := accessControlKeeper.HasRole(context.TODO(), common.HexToHash(fxcontract.TransferModuleRole), contract.Caller())
has, err := accessControlKeeper.HasRole(context.Background(), common.HexToHash(fxcontract.TransferModuleRole), contract.Caller())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func (m *TransferFromAccountToModuleMethod) RequiredGas() uint64 {

func (m *TransferFromAccountToModuleMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) {
accessControlKeeper := fxcontract.NewAccessControlKeeper(pretypes.NewVMCall(evm, contract), fxcontract.AccessControlAddress)
has, err := accessControlKeeper.HasRole(context.TODO(), common.HexToHash(fxcontract.TransferModuleRole), contract.Caller())
has, err := accessControlKeeper.HasRole(context.Background(), common.HexToHash(fxcontract.TransferModuleRole), contract.Caller())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -eo pipefail

patternLimits=(
"nolint:21"
"nolint:22"
"#nosec:5"
"CrossChain:4"
"cross chain:0"
Expand Down
23 changes: 17 additions & 6 deletions x/crosschain/keeper/bridge_call_in_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package keeper_test

import (
"strings"

sdkmath "cosmossdk.io/math"
"github.com/ethereum/go-ethereum/common"

"github.com/pundiai/fx-core/v8/contract"
"github.com/pundiai/fx-core/v8/testutil/helpers"
"github.com/pundiai/fx-core/v8/x/crosschain/types"
erc20types "github.com/pundiai/fx-core/v8/x/erc20/types"
)

func (suite *KeeperTestSuite) TestBridgeCallHandler() {
suite.T().SkipNow() // todo: re-enable this test
testCases := []struct {
Name string
Msg types.MsgBridgeCallClaim
Expand Down Expand Up @@ -38,6 +39,7 @@ func (suite *KeeperTestSuite) TestBridgeCallHandler() {
To: helpers.GenExternalAddr(suite.chainName),
Data: "",
QuoteId: sdkmath.ZeroInt(),
GasLimit: sdkmath.ZeroInt(),
Memo: "",
TxOrigin: helpers.GenExternalAddr(suite.chainName),
},
Expand All @@ -48,15 +50,24 @@ func (suite *KeeperTestSuite) TestBridgeCallHandler() {

for _, tc := range testCases {
suite.Run(tc.Name, func() {
erc20Addrs := make([]common.Address, len(tc.Msg.TokenContracts))
erc20Tokens := make([]erc20types.ERC20Token, 0, len(tc.Msg.TokenContracts))
for _, tokenContract := range tc.Msg.TokenContracts {
denom := helpers.NewRandDenom()
err := suite.App.Erc20Keeper.AddBridgeToken(suite.Ctx, denom, suite.chainName, tokenContract, false)
suite.Require().NoError(err)

erc20Token, err := suite.App.Erc20Keeper.RegisterNativeCoin(suite.Ctx, denom, strings.ToUpper(denom), 18)
suite.Require().NoError(err)
erc20Tokens = append(erc20Tokens, erc20Token)
}

err := suite.Keeper().BridgeCallHandler(suite.Ctx, &tc.Msg)
if tc.Success {
suite.Require().NoError(err)
if !tc.CallContract {
for i, addr := range erc20Addrs {
erc20Token := contract.NewERC20TokenKeeper(suite.App.EvmKeeper)
balanceOf, err := erc20Token.BalanceOf(suite.Ctx, addr, tc.Msg.GetToAddr())
for i, erc20Token := range erc20Tokens {
erc20TokenKeeper := contract.NewERC20TokenKeeper(suite.App.EvmKeeper)
balanceOf, err := erc20TokenKeeper.BalanceOf(suite.Ctx, erc20Token.GetERC20Contract(), tc.Msg.GetToAddr())
suite.Require().NoError(err)
suite.Equal(tc.Msg.Amounts[i].BigInt().String(), balanceOf.String())
}
Expand Down
4 changes: 4 additions & 0 deletions x/crosschain/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func (m *MsgBridgeCallClaim) ValidateBasic() (err error) {
return m.validate()
}

//nolint:gocyclo // validate
func (m *MsgBridgeCallClaim) validate() (err error) {
if _, err = sdk.AccAddressFromBech32(m.BridgerAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid bridger address: %s", err)
Expand All @@ -400,6 +401,9 @@ func (m *MsgBridgeCallClaim) validate() (err error) {
if m.QuoteId.IsNil() || m.QuoteId.IsNegative() {
return sdkerrors.ErrInvalidRequest.Wrap("invalid quote ID")
}
if m.GasLimit.IsNil() || m.GasLimit.IsNegative() {
return sdkerrors.ErrInvalidRequest.Wrap("invalid gas limit")
}
if len(m.Data) > 0 {
if _, err = hex.DecodeString(m.Data); err != nil {
return sdkerrors.ErrInvalidRequest.Wrap("invalid data")
Expand Down
4 changes: 4 additions & 0 deletions x/crosschain/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,7 @@ func TestMsgBridgeCallClaim_ValidateBasic(t *testing.T) {
To: helpers.GenExternalAddr(moduleName),
Data: "",
QuoteId: sdkmath.NewInt(1),
GasLimit: sdkmath.ZeroInt(),
Memo: "",
TxOrigin: helpers.GenExternalAddr(moduleName),
},
Expand All @@ -1632,6 +1633,7 @@ func TestMsgBridgeCallClaim_ValidateBasic(t *testing.T) {
To: helpers.GenExternalAddr(moduleName),
Data: "",
QuoteId: sdkmath.NewInt(1),
GasLimit: sdkmath.ZeroInt(),
Memo: "",
TxOrigin: helpers.GenExternalAddr(moduleName),
},
Expand All @@ -1651,6 +1653,7 @@ func TestMsgBridgeCallClaim_ValidateBasic(t *testing.T) {
To: helpers.GenExternalAddr(moduleName),
Data: "",
QuoteId: sdkmath.NewInt(1),
GasLimit: sdkmath.ZeroInt(),
Memo: "",
TxOrigin: helpers.GenExternalAddr(moduleName),
},
Expand All @@ -1670,6 +1673,7 @@ func TestMsgBridgeCallClaim_ValidateBasic(t *testing.T) {
To: helpers.GenExternalAddr(moduleName),
Data: "",
QuoteId: sdkmath.NewInt(1),
GasLimit: sdkmath.ZeroInt(),
Memo: "",
TxOrigin: helpers.GenExternalAddr(moduleName),
},
Expand Down
3 changes: 2 additions & 1 deletion x/crosschain/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/pundiai/fx-core/v8/contract"
fxtypes "github.com/pundiai/fx-core/v8/types"
erc20types "github.com/pundiai/fx-core/v8/x/erc20/types"
)

func NewDelegateAmount(amount sdkmath.Int) sdk.Coin {
Expand Down Expand Up @@ -490,7 +491,7 @@ func (m *OutgoingBridgeCall) GetCheckpoint(gravityIDString string) ([]byte, erro
}

func NewBridgeDenom(moduleName, token string) string {
return fmt.Sprintf("%s%s", moduleName, token)
return erc20types.NewBridgeDenom(moduleName, token)
}

func (m *MsgBridgeCallClaim) GetERC20Tokens() []ERC20Token {
Expand Down
6 changes: 5 additions & 1 deletion x/erc20/types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (m *BridgeToken) BridgeDenom() string {
if m.IsOrigin() {
return m.Denom
}
return fmt.Sprintf("%s%s", m.ChainName, m.Contract) // TODO: replace with crosschaintypes.NewBridgeToken
return NewBridgeDenom(m.ChainName, m.Contract)
}

func (m *BridgeToken) IsOrigin() bool {
Expand All @@ -36,3 +36,7 @@ func (m *BridgeToken) IsOrigin() bool {
func (m *BridgeToken) GetContractAddress() common.Address {
return common.HexToAddress(m.Contract)
}

func NewBridgeDenom(moduleName, token string) string {
return fmt.Sprintf("%s%s", moduleName, token)
}

0 comments on commit 23c2920

Please sign in to comment.