-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relayer): only process profitable transactions (#408)
- Loading branch information
1 parent
e7ef53e
commit b5d8180
Showing
25 changed files
with
445 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,5 @@ var ( | |
) | ||
|
||
type HTTPOnly bool | ||
|
||
type ProfitableOnly bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
) | ||
|
||
func (p *Processor) getLatestNonce(ctx context.Context, auth *bind.TransactOpts) error { | ||
pendingNonce, err := p.destEthClient.PendingNonceAt(ctx, p.relayerAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if pendingNonce > p.destNonce { | ||
p.setLatestNonce(pendingNonce) | ||
} | ||
|
||
auth.Nonce = big.NewInt(int64(p.destNonce)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/taikoxyz/taiko-mono/packages/relayer/mock" | ||
) | ||
|
||
func Test_getLatestNonce(t *testing.T) { | ||
p := newTestProcessor(true) | ||
|
||
err := p.getLatestNonce(context.Background(), &bind.TransactOpts{}) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, p.destNonce, mock.PendingNonce) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/pkg/errors" | ||
"github.com/taikoxyz/taiko-mono/packages/relayer/contracts" | ||
) | ||
|
||
func (p *Processor) isProfitable(ctx context.Context, message contracts.IBridgeMessage, proof []byte) (bool, error) { | ||
processingFee := message.ProcessingFee | ||
|
||
if processingFee == nil || processingFee.Cmp(big.NewInt(0)) != 1 { | ||
return false, nil | ||
} | ||
|
||
auth, err := bind.NewKeyedTransactorWithChainID(p.ecdsaKey, message.DestChainId) | ||
if err != nil { | ||
return false, errors.Wrap(err, "bind.NewKeyedTransactorWithChainID") | ||
} | ||
|
||
auth.NoSend = true | ||
|
||
auth.Context = ctx | ||
|
||
// estimate gas with auth.NoSend set to true | ||
tx, err := p.destBridge.ProcessMessage(auth, message, proof) | ||
if err != nil { | ||
return false, errors.Wrap(err, "p.destBridge.ProcessMessage") | ||
} | ||
|
||
cost := tx.Cost() | ||
|
||
if processingFee.Cmp(cost) != 1 { | ||
return false, nil | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/taikoxyz/taiko-mono/packages/relayer/contracts" | ||
"github.com/taikoxyz/taiko-mono/packages/relayer/mock" | ||
) | ||
|
||
func Test_isProfitable(t *testing.T) { | ||
p := newTestProcessor(true) | ||
|
||
tests := []struct { | ||
name string | ||
message contracts.IBridgeMessage | ||
proof []byte | ||
wantProfitable bool | ||
wantErr error | ||
}{ | ||
{ | ||
"zeroProcessingFee", | ||
contracts.IBridgeMessage{ | ||
ProcessingFee: big.NewInt(0), | ||
}, | ||
nil, | ||
false, | ||
nil, | ||
}, | ||
{ | ||
"nilProcessingFee", | ||
contracts.IBridgeMessage{}, | ||
nil, | ||
false, | ||
nil, | ||
}, | ||
{ | ||
"lowProcessingFee", | ||
contracts.IBridgeMessage{ | ||
ProcessingFee: new(big.Int).Sub(mock.ProcessMessageTx.Cost(), big.NewInt(1)), | ||
DestChainId: big.NewInt(167001), | ||
}, | ||
nil, | ||
false, | ||
nil, | ||
}, | ||
{ | ||
"profitableProcessingFee", | ||
contracts.IBridgeMessage{ | ||
ProcessingFee: new(big.Int).Add(mock.ProcessMessageTx.Cost(), big.NewInt(1)), | ||
DestChainId: big.NewInt(167001), | ||
}, | ||
nil, | ||
true, | ||
nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
profitable, err := p.isProfitable( | ||
context.Background(), | ||
tt.message, | ||
tt.proof, | ||
) | ||
|
||
assert.Equal(t, tt.wantProfitable, profitable) | ||
assert.Equal(t, tt.wantErr, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.