Skip to content

Commit

Permalink
Invoke estimate gas limit if gas limit not already provided (#1582)
Browse files Browse the repository at this point in the history
* Invoke estimate gas limit if gas limit not already provided

* Added sender address in txn. Needed for estimateGasLimit

* Fixed failing e2e tests

* Provide fix gas limit for exit transaction (pass child chain mintable e2e test)

* Fix failing E2E tests

* uint64 to big.int change

---------

Co-authored-by: Stefan Negovanović <stefan@ethernal.tech>
Co-authored-by: Igor Crevar <crewce@gmail.com>
  • Loading branch information
3 people committed Jun 27, 2023
1 parent 1bfb902 commit bb8f77c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions command/bridge/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction,
From: sender,
To: &exitHelperAddr,
Input: input,
Gas: txrelayer.DefaultGasLimit,
}

return txn, exitEvent, err
Expand Down
4 changes: 2 additions & 2 deletions e2e-polybft/e2e/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ func TestE2E_Bridge_ChildChainMintableTokensTransfer(t *testing.T) {
cluster := framework.NewTestCluster(t, 5,
framework.WithNumBlockConfirmations(0),
framework.WithEpochSize(epochSize),
framework.WithNativeTokenConfig(fmt.Sprintf("Mintable Edge Coin:MEC:18:true:%s", adminAddr)),
framework.WithNativeTokenConfig(fmt.Sprintf(nativeTokenMintableTestCfg, adminAddr)),
framework.WithBridgeAllowListAdmin(adminAddr),
framework.WithBridgeBlockListAdmin(adminAddr),
framework.WithPremine(append(depositors, adminAddr)...)) //nolint:makezero
Expand Down Expand Up @@ -1295,7 +1295,7 @@ func TestE2E_Bridge_Transfers_AccessLists(t *testing.T) {
contracts.ChildERC20PredicateContract,
contracts.NativeERC20TokenContract,
false)
require.ErrorContains(t, err, "failed to execute withdrawal")
require.ErrorContains(t, err, "failed to send withdraw transaction")

currentBlock, err := childEthEndpoint.GetBlockByNumber(ethgo.Latest, false)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ func TestE2E_Consensus_MintableERC20NativeToken(t *testing.T) {
To: &nativeTokenAddr,
Input: mintInput,
}, nonMinterAcc.Ecdsa)
require.NoError(t, err)
require.Equal(t, uint64(types.ReceiptFailed), receipt.Status)
require.Error(t, err)
require.Nil(t, receipt)
}

func TestE2E_Consensus_CustomRewardToken(t *testing.T) {
Expand Down
21 changes: 14 additions & 7 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,23 +857,23 @@ func (c *TestCluster) Call(t *testing.T, to types.Address, method *abi.Method,
func (c *TestCluster) Deploy(t *testing.T, sender ethgo.Key, bytecode []byte) *TestTxn {
t.Helper()

return c.SendTxn(t, sender, &ethgo.Transaction{Input: bytecode})
return c.SendTxn(t, sender, &ethgo.Transaction{From: sender.Address(), Input: bytecode})
}

func (c *TestCluster) Transfer(t *testing.T, sender ethgo.Key, target types.Address, value *big.Int) *TestTxn {
t.Helper()

targetAddr := ethgo.Address(target)

return c.SendTxn(t, sender, &ethgo.Transaction{To: &targetAddr, Value: value})
return c.SendTxn(t, sender, &ethgo.Transaction{From: sender.Address(), To: &targetAddr, Value: value})
}

func (c *TestCluster) MethodTxn(t *testing.T, sender ethgo.Key, target types.Address, input []byte) *TestTxn {
t.Helper()

targetAddr := ethgo.Address(target)

return c.SendTxn(t, sender, &ethgo.Transaction{To: &targetAddr, Input: input})
return c.SendTxn(t, sender, &ethgo.Transaction{From: sender.Address(), To: &targetAddr, Input: input})
}

// SendTxn sends a transaction
Expand Down Expand Up @@ -906,7 +906,16 @@ func (c *TestCluster) SendTxn(t *testing.T, sender ethgo.Key, txn *ethgo.Transac
}

if txn.Gas == 0 {
txn.Gas = txrelayer.DefaultGasLimit
callMsg := txrelayer.ConvertTxnToCallMsg(txn)

gasLimit, err := client.Eth().EstimateGas(callMsg)
if err != nil {
// gas estimation can fail in case an account is not allow-listed
// (fallback it to default gas limit in that case)
txn.Gas = txrelayer.DefaultGasLimit
} else {
txn.Gas = gasLimit
}
}

chainID, err := client.Eth().ChainID()
Expand All @@ -922,13 +931,11 @@ func (c *TestCluster) SendTxn(t *testing.T, sender ethgo.Key, txn *ethgo.Transac
hash, err := client.Eth().SendRawTransaction(txnRaw)
require.NoError(t, err)

tTxn := &TestTxn{
return &TestTxn{
client: client.Eth(),
txn: txn,
hash: hash,
}

return tTxn
}

type TestTxn struct {
Expand Down
30 changes: 28 additions & 2 deletions txrelayer/txrelayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"math/big"
"sync"
"time"

Expand Down Expand Up @@ -98,6 +99,8 @@ func (t *TxRelayerImpl) sendTransactionLocked(txn *ethgo.Transaction, key ethgo.
t.lock.Lock()
defer t.lock.Unlock()

txn.From = key.Address()

nonce, err := t.client.Eth().GetNonce(key.Address(), ethgo.Pending)
if err != nil {
return ethgo.ZeroHash, err
Expand All @@ -117,7 +120,12 @@ func (t *TxRelayerImpl) sendTransactionLocked(txn *ethgo.Transaction, key ethgo.
}

if txn.Gas == 0 {
txn.Gas = DefaultGasLimit
gasLimit, err := t.client.Eth().EstimateGas(ConvertTxnToCallMsg(txn))
if err != nil {
return ethgo.ZeroHash, err
}

txn.Gas = gasLimit
}

chainID, err := t.client.Eth().ChainID()
Expand Down Expand Up @@ -157,7 +165,13 @@ func (t *TxRelayerImpl) SendTransactionLocal(txn *ethgo.Transaction) (*ethgo.Rec
}

txn.From = accounts[0]
txn.Gas = DefaultGasLimit

gasLimit, err := t.client.Eth().EstimateGas(ConvertTxnToCallMsg(txn))
if err != nil {
return nil, err
}

txn.Gas = gasLimit
txn.GasPrice = defaultGasPrice

txnHash, err := t.client.Eth().SendTransaction(txn)
Expand Down Expand Up @@ -192,6 +206,18 @@ func (t *TxRelayerImpl) waitForReceipt(hash ethgo.Hash) (*ethgo.Receipt, error)
}
}

// ConvertTxnToCallMsg converts txn instance to call message
func ConvertTxnToCallMsg(txn *ethgo.Transaction) *ethgo.CallMsg {
return &ethgo.CallMsg{
From: txn.From,
To: txn.To,
Data: txn.Input,
GasPrice: txn.GasPrice,
Value: txn.Value,
Gas: new(big.Int).SetUint64(txn.Gas),
}
}

type TxRelayerOption func(*TxRelayerImpl)

func WithClient(client *jsonrpc.Client) TxRelayerOption {
Expand Down

0 comments on commit bb8f77c

Please sign in to comment.