Skip to content

Commit

Permalink
fix(relayer): profitability changes (#18124)
Browse files Browse the repository at this point in the history
Co-authored-by: Karim <[email protected]>
  • Loading branch information
2 people authored and dantaik committed Sep 18, 2024
1 parent d04998d commit b01b0a5
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/relayer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ jobs:
with:
access_token: ${{ github.token }}

- name: Install Git
run: sudo apt-get update && sudo apt-get install -y git

- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
Expand Down
4 changes: 2 additions & 2 deletions packages/relayer/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (i *Indexer) Start() error {
return errors.Wrap(err, "i.setInitialIndexingBlockByMode")
}

go i.eventLoop(i.ctx, i.latestIndexedBlockNumber)
go i.eventLoop(i.ctx)

go func() {
if err := backoff.Retry(func() error {
Expand All @@ -291,7 +291,7 @@ func (i *Indexer) Start() error {
return nil
}

func (i *Indexer) eventLoop(ctx context.Context, startBlockID uint64) {
func (i *Indexer) eventLoop(ctx context.Context) {
i.wg.Add(1)
defer i.wg.Done()

Expand Down
1 change: 1 addition & 0 deletions packages/relayer/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"database/sql"

"github.com/cyberhorsey/errors"
"gorm.io/gorm"
)
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/pkg/http/get_events_by_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func Test_GetEventsByAddress(t *testing.T) {
srv := newTestServer("")
srv := newTestServer()

_, err := srv.eventRepo.Save(context.Background(), &relayer.SaveEventOpts{
Name: "name",
Expand Down
7 changes: 3 additions & 4 deletions packages/relayer/pkg/http/get_recommended_processing_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ func (srv *Server) GetRecommendedProcessingFees(c echo.Context) error {
for _, f := range feeTypes {
fees = append(fees, fee{
Type: f.String(),
Amount: srv.getCost(c.Request().Context(), uint64(f), destGasTipCap, destBaseFee, Layer1).String(),
Amount: srv.getCost(uint64(f), destGasTipCap, destBaseFee, Layer1).String(),
DestChainID: srcChainID.Uint64(),
GasLimit: strconv.Itoa(int(f)),
})

fees = append(fees, fee{
Type: f.String(),
Amount: srv.getCost(c.Request().Context(), uint64(f), srcGasTipCap, srcBaseFee, Layer2).String(),
Amount: srv.getCost(uint64(f), srcGasTipCap, srcBaseFee, Layer2).String(),
DestChainID: destChainID.Uint64(),
GasLimit: strconv.Itoa(int(f)),
})
Expand All @@ -143,15 +143,14 @@ func (srv *Server) GetRecommendedProcessingFees(c echo.Context) error {
}

func (srv *Server) getCost(
ctx context.Context,
gasLimit uint64,
gasTipCap *big.Int,
baseFee *big.Int,
destLayer layer,
) *big.Int {
cost := new(big.Int).Mul(
new(big.Int).SetUint64(gasLimit),
new(big.Int).Add(gasTipCap, baseFee))
new(big.Int).Add(gasTipCap, new(big.Int).Mul(baseFee, big.NewInt(2))))

if destLayer == Layer2 {
return cost
Expand Down
8 changes: 4 additions & 4 deletions packages/relayer/pkg/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/taikoxyz/taiko-mono/packages/relayer/pkg/repo"
)

func newTestServer(url string) *Server {
func newTestServer() *Server {
_ = godotenv.Load("../.test.env")

srv := &Server{
Expand Down Expand Up @@ -104,7 +104,7 @@ func Test_NewServer(t *testing.T) {
}

func Test_Health(t *testing.T) {
srv := newTestServer("")
srv := newTestServer()

req, _ := http.NewRequest(echo.GET, "/healthz", nil)
rec := httptest.NewRecorder()
Expand All @@ -117,7 +117,7 @@ func Test_Health(t *testing.T) {
}

func Test_Root(t *testing.T) {
srv := newTestServer("")
srv := newTestServer()

req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
Expand All @@ -130,7 +130,7 @@ func Test_Root(t *testing.T) {
}

func Test_StartShutdown(t *testing.T) {
srv := newTestServer("")
srv := newTestServer()

go func() {
_ = srv.Start(":3928")
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/processor/can_process_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// or its processed, failed, and is in Retriable or Failed state, where the user
// should finish manually.
func canProcessMessage(
ctx context.Context,
_ context.Context,
eventStatus relayer.EventStatus,
messageOwner common.Address,
relayerAddress common.Address,
Expand Down
14 changes: 8 additions & 6 deletions packages/relayer/processor/is_profitable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package processor
import (
"context"
"log/slog"
"time"

"github.com/pkg/errors"
"github.com/taikoxyz/taiko-mono/packages/relayer"
Expand Down Expand Up @@ -51,12 +52,13 @@ func (p *Processor) isProfitable(
)

opts := relayer.UpdateFeesAndProfitabilityOpts{
Fee: fee,
DestChainBaseFee: destChainBaseFee,
GasTipCap: gasTipCap,
GasLimit: gasLimit,
IsProfitable: shouldProcess,
EstimatedOnchainFee: estimatedOnchainFee,
Fee: fee,
DestChainBaseFee: destChainBaseFee,
GasTipCap: gasTipCap,
GasLimit: gasLimit,
IsProfitable: shouldProcess,
EstimatedOnchainFee: estimatedOnchainFee,
IsProfitableEvaluatedAt: time.Now().UTC(),
}

if err := p.eventRepo.UpdateFeesAndProfitability(ctx, id, &opts); err != nil {
Expand Down
18 changes: 6 additions & 12 deletions packages/relayer/processor/process_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (p *Processor) processMessage(
return false, msgBody.TimesRetried, nil
}

if err := p.waitForConfirmations(ctx, msgBody.Event.Raw.TxHash, msgBody.Event.Raw.BlockNumber); err != nil {
if err := p.waitForConfirmations(ctx, msgBody.Event.Raw.TxHash); err != nil {
return false, msgBody.TimesRetried, err
}

Expand Down Expand Up @@ -257,18 +257,12 @@ func (p *Processor) generateEncodedSignalProof(ctx context.Context,
var hopChainID *big.Int

for _, hop := range p.hops {
hop.blockNum = blockNum

event, err := p.waitHeaderSynced(ctx, hopEthClient, hop.chainID.Uint64(), blockNum)

if err != nil {
return nil, errors.Wrap(err, "p.waitHeaderSynced")
}

if err != nil {
return nil, errors.Wrap(err, "hop.headerSyncer.GetSyncedSnippet")
}

blockNum = event.SyncedInBlockID

hopEthClient = hop.ethClient
Expand Down Expand Up @@ -443,9 +437,9 @@ func (p *Processor) sendProcessMessageCall(
}

// mul by 1.05 for padding
gasLimit := uint64(float64(event.Message.GasLimit) * 1.05)
gasLimit := uint64(float64(event.Message.GasLimit))

var estimatedCost uint64 = 0
var estimatedMaxCost uint64

if bool(p.profitableOnly) {
profitable, err := p.isProfitable(
Expand Down Expand Up @@ -492,7 +486,7 @@ func (p *Processor) sendProcessMessageCall(
return nil, relayer.ErrUnprofitable
}

estimatedCost = gasUsed * (baseFee.Uint64() + gasTipCap.Uint64())
estimatedMaxCost = gasUsed * ((baseFee.Uint64() * 2) + gasTipCap.Uint64())
}

// we should check event status one more time, after we have waiting for
Expand Down Expand Up @@ -550,10 +544,10 @@ func (p *Processor) sendProcessMessageCall(
slog.Info("tx cost", "txHash", hex.EncodeToString(receipt.TxHash.Bytes()),
"srcTxHash", event.Raw.TxHash.Hex(),
"actualCost", cost,
"estimatedCost", estimatedCost,
"estimatedMaxCost", estimatedMaxCost,
)

if cost > estimatedCost {
if cost > estimatedMaxCost {
relayer.UnprofitableMessageAfterTransacting.Inc()
} else {
relayer.ProfitableMessageAfterTransacting.Inc()
Expand Down
1 change: 0 additions & 1 deletion packages/relayer/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ type hop struct {
taikoAddress common.Address
ethClient ethClient
caller relayer.Caller
blockNum uint64
}

// Processor is the main struct which handles message processing and queue
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/processor/wait_for_confirmations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// waitForConfirmations waits for the given transaction to reach N confs
// before returning
func (p *Processor) waitForConfirmations(ctx context.Context, txHash common.Hash, blockNumber uint64) error {
func (p *Processor) waitForConfirmations(ctx context.Context, txHash common.Hash) error {
ctx, cancelFunc := context.WithTimeout(ctx, time.Duration(p.confTimeoutInSeconds)*time.Second)

defer cancelFunc()
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/processor/wait_for_confirmations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (
func Test_waitForConfirmations(t *testing.T) {
p := newTestProcessor(true)

err := p.waitForConfirmations(context.TODO(), mock.SucceedTxHash, uint64(mock.BlockNum))
err := p.waitForConfirmations(context.TODO(), mock.SucceedTxHash)
assert.Nil(t, err)
}

0 comments on commit b01b0a5

Please sign in to comment.