Skip to content

Commit

Permalink
Merge pull request #105 from InjectiveLabs/fix/estimate-gas
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchon authored Jan 11, 2024
2 parents 34baacb + 6ea8271 commit e8089a7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion orchestrator/batch_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (l *batchRequestLoop) requestBatches(ctx context.Context) error {
}

if len(fees) == 0 {
l.Logger().Debugln("no outgoing withdrawals to batch")
l.Logger().Debugln("no withdrawals to batch")
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions orchestrator/ethereum/committer/eth_committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package committer

import (
"context"
"github.com/ethereum/go-ethereum"
"math/big"
"strings"

Expand Down Expand Up @@ -114,6 +115,22 @@ func (e *ethCommitter) SendTx(
return common.Hash{}, errors.Errorf("Suggested gas price %v is greater than max gas price %v", opts.GasPrice.Int64(), maxGasPrice.Int64())
}

// estimate gas limit
msg := ethereum.CallMsg{
From: opts.From,
To: &recipient,
GasPrice: gasPrice,
Value: new(big.Int),
Data: txData,
}

gasLimit, err := e.evmProvider.EstimateGas(opts.Context, msg)
if err != nil {
return common.Hash{}, errors.Wrap(err, "failed to estimate gas")
}

opts.GasLimit = gasLimit

resyncNonces := func(from common.Address) {
e.nonceCache.Sync(from, func() (uint64, error) {
nonce, err := e.evmProvider.PendingNonceAt(context.TODO(), from)
Expand Down
6 changes: 3 additions & 3 deletions orchestrator/ethereum/peggy/submit_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (s *peggyContract) SendTransactionBatch(
log.WithFields(log.Fields{
"token_contract": batch.TokenContract,
"batch_nonce": batch.BatchNonce,
"transactions": len(batch.Transactions),
"txs": len(batch.Transactions),
"confirmations": len(confirms),
}).Debugln("checking signatures and submitting batch")
}).Infoln("checking signatures and submitting batch")

validators, powers, sigV, sigR, sigS, err := checkBatchSigsAndRepack(currentValset, confirms)
if err != nil {
Expand Down Expand Up @@ -93,7 +93,7 @@ func (s *peggyContract) SendTransactionBatch(
txHash, err := s.SendTx(ctx, s.peggyAddress, txData)
if err != nil {
metrics.ReportFuncError(s.svcTags)
log.WithError(err).WithField("tx_hash", txHash.Hex()).Errorln("Failed to sign and submit (Peggy submitBatch) to EVM")
log.WithError(err).WithField("tx_hash", txHash.Hex()).Errorln("failed to sign and submit (Peggy submitBatch) to EVM")
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion orchestrator/ethereum/peggy/valset_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *peggyContract) SendEthValsetUpdate(
"valset_nonce": newValset.Nonce,
"validators": len(newValset.Members),
"confirmations": len(confirms),
}).Debugln("checking signatures and submitting valset update")
}).Infoln("checking signatures and submitting valset update")

newValidators, newPowers := validatorsAndPowers(newValset)
newValsetNonce := new(big.Int).SetUint64(newValset.Nonce)
Expand Down
18 changes: 11 additions & 7 deletions orchestrator/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (l *relayerLoop) relayValset(ctx context.Context) error {
}

if oldestConfirmedValset.Nonce <= currentEthValset.Nonce {
l.Logger().WithFields(log.Fields{"eth_nonce": currentEthValset.Nonce, "inj_nonce": currentEthValset.Nonce}).Debugln("valset already updated on Ethereum")
return nil
}

Expand All @@ -135,10 +136,11 @@ func (l *relayerLoop) relayValset(ctx context.Context) error {

// Check if other validators already updated the valset
if oldestConfirmedValset.Nonce <= latestEthereumValsetNonce.Uint64() {
l.Logger().WithFields(log.Fields{"eth_nonce": latestEthereumValsetNonce, "inj_nonce": currentEthValset.Nonce}).Debugln("valset already updated on Ethereum")
return nil
}

l.Logger().WithFields(log.Fields{"inj_valset_nonce": oldestConfirmedValset.Nonce, "eth_valset_nonce": latestEthereumValsetNonce.Uint64()}).Debugln("latest valset updates")
l.Logger().WithFields(log.Fields{"inj_nonce": oldestConfirmedValset.Nonce, "eth_nonce": latestEthereumValsetNonce.Uint64()}).Debugln("latest valset updates")

// Check custom time delay offset
blockTime, err := l.inj.GetBlockCreationTime(ctx, int64(oldestConfirmedValset.Height))
Expand All @@ -148,7 +150,7 @@ func (l *relayerLoop) relayValset(ctx context.Context) error {

if timeElapsed := time.Since(blockTime); timeElapsed <= l.relayValsetOffsetDur {
timeRemaining := time.Duration(int64(l.relayValsetOffsetDur) - int64(timeElapsed))
l.Logger().WithField("time_remaining", timeRemaining.String()).Debugln("valset relay offset duration not expired")
l.Logger().WithField("time_remaining", timeRemaining.String()).Debugln("valset relay offset not reached yet")
return nil
}

Expand All @@ -162,7 +164,7 @@ func (l *relayerLoop) relayValset(ctx context.Context) error {
return err
}

l.Logger().WithField("tx_hash", txHash.Hex()).Infoln("sent valset update to Ethereum")
l.Logger().WithField("tx_hash", txHash.Hex()).Infoln("sent valset tx to Ethereum")

return nil
}
Expand Down Expand Up @@ -195,7 +197,7 @@ func (l *relayerLoop) relayBatch(ctx context.Context) error {
}

if oldestConfirmedBatch == nil {
l.Logger().Debugln("no outgoing batch to relay")
l.Logger().Debugln("no batch to relay")
return nil
}

Expand All @@ -212,6 +214,7 @@ func (l *relayerLoop) relayBatch(ctx context.Context) error {
}

if oldestConfirmedBatch.BatchNonce <= latestEthereumBatch.Uint64() {
l.Logger().WithFields(log.Fields{"eth_nonce": latestEthereumBatch.Uint64(), "inj_nonce": oldestConfirmedBatch.BatchNonce}).Debugln("batch already updated on Ethereum")
return nil
}

Expand All @@ -222,10 +225,11 @@ func (l *relayerLoop) relayBatch(ctx context.Context) error {

// Check if ethereum batch was updated by other validators
if oldestConfirmedBatch.BatchNonce <= latestEthereumBatch.Uint64() {
l.Logger().WithFields(log.Fields{"eth_nonce": latestEthereumBatch.Uint64(), "inj_nonce": oldestConfirmedBatch.BatchNonce}).Debugln("batch already updated on Ethereum")
return nil
}

l.Logger().WithFields(log.Fields{"inj_batch_nonce": oldestConfirmedBatch.BatchNonce, "eth_batch_nonce": latestEthereumBatch.Uint64()}).Debugln("latest batch updates")
l.Logger().WithFields(log.Fields{"inj_nonce": oldestConfirmedBatch.BatchNonce, "eth_nonce": latestEthereumBatch.Uint64()}).Debugln("latest batch updates")

// Check custom time delay offset
blockTime, err := l.inj.GetBlockCreationTime(ctx, int64(oldestConfirmedBatch.Block))
Expand All @@ -235,7 +239,7 @@ func (l *relayerLoop) relayBatch(ctx context.Context) error {

if timeElapsed := time.Since(blockTime); timeElapsed <= l.relayBatchOffsetDur {
timeRemaining := time.Duration(int64(l.relayBatchOffsetDur) - int64(timeElapsed))
l.Logger().WithField("time_remaining", timeRemaining.String()).Debugln("batch relay offset duration not expired")
l.Logger().WithField("time_remaining", timeRemaining.String()).Debugln("batch relay offset not reached yet")
return nil
}

Expand All @@ -245,7 +249,7 @@ func (l *relayerLoop) relayBatch(ctx context.Context) error {
return err
}

l.Logger().WithField("tx_hash", txHash.Hex()).Infoln("sent tx batch to Ethereum")
l.Logger().WithField("tx_hash", txHash.Hex()).Infoln("sent batch tx to Ethereum")

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (l *ethSignerLoop) signNewBatch(ctx context.Context) error {
}

if oldestUnsignedTransactionBatch == nil {
l.Logger().Debugln("no outgoing batch to confirm")
l.Logger().Debugln("no batch to confirm")
return nil
}

Expand Down

0 comments on commit e8089a7

Please sign in to comment.