Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit gas check #430

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions messages/teleporter/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
"google.golang.org/grpc"
)

// The maximum gas limit that can be specified for a Teleporter message
// Based on the C-Chain 15_000_000 gas limit per block, with other Warp message gas overhead conservatively estimated.
const maxTeleporterGasLimit = 12_000_000

type factory struct {
messageConfig Config
protocolAddress common.Address
Expand Down Expand Up @@ -159,6 +163,19 @@ func (m *messageHandler) ShouldSendMessage(destinationClient vms.DestinationClie
return false, fmt.Errorf("failed to calculate Teleporter message ID: %w", err)
}

// Check if the specified gas limit is below the maximum threshold
if m.teleporterMessage.RequiredGasLimit.Uint64() > maxTeleporterGasLimit {
m.logger.Info(
"Gas limit exceeds maximum threshold",
zap.String("destinationBlockchainID", destinationBlockchainID.String()),
zap.String("teleporterMessageID", teleporterMessageID.String()),
zap.Uint64("requiredGasLimit", m.teleporterMessage.RequiredGasLimit.Uint64()),
zap.Uint64("maxGasLimit", maxTeleporterGasLimit),
)
return false, nil
}

// Check if the relayer is allowed to deliver this message
senderAddress := destinationClient.SenderAddress()
if !isAllowedRelayer(m.teleporterMessage.AllowedRelayerAddresses, senderAddress) {
m.logger.Info(
Expand Down Expand Up @@ -191,6 +208,7 @@ func (m *messageHandler) ShouldSendMessage(destinationClient vms.DestinationClie
)
return false, nil
}

// Dispatch to the external decider service. If the service is unavailable or returns
// an error, then use the decision that has already been made, i.e. return true
decision, err := m.getShouldSendMessageFromDecider()
Expand Down
25 changes: 25 additions & 0 deletions messages/teleporter/message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ func TestShouldSendMessage(t *testing.T) {
)
require.NoError(t, err)

gasLimitExceededTeleporterMessage := validTeleporterMessage
gasLimitExceededTeleporterMessage.RequiredGasLimit = big.NewInt(maxTeleporterGasLimit + 1)
gasLimitExceededTeleporterMessageBytes, err :=
teleportermessenger.PackTeleporterMessage(gasLimitExceededTeleporterMessage)
require.NoError(t, err)

gasLimitExceededAddressedCall, err := warpPayload.NewAddressedCall(
messageProtocolAddress.Bytes(),
gasLimitExceededTeleporterMessageBytes,
)
require.NoError(t, err)

gasLimitExceededWarpUnsignedMessage, err := warp.NewUnsignedMessage(
0,
sourceBlockchainID,
gasLimitExceededAddressedCall.Bytes(),
)
require.NoError(t, err)

testCases := []struct {
name string
destinationBlockchainID ids.ID
Expand Down Expand Up @@ -177,6 +196,12 @@ func TestShouldSendMessage(t *testing.T) {
},
expectedResult: false,
},
{
name: "gas limit exceeded",
destinationBlockchainID: destinationBlockchainID,
warpUnsignedMessage: gasLimitExceededWarpUnsignedMessage,
expectedResult: false,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tests/batch_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func BatchRelay(network interfaces.LocalNetwork) {
Expect(err).Should(BeNil())
defer sub.Unsubscribe()

numMessages := 50
numMessages := 40
sentMessages := set.NewSet[string](numMessages)
for i := 0; i < numMessages; i++ {
sentMessages.Add(strconv.Itoa(i))
Expand Down
Loading