Skip to content

Commit

Permalink
move destination blockGasLimit validation into config package
Browse files Browse the repository at this point in the history
  • Loading branch information
najeal committed Jan 12, 2025
1 parent 5cc0762 commit 268aba7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 63 deletions.
2 changes: 1 addition & 1 deletion messages/teleporter/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (m *messageHandler) ShouldSendMessage(destinationClient vms.DestinationClie
return false, fmt.Errorf("failed to calculate Teleporter message ID: %w", err)
}
requiredGasLimit := m.teleporterMessage.RequiredGasLimit.Uint64()
maxGasLimit := destinationClient.TeleporterMaxGasLimit()
maxGasLimit := destinationClient.BlockGasLimit()
// Check if the specified gas limit is below the maximum threshold
if requiredGasLimit > maxGasLimit {
m.logger.Info(
Expand Down
3 changes: 1 addition & 2 deletions messages/teleporter/message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestShouldSendMessage(t *testing.T) {
require.NoError(t, err)

gasLimitExceededTeleporterMessage := validTeleporterMessage
gasLimitExceededTeleporterMessage.RequiredGasLimit = big.NewInt(config.DefaultTeleporterMaxGasLimit + 1)
gasLimitExceededTeleporterMessage.RequiredGasLimit = big.NewInt(config.DefaultBlockGasLimit + 1)
gasLimitExceededTeleporterMessageBytes, err := gasLimitExceededTeleporterMessage.Pack()
require.NoError(t, err)

Expand Down Expand Up @@ -233,7 +233,6 @@ func TestShouldSendMessage(t *testing.T) {
SenderAddress().
Return(test.senderAddressResult).
Times(test.senderAddressTimes)
mockClient.EXPECT().TeleporterMaxGasLimit().Return(uint64(config.DefaultTeleporterMaxGasLimit)).AnyTimes()
mockClient.EXPECT().DestinationBlockchainID().Return(destinationBlockchainID).AnyTimes()
if test.messageReceivedCall != nil {
messageReceivedInput := interfaces.CallMsg{
Expand Down
27 changes: 16 additions & 11 deletions relayer/config/destination_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import (
const (
// 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.
DefaultTeleporterMaxGasLimit = 12_000_000
DefaultBlockGasLimit = 12_000_000
)

// Destination blockchain configuration. Specifies how to connect to and issue
// transactions on the destination blockchain.
type DestinationBlockchain struct {
SubnetID string `mapstructure:"subnet-id" json:"subnet-id"`
BlockchainID string `mapstructure:"blockchain-id" json:"blockchain-id"`
VM string `mapstructure:"vm" json:"vm"`
RPCEndpoint basecfg.APIConfig `mapstructure:"rpc-endpoint" json:"rpc-endpoint"`
KMSKeyID string `mapstructure:"kms-key-id" json:"kms-key-id"`
KMSAWSRegion string `mapstructure:"kms-aws-region" json:"kms-aws-region"`
AccountPrivateKey string `mapstructure:"account-private-key" json:"account-private-key"`
TeleporterMaxGasLimit uint64 `mapstructure:"teleporter-max-gas-limit" json:"teleporter-max-gas-limit"`
SubnetID string `mapstructure:"subnet-id" json:"subnet-id"`
BlockchainID string `mapstructure:"blockchain-id" json:"blockchain-id"`
VM string `mapstructure:"vm" json:"vm"`
RPCEndpoint basecfg.APIConfig `mapstructure:"rpc-endpoint" json:"rpc-endpoint"`
KMSKeyID string `mapstructure:"kms-key-id" json:"kms-key-id"`
KMSAWSRegion string `mapstructure:"kms-aws-region" json:"kms-aws-region"`
AccountPrivateKey string `mapstructure:"account-private-key" json:"account-private-key"`
BlockGasLimit uint64 `mapstructure:"teleporter-max-gas-limit" json:"teleporter-max-gas-limit"`

// Fetched from the chain after startup
warpConfig WarpConfig
Expand All @@ -41,8 +41,8 @@ type DestinationBlockchain struct {

// Validates the destination subnet configuration
func (s *DestinationBlockchain) Validate() error {
if s.TeleporterMaxGasLimit == 0 {
s.TeleporterMaxGasLimit = DefaultTeleporterMaxGasLimit
if s.BlockGasLimit == 0 {
s.BlockGasLimit = DefaultBlockGasLimit
}
if err := s.RPCEndpoint.Validate(); err != nil {
return fmt.Errorf("invalid rpc-endpoint in destination subnet configuration: %w", err)
Expand Down Expand Up @@ -78,6 +78,11 @@ func (s *DestinationBlockchain) Validate() error {
}
s.subnetID = subnetID

if s.subnetID == constants.PrimaryNetworkID &&
s.BlockGasLimit > DefaultBlockGasLimit {
return fmt.Errorf("C-Chain max-gas-limit '%d' exceeded", s.BlockGasLimit)
}

return nil
}

Expand Down
22 changes: 5 additions & 17 deletions vms/destination_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
package vms

import (
"context"
"fmt"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/icm-services/peers"
"github.com/ava-labs/icm-services/relayer/config"
"github.com/ava-labs/icm-services/vms/evm"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -37,16 +35,16 @@ type DestinationClient interface {
// DestinationBlockchainID returns the ID of the destination chain
DestinationBlockchainID() ids.ID

// MaxGasLimit returns destination blockchain max gas limit
TeleporterMaxGasLimit() uint64
// BlockGasLimit returns destination blockchain block gas limit
BlockGasLimit() uint64
}

func NewDestinationClient(
logger logging.Logger, subnetInfo *config.DestinationBlockchain, cChainID ids.ID,
logger logging.Logger, subnetInfo *config.DestinationBlockchain,
) (DestinationClient, error) {
switch config.ParseVM(subnetInfo.VM) {
case config.EVM:
return evm.NewDestinationClient(logger, subnetInfo, cChainID)
return evm.NewDestinationClient(logger, subnetInfo)
default:
return nil, fmt.Errorf("invalid vm")
}
Expand All @@ -58,16 +56,6 @@ func CreateDestinationClients(
relayerConfig config.Config,
) (map[ids.ID]DestinationClient, error) {
destinationClients := make(map[ids.ID]DestinationClient)
infoClient, err := peers.NewInfoAPI(relayerConfig.InfoAPI)
if err != nil {
logger.Error("Failed to create info API", zap.Error(err))
return nil, err
}
cChainID, err := infoClient.GetBlockchainID(context.Background(), "C")
if err != nil {
logger.Error("Failed to get C-Chain ID", zap.Error(err))
return nil, err
}
for _, subnetInfo := range relayerConfig.DestinationBlockchains {
blockchainID, err := ids.FromString(subnetInfo.BlockchainID)
if err != nil {
Expand All @@ -86,7 +74,7 @@ func CreateDestinationClients(
continue
}

destinationClient, err := NewDestinationClient(logger, subnetInfo, cChainID)
destinationClient, err := NewDestinationClient(logger, subnetInfo)
if err != nil {
logger.Error(
"Could not create destination client",
Expand Down
22 changes: 4 additions & 18 deletions vms/evm/destination_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package evm

import (
"context"
"fmt"
"math/big"
"sync"

Expand Down Expand Up @@ -45,14 +44,13 @@ type destinationClient struct {
signer signer.Signer
evmChainID *big.Int
currentNonce uint64
teleporterMaxGasLimit uint64
blockGasLimit uint64
logger logging.Logger
}

func NewDestinationClient(
logger logging.Logger,
destinationBlockchain *config.DestinationBlockchain,
cChainID ids.ID,
) (*destinationClient, error) {
destinationID, err := ids.FromString(destinationBlockchain.BlockchainID)
if err != nil {
Expand All @@ -63,18 +61,6 @@ func NewDestinationClient(
return nil, err
}

maxGasLimit := destinationBlockchain.TeleporterMaxGasLimit
isCChain := destinationID == cChainID
if isCChain && maxGasLimit > config.DefaultTeleporterMaxGasLimit {
logger.Error("C-Chain max-gas-limit exceeded",
zap.Uint64("value", maxGasLimit),
zap.Uint64("cChainMaxValue", config.DefaultTeleporterMaxGasLimit),
)
return nil, fmt.Errorf(
"C-Chain max-gas-limit max gas limit %d exceeded", config.DefaultTeleporterMaxGasLimit,
)
}

sgnr, err := signer.NewSigner(destinationBlockchain)
if err != nil {
logger.Error(
Expand Down Expand Up @@ -132,7 +118,7 @@ func NewDestinationClient(
evmChainID: evmChainID,
currentNonce: nonce,
logger: logger,
teleporterMaxGasLimit: maxGasLimit,
blockGasLimit: destinationBlockchain.BlockGasLimit,
}, nil
}

Expand Down Expand Up @@ -227,6 +213,6 @@ func (c *destinationClient) DestinationBlockchainID() ids.ID {
return c.destinationBlockchainID
}

func (c *destinationClient) TeleporterMaxGasLimit() uint64 {
return c.teleporterMaxGasLimit
func (c *destinationClient) BlockGasLimit() uint64 {
return c.blockGasLimit
}
28 changes: 14 additions & 14 deletions vms/mocks/mock_destination_client.go

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

0 comments on commit 268aba7

Please sign in to comment.