-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1c8461e
remove awm-relayer dependency
gwen917 e136ae6
refactor
gwen917 7972601
fix
gwen917 e897ada
fix typo
gwen917 d1fe1dd
restructure go utils directory
cam-schultz 96b58b4
fix unpack teleporter message
cam-schultz aa527d2
rename createTestTeleporterMessage
cam-schultz 0bc07c9
add go unit test job
cam-schultz 2984722
go unit test job
cam-schultz 3283c7a
restructure repo
cam-schultz acda69d
rename abi script
cam-schultz b1ba8d1
add abi bindings readme
cam-schultz 07c40a2
Merge branch 'main' into abi-pack
cam-schultz 14d739f
checkout recursive
cam-schultz 3f11cf0
fix unit test
cam-schultz 2673ffe
only run bindings unit tests
cam-schultz d5c47e1
readme header
cam-schultz e4a85ff
Merge branch 'main' into abi-pack
cam-schultz ab76344
Merge branch 'main' into abi-pack
cam-schultz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
119 changes: 119 additions & 0 deletions
119
abi-bindings/go/Teleporter/TeleporterMessenger/packing.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. Runninggo 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.