Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into revert-custom-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Lam committed Oct 18, 2023
2 parents 75a9b0e + 353b330 commit 4104849
Show file tree
Hide file tree
Showing 23 changed files with 283 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,26 @@ 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
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 repositories and submodules
uses: actions/checkout@v4
with:
submodules: recursive

- name: Run ABI Binding Unit Tests
run: |
cd abi-bindings/go
go test ./...
3 changes: 3 additions & 0 deletions abi-bindings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +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.
119 changes: 119 additions & 0 deletions abi-bindings/go/Teleporter/TeleporterMessenger/packing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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"
)

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 {
panic(fmt.Sprintf("failed to create TeleporterMessage ABI type: %v", err))
}
}

func UnpackTeleporterMessage(messageBytes []byte) (*TeleporterMessage, error) {
args := abi.Arguments{
{
Name: "teleporterMessage",
Type: teleporterMessageType,
},
}
unpacked, err := args.Unpack(messageBytes)
if err != nil {
return nil, fmt.Errorf("failed to unpack to teleporter message with err: %v", err)
}
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) {
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 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, 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, feeInfo)
return hashes, err
}
66 changes: 66 additions & 0 deletions abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 createTestTeleporterMessage(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 := createTestTeleporterMessage(messageID)

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()
}

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))
}
2 changes: 1 addition & 1 deletion docker/run_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ 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
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.28.0
github.com/pkg/errors v0.9.1
)
Expand Down Expand Up @@ -128,7 +127,7 @@ require (
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -447,8 +445,8 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.12.0 h1:UIVDowFPwpg6yMUpPjGkYvf06K3RAiJXUhCxEwQVHRI=
github.com/onsi/ginkgo/v2 v2.12.0/go.mod h1:ZNEzXISYlqpb8S36iN71ifqLi3vVD1rVJGvWRCJOUpQ=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
Expand Down Expand Up @@ -807,8 +805,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/ginkgo/v2 v2.12.0/go.mod h1:ZNEzXISYlqpb8S36iN71ifqLi3vVD1rVJGvWRCJOUpQ=
github.com/onsi/gomega v1.24.0/go.mod h1:Z/NWtiqwBrwUt4/2loMmHL63EDLnYHmVbuBpDr2vQAg=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down Expand Up @@ -969,6 +970,7 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
Expand Down
7 changes: 4 additions & 3 deletions scripts/abi_go_bindings.sh → scripts/abi_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
Expand Down Expand Up @@ -71,11 +71,12 @@ do
fi

echo "Generating Go bindings for $contract_name..."
mkdir -p $TELEPORTER_PATH/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) \
--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

Expand Down
14 changes: 5 additions & 9 deletions tests/basic_one_way_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ 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"
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"
Expand All @@ -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),
},
Expand Down Expand Up @@ -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 := teleportermessenger.PackMessageReceived(subnetAInfo.BlockchainID, teleporterMessageID)
Expect(err).Should(BeNil())
callMessage := interfaces.CallMsg{
To: &teleporterContractAddress,
Expand All @@ -79,7 +75,7 @@ func BasicOneWaySend() {
Expect(err).Should(BeNil())

// check the contract call result
delivered, err := teleporter.UnpackMessageReceivedResult(result)
delivered, err := teleportermessenger.UnpackMessageReceivedResult(result)
Expect(err).Should(BeNil())
Expect(delivered).Should(BeTrue())
}
2 changes: 1 addition & 1 deletion tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"
"testing"

deploymentUtils "github.com/ava-labs/teleporter/contract-deployment/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"
Expand Down
Loading

0 comments on commit 4104849

Please sign in to comment.