Skip to content

Commit

Permalink
Merge ee9eb85 into 60f0e4c
Browse files Browse the repository at this point in the history
  • Loading branch information
parodime authored Jan 28, 2025
2 parents 60f0e4c + ee9eb85 commit 6f21782
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 21 deletions.
3 changes: 3 additions & 0 deletions docs/bridge/docs/06-Services/04-Submitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ submitter_config:
dynamic_gas_estimate: true
# SupportsEIP1559 is whether or not this chain supports EIP1559.
supports_eip_1559: true
# DynamicGasUnitAddPercentage - increase gas unit limit (ie: "gas" field on a typical tx) by X% from what dynamic gas estimate returns
# Has no effect if dynamic gas estimation is not also enabled.
dynamic_gas_unit_add_percentage: 5
43114:
max_gas_price: 100000000000 # 100 Gwei
10:
Expand Down
28 changes: 26 additions & 2 deletions ethergo/submitter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type ChainConfig struct {
DynamicGasEstimate bool `yaml:"dynamic_gas_estimate"`
// SupportsEIP1559 is whether or not this chain supports EIP1559
SupportsEIP1559 bool `yaml:"supports_eip_1559"`
// DynamicGasUnitAddPercentage - increase gas unit limit (ie: "gas" field on a typical tx) by X% from what dynamic gas estimate returns
// Has no effect if dynamic gas estimation is not also enabled.
DynamicGasUnitAddPercentage int `yaml:"dynamic_gas_unit_add_percentage"`
}

const (
Expand All @@ -64,6 +67,9 @@ const (

// DefaultGasEstimate is the default gas estimate to use for transactions.
DefaultGasEstimate = uint64(1200000)

// DefaultDynamicGasUnitAddPercentage is the default percentage to bump the gas limit by.
DefaultDynamicGasUnitAddPercentage = 5
)

// DefaultMaxPrice is the default max price of a tx.
Expand Down Expand Up @@ -188,19 +194,37 @@ func (c *Config) GetGasBumpPercentage(chainID int) (gasBumpPercentage int) {
return gasBumpPercentage
}

// GetDynamicGasUnitAddPercentage returns the percentage to bump the gas limit by
func (c *Config) GetDynamicGasUnitAddPercentage(chainID int) (dynamicGasUnitAddPercentage int) {
chainConfig, ok := c.Chains[chainID]
if ok {
dynamicGasUnitAddPercentage = chainConfig.DynamicGasUnitAddPercentage
}
// if dynamicGasUnitAddPercentage is not set for the chain, use the global config
if dynamicGasUnitAddPercentage == 0 {
dynamicGasUnitAddPercentage = c.DynamicGasUnitAddPercentage
}

// if the dynamicGasUnitAddPercentage isn't set at all, use the default
if dynamicGasUnitAddPercentage == 0 {
dynamicGasUnitAddPercentage = DefaultDynamicGasUnitAddPercentage
}
return dynamicGasUnitAddPercentage
}

// GetGasEstimate returns the gas estimate to use for transactions
// TODO: test this method.
func (c *Config) GetGasEstimate(chainID int) (gasEstimate uint64) {
chainConfig, ok := c.Chains[chainID]
if ok {
gasEstimate = chainConfig.GasEstimate
}
// if gasBumpPercentage is not set for the chain, use the global config
// if gasEstimate is not set for the chain, use the global config
if gasEstimate == 0 {
gasEstimate = c.GasEstimate
}

// if the gasBumpPercentage isn't set at all, use the default
// if the gasEstimate isn't set at all, use the default
if gasEstimate == 0 {
gasEstimate = DefaultGasEstimate
}
Expand Down
2 changes: 2 additions & 0 deletions ethergo/submitter/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ gas_bump_percentage: 10
gas_estimate: 1000
is_l2: true
dynamic_gas_estimate: true
dynamic_gas_unit_add_percentage: 20
supports_eip_1559: true`
var cfg config.Config
err := yaml.Unmarshal([]byte(cfgStr), &cfg)
assert.NoError(t, err)
assert.Equal(t, big.NewInt(250000000000), cfg.MaxGasPrice)
assert.Equal(t, 60, cfg.BumpIntervalSeconds)
assert.Equal(t, 10, cfg.GasBumpPercentage)
assert.Equal(t, 20, cfg.DynamicGasUnitAddPercentage)
assert.Equal(t, uint64(1000), cfg.GasEstimate)
assert.Equal(t, true, cfg.DynamicGasEstimate)
assert.Equal(t, true, cfg.SupportsEIP1559(0))
Expand Down
2 changes: 2 additions & 0 deletions ethergo/submitter/config/iconfig_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 88 additions & 9 deletions ethergo/submitter/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ func (t *txSubmitterImpl) SubmitTransaction(parentCtx context.Context, chainID *
// this also prevents a bug in the caller from breaking our lock
transactor.Nonce = new(big.Int).Add(new(big.Int).SetUint64(math.MaxUint64), big.NewInt(1))

//tmpdebug
fmt.Printf("SubmitTransaction>setGasPrice\n")

err = t.setGasPrice(ctx, chainClient, transactor, chainID, nil)
if err != nil {
span.AddEvent("could not set gas price", trace.WithAttributes(attribute.String("error", err.Error())))
}
if !t.config.GetDynamicGasEstimate(int(chainID.Uint64())) {
transactor.GasLimit = t.config.GetGasEstimate(int(chainID.Uint64()))
}

transactor.Signer = func(address common.Address, transaction *types.Transaction) (_ *types.Transaction, err error) {
locker = t.nonceMux.Lock(chainID)
Expand All @@ -421,12 +421,80 @@ func (t *txSubmitterImpl) SubmitTransaction(parentCtx context.Context, chainID *
//nolint: wrapcheck
return parentTransactor.Signer(address, transaction)
}

//tmpdebug
fmt.Printf("test ver 6\n")

// if dynamic gas estimation is not enabled, use cfg var gas_estimate as a gas limit default and do not run a pre-flight simulation
// since we do not need it to determine proper gas units
if !t.config.GetDynamicGasEstimate(int(chainID.Uint64())) {

//tmpdebug
fmt.Printf("Using Default \n")

transactor.GasLimit = t.config.GetGasEstimate(int(chainID.Uint64()))
} else {

// //tmpdebug
// fmt.Printf("SubmitTransaction>forGasEst call \n")

// transactor_forGasEstimate := copyTransactOpts(transactor)

// transactor_forGasEstimate.Nonce.Add(transactor_forGasEstimate.Nonce, big.NewInt(1))

// tx_forGasEstimate, err := call(transactor_forGasEstimate)

// fmt.Printf("tx_forGasEstimate: %v\n", tx_forGasEstimate.Gas())

// if err != nil {
// return 0, fmt.Errorf("err contract call for gas est: %w", err)
// }

// // gasEstimate, err := t.getGasEstimate(ctx, chainClient, int(chainID.Uint64()), tx_forGasEstimate)
// // if err != nil {
// // return 0, fmt.Errorf("err getGasEstimate: %w", err)
// // }

// transactor.GasLimit = tx_forGasEstimate.Gas() + 555

transactor.GasLimit = 21555 // intentionally set too low

}

//tmpdebug
fmt.Printf("transactor.GasLimit: %d\n", transactor.GasLimit)

tx, err := call(transactor)
if err != nil {
return 0, fmt.Errorf("could not call contract: %w", err)
return 0, fmt.Errorf("err contract call for tx: %w", err)
}

// if t.config.GetDynamicGasEstimate(int(chainID.Uint64())) {

// //tmpdebug
// fmt.Printf("tx.Gas (pre): %d\n", tx.Gas())
// transactor.GasLimit = tx.Gas() + 555

// //tmpdebug
// fmt.Printf("transactor.GasLimit2: %d\n", transactor.GasLimit)

// tx, err = call(transactor)
// if err != nil {
// return 0, fmt.Errorf("err contract call 2 for tx: %w", err)
// }

// //tmpdebug
// fmt.Printf("tx.Gas (post): %d\n", tx.Gas())
// }

//tmpdebug
fmt.Printf("tx.Gas: %d\n", tx.Gas())

defer locker.Unlock()

//tmpdebug
fmt.Printf("SubmitTransaction>storeTX\n")

// now that we've stored the tx
err = t.storeTX(ctx, tx, db.Stored, uuid.New().String())
if err != nil {
Expand All @@ -436,6 +504,9 @@ func (t *txSubmitterImpl) SubmitTransaction(parentCtx context.Context, chainID *
span.AddEvent("trigger reprocess")
t.triggerProcessQueue(ctx)

//tmpdebug
fmt.Printf("SubmitTransaction>tx.Nonce: %d\n", tx.Nonce())

return tx.Nonce(), nil
}

Expand Down Expand Up @@ -677,13 +748,23 @@ func (t *txSubmitterImpl) getGasBlock(ctx context.Context, chainClient client.EV
// getGasEstimate gets the gas estimate for the given transaction.
// TODO: handle l2s w/ custom gas pricing through contracts.
func (t *txSubmitterImpl) getGasEstimate(ctx context.Context, chainClient client.EVM, chainID int, tx *types.Transaction) (gasEstimate uint64, err error) {

// if dynamic gas estimation is not enabled, use cfg var gas_estimate as a default
if !t.config.GetDynamicGasEstimate(chainID) {
return t.config.GetGasEstimate(chainID), nil
}

//tmpdebug
fmt.Println("getGasEstimate>start")

gasUnitAddPercentage := t.config.GetDynamicGasUnitAddPercentage(chainID)

ctx, span := t.metrics.Tracer().Start(ctx, "submitter.getGasEstimate", trace.WithAttributes(
attribute.Int(metrics.ChainID, chainID),

attribute.String(metrics.TxHash, tx.Hash().String()),

attribute.Int("gasUnitAddPercentage", gasUnitAddPercentage),
))

defer func() {
Expand All @@ -696,20 +777,18 @@ func (t *txSubmitterImpl) getGasEstimate(ctx context.Context, chainClient client
if err != nil {
return 0, fmt.Errorf("could not convert tx to call: %w", err)
}
// tmpdebug
fmt.Printf("Debug Calling EstimateGas")

gasEstimate, err = chainClient.EstimateGas(ctx, *call)
if err != nil {
span.AddEvent("could not estimate gas", trace.WithAttributes(attribute.String("error", err.Error())))

// tmpdebug
fmt.Printf("Debug Default Gas Estimate: %d\n", t.config.GetGasEstimate(chainID))

// fallback to default
return t.config.GetGasEstimate(chainID), nil
}

// Modify the gasEstimate by the configured percentage
gasEstimate = gasEstimate + (gasEstimate * uint64(gasUnitAddPercentage) / 100)

return gasEstimate, nil
}

Expand Down
15 changes: 15 additions & 0 deletions ethergo/submitter/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ func (s *SubmitterSuite) TestGroupTxesByNonce() {
}
}

func (s *SubmitterSuite) TestOpStackGas() {

mockTx := mocks.GetMockTxes(s.GetTestContext(), s.T(), 1, types.LegacyTxType)[0]

fmt.Printf("Original Transaction Gas Limit: %d\n", mockTx.Gas())

}

func TestBox(t *testing.T) {
const testTxCount = 10
mockTx := mocks.GetMockTxes(context.Background(), t, testTxCount, 0)

fmt.Printf("Original Transaction Gas Limit: %d\n", mockTx[0].Gas())
}

// Test for the outersection function.
func TestOutersection(t *testing.T) {
set := []*big.Int{
Expand Down
20 changes: 20 additions & 0 deletions services/rfq/relayer/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (c Chain) SubmitRelay(ctx context.Context, request reldb.QuoteRequest) (uin
gasAmount := big.NewInt(0)
var err error

//tmpdebug
fmt.Println("SubmitRelay>start: ", request.OriginTxHash)

// Check to see if ETH should be sent to destination
if util.IsGasToken(request.Transaction.DestToken) {
gasAmount = request.Transaction.DestAmount
Expand All @@ -86,19 +89,36 @@ func (c Chain) SubmitRelay(ctx context.Context, request reldb.QuoteRequest) (uin
}
}

//tmpdebug
fmt.Println("SubmitRelay>SubmitTransaction: ", request.OriginTxHash)

nonce, err := c.SubmitTransaction(ctx, func(transactor *bind.TransactOpts) (tx *types.Transaction, err error) {
transactor.Value = core.CopyBigInt(gasAmount)

callType := "exec"
if transactor.GasLimit == 0 {
callType = "sim"
}
//tmpdebug
fmt.Println(callType, "SubmitTransaction>RelayV2: ", request.OriginTxHash)

tx, err = c.Bridge.RelayV2(transactor, request.RawRequest, c.submitter.Address())

if err != nil {
return nil, fmt.Errorf("could not relay: %w", err)
}

//tmpdebug
fmt.Println(callType, "RelayV2 OriginTxHash:", request.OriginTxHash)
fmt.Printf(callType+" RelayV2 transaction: %+v\n", tx)

return tx, nil
})
if err != nil {
return 0, nil, fmt.Errorf("could not submit transaction: %w", err)
}
//tmpdebug
fmt.Println("SubmitRelay nonce:", nonce, "gas amount:", gasAmount)

return nonce, gasAmount, nil
}
9 changes: 0 additions & 9 deletions services/rfq/relayer/quoter/quoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,28 +693,19 @@ func (m *Manager) generateQuote(ctx context.Context, input QuoteInput) (quote *m
return nil, fmt.Errorf("error getting total fee: %w", err)
}

// tmpdebug
fmt.Printf("Debug Total Fee Amt: %s\n", fee.String())

originRFQAddr, err := m.config.GetRFQAddress(input.OriginChainID)
if err != nil {
logger.Error("Error getting RFQ address", "error", err)
return nil, fmt.Errorf("error getting RFQ address: %w", err)
}

// tmpdebug
fmt.Printf("Debug originRFQAddr: %s\n", originRFQAddr.String())

// Build the quote
destAmount, err := m.getDestAmount(ctx, originAmount, destToken, input)
if err != nil {
logger.Error("Error getting dest amount", "error", err)
return nil, fmt.Errorf("error getting dest amount: %w", err)
}

// tmpdebug
fmt.Printf("Debug destAmount: %s\n", destAmount.String())

quote = &model.PutRelayerQuoteRequest{
OriginChainID: input.OriginChainID,
OriginTokenAddr: input.OriginTokenAddr.Hex(),
Expand Down
13 changes: 12 additions & 1 deletion services/rfq/relayer/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ func (q *QuoteRequestHandler) handleCommitPending(ctx context.Context, span trac
// This is the fourth step in the bridge process. Here we submit the relay transaction to the destination chain.
// TODO: just to be safe, we should probably check if another relayer has already relayed this.
func (q *QuoteRequestHandler) handleCommitConfirmed(ctx context.Context, span trace.Span, request reldb.QuoteRequest) (err error) {

//tmpdebug
fmt.Println("handleCommitConfirmed>SubmitRelay: ", request.OriginTxHash)

// TODO: store the dest txhash connected to the nonce
nonce, _, err := q.Dest.SubmitRelay(ctx, request)
if err != nil {
Expand All @@ -384,11 +388,15 @@ func (q *QuoteRequestHandler) handleCommitConfirmed(ctx context.Context, span tr
span.AddEvent("relay successfully submitted")
span.SetAttributes(attribute.Int("relay_nonce", int(nonce)))

//tmpdebug
fmt.Println("handleCommitConfirmed>UpdateQuoteRequestStatus: ", request.OriginTxHash)
err = q.db.UpdateQuoteRequestStatus(ctx, request.TransactionID, reldb.RelayStarted, &request.Status)
if err != nil {
return fmt.Errorf("could not update quote request status: %w", err)
}

//tmpdebug
fmt.Println("handleCommitConfirmed>UpdateRelayNonce: ", request.OriginTxHash)
err = q.db.UpdateRelayNonce(ctx, request.TransactionID, nonce)
if err != nil {
return fmt.Errorf("could not update relay nonce: %w", err)
Expand Down Expand Up @@ -472,11 +480,14 @@ func (q *QuoteRequestHandler) handleRelayCompleted(ctx context.Context, span tra

// relay has been finalized, it's time to go back to the origin chain and try to prove
_, err = q.Origin.SubmitTransaction(ctx, func(transactor *bind.TransactOpts) (tx *types.Transaction, err error) {

fmt.Println("SubmitTransaction>Prove ", request.OriginTxHash, "Nonce:", transactor.Nonce)
tx, err = q.Origin.Bridge.Prove(transactor, request.RawRequest, request.DestTxHash)
if err != nil {
return nil, fmt.Errorf("could not relay: %w", err)
return nil, fmt.Errorf("could not prove: %w", err)
}

fmt.Println("SubmitTransaction>Prove return tx.gas ", tx.Gas())
return tx, nil
})
if err != nil {
Expand Down

0 comments on commit 6f21782

Please sign in to comment.