From 1c8461ef8d103804a89100000277c4603eecf187 Mon Sep 17 00:00:00 2001 From: Gwen Date: Fri, 6 Oct 2023 09:19:55 -0400 Subject: [PATCH 01/16] remove awm-relayer dependency --- go.mod | 1 - go.sum | 2 - tests/basic_one_way_send.go | 12 ++---- tests/utils/abis.go | 72 +++++++++++++++++++++++++++++++ tests/utils/network_setup.go | 9 ++-- tests/utils/utils.go | 84 +++++++++++++++++++++++++----------- 6 files changed, 139 insertions(+), 41 deletions(-) create mode 100644 tests/utils/abis.go diff --git a/go.mod b/go.mod index 00b66e020..253d501c0 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( require ( github.com/ava-labs/avalanche-network-runner v1.7.2 - github.com/ava-labs/awm-relayer v0.2.2 github.com/ava-labs/subnet-evm v0.5.6 github.com/ethereum/go-ethereum v1.12.0 github.com/onsi/ginkgo/v2 v2.12.0 diff --git a/go.sum b/go.sum index 7fab0139c..10f5bd6c0 100644 --- a/go.sum +++ b/go.sum @@ -63,8 +63,6 @@ github.com/ava-labs/avalanche-network-runner v1.7.2 h1:XFad/wZfYzDnqbLzPAPPRYU3a github.com/ava-labs/avalanche-network-runner v1.7.2/go.mod h1:naLveusSrP7YgTAqRykD1SyLOAUilCp9jGjk3MDxoPI= github.com/ava-labs/avalanchego v1.10.10 h1:EYX4LVotcfdtIQ0nJSBTcoisubx/Bzk2tM1aP3yiYiw= github.com/ava-labs/avalanchego v1.10.10/go.mod h1:6UA0nxxTvvpyuCbP2DSzx9+7uWQfQx9DPApK8JptLiE= -github.com/ava-labs/awm-relayer v0.2.2 h1:TW/cJr13bVMHL5NjBF9aTTJQTyjpwbRMH6ffX78DFmo= -github.com/ava-labs/awm-relayer v0.2.2/go.mod h1:UIc0Nl/ZIRVm5fBmUWDxWahNsQWZoSfJb3ei25q5ZlQ= github.com/ava-labs/coreth v0.12.5-rc.6 h1:OajGUyKkO5Q82XSuMa8T5UD6QywtCHUiZ4Tv3RFmRBU= github.com/ava-labs/coreth v0.12.5-rc.6/go.mod h1:s5wVyy+5UCCk2m0Tq3jVmy0UqOpKBDYqRE13gInCJVs= github.com/ava-labs/subnet-evm v0.5.6 h1:u+xBvEExOa362Up02hgSgeF+aqDona57whhRIeEIim8= diff --git a/tests/basic_one_way_send.go b/tests/basic_one_way_send.go index 496f94b0b..e6dc8b2b3 100644 --- a/tests/basic_one_way_send.go +++ b/tests/basic_one_way_send.go @@ -4,7 +4,6 @@ import ( "context" "math/big" - "github.com/ava-labs/awm-relayer/messages/teleporter" "github.com/ava-labs/subnet-evm/interfaces" teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/Teleporter/TeleporterMessenger" "github.com/ava-labs/teleporter/tests/utils" @@ -33,10 +32,10 @@ func BasicOneWaySend() { // ctx := context.Background() - sendCrossChainMessageInput := utils.SendCrossChainMessageInput{ + sendCrossChainMessageInput := teleportermessenger.TeleporterMessageInput{ DestinationChainID: subnetBInfo.BlockchainID, DestinationAddress: fundedAddress, - FeeInfo: utils.FeeInfo{ + FeeInfo: teleportermessenger.TeleporterFeeInfo{ ContractAddress: fundedAddress, Amount: big.NewInt(0), }, @@ -66,10 +65,7 @@ func BasicOneWaySend() { // // Check Teleporter message received on the destination // - data, err := teleporter.PackMessageReceived(teleporter.MessageReceivedInput{ - OriginChainID: subnetAInfo.BlockchainID, - MessageID: teleporterMessageID, - }) + data, err := utils.TeleporterABI.PackMessageReceived(subnetAInfo.BlockchainID, teleporterMessageID) Expect(err).Should(BeNil()) callMessage := interfaces.CallMsg{ To: &teleporterContractAddress, @@ -79,7 +75,7 @@ func BasicOneWaySend() { Expect(err).Should(BeNil()) // check the contract call result - delivered, err := teleporter.UnpackMessageReceivedResult(result) + delivered, err := utils.TeleporterABI.UnpackMessageReceivedResult(result) Expect(err).Should(BeNil()) Expect(delivered).Should(BeTrue()) } diff --git a/tests/utils/abis.go b/tests/utils/abis.go new file mode 100644 index 000000000..01c4540e8 --- /dev/null +++ b/tests/utils/abis.go @@ -0,0 +1,72 @@ +package utils + +import ( + "math/big" + "strings" + + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/subnet-evm/accounts/abi" + "github.com/ethereum/go-ethereum/common" + "github.com/pkg/errors" + + teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/Teleporter/TeleporterMessenger" +) + +type teleporterABI struct { + teleporterMessengerABI abi.ABI +} + +func NewTeleporterABI() *teleporterABI { + parsed, err := abi.JSON(strings.NewReader(teleportermessenger.TeleporterMessengerABI)) + if err != nil { + panic(err) + } + + return &teleporterABI{ + teleporterMessengerABI: parsed, + } +} + +// UnpackTeleporterMessage unpacks message bytes according to EVM ABI encoding rules into a TeleporterMessage +func (t *teleporterABI) UnpackTeleporterMessage(messageBytes []byte) (*teleportermessenger.TeleporterMessage, error) { + var teleporterMessage teleportermessenger.TeleporterMessage + err := t.teleporterMessengerABI.UnpackIntoInterface(&teleporterMessage, "teleporterMessage", messageBytes) + if err != nil { + return nil, errors.Wrap(err, "failed to unpack to teleporter message") + } + + return &teleporterMessage, nil +} + +func (t *teleporterABI) PackSendCrossChainMessage(input teleportermessenger.TeleporterMessageInput) ([]byte, error) { + return t.teleporterMessengerABI.Pack("sendCrossChainMessage", input) +} + +// PackReceiveCrossChainMessage packs a ReceiveCrossChainMessageInput to form a call to the receiveCrossChainMessage function +func (t *teleporterABI) PackReceiveCrossChainMessage(messageIndex uint32, relayerRewardAddress common.Address) ([]byte, error) { + return t.teleporterMessengerABI.Pack("receiveCrossChainMessage", messageIndex, relayerRewardAddress) +} + +// PackMessageReceived packs a MessageReceivedInput to form a call to the messageReceived function +func (t *teleporterABI) PackMessageReceived(originChainID ids.ID, messageID *big.Int) ([]byte, error) { + return t.teleporterMessengerABI.Pack("messageReceived", originChainID, messageID) +} + +// UnpackMessageReceivedResult attempts to unpack result bytes to a bool indicating whether the message was received +func (t *teleporterABI) UnpackMessageReceivedResult(result []byte) (bool, error) { + var success bool + err := t.teleporterMessengerABI.UnpackIntoInterface(&success, "messageReceived", result) + return success, err +} + +func (t *teleporterABI) PackMessageReceivedOutput(success bool) ([]byte, error) { + return t.teleporterMessengerABI.PackOutput("messageReceived", success) +} + +// CAUTION: PackEvent is documented as not supporting struct types, so this should only be used for testing puposes. +// In a real setting, the Teleporter contract should pack the event. +// PackSendCrossChainMessageEvent packs the SendCrossChainMessage event type. +func (t *teleporterABI) PackSendCrossChainMessageEvent(destinationChainID common.Hash, message teleportermessenger.TeleporterMessage) ([]byte, error) { + _, hashes, err := t.teleporterMessengerABI.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message) + return hashes, err +} diff --git a/tests/utils/network_setup.go b/tests/utils/network_setup.go index 1ee6b16f3..4f2edb58d 100644 --- a/tests/utils/network_setup.go +++ b/tests/utils/network_setup.go @@ -9,7 +9,6 @@ import ( "github.com/ava-labs/avalanche-network-runner/rpcpb" "github.com/ava-labs/avalanchego/ids" - relayerEvm "github.com/ava-labs/awm-relayer/vms/evm" "github.com/ava-labs/subnet-evm/core/types" "github.com/ava-labs/subnet-evm/ethclient" "github.com/ava-labs/subnet-evm/plugin/evm" @@ -23,7 +22,9 @@ import ( ) const ( - fundedKeyStr = "56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027" + fundedKeyStr = "56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027" + baseFeeFactor = 2 + maxPriorityFeePerGas = 2500000000 // 2.5 gwei ) var ( @@ -222,8 +223,8 @@ func DeployTeleporterContracts(transactionBytes []byte, deployerAddress common.A Expect(err).Should(BeNil()) baseFee, err := client.EstimateBaseFee(context.Background()) Expect(err).Should(BeNil()) - gasFeeCap := baseFee.Mul(baseFee, big.NewInt(relayerEvm.BaseFeeFactor)) - gasFeeCap.Add(gasFeeCap, big.NewInt(relayerEvm.MaxPriorityFeePerGas)) + gasFeeCap := baseFee.Mul(baseFee, big.NewInt(baseFeeFactor)) + gasFeeCap.Add(gasFeeCap, big.NewInt(maxPriorityFeePerGas)) // Fund the deployer address { value := big.NewInt(0).Mul(big.NewInt(1e18), big.NewInt(10)) // 10eth diff --git a/tests/utils/utils.go b/tests/utils/utils.go index f18542398..594d9db9d 100644 --- a/tests/utils/utils.go +++ b/tests/utils/utils.go @@ -13,11 +13,12 @@ import ( "time" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/utils/math" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" warpBackend "github.com/ava-labs/subnet-evm/warp" teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/Teleporter/TeleporterMessenger" + "github.com/pkg/errors" - "github.com/ava-labs/awm-relayer/messages/teleporter" "github.com/ava-labs/subnet-evm/core/types" "github.com/ava-labs/subnet-evm/ethclient" "github.com/ava-labs/subnet-evm/interfaces" @@ -32,39 +33,47 @@ import ( . "github.com/onsi/gomega" ) +const ( + receiveCrossChainMessageStaticGasCost uint64 = 2_000_000 + receiveCrossChainMessageGasCostPerAggregatedKey uint64 = 1_000 + receiveMessageGasLimitBufferAmount = 100_000 +) + var ( DefaultTeleporterTransactionGas uint64 = 200_000 DefaultTeleporterTransactionGasFeeCap = big.NewInt(225 * params.GWei) DefaultTeleporterTransactionGasTipCap = big.NewInt(params.GWei) DefaultTeleporterTransactionValue = common.Big0 + + TeleporterABI = NewTeleporterABI() ) type SendCrossChainMessageEvent struct { DestinationChainID ids.ID MessageID *big.Int - Message teleporter.TeleporterMessage + Message teleportermessenger.TeleporterMessage } type ReceiveCrossChainMessageEvent struct { OriginChainID ids.ID MessageID *big.Int - Message teleporter.TeleporterMessage + Message teleportermessenger.TeleporterMessage } // Teleporter contract sendCrossChainMessage input type -type SendCrossChainMessageInput struct { - DestinationChainID ids.ID - DestinationAddress common.Address - FeeInfo FeeInfo - RequiredGasLimit *big.Int - Message []byte - AllowedRelayerAddresses []common.Address -} - -type FeeInfo struct { - ContractAddress common.Address - Amount *big.Int -} +// type SendCrossChainMessageInput struct { +// DestinationChainID ids.ID +// DestinationAddress common.Address +// FeeInfo FeeInfo +// RequiredGasLimit *big.Int +// Message []byte +// AllowedRelayerAddresses []common.Address +// } + +// type FeeInfo struct { +// ContractAddress common.Address +// Amount *big.Int +// } // // Test utility functions @@ -132,15 +141,12 @@ func GetURIHostAndPort(uri string) (string, uint32, error) { func CreateSendCrossChainMessageTransaction( ctx context.Context, source SubnetTestInfo, - input SendCrossChainMessageInput, + input teleportermessenger.TeleporterMessageInput, fundedAddress common.Address, fundedKey *ecdsa.PrivateKey, teleporterContractAddress common.Address, ) *types.Transaction { - data, err := teleporter.EVMTeleporterContractABI.Pack( - "sendCrossChainMessage", - input, - ) + data, err := TeleporterABI.PackSendCrossChainMessage(input) Expect(err).Should(BeNil()) gasFeeCap, gasTipCap, nonce := calculateTxParams(ctx, source.ChainWSClient, fundedAddress) @@ -180,13 +186,10 @@ func CreateReceiveCrossChainMessageTransaction( numSigners, err := signedMessage.Signature.NumSigners() Expect(err).Should(BeNil()) - gasLimit, err := teleporter.CalculateReceiveMessageGasLimit(numSigners, requiredGasLimit) + gasLimit, err := calculateReceiveMessageGasLimit(numSigners, requiredGasLimit) Expect(err).Should(BeNil()) - callData, err := teleporter.PackReceiveCrossChainMessage(teleporter.ReceiveCrossChainMessageInput{ - MessageIndex: 0, - RelayerRewardAddress: fundedAddress, - }) + callData, err := TeleporterABI.PackReceiveCrossChainMessage(0, fundedAddress) Expect(err).Should(BeNil()) gasFeeCap, gasTipCap, nonce := calculateTxParams(ctx, wsClient, fundedAddress) @@ -365,3 +368,32 @@ func calculateTxParams(ctx context.Context, wsClient ethclient.Client, fundedAdd return gasFeeCap, gasTipCap, nonce } + +// CalculateReceiveMessageGasLimit calculates the estimated gas amount used by a single call +// to receiveCrossChainMessage for the given message and validator bit vector. The result amount +// depends on the required limit for the message execution, the number of validator signatures +// included in the aggregate signature, the static gas cost defined by the precompile, and an +// extra buffer amount defined here to ensure the call doesn't run out of gas. +func calculateReceiveMessageGasLimit(numSigners int, executionRequiredGasLimit *big.Int) (uint64, error) { + if !executionRequiredGasLimit.IsUint64() { + return 0, errors.New("required gas limit too high") + } + + gasAmounts := []uint64{ + executionRequiredGasLimit.Uint64(), + receiveCrossChainMessageStaticGasCost, + uint64(numSigners) * receiveCrossChainMessageGasCostPerAggregatedKey, + receiveMessageGasLimitBufferAmount, + } + + res := gasAmounts[0] + var err error + for i := 1; i < len(gasAmounts); i++ { + res, err = math.Add64(res, gasAmounts[i]) + if err != nil { + return 0, err + } + } + + return res, nil +} From e136ae64c754eed960ab323cd9257b738060099e Mon Sep 17 00:00:00 2001 From: Gwen Date: Fri, 6 Oct 2023 14:33:10 -0400 Subject: [PATCH 02/16] refactor --- .../ERC20Bridge/BridgeToken/BridgeToken.go | 0 .../ERC20Bridge/ERC20Bridge/ERC20Bridge.go | 0 .../ExampleCrossChainMessenger.go | 0 .../BlockHashPublisher/BlockHashPublisher.go | 0 .../BlockHashReceiver/BlockHashReceiver.go | 0 .../TeleporterMessenger.go | 0 .../Teleporter/TeleporterMessenger/packing.go | 87 +++++++++++++++++++ go/teleporter-utilities/utils.go | 46 ++++++++++ scripts/abi_go_bindings.sh | 5 +- tests/basic_one_way_send.go | 6 +- tests/utils/abis.go | 72 --------------- tests/utils/network_setup.go | 9 +- tests/utils/utils.go | 69 ++------------- 13 files changed, 148 insertions(+), 146 deletions(-) rename {abi-bindings => go/abi-bindings}/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go (100%) rename {abi-bindings => go/abi-bindings}/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go (100%) rename {abi-bindings => go/abi-bindings}/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go (100%) rename {abi-bindings => go/abi-bindings}/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go (100%) rename {abi-bindings => go/abi-bindings}/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go (100%) rename {abi-bindings => go/abi-bindings}/Teleporter/TeleporterMessenger/TeleporterMessenger.go (100%) create mode 100644 go/abi-bindings/Teleporter/TeleporterMessenger/packing.go create mode 100644 go/teleporter-utilities/utils.go delete mode 100644 tests/utils/abis.go diff --git a/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go b/go/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go similarity index 100% rename from abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go rename to go/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go diff --git a/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go b/go/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go similarity index 100% rename from abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go rename to go/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go diff --git a/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go b/go/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go similarity index 100% rename from abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go rename to go/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go diff --git a/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go b/go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go similarity index 100% rename from abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go rename to go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go diff --git a/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go b/go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go similarity index 100% rename from abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go rename to go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go diff --git a/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go b/go/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go similarity index 100% rename from abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go rename to go/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go diff --git a/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go b/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go new file mode 100644 index 000000000..347c0e7ef --- /dev/null +++ b/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go @@ -0,0 +1,87 @@ +package teleportermessenger + +import ( + "math/big" + + "github.com/ava-labs/avalanchego/ids" + "github.com/ethereum/go-ethereum/common" + "github.com/pkg/errors" +) + +// UnpackTeleporterMessage unpacks message bytes according to EVM ABI encoding rules into a TeleporterMessage +func UnpackTeleporterMessage(messageBytes []byte) (*TeleporterMessage, error) { + abi, err := TeleporterMessengerMetaData.GetAbi() + if err != nil { + return nil, errors.Wrap(err, "failed to get abi") + } + + var teleporterMessage TeleporterMessage + err = abi.UnpackIntoInterface(&teleporterMessage, "teleporterMessage", messageBytes) + if err != nil { + return nil, errors.Wrap(err, "failed to unpack to teleporter message") + } + + return &teleporterMessage, nil +} + +func PackSendCrossChainMessage(input TeleporterMessageInput) ([]byte, error) { + abi, err := TeleporterMessengerMetaData.GetAbi() + if err != nil { + return nil, errors.Wrap(err, "failed to get abi") + } + + return abi.Pack("sendCrossChainMessage", input) +} + +// PackReceiveCrossChainMessage packs a ReceiveCrossChainMessageInput to form a call to the receiveCrossChainMessage function +func PackReceiveCrossChainMessage(messageIndex uint32, relayerRewardAddress common.Address) ([]byte, error) { + abi, err := TeleporterMessengerMetaData.GetAbi() + if err != nil { + return nil, errors.Wrap(err, "failed to get abi") + } + + return abi.Pack("receiveCrossChainMessage", messageIndex, relayerRewardAddress) +} + +// PackMessageReceived packs a MessageReceivedInput to form a call to the messageReceived function +func PackMessageReceived(originChainID ids.ID, messageID *big.Int) ([]byte, error) { + abi, err := TeleporterMessengerMetaData.GetAbi() + if err != nil { + return nil, errors.Wrap(err, "failed to get abi") + } + return abi.Pack("messageReceived", originChainID, messageID) +} + +// UnpackMessageReceivedResult attempts to unpack result bytes to a bool indicating whether the message was received +func UnpackMessageReceivedResult(result []byte) (bool, error) { + abi, err := TeleporterMessengerMetaData.GetAbi() + if err != nil { + return false, errors.Wrap(err, "failed to get abi") + } + + var success bool + err = abi.UnpackIntoInterface(&success, "messageReceived", result) + return success, err +} + +func PackMessageReceivedOutput(success bool) ([]byte, error) { + abi, err := TeleporterMessengerMetaData.GetAbi() + if err != nil { + return nil, errors.Wrap(err, "failed to get abi") + } + + return abi.PackOutput("messageReceived", success) +} + +// CAUTION: PackEvent is documented as not supporting struct types, so this should only be used for testing puposes. +// In a real setting, the Teleporter contract should pack the event. +// PackSendCrossChainMessageEvent packs the SendCrossChainMessage event type. +func PackSendCrossChainMessageEvent(destinationChainID common.Hash, message TeleporterMessage) ([]byte, error) { + abi, err := TeleporterMessengerMetaData.GetAbi() + if err != nil { + return nil, errors.Wrap(err, "failed to get abi") + } + + _, hashes, err := abi.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message) + return hashes, err +} diff --git a/go/teleporter-utilities/utils.go b/go/teleporter-utilities/utils.go new file mode 100644 index 000000000..7b3b97ad5 --- /dev/null +++ b/go/teleporter-utilities/utils.go @@ -0,0 +1,46 @@ +package teleporterutilities + +import ( + "errors" + "math/big" + + "github.com/ava-labs/avalanchego/utils/math" +) + +const ( + receiveCrossChainMessageStaticGasCost uint64 = 2_000_000 + receiveCrossChainMessageGasCostPerAggregatedKey uint64 = 1_000 + receiveMessageGasLimitBufferAmount uint64 = 100_000 + + BaseFeeFactor = 2 + MaxPriorityFeePerGas = 2500000000 // 2.5 gwei +) + +// CalculateReceiveMessageGasLimit calculates the estimated gas amount used by a single call +// to receiveCrossChainMessage for the given message and validator bit vector. The result amount +// depends on the required limit for the message execution, the number of validator signatures +// included in the aggregate signature, the static gas cost defined by the precompile, and an +// extra buffer amount defined here to ensure the call doesn't run out of gas. +func CalculateReceiveMessageGasLimit(numSigners int, executionRequiredGasLimit *big.Int) (uint64, error) { + if !executionRequiredGasLimit.IsUint64() { + return 0, errors.New("required gas limit too high") + } + + gasAmounts := []uint64{ + executionRequiredGasLimit.Uint64(), + receiveCrossChainMessageStaticGasCost, + uint64(numSigners) * receiveCrossChainMessageGasCostPerAggregatedKey, + receiveMessageGasLimitBufferAmount, + } + + res := gasAmounts[0] + var err error + for i := 1; i < len(gasAmounts); i++ { + res, err = math.Add64(res, gasAmounts[i]) + if err != nil { + return 0, err + } + } + + return res, nil +} diff --git a/scripts/abi_go_bindings.sh b/scripts/abi_go_bindings.sh index 242a77299..4ebdf7ddc 100755 --- a/scripts/abi_go_bindings.sh +++ b/scripts/abi_go_bindings.sh @@ -69,11 +69,12 @@ do fi echo "Generating Go bindings for $contract_name..." - mkdir -p $TELEPORTER_PATH/abi-bindings/$dir/$contract_name + gen_path=$TELEPORTER_PATH/go/abi-bindings/$dir/$contract_name + mkdir -p $gen_path $GOPATH/bin/abigen --abi $abi_file \ --pkg $(convertToLower $contract_name) \ --type $contract_name \ - --out $TELEPORTER_PATH/abi-bindings/$dir/$contract_name/$contract_name.go + --out $gen_path/$contract_name.go echo "Done generating Go bindings for $contract_name." done diff --git a/tests/basic_one_way_send.go b/tests/basic_one_way_send.go index e6dc8b2b3..1babe7ba6 100644 --- a/tests/basic_one_way_send.go +++ b/tests/basic_one_way_send.go @@ -5,7 +5,7 @@ import ( "math/big" "github.com/ava-labs/subnet-evm/interfaces" - teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/Teleporter/TeleporterMessenger" + teleportermessenger "github.com/ava-labs/teleporter/go/abi-bindings/Teleporter/TeleporterMessenger" "github.com/ava-labs/teleporter/tests/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" @@ -65,7 +65,7 @@ func BasicOneWaySend() { // // Check Teleporter message received on the destination // - data, err := utils.TeleporterABI.PackMessageReceived(subnetAInfo.BlockchainID, teleporterMessageID) + data, err := teleportermessenger.PackMessageReceived(subnetAInfo.BlockchainID, teleporterMessageID) Expect(err).Should(BeNil()) callMessage := interfaces.CallMsg{ To: &teleporterContractAddress, @@ -75,7 +75,7 @@ func BasicOneWaySend() { Expect(err).Should(BeNil()) // check the contract call result - delivered, err := utils.TeleporterABI.UnpackMessageReceivedResult(result) + delivered, err := teleportermessenger.UnpackMessageReceivedResult(result) Expect(err).Should(BeNil()) Expect(delivered).Should(BeTrue()) } diff --git a/tests/utils/abis.go b/tests/utils/abis.go deleted file mode 100644 index 01c4540e8..000000000 --- a/tests/utils/abis.go +++ /dev/null @@ -1,72 +0,0 @@ -package utils - -import ( - "math/big" - "strings" - - "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/subnet-evm/accounts/abi" - "github.com/ethereum/go-ethereum/common" - "github.com/pkg/errors" - - teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/Teleporter/TeleporterMessenger" -) - -type teleporterABI struct { - teleporterMessengerABI abi.ABI -} - -func NewTeleporterABI() *teleporterABI { - parsed, err := abi.JSON(strings.NewReader(teleportermessenger.TeleporterMessengerABI)) - if err != nil { - panic(err) - } - - return &teleporterABI{ - teleporterMessengerABI: parsed, - } -} - -// UnpackTeleporterMessage unpacks message bytes according to EVM ABI encoding rules into a TeleporterMessage -func (t *teleporterABI) UnpackTeleporterMessage(messageBytes []byte) (*teleportermessenger.TeleporterMessage, error) { - var teleporterMessage teleportermessenger.TeleporterMessage - err := t.teleporterMessengerABI.UnpackIntoInterface(&teleporterMessage, "teleporterMessage", messageBytes) - if err != nil { - return nil, errors.Wrap(err, "failed to unpack to teleporter message") - } - - return &teleporterMessage, nil -} - -func (t *teleporterABI) PackSendCrossChainMessage(input teleportermessenger.TeleporterMessageInput) ([]byte, error) { - return t.teleporterMessengerABI.Pack("sendCrossChainMessage", input) -} - -// PackReceiveCrossChainMessage packs a ReceiveCrossChainMessageInput to form a call to the receiveCrossChainMessage function -func (t *teleporterABI) PackReceiveCrossChainMessage(messageIndex uint32, relayerRewardAddress common.Address) ([]byte, error) { - return t.teleporterMessengerABI.Pack("receiveCrossChainMessage", messageIndex, relayerRewardAddress) -} - -// PackMessageReceived packs a MessageReceivedInput to form a call to the messageReceived function -func (t *teleporterABI) PackMessageReceived(originChainID ids.ID, messageID *big.Int) ([]byte, error) { - return t.teleporterMessengerABI.Pack("messageReceived", originChainID, messageID) -} - -// UnpackMessageReceivedResult attempts to unpack result bytes to a bool indicating whether the message was received -func (t *teleporterABI) UnpackMessageReceivedResult(result []byte) (bool, error) { - var success bool - err := t.teleporterMessengerABI.UnpackIntoInterface(&success, "messageReceived", result) - return success, err -} - -func (t *teleporterABI) PackMessageReceivedOutput(success bool) ([]byte, error) { - return t.teleporterMessengerABI.PackOutput("messageReceived", success) -} - -// CAUTION: PackEvent is documented as not supporting struct types, so this should only be used for testing puposes. -// In a real setting, the Teleporter contract should pack the event. -// PackSendCrossChainMessageEvent packs the SendCrossChainMessage event type. -func (t *teleporterABI) PackSendCrossChainMessageEvent(destinationChainID common.Hash, message teleportermessenger.TeleporterMessage) ([]byte, error) { - _, hashes, err := t.teleporterMessengerABI.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message) - return hashes, err -} diff --git a/tests/utils/network_setup.go b/tests/utils/network_setup.go index 4f2edb58d..3ee768ebe 100644 --- a/tests/utils/network_setup.go +++ b/tests/utils/network_setup.go @@ -14,6 +14,7 @@ import ( "github.com/ava-labs/subnet-evm/plugin/evm" "github.com/ava-labs/subnet-evm/rpc" "github.com/ava-labs/subnet-evm/tests/utils/runner" + teleporterutilities "github.com/ava-labs/teleporter/go/teleporter-utilities" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" @@ -22,9 +23,7 @@ import ( ) const ( - fundedKeyStr = "56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027" - baseFeeFactor = 2 - maxPriorityFeePerGas = 2500000000 // 2.5 gwei + fundedKeyStr = "56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027" ) var ( @@ -223,8 +222,8 @@ func DeployTeleporterContracts(transactionBytes []byte, deployerAddress common.A Expect(err).Should(BeNil()) baseFee, err := client.EstimateBaseFee(context.Background()) Expect(err).Should(BeNil()) - gasFeeCap := baseFee.Mul(baseFee, big.NewInt(baseFeeFactor)) - gasFeeCap.Add(gasFeeCap, big.NewInt(maxPriorityFeePerGas)) + gasFeeCap := baseFee.Mul(baseFee, big.NewInt(teleporterutilities.BaseFeeFactor)) + gasFeeCap.Add(gasFeeCap, big.NewInt(teleporterutilities.MaxPriorityFeePerGas)) // Fund the deployer address { value := big.NewInt(0).Mul(big.NewInt(1e18), big.NewInt(10)) // 10eth diff --git a/tests/utils/utils.go b/tests/utils/utils.go index 594d9db9d..aebdc39c8 100644 --- a/tests/utils/utils.go +++ b/tests/utils/utils.go @@ -13,11 +13,10 @@ import ( "time" "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/utils/math" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" warpBackend "github.com/ava-labs/subnet-evm/warp" - teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/Teleporter/TeleporterMessenger" - "github.com/pkg/errors" + teleportermessenger "github.com/ava-labs/teleporter/go/abi-bindings/Teleporter/TeleporterMessenger" + teleporterutilities "github.com/ava-labs/teleporter/go/teleporter-utilities" "github.com/ava-labs/subnet-evm/core/types" "github.com/ava-labs/subnet-evm/ethclient" @@ -44,37 +43,8 @@ var ( DefaultTeleporterTransactionGasFeeCap = big.NewInt(225 * params.GWei) DefaultTeleporterTransactionGasTipCap = big.NewInt(params.GWei) DefaultTeleporterTransactionValue = common.Big0 - - TeleporterABI = NewTeleporterABI() ) -type SendCrossChainMessageEvent struct { - DestinationChainID ids.ID - MessageID *big.Int - Message teleportermessenger.TeleporterMessage -} - -type ReceiveCrossChainMessageEvent struct { - OriginChainID ids.ID - MessageID *big.Int - Message teleportermessenger.TeleporterMessage -} - -// Teleporter contract sendCrossChainMessage input type -// type SendCrossChainMessageInput struct { -// DestinationChainID ids.ID -// DestinationAddress common.Address -// FeeInfo FeeInfo -// RequiredGasLimit *big.Int -// Message []byte -// AllowedRelayerAddresses []common.Address -// } - -// type FeeInfo struct { -// ContractAddress common.Address -// Amount *big.Int -// } - // // Test utility functions // @@ -146,7 +116,7 @@ func CreateSendCrossChainMessageTransaction( fundedKey *ecdsa.PrivateKey, teleporterContractAddress common.Address, ) *types.Transaction { - data, err := TeleporterABI.PackSendCrossChainMessage(input) + data, err := teleportermessenger.PackSendCrossChainMessage(input) Expect(err).Should(BeNil()) gasFeeCap, gasTipCap, nonce := calculateTxParams(ctx, source.ChainWSClient, fundedAddress) @@ -186,10 +156,10 @@ func CreateReceiveCrossChainMessageTransaction( numSigners, err := signedMessage.Signature.NumSigners() Expect(err).Should(BeNil()) - gasLimit, err := calculateReceiveMessageGasLimit(numSigners, requiredGasLimit) + gasLimit, err := teleporterutilities.CalculateReceiveMessageGasLimit(numSigners, requiredGasLimit) Expect(err).Should(BeNil()) - callData, err := TeleporterABI.PackReceiveCrossChainMessage(0, fundedAddress) + callData, err := teleportermessenger.PackReceiveCrossChainMessage(0, fundedAddress) Expect(err).Should(BeNil()) gasFeeCap, gasTipCap, nonce := calculateTxParams(ctx, wsClient, fundedAddress) @@ -368,32 +338,3 @@ func calculateTxParams(ctx context.Context, wsClient ethclient.Client, fundedAdd return gasFeeCap, gasTipCap, nonce } - -// CalculateReceiveMessageGasLimit calculates the estimated gas amount used by a single call -// to receiveCrossChainMessage for the given message and validator bit vector. The result amount -// depends on the required limit for the message execution, the number of validator signatures -// included in the aggregate signature, the static gas cost defined by the precompile, and an -// extra buffer amount defined here to ensure the call doesn't run out of gas. -func calculateReceiveMessageGasLimit(numSigners int, executionRequiredGasLimit *big.Int) (uint64, error) { - if !executionRequiredGasLimit.IsUint64() { - return 0, errors.New("required gas limit too high") - } - - gasAmounts := []uint64{ - executionRequiredGasLimit.Uint64(), - receiveCrossChainMessageStaticGasCost, - uint64(numSigners) * receiveCrossChainMessageGasCostPerAggregatedKey, - receiveMessageGasLimitBufferAmount, - } - - res := gasAmounts[0] - var err error - for i := 1; i < len(gasAmounts); i++ { - res, err = math.Add64(res, gasAmounts[i]) - if err != nil { - return 0, err - } - } - - return res, nil -} From 797260170831695d3a78e7dcfefb2c7401c4f770 Mon Sep 17 00:00:00 2001 From: Gwen Date: Fri, 6 Oct 2023 14:39:01 -0400 Subject: [PATCH 03/16] fix --- tests/utils/utils.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/utils/utils.go b/tests/utils/utils.go index aebdc39c8..33657bada 100644 --- a/tests/utils/utils.go +++ b/tests/utils/utils.go @@ -32,12 +32,6 @@ import ( . "github.com/onsi/gomega" ) -const ( - receiveCrossChainMessageStaticGasCost uint64 = 2_000_000 - receiveCrossChainMessageGasCostPerAggregatedKey uint64 = 1_000 - receiveMessageGasLimitBufferAmount = 100_000 -) - var ( DefaultTeleporterTransactionGas uint64 = 200_000 DefaultTeleporterTransactionGasFeeCap = big.NewInt(225 * params.GWei) From e897adaa33a351becf2edb659b085bda5587a44e Mon Sep 17 00:00:00 2001 From: Gwen Date: Fri, 13 Oct 2023 11:14:29 -0400 Subject: [PATCH 04/16] fix typo --- go/abi-bindings/Teleporter/TeleporterMessenger/packing.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go b/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go index 347c0e7ef..11f4637ce 100644 --- a/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go +++ b/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go @@ -73,7 +73,7 @@ func PackMessageReceivedOutput(success bool) ([]byte, error) { return abi.PackOutput("messageReceived", success) } -// CAUTION: PackEvent is documented as not supporting struct types, so this should only be used for testing puposes. +// CAUTION: PackEvent is documented as not supporting struct types, so this should only be used for testing purposes. // In a real setting, the Teleporter contract should pack the event. // PackSendCrossChainMessageEvent packs the SendCrossChainMessage event type. func PackSendCrossChainMessageEvent(destinationChainID common.Hash, message TeleporterMessage) ([]byte, error) { From d1fe1dd343ca22a98deaae2ac4544791a1621dbb Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Tue, 17 Oct 2023 22:26:57 +0000 Subject: [PATCH 05/16] restructure go utils directory --- contract-deployment/contractDeploymentTools.go | 6 +++--- .../ERC20Bridge/BridgeToken/BridgeToken.go | 0 .../ERC20Bridge/ERC20Bridge/ERC20Bridge.go | 0 .../ExampleCrossChainMessenger.go | 0 .../BlockHashPublisher/BlockHashPublisher.go | 0 .../BlockHashReceiver/BlockHashReceiver.go | 0 .../TeleporterMessenger/TeleporterMessenger.go | 0 .../Teleporter/TeleporterMessenger/packing.go | 0 .../utils/deployment_utils.go | 0 .../utils.go => go-utils/utils/gas_utils.go | 14 +++++++------- scripts/abi_go_bindings.sh | 2 +- tests/basic_one_way_send.go | 2 +- tests/e2e_test.go | 4 ++-- tests/utils/network_setup.go | 2 +- tests/utils/utils.go | 4 ++-- 15 files changed, 17 insertions(+), 17 deletions(-) rename {go => go-utils}/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go (100%) rename {go => go-utils}/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go (100%) rename {go => go-utils}/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go (100%) rename {go => go-utils}/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go (100%) rename {go => go-utils}/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go (100%) rename {go => go-utils}/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go (100%) rename {go => go-utils}/abi-bindings/Teleporter/TeleporterMessenger/packing.go (100%) rename {contract-deployment => go-utils}/utils/deployment_utils.go (100%) rename go/teleporter-utilities/utils.go => go-utils/utils/gas_utils.go (75%) diff --git a/contract-deployment/contractDeploymentTools.go b/contract-deployment/contractDeploymentTools.go index 7a21eef6c..5061e8b63 100644 --- a/contract-deployment/contractDeploymentTools.go +++ b/contract-deployment/contractDeploymentTools.go @@ -9,7 +9,7 @@ import ( "os" "strconv" - deploymentUtils "github.com/ava-labs/teleporter/contract-deployment/utils" + teleporterUtils "github.com/ava-labs/teleporter/go-utils/utils" "github.com/ethereum/go-ethereum/common" ) @@ -25,7 +25,7 @@ func main() { if len(os.Args) != 3 { log.Panic("Invalid argument count. Must provide JSON file containing contract bytecode.") } - _, _, _, err := deploymentUtils.ConstructKeylessTransaction(os.Args[2], true) + _, _, _, err := teleporterUtils.ConstructKeylessTransaction(os.Args[2], true) if err != nil { log.Panic("Failed to construct keyless transaction.", err) } @@ -41,7 +41,7 @@ func main() { log.Panic("Failed to parse nonce as uint", err) } - resultAddress, err := deploymentUtils.DeriveEVMContractAddress(deployerAddress, nonce) + resultAddress, err := teleporterUtils.DeriveEVMContractAddress(deployerAddress, nonce) if err != nil { log.Panic("Failed to derive contract address.", err) } diff --git a/go/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go b/go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go similarity index 100% rename from go/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go rename to go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go diff --git a/go/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go b/go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go similarity index 100% rename from go/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go rename to go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go diff --git a/go/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go b/go-utils/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go similarity index 100% rename from go/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go rename to go-utils/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go diff --git a/go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go b/go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go similarity index 100% rename from go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go rename to go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go diff --git a/go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go b/go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go similarity index 100% rename from go/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go rename to go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go diff --git a/go/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go similarity index 100% rename from go/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go rename to go-utils/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go diff --git a/go/abi-bindings/Teleporter/TeleporterMessenger/packing.go b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go similarity index 100% rename from go/abi-bindings/Teleporter/TeleporterMessenger/packing.go rename to go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go diff --git a/contract-deployment/utils/deployment_utils.go b/go-utils/utils/deployment_utils.go similarity index 100% rename from contract-deployment/utils/deployment_utils.go rename to go-utils/utils/deployment_utils.go diff --git a/go/teleporter-utilities/utils.go b/go-utils/utils/gas_utils.go similarity index 75% rename from go/teleporter-utilities/utils.go rename to go-utils/utils/gas_utils.go index 7b3b97ad5..f2bd6b49d 100644 --- a/go/teleporter-utilities/utils.go +++ b/go-utils/utils/gas_utils.go @@ -1,4 +1,4 @@ -package teleporterutilities +package utils import ( "errors" @@ -8,9 +8,9 @@ import ( ) const ( - receiveCrossChainMessageStaticGasCost uint64 = 2_000_000 - receiveCrossChainMessageGasCostPerAggregatedKey uint64 = 1_000 - receiveMessageGasLimitBufferAmount uint64 = 100_000 + ReceiveCrossChainMessageStaticGasCost uint64 = 2_000_000 + ReceiveCrossChainMessageGasCostPerAggregatedKey uint64 = 1_000 + ReceiveMessageGasLimitBufferAmount uint64 = 100_000 BaseFeeFactor = 2 MaxPriorityFeePerGas = 2500000000 // 2.5 gwei @@ -28,9 +28,9 @@ func CalculateReceiveMessageGasLimit(numSigners int, executionRequiredGasLimit * gasAmounts := []uint64{ executionRequiredGasLimit.Uint64(), - receiveCrossChainMessageStaticGasCost, - uint64(numSigners) * receiveCrossChainMessageGasCostPerAggregatedKey, - receiveMessageGasLimitBufferAmount, + ReceiveCrossChainMessageStaticGasCost, + uint64(numSigners) * ReceiveCrossChainMessageGasCostPerAggregatedKey, + ReceiveMessageGasLimitBufferAmount, } res := gasAmounts[0] diff --git a/scripts/abi_go_bindings.sh b/scripts/abi_go_bindings.sh index 4ebdf7ddc..1b85de209 100755 --- a/scripts/abi_go_bindings.sh +++ b/scripts/abi_go_bindings.sh @@ -69,7 +69,7 @@ do fi echo "Generating Go bindings for $contract_name..." - gen_path=$TELEPORTER_PATH/go/abi-bindings/$dir/$contract_name + gen_path=$TELEPORTER_PATH/go-utils/abi-bindings/$dir/$contract_name mkdir -p $gen_path $GOPATH/bin/abigen --abi $abi_file \ --pkg $(convertToLower $contract_name) \ diff --git a/tests/basic_one_way_send.go b/tests/basic_one_way_send.go index 1babe7ba6..812aaa633 100644 --- a/tests/basic_one_way_send.go +++ b/tests/basic_one_way_send.go @@ -5,7 +5,7 @@ import ( "math/big" "github.com/ava-labs/subnet-evm/interfaces" - teleportermessenger "github.com/ava-labs/teleporter/go/abi-bindings/Teleporter/TeleporterMessenger" + teleportermessenger "github.com/ava-labs/teleporter/go-utils/abi-bindings/Teleporter/TeleporterMessenger" "github.com/ava-labs/teleporter/tests/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" diff --git a/tests/e2e_test.go b/tests/e2e_test.go index fde110e1e..5516833ea 100644 --- a/tests/e2e_test.go +++ b/tests/e2e_test.go @@ -7,7 +7,7 @@ import ( "os" "testing" - deploymentUtils "github.com/ava-labs/teleporter/contract-deployment/utils" + teleporterUtils "github.com/ava-labs/teleporter/go-utils/utils" testUtils "github.com/ava-labs/teleporter/tests/utils" "github.com/ethereum/go-ethereum/log" "github.com/onsi/ginkgo/v2" @@ -32,7 +32,7 @@ func TestE2E(t *testing.T) { var _ = ginkgo.BeforeSuite(func() { testUtils.SetupNetwork(warpGenesisFile) // Generate the Teleporter deployment values - teleporterDeployerTransaction, teleporterDeployerAddress, teleporterContractAddress, err := deploymentUtils.ConstructKeylessTransaction(teleporterByteCodeFile, false) + teleporterDeployerTransaction, teleporterDeployerAddress, teleporterContractAddress, err := teleporterUtils.ConstructKeylessTransaction(teleporterByteCodeFile, false) Expect(err).Should(BeNil()) testUtils.DeployTeleporterContracts(teleporterDeployerTransaction, teleporterDeployerAddress, teleporterContractAddress) diff --git a/tests/utils/network_setup.go b/tests/utils/network_setup.go index 3ee768ebe..41f72e90a 100644 --- a/tests/utils/network_setup.go +++ b/tests/utils/network_setup.go @@ -14,7 +14,7 @@ import ( "github.com/ava-labs/subnet-evm/plugin/evm" "github.com/ava-labs/subnet-evm/rpc" "github.com/ava-labs/subnet-evm/tests/utils/runner" - teleporterutilities "github.com/ava-labs/teleporter/go/teleporter-utilities" + teleporterutilities "github.com/ava-labs/teleporter/go-utils/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" diff --git a/tests/utils/utils.go b/tests/utils/utils.go index 33657bada..4494d3472 100644 --- a/tests/utils/utils.go +++ b/tests/utils/utils.go @@ -15,8 +15,8 @@ import ( "github.com/ava-labs/avalanchego/ids" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" warpBackend "github.com/ava-labs/subnet-evm/warp" - teleportermessenger "github.com/ava-labs/teleporter/go/abi-bindings/Teleporter/TeleporterMessenger" - teleporterutilities "github.com/ava-labs/teleporter/go/teleporter-utilities" + teleportermessenger "github.com/ava-labs/teleporter/go-utils/abi-bindings/Teleporter/TeleporterMessenger" + teleporterutilities "github.com/ava-labs/teleporter/go-utils/utils" "github.com/ava-labs/subnet-evm/core/types" "github.com/ava-labs/subnet-evm/ethclient" From 96b58b4613ff1a9b8969eecb595ba2ac92515578 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 03:33:48 +0000 Subject: [PATCH 06/16] fix unpack teleporter message --- .../Teleporter/TeleporterMessenger/packing.go | 50 ++++++++++++--- .../TeleporterMessenger/packing_test.go | 63 +++++++++++++++++++ 2 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go diff --git a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go index 11f4637ce..e09a9dcab 100644 --- a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go +++ b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go @@ -1,27 +1,59 @@ package teleportermessenger import ( + "fmt" "math/big" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/subnet-evm/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/pkg/errors" ) -// UnpackTeleporterMessage unpacks message bytes according to EVM ABI encoding rules into a TeleporterMessage -func UnpackTeleporterMessage(messageBytes []byte) (*TeleporterMessage, error) { - abi, err := TeleporterMessengerMetaData.GetAbi() +var teleporterMessageType abi.Type + +func init() { + // Create an ABI binding for TeleporterMessage, defined in ITeleporterMessenger.sol + // abigen does not support ABI bindings for standalone structs, only methods and events, + // so we must manually keep this up-to-date with the struct defined in the contract. + var err error + teleporterMessageType, err = abi.NewType("tuple", "struct Overloader.F", []abi.ArgumentMarshaling{ + {Name: "messageID", Type: "uint256"}, + {Name: "senderAddress", Type: "address"}, + {Name: "destinationAddress", Type: "address"}, + {Name: "requiredGasLimit", Type: "uint256"}, + {Name: "allowedRelayerAddresses", Type: "address[]"}, + {Name: "receipts", Type: "tuple[]", Components: []abi.ArgumentMarshaling{ + {Name: "receivedMessageID", Type: "uint256"}, + {Name: "relayerRewardAddress", Type: "address"}, + }}, + {Name: "message", Type: "bytes"}, + }) if err != nil { - return nil, errors.Wrap(err, "failed to get abi") + panic(fmt.Sprintf("failed to create TeleporterMessage ABI type: %v", err)) } +} - var teleporterMessage TeleporterMessage - err = abi.UnpackIntoInterface(&teleporterMessage, "teleporterMessage", messageBytes) +func UnpackTeleporterMessage(messageBytes []byte) (*TeleporterMessage, error) { + args := abi.Arguments{ + { + Name: "teleporterMessage", + Type: teleporterMessageType, + }, + } + unpacked, err := args.Unpack(messageBytes) if err != nil { - return nil, errors.Wrap(err, "failed to unpack to teleporter message") + return nil, fmt.Errorf("failed to unpack to teleporter message with err: %v", err) } - - return &teleporterMessage, nil + type teleporterMessageArg struct { + TeleporterMessage TeleporterMessage `json:"teleporterMessage"` + } + var teleporterMessage teleporterMessageArg + err = args.Copy(&teleporterMessage, unpacked) + if err != nil { + return nil, err + } + return &teleporterMessage.TeleporterMessage, nil } func PackSendCrossChainMessage(input TeleporterMessageInput) ([]byte, error) { diff --git a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go new file mode 100644 index 000000000..48541cd3b --- /dev/null +++ b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go @@ -0,0 +1,63 @@ +// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package teleportermessenger + +import ( + "bytes" + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/require" +) + +func testTeleporterMessage(messageID int64) TeleporterMessage { + m := TeleporterMessage{ + MessageID: big.NewInt(messageID), + SenderAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), + DestinationAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), + RequiredGasLimit: big.NewInt(2), + AllowedRelayerAddresses: []common.Address{ + common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), + }, + Receipts: []TeleporterMessageReceipt{ + { + ReceivedMessageID: big.NewInt(1), + RelayerRewardAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), + }, + }, + Message: []byte{1, 2, 3, 4}, + } + return m +} + +func TestPackUnpackTeleporterMessage(t *testing.T) { + var ( + messageID int64 = 4 + ) + message := testTeleporterMessage(messageID) + + b, err := PackSendCrossChainMessageEvent(common.HexToHash("0x03"), message) + if err != nil { + t.Errorf("failed to pack teleporter message: %v", err) + t.FailNow() + } + + unpacked, err := UnpackTeleporterMessage(b) + if err != nil { + t.Errorf("failed to unpack teleporter message: %v", err) + t.FailNow() + } + + for i := 0; i < len(message.AllowedRelayerAddresses); i++ { + require.Equal(t, unpacked.AllowedRelayerAddresses[i], message.AllowedRelayerAddresses[i]) + } + + for i := 0; i < len(message.Receipts); i++ { + require.Equal(t, message.Receipts[i].ReceivedMessageID, unpacked.Receipts[i].ReceivedMessageID) + require.Equal(t, message.Receipts[i].RelayerRewardAddress, unpacked.Receipts[i].RelayerRewardAddress) + } + + require.True(t, bytes.Equal(message.Message, unpacked.Message)) +} From aa527d2accfcd5f814bc101e8ccd3474575dc1bc Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 16:20:02 +0000 Subject: [PATCH 07/16] rename createTestTeleporterMessage --- .../Teleporter/TeleporterMessenger/packing_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go index 48541cd3b..568b05b27 100644 --- a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go +++ b/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func testTeleporterMessage(messageID int64) TeleporterMessage { +func createTestTeleporterMessage(messageID int64) TeleporterMessage { m := TeleporterMessage{ MessageID: big.NewInt(messageID), SenderAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), @@ -36,7 +36,7 @@ func TestPackUnpackTeleporterMessage(t *testing.T) { var ( messageID int64 = 4 ) - message := testTeleporterMessage(messageID) + message := createTestTeleporterMessage(messageID) b, err := PackSendCrossChainMessageEvent(common.HexToHash("0x03"), message) if err != nil { From 0bc07c94824245064672efa27cd63f740e8bb522 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 16:20:12 +0000 Subject: [PATCH 08/16] add go unit test job --- .github/workflows/abi_go_bindings_checker.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/abi_go_bindings_checker.yml b/.github/workflows/abi_go_bindings_checker.yml index b7f839c87..5a1f80b9c 100644 --- a/.github/workflows/abi_go_bindings_checker.yml +++ b/.github/workflows/abi_go_bindings_checker.yml @@ -37,3 +37,18 @@ jobs: - name: Check for clean branch run: .github/workflows/check_clean_branch.sh + unit_tests: + name: Unit tests + runs-on: ubuntu-20.04 + + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Checkout teleporter repository + uses: actions/checkout@v4 + + - name: Run ABI Binding Unit Tests + run: go test ./... From 2984722a8875f46854f04b14d5131e4c9a3a9c6e Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 16:43:30 +0000 Subject: [PATCH 09/16] go unit test job --- .github/workflows/abi_go_bindings_checker.yml | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/abi_go_bindings_checker.yml b/.github/workflows/abi_go_bindings_checker.yml index 5a1f80b9c..781f6fbca 100644 --- a/.github/workflows/abi_go_bindings_checker.yml +++ b/.github/workflows/abi_go_bindings_checker.yml @@ -37,18 +37,18 @@ jobs: - name: Check for clean branch run: .github/workflows/check_clean_branch.sh - unit_tests: - name: Unit tests - runs-on: ubuntu-20.04 + unit_tests: + name: Unit tests + runs-on: ubuntu-20.04 - steps: - - name: Setup Go - uses: actions/setup-go@v4 - with: - go-version: ${{ env.GO_VERSION }} + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: ${{ env.GO_VERSION }} - - name: Checkout teleporter repository - uses: actions/checkout@v4 + - name: Checkout teleporter repository + uses: actions/checkout@v4 - - name: Run ABI Binding Unit Tests - run: go test ./... + - name: Run ABI Binding Unit Tests + run: go test ./... From 3283c7aeae9e59a827dbc2499ef584067e8bceb8 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 17:40:18 +0000 Subject: [PATCH 10/16] restructure repo --- .../ERC20Bridge/BridgeToken/BridgeToken.go | 0 .../ERC20Bridge/ERC20Bridge/ERC20Bridge.go | 0 .../ExampleCrossChainMessenger.go | 0 .../BlockHashPublisher/BlockHashPublisher.go | 0 .../BlockHashReceiver/BlockHashReceiver.go | 0 .../Teleporter/TeleporterMessenger/TeleporterMessenger.go | 0 .../go}/Teleporter/TeleporterMessenger/packing.go | 0 .../go}/Teleporter/TeleporterMessenger/packing_test.go | 0 docker/run_setup.sh | 2 +- scripts/abi_go_bindings.sh | 2 +- tests/basic_one_way_send.go | 2 +- tests/e2e_test.go | 4 ++-- tests/utils/network_setup.go | 6 +++--- tests/utils/utils.go | 6 +++--- .../contract-deployment}/README.md | 0 .../contract-deployment}/contractDeploymentTools.go | 6 +++--- .../utils => utils/deployment-utils}/deployment_utils.go | 0 {go-utils/utils => utils/gas-utils}/gas_utils.go | 0 18 files changed, 14 insertions(+), 14 deletions(-) rename {go-utils/abi-bindings => abi-bindings/go}/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go (100%) rename {go-utils/abi-bindings => abi-bindings/go}/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go (100%) rename {go-utils/abi-bindings => abi-bindings/go}/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go (100%) rename {go-utils/abi-bindings => abi-bindings/go}/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go (100%) rename {go-utils/abi-bindings => abi-bindings/go}/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go (100%) rename {go-utils/abi-bindings => abi-bindings/go}/Teleporter/TeleporterMessenger/TeleporterMessenger.go (100%) rename {go-utils/abi-bindings => abi-bindings/go}/Teleporter/TeleporterMessenger/packing.go (100%) rename {go-utils/abi-bindings => abi-bindings/go}/Teleporter/TeleporterMessenger/packing_test.go (100%) rename {contract-deployment => utils/contract-deployment}/README.md (100%) rename {contract-deployment => utils/contract-deployment}/contractDeploymentTools.go (87%) rename {go-utils/utils => utils/deployment-utils}/deployment_utils.go (100%) rename {go-utils/utils => utils/gas-utils}/gas_utils.go (100%) diff --git a/go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go b/abi-bindings/go/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go similarity index 100% rename from go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go rename to abi-bindings/go/CrossChainApplications/ERC20Bridge/BridgeToken/BridgeToken.go diff --git a/go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go b/abi-bindings/go/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go similarity index 100% rename from go-utils/abi-bindings/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go rename to abi-bindings/go/CrossChainApplications/ERC20Bridge/ERC20Bridge/ERC20Bridge.go diff --git a/go-utils/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go b/abi-bindings/go/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go similarity index 100% rename from go-utils/abi-bindings/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go rename to abi-bindings/go/CrossChainApplications/ExampleMessenger/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go diff --git a/go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go b/abi-bindings/go/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go similarity index 100% rename from go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go rename to abi-bindings/go/CrossChainApplications/VerifiedBlockHash/BlockHashPublisher/BlockHashPublisher.go diff --git a/go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go b/abi-bindings/go/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go similarity index 100% rename from go-utils/abi-bindings/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go rename to abi-bindings/go/CrossChainApplications/VerifiedBlockHash/BlockHashReceiver/BlockHashReceiver.go diff --git a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go b/abi-bindings/go/Teleporter/TeleporterMessenger/TeleporterMessenger.go similarity index 100% rename from go-utils/abi-bindings/Teleporter/TeleporterMessenger/TeleporterMessenger.go rename to abi-bindings/go/Teleporter/TeleporterMessenger/TeleporterMessenger.go diff --git a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go b/abi-bindings/go/Teleporter/TeleporterMessenger/packing.go similarity index 100% rename from go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go rename to abi-bindings/go/Teleporter/TeleporterMessenger/packing.go diff --git a/go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go b/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go similarity index 100% rename from go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing_test.go rename to abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go diff --git a/docker/run_setup.sh b/docker/run_setup.sh index 8950c27a9..02844aa55 100755 --- a/docker/run_setup.sh +++ b/docker/run_setup.sh @@ -94,7 +94,7 @@ if [ ! -e $dir_prefix/NETWORK_RUNNING ]; then cd contracts forge build cd .. - go run contract-deployment/contractDeploymentTools.go constructKeylessTx contracts/out/TeleporterMessenger.sol/TeleporterMessenger.json + go run utils/contract-deployment/contractDeploymentTools.go constructKeylessTx contracts/out/TeleporterMessenger.sol/TeleporterMessenger.json teleporter_deploy_address=$(cat UniversalTeleporterDeployerAddress.txt) teleporter_deploy_tx=$(cat UniversalTeleporterDeployerTransaction.txt) teleporter_contract_address=$(cat UniversalTeleporterMessengerContractAddress.txt) diff --git a/scripts/abi_go_bindings.sh b/scripts/abi_go_bindings.sh index 1b85de209..6dc089d5a 100755 --- a/scripts/abi_go_bindings.sh +++ b/scripts/abi_go_bindings.sh @@ -69,7 +69,7 @@ do fi echo "Generating Go bindings for $contract_name..." - gen_path=$TELEPORTER_PATH/go-utils/abi-bindings/$dir/$contract_name + gen_path=$TELEPORTER_PATH/abi-bindings/go/$dir/$contract_name mkdir -p $gen_path $GOPATH/bin/abigen --abi $abi_file \ --pkg $(convertToLower $contract_name) \ diff --git a/tests/basic_one_way_send.go b/tests/basic_one_way_send.go index 812aaa633..1f727e81d 100644 --- a/tests/basic_one_way_send.go +++ b/tests/basic_one_way_send.go @@ -5,7 +5,7 @@ import ( "math/big" "github.com/ava-labs/subnet-evm/interfaces" - teleportermessenger "github.com/ava-labs/teleporter/go-utils/abi-bindings/Teleporter/TeleporterMessenger" + teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/go/Teleporter/TeleporterMessenger" "github.com/ava-labs/teleporter/tests/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" diff --git a/tests/e2e_test.go b/tests/e2e_test.go index 5516833ea..79730c7e5 100644 --- a/tests/e2e_test.go +++ b/tests/e2e_test.go @@ -7,8 +7,8 @@ import ( "os" "testing" - teleporterUtils "github.com/ava-labs/teleporter/go-utils/utils" testUtils "github.com/ava-labs/teleporter/tests/utils" + deploymentUtils "github.com/ava-labs/teleporter/utils/deployment-utils" "github.com/ethereum/go-ethereum/log" "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -32,7 +32,7 @@ func TestE2E(t *testing.T) { var _ = ginkgo.BeforeSuite(func() { testUtils.SetupNetwork(warpGenesisFile) // Generate the Teleporter deployment values - teleporterDeployerTransaction, teleporterDeployerAddress, teleporterContractAddress, err := teleporterUtils.ConstructKeylessTransaction(teleporterByteCodeFile, false) + teleporterDeployerTransaction, teleporterDeployerAddress, teleporterContractAddress, err := deploymentUtils.ConstructKeylessTransaction(teleporterByteCodeFile, false) Expect(err).Should(BeNil()) testUtils.DeployTeleporterContracts(teleporterDeployerTransaction, teleporterDeployerAddress, teleporterContractAddress) diff --git a/tests/utils/network_setup.go b/tests/utils/network_setup.go index 41f72e90a..2c1594649 100644 --- a/tests/utils/network_setup.go +++ b/tests/utils/network_setup.go @@ -14,7 +14,7 @@ import ( "github.com/ava-labs/subnet-evm/plugin/evm" "github.com/ava-labs/subnet-evm/rpc" "github.com/ava-labs/subnet-evm/tests/utils/runner" - teleporterutilities "github.com/ava-labs/teleporter/go-utils/utils" + gasUtils "github.com/ava-labs/teleporter/utils/gas-utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" @@ -222,8 +222,8 @@ func DeployTeleporterContracts(transactionBytes []byte, deployerAddress common.A Expect(err).Should(BeNil()) baseFee, err := client.EstimateBaseFee(context.Background()) Expect(err).Should(BeNil()) - gasFeeCap := baseFee.Mul(baseFee, big.NewInt(teleporterutilities.BaseFeeFactor)) - gasFeeCap.Add(gasFeeCap, big.NewInt(teleporterutilities.MaxPriorityFeePerGas)) + gasFeeCap := baseFee.Mul(baseFee, big.NewInt(gasUtils.BaseFeeFactor)) + gasFeeCap.Add(gasFeeCap, big.NewInt(gasUtils.MaxPriorityFeePerGas)) // Fund the deployer address { value := big.NewInt(0).Mul(big.NewInt(1e18), big.NewInt(10)) // 10eth diff --git a/tests/utils/utils.go b/tests/utils/utils.go index 4494d3472..6841d1ff9 100644 --- a/tests/utils/utils.go +++ b/tests/utils/utils.go @@ -15,8 +15,8 @@ import ( "github.com/ava-labs/avalanchego/ids" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" warpBackend "github.com/ava-labs/subnet-evm/warp" - teleportermessenger "github.com/ava-labs/teleporter/go-utils/abi-bindings/Teleporter/TeleporterMessenger" - teleporterutilities "github.com/ava-labs/teleporter/go-utils/utils" + teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/go/Teleporter/TeleporterMessenger" + gasUtils "github.com/ava-labs/teleporter/utils/gas-utils" "github.com/ava-labs/subnet-evm/core/types" "github.com/ava-labs/subnet-evm/ethclient" @@ -150,7 +150,7 @@ func CreateReceiveCrossChainMessageTransaction( numSigners, err := signedMessage.Signature.NumSigners() Expect(err).Should(BeNil()) - gasLimit, err := teleporterutilities.CalculateReceiveMessageGasLimit(numSigners, requiredGasLimit) + gasLimit, err := gasUtils.CalculateReceiveMessageGasLimit(numSigners, requiredGasLimit) Expect(err).Should(BeNil()) callData, err := teleportermessenger.PackReceiveCrossChainMessage(0, fundedAddress) diff --git a/contract-deployment/README.md b/utils/contract-deployment/README.md similarity index 100% rename from contract-deployment/README.md rename to utils/contract-deployment/README.md diff --git a/contract-deployment/contractDeploymentTools.go b/utils/contract-deployment/contractDeploymentTools.go similarity index 87% rename from contract-deployment/contractDeploymentTools.go rename to utils/contract-deployment/contractDeploymentTools.go index 5061e8b63..bf7fbe26e 100644 --- a/contract-deployment/contractDeploymentTools.go +++ b/utils/contract-deployment/contractDeploymentTools.go @@ -9,7 +9,7 @@ import ( "os" "strconv" - teleporterUtils "github.com/ava-labs/teleporter/go-utils/utils" + deploymentUtils "github.com/ava-labs/teleporter/utils/deployment-utils" "github.com/ethereum/go-ethereum/common" ) @@ -25,7 +25,7 @@ func main() { if len(os.Args) != 3 { log.Panic("Invalid argument count. Must provide JSON file containing contract bytecode.") } - _, _, _, err := teleporterUtils.ConstructKeylessTransaction(os.Args[2], true) + _, _, _, err := deploymentUtils.ConstructKeylessTransaction(os.Args[2], true) if err != nil { log.Panic("Failed to construct keyless transaction.", err) } @@ -41,7 +41,7 @@ func main() { log.Panic("Failed to parse nonce as uint", err) } - resultAddress, err := teleporterUtils.DeriveEVMContractAddress(deployerAddress, nonce) + resultAddress, err := deploymentUtils.DeriveEVMContractAddress(deployerAddress, nonce) if err != nil { log.Panic("Failed to derive contract address.", err) } diff --git a/go-utils/utils/deployment_utils.go b/utils/deployment-utils/deployment_utils.go similarity index 100% rename from go-utils/utils/deployment_utils.go rename to utils/deployment-utils/deployment_utils.go diff --git a/go-utils/utils/gas_utils.go b/utils/gas-utils/gas_utils.go similarity index 100% rename from go-utils/utils/gas_utils.go rename to utils/gas-utils/gas_utils.go From acda69d489890a332b1753aed49fc986c866a5a2 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 19:51:42 +0000 Subject: [PATCH 11/16] rename abi script --- .../{abi_go_bindings_checker.yml => abi_bindings_checker.yml} | 2 +- scripts/{abi_go_bindings.sh => abi_bindings.sh} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename .github/workflows/{abi_go_bindings_checker.yml => abi_bindings_checker.yml} (96%) rename scripts/{abi_go_bindings.sh => abi_bindings.sh} (97%) diff --git a/.github/workflows/abi_go_bindings_checker.yml b/.github/workflows/abi_bindings_checker.yml similarity index 96% rename from .github/workflows/abi_go_bindings_checker.yml rename to .github/workflows/abi_bindings_checker.yml index 781f6fbca..0feffd442 100644 --- a/.github/workflows/abi_go_bindings_checker.yml +++ b/.github/workflows/abi_bindings_checker.yml @@ -33,7 +33,7 @@ jobs: export GOPATH=$HOME/go export PATH="$PATH:$BASE_DIR/.foundry/bin" export PATH="$PATH:$GOPATH/bin" - ./scripts/abi_go_bindings.sh + ./scripts/abi_bindings.sh - name: Check for clean branch run: .github/workflows/check_clean_branch.sh diff --git a/scripts/abi_go_bindings.sh b/scripts/abi_bindings.sh similarity index 97% rename from scripts/abi_go_bindings.sh rename to scripts/abi_bindings.sh index 6dc089d5a..533a6b1cd 100755 --- a/scripts/abi_go_bindings.sh +++ b/scripts/abi_bindings.sh @@ -26,7 +26,7 @@ while [ $# -gt 0 ]; do done if [ "$HELP" = true ]; then - echo "Usage: ./scripts/abi_go_bindings.sh [OPTIONS]" + echo "Usage: ./scripts/abi_bindings.sh [OPTIONS]" echo "Build contracts and generate Go bindings" echo "" echo "Options:" From b1ba8d12360781c8d148dd357d74b89fa229f2a8 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 19:51:56 +0000 Subject: [PATCH 12/16] add abi bindings readme --- abi-bindings/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 abi-bindings/README.md diff --git a/abi-bindings/README.md b/abi-bindings/README.md new file mode 100644 index 000000000..5b4d6c901 --- /dev/null +++ b/abi-bindings/README.md @@ -0,0 +1 @@ +This directory contains ABI bindings for the Solidity contracts in the `contracts/src/CrossChainApplications` and `contracts/src/Teleporter` directories. The files with the same name as the Solidity source files are automatically generated by the `scripts/abi_bindings.sh` script. Other files in this directory (such as the packing utilities) are manually created and maintained. \ No newline at end of file From 14d739f37ca02a4163e3ad27b18a17c70e23d237 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 20:05:42 +0000 Subject: [PATCH 13/16] checkout recursive --- .github/workflows/abi_bindings_checker.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/abi_bindings_checker.yml b/.github/workflows/abi_bindings_checker.yml index 0feffd442..28040f140 100644 --- a/.github/workflows/abi_bindings_checker.yml +++ b/.github/workflows/abi_bindings_checker.yml @@ -47,8 +47,10 @@ jobs: with: go-version: ${{ env.GO_VERSION }} - - name: Checkout teleporter repository + - name: Checkout repositories and submodules uses: actions/checkout@v4 + with: + submodules: recursive - name: Run ABI Binding Unit Tests run: go test ./... From 3f11cf0de271df80a8ea8a59d3508dfc84538bef Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 20:10:46 +0000 Subject: [PATCH 14/16] fix unit test --- abi-bindings/go/Teleporter/TeleporterMessenger/packing.go | 4 ++-- .../go/Teleporter/TeleporterMessenger/packing_test.go | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/abi-bindings/go/Teleporter/TeleporterMessenger/packing.go b/abi-bindings/go/Teleporter/TeleporterMessenger/packing.go index e09a9dcab..018811ad2 100644 --- a/abi-bindings/go/Teleporter/TeleporterMessenger/packing.go +++ b/abi-bindings/go/Teleporter/TeleporterMessenger/packing.go @@ -108,12 +108,12 @@ func PackMessageReceivedOutput(success bool) ([]byte, error) { // CAUTION: PackEvent is documented as not supporting struct types, so this should only be used for testing purposes. // In a real setting, the Teleporter contract should pack the event. // PackSendCrossChainMessageEvent packs the SendCrossChainMessage event type. -func PackSendCrossChainMessageEvent(destinationChainID common.Hash, message TeleporterMessage) ([]byte, error) { +func PackSendCrossChainMessageEvent(destinationChainID common.Hash, message TeleporterMessage, feeInfo TeleporterFeeInfo) ([]byte, error) { abi, err := TeleporterMessengerMetaData.GetAbi() if err != nil { return nil, errors.Wrap(err, "failed to get abi") } - _, hashes, err := abi.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message) + _, hashes, err := abi.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message, feeInfo) return hashes, err } diff --git a/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go b/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go index 568b05b27..81766bb07 100644 --- a/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go +++ b/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go @@ -38,7 +38,10 @@ func TestPackUnpackTeleporterMessage(t *testing.T) { ) message := createTestTeleporterMessage(messageID) - b, err := PackSendCrossChainMessageEvent(common.HexToHash("0x03"), message) + b, err := PackSendCrossChainMessageEvent(common.HexToHash("0x03"), message, TeleporterFeeInfo{ + ContractAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), + Amount: big.NewInt(1), + }) if err != nil { t.Errorf("failed to pack teleporter message: %v", err) t.FailNow() From 2673ffebd8ab45ef2f0ebc56f9cfa21a92cff575 Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 21:23:06 +0000 Subject: [PATCH 15/16] only run bindings unit tests --- .github/workflows/abi_bindings_checker.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/abi_bindings_checker.yml b/.github/workflows/abi_bindings_checker.yml index 28040f140..d2c08972c 100644 --- a/.github/workflows/abi_bindings_checker.yml +++ b/.github/workflows/abi_bindings_checker.yml @@ -53,4 +53,6 @@ jobs: submodules: recursive - name: Run ABI Binding Unit Tests - run: go test ./... + run: | + cd abi-bindings/go + go test ./... From d5c47e1c5857de970233aa9b877d38c672ce62dd Mon Sep 17 00:00:00 2001 From: cam-schultz Date: Wed, 18 Oct 2023 21:25:55 +0000 Subject: [PATCH 16/16] readme header --- abi-bindings/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/abi-bindings/README.md b/abi-bindings/README.md index 5b4d6c901..a38697b47 100644 --- a/abi-bindings/README.md +++ b/abi-bindings/README.md @@ -1 +1,3 @@ +## ABI Bindings + This directory contains ABI bindings for the Solidity contracts in the `contracts/src/CrossChainApplications` and `contracts/src/Teleporter` directories. The files with the same name as the Solidity source files are automatically generated by the `scripts/abi_bindings.sh` script. Other files in this directory (such as the packing utilities) are manually created and maintained. \ No newline at end of file