diff --git a/precompiles/bank/transfer.go b/precompiles/bank/transfer.go index 4e4b6f40..df841a39 100644 --- a/precompiles/bank/transfer.go +++ b/precompiles/bank/transfer.go @@ -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 } @@ -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 } diff --git a/scripts/linter.sh b/scripts/linter.sh index b0ef51e4..3df171ff 100755 --- a/scripts/linter.sh +++ b/scripts/linter.sh @@ -3,7 +3,7 @@ set -eo pipefail patternLimits=( - "nolint:21" + "nolint:22" "#nosec:5" "CrossChain:4" "cross chain:0" diff --git a/x/crosschain/keeper/bridge_call_in_test.go b/x/crosschain/keeper/bridge_call_in_test.go index cf602248..b2db1ff2 100644 --- a/x/crosschain/keeper/bridge_call_in_test.go +++ b/x/crosschain/keeper/bridge_call_in_test.go @@ -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 @@ -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), }, @@ -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()) } diff --git a/x/crosschain/types/msgs.go b/x/crosschain/types/msgs.go index bb107116..8e1cb26f 100644 --- a/x/crosschain/types/msgs.go +++ b/x/crosschain/types/msgs.go @@ -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) @@ -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") diff --git a/x/crosschain/types/msgs_test.go b/x/crosschain/types/msgs_test.go index 366c28b2..5c4ba856 100644 --- a/x/crosschain/types/msgs_test.go +++ b/x/crosschain/types/msgs_test.go @@ -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), }, @@ -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), }, @@ -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), }, @@ -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), }, diff --git a/x/crosschain/types/types.go b/x/crosschain/types/types.go index 5dd4551c..f9d03391 100644 --- a/x/crosschain/types/types.go +++ b/x/crosschain/types/types.go @@ -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 { @@ -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 { diff --git a/x/erc20/types/token.go b/x/erc20/types/token.go index 419c4feb..bb212eca 100644 --- a/x/erc20/types/token.go +++ b/x/erc20/types/token.go @@ -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 { @@ -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) +}