Skip to content

Commit

Permalink
Merge branch 'main' into feat/bridge-ui--update-to-new-protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK authored Mar 22, 2024
2 parents 399b066 + 34e7207 commit df64a84
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
9 changes: 9 additions & 0 deletions packages/relayer/cmd/flags/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ var (
EnvVars: []string{"CACHE_OPTION"},
Value: 3,
}
GasIncreaseRate = &cli.Uint64Flag{
Name: "gasIncreaseRate",
Usage: "Rate at which to increase gas when 'replacement transaction underpriced' error",
Category: processorCategory,
Required: false,
EnvVars: []string{"GAS_INCREASE_RATE"},
Value: 15,
}
)

var ProcessorFlags = MergeFlags(CommonFlags, QueueFlags, []cli.Flag{
Expand All @@ -143,4 +151,5 @@ var ProcessorFlags = MergeFlags(CommonFlags, QueueFlags, []cli.Flag{
DestBridgeAddress,
TargetTxHash,
CacheOption,
GasIncreaseRate,
})
4 changes: 3 additions & 1 deletion packages/relayer/processor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ type Config struct {

hopConfigs []hopConfig

CacheOption int
CacheOption int
GasIncreaseRate uint64
}

// NewConfigFromCliContext creates a new config instance from command line flags.
Expand Down Expand Up @@ -148,6 +149,7 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
ETHClientTimeout: c.Uint64(flags.ETHClientTimeout.Name),
TargetTxHash: targetTxHash,
CacheOption: c.Int(flags.CacheOption.Name),
GasIncreaseRate: c.Uint64(flags.GasIncreaseRate.Name),
OpenDBFunc: func() (DB, error) {
return db.OpenDBConnection(db.DBConnectionOpts{
Name: c.String(flags.DatabaseUsername.Name),
Expand Down
73 changes: 64 additions & 9 deletions packages/relayer/processor/process_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,35 @@ func (p *Processor) sendProcessMessageAndWaitForReceipt(

var err error

var updateGas bool = true

auth, err := bind.NewKeyedTransactorWithChainID(
p.ecdsaKey,
new(big.Int).SetUint64(msgBody.Event.Message.DestChainId))
if err != nil {
return nil, errors.Wrap(err, "bind.NewKeyedTransactorWithChainID")
}

sendTx := func() error {
if ctx.Err() != nil {
return nil
}

tx, err = p.sendProcessMessageCall(ctx, msgBody.Event, encodedSignalProof)
tx, err = p.sendProcessMessageCall(ctx, auth, msgBody.Event, encodedSignalProof, updateGas)
if err != nil {
if strings.Contains(err.Error(), "transaction underpriced") {
slog.Warn(
"Replacement transaction underpriced",
"nonce", tx.Nonce(),
"hash", tx.Hash(),
"err", err,
)

p.increaseGas(ctx, auth)

updateGas = false
}

return err
}

Expand Down Expand Up @@ -249,6 +271,40 @@ func (p *Processor) sendProcessMessageAndWaitForReceipt(
return receipt, nil
}

func (p *Processor) increaseGas(ctx context.Context, auth *bind.TransactOpts) {
slog.Info("increasing gas fee for retry",
"gasFeeCap", auth.GasFeeCap,
"gasTipCap", auth.GasTipCap,
)

// Increase the gas price by at least 10%
if auth.GasFeeCap != nil {
gasFeeCap := auth.GasFeeCap.Int64()
gasFeeCap += gasFeeCap * int64(p.gasIncreaseRate) / 100
auth.GasFeeCap = big.NewInt(gasFeeCap)
}

if auth.GasTipCap != nil {
gasTipCap := auth.GasTipCap.Int64()
gasTipCap += gasTipCap * int64(p.gasIncreaseRate) / 100
auth.GasTipCap = big.NewInt(gasTipCap)
}

if auth.GasPrice != nil {
gasPrice := auth.GasPrice.Int64()
gasPrice += gasPrice * int64(p.gasIncreaseRate) / 100
auth.GasPrice = big.NewInt(gasPrice)
}

slog.Info("updated gas",
"gasFeeCap",
auth.GasFeeCap,
"gasTipCap",
auth.GasTipCap,
"gasPrice", auth.GasPrice,
)
}

// waitForInvocationDelay will return when the invocation delay has been met,
// if one exists, or return immediately if not.
func (p *Processor) waitForInvocationDelay(
Expand Down Expand Up @@ -485,20 +541,17 @@ func (p *Processor) generateEncodedSignalProof(ctx context.Context,
// after estimating gas, and checking profitability.
func (p *Processor) sendProcessMessageCall(
ctx context.Context,
auth *bind.TransactOpts,
event *bridge.BridgeMessageSent,
proof []byte,
updateGas bool,
) (*types.Transaction, error) {
auth, err := bind.NewKeyedTransactorWithChainID(p.ecdsaKey, new(big.Int).SetUint64(event.Message.DestChainId))
if err != nil {
return nil, errors.Wrap(err, "bind.NewKeyedTransactorWithChainID")
}

auth.Context = ctx

p.mu.Lock()
defer p.mu.Unlock()

err = p.getLatestNonce(ctx, auth)
err := p.getLatestNonce(ctx, auth)
if err != nil {
return nil, errors.New("p.getLatestNonce")
}
Expand Down Expand Up @@ -536,8 +589,10 @@ func (p *Processor) sendProcessMessageCall(
}
}

if err = utils.SetGasTipOrPrice(ctx, auth, p.destEthClient); err != nil {
return nil, errors.Wrap(err, "p.setGasTipOrPrice")
if updateGas {
if err = utils.SetGasTipOrPrice(ctx, auth, p.destEthClient); err != nil {
return nil, errors.Wrap(err, "p.setGasTipOrPrice")
}
}

cost, err = p.getCost(ctx, auth)
Expand Down
4 changes: 3 additions & 1 deletion packages/relayer/processor/process_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
Expand All @@ -22,6 +23,7 @@ func Test_sendProcessMessageCall(t *testing.T) {

_, err := p.sendProcessMessageCall(
context.Background(),
&bind.TransactOpts{},
&bridge.BridgeMessageSent{
Message: bridge.IBridgeMessage{
DestChainId: mock.MockChainID.Uint64(),
Expand All @@ -36,7 +38,7 @@ func Test_sendProcessMessageCall(t *testing.T) {
},
Data: []byte{0xff},
},
}, []byte{})
}, []byte{}, true)

assert.Nil(t, err)

Expand Down
3 changes: 3 additions & 0 deletions packages/relayer/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ type Processor struct {
targetTxHash *common.Hash // optional, set to target processing a specific txHash only

cfg *Config

gasIncreaseRate uint64
}

// InitFromCli creates a new processor from a cli context
Expand Down Expand Up @@ -331,6 +333,7 @@ func InitFromConfig(ctx context.Context, p *Processor, cfg *Config) error {
p.ethClientTimeout = time.Duration(cfg.ETHClientTimeout) * time.Second

p.targetTxHash = cfg.TargetTxHash
p.gasIncreaseRate = cfg.GasIncreaseRate

return nil
}
Expand Down

0 comments on commit df64a84

Please sign in to comment.