Skip to content

Commit

Permalink
fix unpack teleporter message
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-schultz committed Oct 18, 2023
1 parent d1fe1dd commit 96b58b4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
50 changes: 41 additions & 9 deletions go-utils/abi-bindings/Teleporter/TeleporterMessenger/packing.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 96b58b4

Please sign in to comment.