Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
fix: fix gomocks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
P1sar committed Aug 29, 2023
1 parent 028fe31 commit 807e1ee
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 31 deletions.
7 changes: 6 additions & 1 deletion chains/evm/calls/contracts/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (c *BridgeContract) deposit(
opts transactor.TransactOptions,
) (*common.Hash, error) {
return c.ExecuteTransaction(
context.Background(),
"deposit",
opts,
destDomainID, resourceID, data,
Expand Down Expand Up @@ -217,6 +218,7 @@ func (c *BridgeContract) ExecuteProposal(
Str("handler", proposal.HandlerAddress.String()).
Msgf("Execute proposal")
return c.ExecuteTransaction(
context.Background(),
"executeProposal",
opts,
proposal.Source, proposal.DepositNonce, proposal.Data, proposal.ResourceId, true,
Expand All @@ -233,6 +235,7 @@ func (c *BridgeContract) VoteProposal(
Str("handler", proposal.HandlerAddress.String()).
Msgf("Vote proposal")
return c.ExecuteTransaction(
context.Background(),
"voteProposal",
opts,
proposal.Source, proposal.DepositNonce, proposal.ResourceId, proposal.Data,
Expand All @@ -255,6 +258,7 @@ func (c *BridgeContract) SimulateVoteProposal(proposal *proposal.Proposal) error
func (c *BridgeContract) Pause(opts transactor.TransactOptions) (*common.Hash, error) {
log.Debug().Msg("Pause transfers")
return c.ExecuteTransaction(
context.Background(),
"adminPauseTransfers",
opts,
)
Expand All @@ -263,6 +267,7 @@ func (c *BridgeContract) Pause(opts transactor.TransactOptions) (*common.Hash, e
func (c *BridgeContract) Unpause(opts transactor.TransactOptions) (*common.Hash, error) {
log.Debug().Msg("Unpause transfers")
return c.ExecuteTransaction(
context.Background(),
"adminUnpauseTransfers",
opts,
)
Expand All @@ -284,7 +289,7 @@ func (c *BridgeContract) Withdraw(
data.Write(common.LeftPadBytes(recipientAddress.Bytes(), 32))
data.Write(common.LeftPadBytes(amountOrTokenId.Bytes(), 32))

return c.ExecuteTransaction("adminWithdraw", opts, handlerAddress, data.Bytes())
return c.ExecuteTransaction(context.Background(), "adminWithdraw", opts, handlerAddress, data.Bytes())
}

func (c *BridgeContract) GetThreshold() (uint8, error) {
Expand Down
17 changes: 16 additions & 1 deletion chains/evm/calls/contracts/bridge/bridge_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bridge_test

import (
"context"
"encoding/hex"
"errors"
"math/big"
Expand Down Expand Up @@ -100,7 +101,7 @@ func (s *ProposalStatusTestSuite) TestPrepare_WithdrawInput_Success() {
recipientAddress := common.HexToAddress("0x8e5F72B158BEDf0ab50EDa78c70dFC118158C272")
amountOrTokenId := big.NewInt(1)

s.mockTransactor.EXPECT().Transact(&s.bridgeAddress, gomock.Any(), gomock.Any()).Times(1).Return(
s.mockTransactor.EXPECT().Transact(context.Background(), &s.bridgeAddress, gomock.Any(), gomock.Any()).Times(1).Return(
&common.Hash{}, nil,
)

Expand Down Expand Up @@ -128,6 +129,7 @@ func (s *ProposalStatusTestSuite) TestDeployContract_Success() {

func (s *ProposalStatusTestSuite) TestBridge_AddRelayer_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -142,6 +144,7 @@ func (s *ProposalStatusTestSuite) TestBridge_AddRelayer_Success() {

func (s *ProposalStatusTestSuite) TestBridge_AdminSetGenericResource_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -156,6 +159,7 @@ func (s *ProposalStatusTestSuite) TestBridge_AdminSetGenericResource_Success() {

func (s *ProposalStatusTestSuite) TestBridge_AdminSetResource_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -170,6 +174,7 @@ func (s *ProposalStatusTestSuite) TestBridge_AdminSetResource_Success() {

func (s *ProposalStatusTestSuite) TestBridge_SetDepositNonce_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -184,6 +189,7 @@ func (s *ProposalStatusTestSuite) TestBridge_SetDepositNonce_Success() {

func (s *ProposalStatusTestSuite) TestBridge_SetThresholdInput_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -198,6 +204,7 @@ func (s *ProposalStatusTestSuite) TestBridge_SetThresholdInput_Success() {

func (s *ProposalStatusTestSuite) TestBridge_SetBurnableInput_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -212,6 +219,7 @@ func (s *ProposalStatusTestSuite) TestBridge_SetBurnableInput_Success() {

func (s *ProposalStatusTestSuite) TestBridge_Erc20Deposit_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -226,6 +234,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Erc20Deposit_Success() {

func (s *ProposalStatusTestSuite) TestBridge_Erc721Deposit_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -240,6 +249,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Erc721Deposit_Success() {

func (s *ProposalStatusTestSuite) TestBridge_GenericDeposit_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -254,6 +264,7 @@ func (s *ProposalStatusTestSuite) TestBridge_GenericDeposit_Success() {

func (s *ProposalStatusTestSuite) TestBridge_ExecuteProposal_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -268,6 +279,7 @@ func (s *ProposalStatusTestSuite) TestBridge_ExecuteProposal_Success() {

func (s *ProposalStatusTestSuite) TestBridge_VoteProposal_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -293,6 +305,7 @@ func (s *ProposalStatusTestSuite) TestBridge_SimulateVoteProposal_Success() {

func (s *ProposalStatusTestSuite) TestBridge_Pause_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -307,6 +320,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Pause_Success() {

func (s *ProposalStatusTestSuite) TestBridge_Unpause_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand All @@ -321,6 +335,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Unpause_Success() {

func (s *ProposalStatusTestSuite) TestBridge_Withdraw_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
Expand Down
18 changes: 12 additions & 6 deletions chains/evm/calls/contracts/contract_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package contracts

import (
"context"
"errors"
"math/big"
"strings"
"testing"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls/consts"
mock_calls "github.com/ChainSafe/chainbridge-core/chains/evm/calls/mock"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
Expand All @@ -10,9 +15,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"math/big"
"strings"
"testing"
)

type ContractTestSuite struct {
Expand Down Expand Up @@ -66,12 +68,13 @@ func (s *ContractTestSuite) TestContract_UnpackResult_InvalidRequest_Fail() {

func (s *ContractTestSuite) TestContract_ExecuteTransaction_ValidRequest_Success() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
&common.Address{},
gomock.Any(),
transactor.TransactOptions{},
).Return(&common.Hash{}, nil)
hash, err := s.contract.ExecuteTransaction(
"approve",
context.Background(), "approve",
transactor.TransactOptions{}, common.Address{}, big.NewInt(10),
)
s.Nil(err)
Expand All @@ -80,11 +83,13 @@ func (s *ContractTestSuite) TestContract_ExecuteTransaction_ValidRequest_Success

func (s *ContractTestSuite) TestContract_ExecuteTransaction_TransactError_Fail() {
s.mockTransactor.EXPECT().Transact(
context.Background(),
&common.Address{},
gomock.Any(),
transactor.TransactOptions{},
).Return(nil, errors.New("error"))
hash, err := s.contract.ExecuteTransaction(
context.Background(),
"approve",
transactor.TransactOptions{}, common.Address{}, big.NewInt(10),
)
Expand All @@ -94,6 +99,7 @@ func (s *ContractTestSuite) TestContract_ExecuteTransaction_TransactError_Fail()

func (s *ContractTestSuite) TestContract_ExecuteTransaction_InvalidRequest_Fail() {
hash, err := s.contract.ExecuteTransaction(
context.Background(),
"approve",
transactor.TransactOptions{}, common.Address{}, // missing one argument
)
Expand Down Expand Up @@ -149,7 +155,7 @@ func (s *ContractTestSuite) TestContract_DeployContract_InvalidRequest_Fail() {

func (s *ContractTestSuite) TestContract_DeployContract_TransactionError_Fail() {
s.mockTransactor.EXPECT().Transact(
nil, gomock.Any(), gomock.Any(),
context.Background(), nil, gomock.Any(), gomock.Any(),
).Times(1).Return(&common.Hash{}, errors.New("error"))
res, err := s.contract.DeployContract("TestERC721", "TST721", "")
s.Equal(common.Address{}, res)
Expand All @@ -158,7 +164,7 @@ func (s *ContractTestSuite) TestContract_DeployContract_TransactionError_Fail()

func (s *ContractTestSuite) TestContract_DeployContract_GetTxByHashError_Fail() {
s.mockTransactor.EXPECT().Transact(
nil, gomock.Any(), gomock.Any(),
context.Background(), nil, gomock.Any(), gomock.Any(),
).Times(1).Return(&common.Hash{}, nil)
s.mockContractCallerDispatcherClient.EXPECT().GetTransactionByHash(
common.Hash{},
Expand Down
12 changes: 7 additions & 5 deletions chains/evm/calls/contracts/erc20/erc20.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package erc20

import (
"context"
"math/big"
"strings"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
"math/big"
"strings"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls/consts"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -43,7 +45,7 @@ func (c *ERC20Contract) MintTokens(
opts transactor.TransactOptions,
) (*common.Hash, error) {
log.Debug().Msgf("Minting %s tokens to %s", amount.String(), to.String())
return c.ExecuteTransaction("mint", opts, to, amount)
return c.ExecuteTransaction(context.Background(), "mint", opts, to, amount)
}

func (c *ERC20Contract) ApproveTokens(
Expand All @@ -52,7 +54,7 @@ func (c *ERC20Contract) ApproveTokens(
opts transactor.TransactOptions,
) (*common.Hash, error) {
log.Debug().Msgf("Approving %s tokens for %s", target.String(), amount.String())
return c.ExecuteTransaction("approve", opts, target, amount)
return c.ExecuteTransaction(context.Background(), "approve", opts, target, amount)
}

func (c *ERC20Contract) MinterRole() ([32]byte, error) {
Expand All @@ -73,5 +75,5 @@ func (c *ERC20Contract) AddMinter(
if err != nil {
return nil, err
}
return c.ExecuteTransaction("grantRole", opts, role, minter)
return c.ExecuteTransaction(context.Background(), "grantRole", opts, role, minter)
}
3 changes: 3 additions & 0 deletions chains/evm/calls/contracts/erc20/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (s *ERC20ContractCallsTestSuite) TestErc20Contract_MintTokens_Success() {
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3, 4, 5}, nil)
res, err := s.erc20contract.MintTokens(common.HexToAddress(testInteractorAddress), big.NewInt(10), transactor.DefaultTransactionOptions)
s.Equal(
Expand All @@ -76,6 +77,7 @@ func (s *ERC20ContractCallsTestSuite) TestErc20Contract_ApproveTokens_Success()
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3, 4, 5, 6, 7, 8, 9}, nil)
res, err := s.erc20contract.ApproveTokens(common.HexToAddress(testInteractorAddress), big.NewInt(100), transactor.DefaultTransactionOptions)
s.Equal(
Expand Down Expand Up @@ -111,6 +113,7 @@ func (s *ERC20ContractCallsTestSuite) TestErc20Contract_AddMinter_Success() {
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3}, nil)
res, err := s.erc20contract.AddMinter(common.HexToAddress(testInteractorAddress), transactor.DefaultTransactionOptions)
s.Equal(
Expand Down
12 changes: 7 additions & 5 deletions chains/evm/calls/contracts/erc721/erc721.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package erc721

import (
"context"
"math/big"
"strings"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/consts"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts"
"math/big"
"strings"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -35,21 +37,21 @@ func (c *ERC721Contract) AddMinter(
if err != nil {
return nil, err
}
return c.ExecuteTransaction("grantRole", opts, role, minter)
return c.ExecuteTransaction(context.Background(), "grantRole", opts, role, minter)
}

func (c *ERC721Contract) Approve(
tokenId *big.Int, recipient common.Address, opts transactor.TransactOptions,
) (*common.Hash, error) {
log.Debug().Msgf("Approving %s token for %s", tokenId.String(), recipient.String())
return c.ExecuteTransaction("approve", opts, recipient, tokenId)
return c.ExecuteTransaction(context.Background(), "approve", opts, recipient, tokenId)
}

func (c *ERC721Contract) Mint(
tokenId *big.Int, metadata string, destination common.Address, opts transactor.TransactOptions,
) (*common.Hash, error) {
log.Debug().Msgf("Minting tokens %s to %s", tokenId.String(), destination.String())
return c.ExecuteTransaction("mint", opts, destination, tokenId, metadata)
return c.ExecuteTransaction(context.Background(), "mint", opts, destination, tokenId, metadata)
}

func (c *ERC721Contract) Owner(tokenId *big.Int) (*common.Address, error) {
Expand Down
3 changes: 3 additions & 0 deletions chains/evm/calls/contracts/erc721/erc721_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (s *ERC721CallsTestSuite) TestERC721Contract_UnpackResult_InvalidData_Fail(

func (s *ERC721CallsTestSuite) TestERC721Contract_Approve_Success() {
s.mockTransactor.EXPECT().Transact(
gomock.Any(),
&s.erc721ContractAddress,
gomock.Any(),
transactor.TransactOptions{},
Expand All @@ -106,6 +107,7 @@ func (s *ERC721CallsTestSuite) TestERC721Contract_AddMinter_Success() {
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3}, nil)
res, err := s.erc721Contract.AddMinter(common.HexToAddress(testInteractorAddress), transactor.DefaultTransactionOptions)
s.Equal(
Expand All @@ -120,6 +122,7 @@ func (s *ERC721CallsTestSuite) TestERC721Contract_MintTokens_Success() {
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3, 4, 5}, nil)
res, err := s.erc721Contract.Mint(big.NewInt(5), "token_uri", common.HexToAddress(testInteractorAddress), transactor.DefaultTransactionOptions)
s.Equal(
Expand Down
Loading

0 comments on commit 807e1ee

Please sign in to comment.