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

Fix nonce not unlocking #167

Merged
merged 9 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ install-subkey:
cargo install --force --git https://github.com/paritytech/substrate subkey

genmocks:
mockgen -destination=./chains/evm/evmgaspricer/mock/gas-pricer.go -source=./chains/evm/evmgaspricer/gas-pricer.go
mockgen -source=./chains/evm/evmgaspricer/gas-pricer.go -destination=./chains/evm/evmgaspricer/mock/gas-pricer.go
mockgen -source=chains/evm/calls/utils.go -destination=chains/evm/calls/mock/utils.go


e2e-test:
./scripts/int_tests.sh

3 changes: 2 additions & 1 deletion chains/evm/calls/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func DeployErc20Handler(c ClientDeployer, txFabric TxFabric, gasPriceClient GasP
}

func deployContract(client ClientDeployer, abi abi.ABI, bytecode []byte, txFabric TxFabric, gasPriceClient GasPricer, params ...interface{}) (common.Address, error) {
defer client.UnlockNonce()

client.LockNonce()
n, err := client.UnsafeNonce()
if err != nil {
Expand Down Expand Up @@ -85,7 +87,6 @@ func deployContract(client ClientDeployer, abi abi.ABI, bytecode []byte, txFabri
if err != nil {
return common.Address{}, err
}
client.UnlockNonce()
// checks bytecode at address
// nil is latest block
if code, err := client.CodeAt(context.Background(), address, nil); err != nil {
Expand Down
149 changes: 149 additions & 0 deletions chains/evm/calls/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package calls_test

import (
"errors"

calls "github.com/ChainSafe/chainbridge-core/chains/evm/calls"
mock_calls "github.com/ChainSafe/chainbridge-core/chains/evm/calls/mock"
"github.com/ChainSafe/chainbridge-core/chains/evm/evmtransaction"
"github.com/ethereum/go-ethereum/common"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"math/big"

"testing"
)

type DeployTestSuite struct {
suite.Suite
mockClientDeployer *mock_calls.MockClientDeployer
mockgasPricer *mock_calls.MockGasPricer
}

func TestRunDeployTestSuite(t *testing.T) {
suite.Run(t, new(DeployTestSuite))
}

func (s *DeployTestSuite) SetupSuite() {
gomockController := gomock.NewController(s.T())
s.mockClientDeployer = mock_calls.NewMockClientDeployer(gomockController)
s.mockgasPricer = mock_calls.NewMockGasPricer(gomockController)
}
func (s *DeployTestSuite) TearDownSuite() {}
func (s *DeployTestSuite) SetupTest() {}
func (s *DeployTestSuite) TearDownTest() {}

func (s *DeployTestSuite) TestDeployErc20NonceUnlockCallWithErrorThrown() {
s.mockClientDeployer.EXPECT().LockNonce().Times(1)
s.mockClientDeployer.EXPECT().UnsafeNonce().Return(big.NewInt(1), nil)
s.mockgasPricer.EXPECT().GasPrice().Return([]*big.Int{big.NewInt(10)}, nil)
s.mockClientDeployer.EXPECT().SignAndSendTransaction(gomock.Any(), gomock.Any()).Times(1).Return(common.Hash{}, nil)
s.mockClientDeployer.EXPECT().WaitAndReturnTxReceipt(gomock.Any()).Times(1).Return(nil, nil)
s.mockClientDeployer.EXPECT().From().Times(1).Return(common.Address{})
s.mockClientDeployer.EXPECT().UnsafeIncreaseNonce().Times(1)
s.mockClientDeployer.EXPECT().CodeAt(gomock.Any(), gomock.Any(), gomock.Any()).Times(1)
s.mockClientDeployer.EXPECT().UnlockNonce().Times(1)

_, _ = calls.DeployErc20(
s.mockClientDeployer,
evmtransaction.NewTransaction,
s.mockgasPricer,
"TEST",
"TST")
}

func (s *DeployTestSuite) TestDeployErc20NonceUnlockCallWithoutErrorsThrown() {
s.mockClientDeployer.EXPECT().LockNonce().Times(1)
s.mockClientDeployer.EXPECT().UnsafeNonce().Return(big.NewInt(1), nil)
s.mockgasPricer.EXPECT().GasPrice().Return([]*big.Int{big.NewInt(10)}, nil)
s.mockClientDeployer.EXPECT().SignAndSendTransaction(gomock.Any(), gomock.Any()).Times(1).Return(common.Hash{}, errors.New("error"))
s.mockClientDeployer.EXPECT().UnlockNonce().Times(1)
s.mockClientDeployer.EXPECT().WaitAndReturnTxReceipt(gomock.Any()).Times(0)
s.mockClientDeployer.EXPECT().UnsafeIncreaseNonce().Times(0)

_, _ = calls.DeployErc20(
s.mockClientDeployer,
evmtransaction.NewTransaction,
s.mockgasPricer,
"TEST",
"TST")
}

func (s *DeployTestSuite) TestDeployBridgeNonceUnlockCall() {
nmlinaric marked this conversation as resolved.
Show resolved Hide resolved
s.mockClientDeployer.EXPECT().LockNonce().Times(1)
s.mockClientDeployer.EXPECT().UnsafeNonce().Return(big.NewInt(1), nil)
s.mockgasPricer.EXPECT().GasPrice().Return([]*big.Int{big.NewInt(10)}, nil)
s.mockClientDeployer.EXPECT().SignAndSendTransaction(gomock.Any(), gomock.Any()).Times(1).Return(common.Hash{}, errors.New("error"))
s.mockClientDeployer.EXPECT().UnlockNonce().Times(1)
s.mockClientDeployer.EXPECT().WaitAndReturnTxReceipt(gomock.Any()).Times(0)
s.mockClientDeployer.EXPECT().UnsafeIncreaseNonce().Times(0)

toAddress := common.HexToAddress("0xtest1")

_, _ = calls.DeployBridge(
s.mockClientDeployer,
evmtransaction.NewTransaction,
s.mockgasPricer,
0x1,
[]common.Address{toAddress},
big.NewInt(2),
big.NewInt(10))
}

func (s *DeployTestSuite) TestDeployBridgeNonceUnlockCallWithoutErrorsThrown() {
s.mockClientDeployer.EXPECT().LockNonce().Times(1)
s.mockClientDeployer.EXPECT().UnsafeNonce().Return(big.NewInt(1), nil)
s.mockgasPricer.EXPECT().GasPrice().Return([]*big.Int{big.NewInt(10)}, nil)
s.mockClientDeployer.EXPECT().SignAndSendTransaction(gomock.Any(), gomock.Any()).Times(1).Return(common.Hash{}, errors.New("error"))
s.mockClientDeployer.EXPECT().UnlockNonce().Times(1)
s.mockClientDeployer.EXPECT().WaitAndReturnTxReceipt(gomock.Any()).Times(0)
s.mockClientDeployer.EXPECT().UnsafeIncreaseNonce().Times(0)

toAddress := common.HexToAddress("0xtest1")

_, _ = calls.DeployBridge(
s.mockClientDeployer,
evmtransaction.NewTransaction,
s.mockgasPricer,
0x1,
[]common.Address{toAddress},
big.NewInt(2),
big.NewInt(10))
}

func (s *DeployTestSuite) TestDeployErc20HandlerNonceUnlockCallWithErrorThrown() {
s.mockClientDeployer.EXPECT().LockNonce().Times(1)
s.mockClientDeployer.EXPECT().UnsafeNonce().Return(big.NewInt(1), nil)
s.mockgasPricer.EXPECT().GasPrice().Return([]*big.Int{big.NewInt(10)}, nil)
s.mockClientDeployer.EXPECT().SignAndSendTransaction(gomock.Any(), gomock.Any()).Times(1).Return(common.Hash{}, errors.New("error"))
s.mockClientDeployer.EXPECT().UnlockNonce().Times(1)
s.mockClientDeployer.EXPECT().WaitAndReturnTxReceipt(gomock.Any()).Times(0)
s.mockClientDeployer.EXPECT().UnsafeIncreaseNonce().Times(0)

toAddress := common.HexToAddress("0xtest1")

_, _ = calls.DeployErc20Handler(
s.mockClientDeployer,
evmtransaction.NewTransaction,
s.mockgasPricer,
toAddress)
}

func (s *DeployTestSuite) TestDeployErc20HandlerNonceUnlockCallWithoutErrorsThrown() {
s.mockClientDeployer.EXPECT().LockNonce().Times(1)
s.mockClientDeployer.EXPECT().UnsafeNonce().Return(big.NewInt(1), nil)
s.mockgasPricer.EXPECT().GasPrice().Return([]*big.Int{big.NewInt(10)}, nil)
s.mockClientDeployer.EXPECT().SignAndSendTransaction(gomock.Any(), gomock.Any()).Times(1).Return(common.Hash{}, errors.New("error"))
s.mockClientDeployer.EXPECT().UnlockNonce().Times(1)
s.mockClientDeployer.EXPECT().WaitAndReturnTxReceipt(gomock.Any()).Times(0)
s.mockClientDeployer.EXPECT().UnsafeIncreaseNonce().Times(0)

toAddress := common.HexToAddress("0xtest1")

_, _ = calls.DeployErc20Handler(
s.mockClientDeployer,
evmtransaction.NewTransaction,
s.mockgasPricer,
toAddress)
}
51 changes: 0 additions & 51 deletions chains/evm/calls/mock/bridge.go

This file was deleted.

Loading