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

Commit

Permalink
feat: implement transaction monitor (#340)
Browse files Browse the repository at this point in the history
* feat: implement transaction monitor

* Increase gas by one if the percentage increase didn't change the value

* Avoid blocking calls if message channel not ready

* Improve logs

* Add increase gas tests

* Rename increaseFactor param

---------

Co-authored-by: Matija Petrunic <matija.petrunic@gmailcom>
  • Loading branch information
mpetrun5 and Matija Petrunic authored May 11, 2023
1 parent 2ce7357 commit 9ad5cad
Show file tree
Hide file tree
Showing 15 changed files with 630 additions and 115 deletions.
1 change: 1 addition & 0 deletions chains/evm/calls/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type GasPricer interface {
type ClientDispatcher interface {
WaitAndReturnTxReceipt(h common.Hash) (*types.Receipt, error)
SignAndSendTransaction(ctx context.Context, tx evmclient.CommonTransaction) (common.Hash, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
GetTransactionByHash(h common.Hash) (tx *types.Transaction, isPending bool, err error)
UnsafeNonce() (*big.Int, error)
LockNonce()
Expand Down
29 changes: 14 additions & 15 deletions chains/evm/calls/contracts/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"

mock_transactor "github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor/mock"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor/signAndSend"
"github.com/ChainSafe/chainbridge-core/chains/evm/executor/proposal"
"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -133,7 +132,7 @@ func (s *ProposalStatusTestSuite) TestBridge_AddRelayer_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3, 4, 5}, nil)
res, err := s.bridgeContract.AddRelayer(common.HexToAddress(testRelayerAddress), signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.AddRelayer(common.HexToAddress(testRelayerAddress), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{1, 2, 3, 4, 5},
res,
Expand All @@ -147,7 +146,7 @@ func (s *ProposalStatusTestSuite) TestBridge_AdminSetGenericResource_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{10, 11, 12, 13, 14}, nil)
res, err := s.bridgeContract.AdminSetGenericResource(common.HexToAddress(testHandlerAddress), [32]byte{1}, common.HexToAddress(testInteractorAddress), [4]byte{2}, big.NewInt(0), [4]byte{3}, signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.AdminSetGenericResource(common.HexToAddress(testHandlerAddress), [32]byte{1}, common.HexToAddress(testInteractorAddress), [4]byte{2}, big.NewInt(0), [4]byte{3}, transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{10, 11, 12, 13, 14},
res,
Expand All @@ -161,7 +160,7 @@ func (s *ProposalStatusTestSuite) TestBridge_AdminSetResource_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{10, 11, 12, 13, 14, 15, 16}, nil)
res, err := s.bridgeContract.AdminSetResource(common.HexToAddress(testHandlerAddress), testResourceId, common.HexToAddress(testContractAddress), signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.AdminSetResource(common.HexToAddress(testHandlerAddress), testResourceId, common.HexToAddress(testContractAddress), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{10, 11, 12, 13, 14, 15, 16},
res,
Expand All @@ -175,7 +174,7 @@ func (s *ProposalStatusTestSuite) TestBridge_SetDepositNonce_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{20, 21, 22, 23}, nil)
res, err := s.bridgeContract.SetDepositNonce(testDomainId, uint64(0), signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.SetDepositNonce(testDomainId, uint64(0), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{20, 21, 22, 23},
res,
Expand All @@ -189,7 +188,7 @@ func (s *ProposalStatusTestSuite) TestBridge_SetThresholdInput_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{22, 23, 24, 25}, nil)
res, err := s.bridgeContract.AdminChangeRelayerThreshold(uint64(2), signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.AdminChangeRelayerThreshold(uint64(2), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{22, 23, 24, 25},
res,
Expand All @@ -203,7 +202,7 @@ func (s *ProposalStatusTestSuite) TestBridge_SetBurnableInput_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{25, 26, 27, 28}, nil)
res, err := s.bridgeContract.SetBurnableInput(common.HexToAddress(testHandlerAddress), common.HexToAddress(testContractAddress), signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.SetBurnableInput(common.HexToAddress(testHandlerAddress), common.HexToAddress(testContractAddress), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{25, 26, 27, 28},
res,
Expand All @@ -217,7 +216,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Erc20Deposit_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{30, 31, 32, 33}, nil)
res, err := s.bridgeContract.Erc20Deposit(common.HexToAddress(testInteractorAddress), big.NewInt(10), testResourceId, testDomainId, signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.Erc20Deposit(common.HexToAddress(testInteractorAddress), big.NewInt(10), testResourceId, testDomainId, transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{30, 31, 32, 33},
res,
Expand All @@ -231,7 +230,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Erc721Deposit_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{32, 33, 34, 35}, nil)
res, err := s.bridgeContract.Erc721Deposit(big.NewInt(55), "token_uri", common.HexToAddress(testInteractorAddress), testResourceId, testDomainId, signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.Erc721Deposit(big.NewInt(55), "token_uri", common.HexToAddress(testInteractorAddress), testResourceId, testDomainId, transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{32, 33, 34, 35},
res,
Expand All @@ -245,7 +244,7 @@ func (s *ProposalStatusTestSuite) TestBridge_GenericDeposit_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{35, 36, 37, 38}, nil)
res, err := s.bridgeContract.GenericDeposit([]byte{1, 2, 3}, testResourceId, testDomainId, signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.GenericDeposit([]byte{1, 2, 3}, testResourceId, testDomainId, transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{35, 36, 37, 38},
res,
Expand All @@ -259,7 +258,7 @@ func (s *ProposalStatusTestSuite) TestBridge_ExecuteProposal_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{36, 37, 38}, nil)
res, err := s.bridgeContract.ExecuteProposal(&s.proposal, signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.ExecuteProposal(&s.proposal, transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{36, 37, 38},
res,
Expand All @@ -273,7 +272,7 @@ func (s *ProposalStatusTestSuite) TestBridge_VoteProposal_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{37, 38, 39}, nil)
res, err := s.bridgeContract.VoteProposal(&s.proposal, signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.VoteProposal(&s.proposal, transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{37, 38, 39},
res,
Expand All @@ -298,7 +297,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Pause_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{40, 41, 42}, nil)
res, err := s.bridgeContract.Pause(signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.Pause(transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{40, 41, 42},
res,
Expand All @@ -312,7 +311,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Unpause_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{42, 43, 44}, nil)
res, err := s.bridgeContract.Unpause(signAndSend.DefaultTransactionOptions)
res, err := s.bridgeContract.Unpause(transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{42, 43, 44},
res,
Expand All @@ -330,7 +329,7 @@ func (s *ProposalStatusTestSuite) TestBridge_Withdraw_Success() {
common.HexToAddress(testHandlerAddress),
common.HexToAddress(testContractAddress),
common.HexToAddress(testInteractorAddress),
big.NewInt(5), signAndSend.DefaultTransactionOptions,
big.NewInt(5), transactor.DefaultTransactionOptions,
)
s.Equal(
&common.Hash{44, 45, 46},
Expand Down
8 changes: 4 additions & 4 deletions chains/evm/calls/contracts/erc20/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

erc20 "github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts/erc20"
mock_calls "github.com/ChainSafe/chainbridge-core/chains/evm/calls/mock"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
mock_transactor "github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor/mock"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor/signAndSend"
"github.com/ethereum/go-ethereum/common"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (s *ERC20ContractCallsTestSuite) TestErc20Contract_MintTokens_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3, 4, 5}, nil)
res, err := s.erc20contract.MintTokens(common.HexToAddress(testInteractorAddress), big.NewInt(10), signAndSend.DefaultTransactionOptions)
res, err := s.erc20contract.MintTokens(common.HexToAddress(testInteractorAddress), big.NewInt(10), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{1, 2, 3, 4, 5},
res,
Expand All @@ -77,7 +77,7 @@ func (s *ERC20ContractCallsTestSuite) TestErc20Contract_ApproveTokens_Success()
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), signAndSend.DefaultTransactionOptions)
res, err := s.erc20contract.ApproveTokens(common.HexToAddress(testInteractorAddress), big.NewInt(100), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{1, 2, 3, 4, 5, 6, 7, 8, 9},
res,
Expand Down Expand Up @@ -112,7 +112,7 @@ func (s *ERC20ContractCallsTestSuite) TestErc20Contract_AddMinter_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3}, nil)
res, err := s.erc20contract.AddMinter(common.HexToAddress(testInteractorAddress), signAndSend.DefaultTransactionOptions)
res, err := s.erc20contract.AddMinter(common.HexToAddress(testInteractorAddress), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{1, 2, 3},
res,
Expand Down
5 changes: 2 additions & 3 deletions chains/evm/calls/contracts/erc721/erc721_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
mock_transactor "github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor/mock"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor/signAndSend"
"github.com/ethereum/go-ethereum/common"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -108,7 +107,7 @@ func (s *ERC721CallsTestSuite) TestERC721Contract_AddMinter_Success() {
gomock.Any(),
gomock.Any(),
).Return(&common.Hash{1, 2, 3}, nil)
res, err := s.erc721Contract.AddMinter(common.HexToAddress(testInteractorAddress), signAndSend.DefaultTransactionOptions)
res, err := s.erc721Contract.AddMinter(common.HexToAddress(testInteractorAddress), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{1, 2, 3},
res,
Expand All @@ -122,7 +121,7 @@ func (s *ERC721CallsTestSuite) TestERC721Contract_MintTokens_Success() {
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), signAndSend.DefaultTransactionOptions)
res, err := s.erc721Contract.Mint(big.NewInt(5), "token_uri", common.HexToAddress(testInteractorAddress), transactor.DefaultTransactionOptions)
s.Equal(
&common.Hash{1, 2, 3, 4, 5},
res,
Expand Down
30 changes: 30 additions & 0 deletions chains/evm/calls/mock/calls.go

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

Loading

0 comments on commit 9ad5cad

Please sign in to comment.