Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Remove awm-relayer dependency #52

Merged
merged 19 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these are not just abi binding unit tests right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Added a cd abi-bindings/go so it will only run the ABI bindings unit tests. Running go test ./... will call the E2E tests. They won't run since the necessary env var isn't set, but that's still not what we're targeting here.

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.
cam-schultz marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.13.0
Expand Down
2 changes: 0 additions & 2 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
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
6 changes: 3 additions & 3 deletions tests/utils/network_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ 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"
"github.com/ava-labs/subnet-evm/rpc"
"github.com/ava-labs/subnet-evm/tests/utils/runner"
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"
Expand Down Expand Up @@ -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(relayerEvm.BaseFeeFactor))
gasFeeCap.Add(gasFeeCap, big.NewInt(relayerEvm.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
Expand Down
Loading