Skip to content

Commit

Permalink
Transaction relayer fetching gas price from current eth client instea…
Browse files Browse the repository at this point in the history
…d of using hardcoded value (#1589)

* modified txRelayer to fetch gasprice from current eth client instead of hardcoded one

* fetch gas price only if default is 0

* Don't set gas price explicitly when using tx relayer

* unexported defaultGasPrice constant

---------

Co-authored-by: adirola <adirola@gmail.com>
  • Loading branch information
dusan-maksimovic and adirola committed Jun 7, 2023
1 parent ef33b05 commit 5318ff4
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 88 deletions.
12 changes: 3 additions & 9 deletions command/rootchain/staking/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return err
}

gasPrice, err := txRelayer.Client().Eth().GasPrice()
if err != nil {
return err
}

approveTxn, err := rootHelper.CreateApproveERC20Txn(params.amountValue,
types.StringToAddress(params.stakeManagerAddr), types.StringToAddress(params.stakeTokenAddr))
if err != nil {
Expand Down Expand Up @@ -134,10 +129,9 @@ func runCommand(cmd *cobra.Command, _ []string) error {

stakeManagerAddr := ethgo.Address(types.StringToAddress(params.stakeManagerAddr))
txn := &ethgo.Transaction{
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: &stakeManagerAddr,
GasPrice: gasPrice,
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: &stakeManagerAddr,
}

receipt, err = txRelayer.SendTransaction(txn, validatorAccount.Ecdsa)
Expand Down
19 changes: 6 additions & 13 deletions command/rootchain/supernet/supernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("enlist validator failed: %w", err)
}

gasPrice, err := txRelayer.Client().Eth().GasPrice()
if err != nil {
return err
}

supernetAddr := ethgo.Address(types.StringToAddress(params.supernetManagerAddress))

if params.finalizeGenesisSet {
Expand All @@ -131,10 +126,9 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

txn := &ethgo.Transaction{
From: ownerKey.Address(),
Input: encoded,
To: &supernetAddr,
GasPrice: gasPrice,
From: ownerKey.Address(),
Input: encoded,
To: &supernetAddr,
}

if _, err = txRelayer.Call(ownerKey.Address(), supernetAddr, encoded); err == nil {
Expand Down Expand Up @@ -185,10 +179,9 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

txn := &ethgo.Transaction{
From: ownerKey.Address(),
Input: encoded,
To: &supernetAddr,
GasPrice: gasPrice,
From: ownerKey.Address(),
Input: encoded,
To: &supernetAddr,
}

receipt, err := txRelayer.SendTransaction(txn, ownerKey)
Expand Down
12 changes: 3 additions & 9 deletions command/rootchain/whitelist/whitelist_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("whitelist validator failed. Could not create tx relayer: %w", err)
}

gasPrice, err := txRelayer.Client().Eth().GasPrice()
if err != nil {
return err
}

whitelistFn := &contractsapi.WhitelistValidatorsCustomSupernetManagerFn{
Validators_: stringSliceToAddressSlice(params.newValidatorAddresses),
}
Expand All @@ -110,10 +105,9 @@ func runCommand(cmd *cobra.Command, _ []string) error {

supernetAddr := ethgo.Address(types.StringToAddress(params.supernetManagerAddress))
txn := &ethgo.Transaction{
From: ecdsaKey.Address(),
Input: encoded,
To: &supernetAddr,
GasPrice: gasPrice,
From: ecdsaKey.Address(),
Input: encoded,
To: &supernetAddr,
}

receipt, err := txRelayer.SendTransaction(txn, ecdsaKey)
Expand Down
12 changes: 3 additions & 9 deletions command/rootchain/withdraw/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,11 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return err
}

gasPrice, err := txRelayer.Client().Eth().GasPrice()
if err != nil {
return err
}

stakeManagerAddr := ethgo.Address(types.StringToAddress(params.stakeManagerAddr))
txn := &ethgo.Transaction{
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: &stakeManagerAddr,
GasPrice: gasPrice,
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: &stakeManagerAddr,
}

receipt, err := txRelayer.SendTransaction(txn, validatorAccount.Ecdsa)
Expand Down
2 changes: 0 additions & 2 deletions command/sidechain/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (

const (
AmountFlag = "amount"

DefaultGasPrice = 1879048192 // 0x70000000
)

func CheckIfDirectoryExist(dir string) error {
Expand Down
7 changes: 3 additions & 4 deletions command/sidechain/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

txn := &ethgo.Transaction{
From: validatorAddr,
Input: encoded,
To: &rewardPoolAddr,
GasPrice: sidechainHelper.DefaultGasPrice,
From: validatorAddr,
Input: encoded,
To: &rewardPoolAddr,
}

receipt, err := txRelayer.SendTransaction(txn, validatorAccount.Ecdsa)
Expand Down
7 changes: 3 additions & 4 deletions command/sidechain/unstaking/unstake.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

txn := &ethgo.Transaction{
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: (*ethgo.Address)(&contracts.ValidatorSetContract),
GasPrice: sidechainHelper.DefaultGasPrice,
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: (*ethgo.Address)(&contracts.ValidatorSetContract),
}

receipt, err := txRelayer.SendTransaction(txn, validatorAccount.Ecdsa)
Expand Down
7 changes: 3 additions & 4 deletions command/sidechain/withdraw/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

txn := &ethgo.Transaction{
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: (*ethgo.Address)(&contracts.ValidatorSetContract),
GasPrice: sidechainHelper.DefaultGasPrice,
From: validatorAccount.Ecdsa.Address(),
Input: encoded,
To: (*ethgo.Address)(&contracts.ValidatorSetContract),
}

receipt, err := txRelayer.SendTransaction(txn, validatorAccount.Ecdsa)
Expand Down
9 changes: 4 additions & 5 deletions consensus/polybft/statesyncrelayer/state_sync_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,10 @@ func (r *StateSyncRelayer) executeStateSync(proof *types.Proof) error {

// execute the state sync
txn := &ethgo.Transaction{
From: r.key.Address(),
To: (*ethgo.Address)(&contracts.StateReceiverContract),
GasPrice: 0,
Gas: types.StateTransactionGasLimit,
Input: input,
From: r.key.Address(),
To: (*ethgo.Address)(&contracts.StateReceiverContract),
Gas: types.StateTransactionGasLimit,
Input: input,
}

receipt, err := r.txRelayer.SendTransaction(txn, r.key)
Expand Down
14 changes: 6 additions & 8 deletions e2e-polybft/e2e/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ func TestE2E_JsonRPC(t *testing.T) {

toAddr := key1.Address()
msg := &ethgo.CallMsg{
From: acct.Address(),
To: &toAddr,
Value: newBalance,
GasPrice: gasPrice,
From: acct.Address(),
To: &toAddr,
Value: newBalance,
}

estimatedGas, err := client.EstimateGas(msg)
Expand All @@ -99,10 +98,9 @@ func TestE2E_JsonRPC(t *testing.T) {
amountToSend := new(big.Int).Sub(newBalance, big.NewInt(int64(txPrice)))
targetAddr := acct.Address()
txn = cluster.SendTxn(t, key1, &ethgo.Transaction{
To: &targetAddr,
Value: amountToSend,
GasPrice: gasPrice,
Gas: estimatedGas,
To: &targetAddr,
Value: amountToSend,
Gas: estimatedGas,
})
require.NoError(t, txn.Wait())
require.True(t, txn.Succeed())
Expand Down
16 changes: 7 additions & 9 deletions e2e-polybft/e2e/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,18 @@ func TestE2E_Migration(t *testing.T) {
//send transaction to user2
sendAmount := ethgo.Gwei(10000)
receipt, err := relayer.SendTransaction(&ethgo.Transaction{
From: userAddr,
To: &userAddr2,
GasPrice: 1048576,
Gas: 1000000,
Value: sendAmount,
From: userAddr,
To: &userAddr2,
Gas: 1000000,
Value: sendAmount,
}, userKey)
assert.NoError(t, err)
assert.NotNil(t, receipt)

receipt, err = relayer.SendTransaction(&ethgo.Transaction{
From: userAddr,
GasPrice: 1048576,
Gas: 1000000,
Input: contractsapi.TestWriteBlockMetadata.Bytecode,
From: userAddr,
Gas: 1000000,
Input: contractsapi.TestWriteBlockMetadata.Bytecode,
}, userKey)
require.NoError(t, err)
require.NotNil(t, receipt)
Expand Down
5 changes: 4 additions & 1 deletion e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,10 @@ func (c *TestCluster) SendTxn(t *testing.T, sender ethgo.Key, txn *ethgo.Transac
}

if txn.GasPrice == 0 {
txn.GasPrice = txrelayer.DefaultGasPrice
gasPrice, err := client.Eth().GasPrice()
require.NoError(t, err)

txn.GasPrice = gasPrice
}

if txn.Gas == 0 {
Expand Down
13 changes: 6 additions & 7 deletions e2e/framework/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,6 @@ func (t *Txn) GasLimit(gas uint64) *Txn {
return t
}

func (t *Txn) GasPrice(price uint64) *Txn {
t.raw.GasPrice = price

return t
}

func (t *Txn) Nonce(nonce uint64) *Txn {
t.raw.Nonce = nonce

Expand All @@ -645,7 +639,12 @@ func (t *Txn) sendImpl() error {
}

if t.raw.GasPrice == 0 {
t.raw.GasPrice = 1048576
gasPrice, err := t.client.GasPrice()
if err != nil {
return fmt.Errorf("failed to get gas price: %w", err)
}

t.raw.GasPrice = gasPrice
}

if t.raw.Nonce == 0 {
Expand Down
1 change: 0 additions & 1 deletion e2e/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func TestJsonRPC(t *testing.T) {
amountToSend := new(big.Int).Sub(newBalance, big.NewInt(int64(txPrice)))
txn, err = srv.Txn(key1).Transfer(fund.Address(), amountToSend).
GasLimit(estimatedGas).
GasPrice(gasPrice).
Send()
require.NoError(t, err)
txn.NoFail(t)
Expand Down
11 changes: 8 additions & 3 deletions txrelayer/txrelayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
DefaultGasPrice = 1879048192 // 0x70000000
defaultGasPrice = 1879048192 // 0x70000000
DefaultGasLimit = 5242880 // 0x500000
DefaultRPCAddress = "http://127.0.0.1:8545"
numRetries = 1000
Expand Down Expand Up @@ -103,7 +103,12 @@ func (t *TxRelayerImpl) sendTransactionLocked(txn *ethgo.Transaction, key ethgo.
txn.Nonce = nonce

if txn.GasPrice == 0 {
txn.GasPrice = DefaultGasPrice
gasPrice, err := t.Client().Eth().GasPrice()
if err != nil {
return ethgo.ZeroHash, err
}

txn.GasPrice = gasPrice
}

if txn.Gas == 0 {
Expand Down Expand Up @@ -142,7 +147,7 @@ func (t *TxRelayerImpl) SendTransactionLocal(txn *ethgo.Transaction) (*ethgo.Rec

txn.From = accounts[0]
txn.Gas = DefaultGasLimit
txn.GasPrice = DefaultGasPrice
txn.GasPrice = defaultGasPrice

txnHash, err := t.client.Eth().SendTransaction(txn)
if err != nil {
Expand Down

0 comments on commit 5318ff4

Please sign in to comment.