Skip to content

Commit

Permalink
unify pack and unpacks, and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Lam committed Sep 20, 2023
1 parent 80ab005 commit cf97a39
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
18 changes: 15 additions & 3 deletions messages/teleporter/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type TeleporterMessage struct {
Message []byte `json:"message"`
}

// TeleporterMessageReceipt corresponds to the receipt of a Teleporter message ID
// and the relayer reward address for that message
type TeleporterMessageReceipt struct {
ReceivedMessageID *big.Int `json:"receivedMessageID"`
RelayerRewardAddress common.Address `json:"relayerRewardAddress"`
Expand All @@ -42,7 +44,7 @@ type MessageReceivedInput struct {
MessageID *big.Int `json:"messageID"`
}

// unpack Teleporter message bytes according to EVM ABI encoding rules
// UnpackTeleporterMessage unpacks message bytes according to EVM ABI encoding rules into a TeleporterMessage
func UnpackTeleporterMessage(messageBytes []byte) (*TeleporterMessage, error) {
args := abi.Arguments{
{
Expand All @@ -65,16 +67,26 @@ func UnpackTeleporterMessage(messageBytes []byte) (*TeleporterMessage, error) {
return &teleporterMessage.TeleporterMessage, nil
}

func PackReceiverMessage(inputStruct ReceiveCrossChainMessageInput) ([]byte, error) {
// PackReceiveCrossChainMessage packs a ReceiveCrossChainMessageInput to form a call to the receiveCrossChainMessage function
func PackReceiveCrossChainMessage(inputStruct ReceiveCrossChainMessageInput) ([]byte, error) {
return EVMTeleporterContractABI.Pack("receiveCrossChainMessage", inputStruct.RelayerRewardAddress)
}

func PackMessageReceivedMessage(inputStruct MessageReceivedInput) ([]byte, error) {
// PackMessageReceived packs a MessageReceivedInput to form a call to the messageReceived function
func PackMessageReceived(inputStruct MessageReceivedInput) ([]byte, error) {
return EVMTeleporterContractABI.Pack("messageReceived", inputStruct.OriginChainID, inputStruct.MessageID)
}

// UnpackMessageReceivedResult attempts to unpack result bytes to a bool indicating whether the message was received
func UnpackMessageReceivedResult(result []byte) (bool, error) {
var success bool
err := EVMTeleporterContractABI.UnpackIntoInterface(&success, "messageReceived", result)
return success, err
}

// PackSendCrossChainMessageEvent packs the SendCrossChainMessage event type. PackEvent is documented as not supporting struct types, so this should be used
// with caution. Here, we only use it for testing purposes. In a real setting, the Teleporter contract should pack the event.
func PackSendCrossChainMessageEvent(destinationChainID common.Hash, message TeleporterMessage) ([]byte, error) {
_, hashes, err := EVMTeleporterContractABI.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message)
return hashes, err
}
4 changes: 2 additions & 2 deletions messages/teleporter/message_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (m *messageManager) messageDelivered(
return false, errors.New("destination client is not an Ethereum client")
}

data, err := PackMessageReceivedMessage(MessageReceivedInput{
data, err := PackMessageReceived(MessageReceivedInput{
OriginChainID: warpMessageInfo.WarpUnsignedMessage.SourceChainID,
MessageID: teleporterMessage.MessageID,
})
Expand Down Expand Up @@ -253,7 +253,7 @@ func (m *messageManager) SendMessage(signedMessage *warp.Message, parsedVmPayloa
return err
}
// Construct the transaction call data to call the receive cross chain message method of the receiver precompile.
callData, err := PackReceiverMessage(ReceiveCrossChainMessageInput{
callData, err := PackReceiveCrossChainMessage(ReceiveCrossChainMessageInput{
RelayerRewardAddress: common.HexToAddress(m.messageConfig.RewardAddress),
})
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions messages/teleporter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/awm-relayer/utils"
"github.com/ethereum/go-ethereum/common"
)

const (
Expand Down Expand Up @@ -48,10 +47,3 @@ func CalculateReceiveMessageGasLimit(numSigners int, executionRequiredGasLimit *

return res, nil
}

// Pack the SendCrossChainMessage event type. PackEvent is documented as not supporting struct types, so this should be used
// with caution. Here, we only use it for testing purposes. In a real setting, the Teleporter contract should pack the event.
func PackSendCrossChainMessageEvent(destinationChainID common.Hash, message TeleporterMessage) ([]byte, error) {
_, hashes, err := EVMTeleporterContractABI.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message)
return hashes, err
}

0 comments on commit cf97a39

Please sign in to comment.