Skip to content

Commit

Permalink
sending teleporter message from a to b
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Lam committed Aug 31, 2023
1 parent 5f64426 commit a84cb32
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
9 changes: 1 addition & 8 deletions messages/teleporter/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,10 @@ func testTeleporterMessage(messageID int64) TeleporterMessage {
return m
}

// 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 packTeleporterMessage(destinationChainID common.Hash, message TeleporterMessage) ([]byte, error) {
_, hashes, err := EVMTeleporterContractABI.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message)
return hashes, err
}

func TestPackUnpackTeleporterMessage(t *testing.T) {
message := testTeleporterMessage(4)

b, err := packTeleporterMessage(common.HexToHash("0x03"), message)
b, err := PackTeleporterMessage(common.HexToHash("0x03"), message)
if err != nil {
t.Errorf("failed to pack teleporter message: %v", err)
t.FailNow()
Expand Down
8 changes: 8 additions & 0 deletions messages/teleporter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 @@ -47,3 +48,10 @@ 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 PackTeleporterMessage(destinationChainID common.Hash, message TeleporterMessage) ([]byte, error) {
_, hashes, err := EVMTeleporterContractABI.PackEvent("SendCrossChainMessage", destinationChainID, message.MessageID, message)
return hashes, err
}
80 changes: 49 additions & 31 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/awm-relayer/config"
"github.com/ava-labs/awm-relayer/messages/teleporter"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/ethclient"
"github.com/ava-labs/subnet-evm/interfaces"
Expand All @@ -40,6 +41,15 @@ var (
warpChainConfigPath string
relayerConfigPath string
teleporterContractAddress = common.HexToAddress("27aE10273D17Cd7e80de8580A51f476960626e5f")
teleporterMessage = teleporter.TeleporterMessage{
MessageID: big.NewInt(1),
SenderAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
DestinationAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
RequiredGasLimit: big.NewInt(1),
AllowedRelayerAddresses: []common.Address{},
Receipts: []teleporter.TeleporterMessageReceipt{},
Message: []byte{1, 2, 3, 4},
}
)

func TestE2E(t *testing.T) {
Expand Down Expand Up @@ -141,18 +151,18 @@ var _ = ginkgo.AfterSuite(func() {
var _ = ginkgo.Describe("[Relayer]", ginkgo.Ordered, func() {
log.Info("Got to ginkgo describe")
var (
subnetA, subnetB ids.ID
blockchainIDA, blockchainIDB ids.ID
chainAURIs, chainBURIs []string
fundedKey *ecdsa.PrivateKey
fundedAddress common.Address
err error
unsignedWarpMsg *avalancheWarp.UnsignedMessage
unsignedWarpMessageID ids.ID
signedWarpMsg *avalancheWarp.Message
subnetA, subnetB ids.ID
blockchainIDA, blockchainIDB ids.ID
chainAURIs, chainBURIs []string
fundedKey *ecdsa.PrivateKey
fundedAddress common.Address
err error
unsignedWarpMsg *avalancheWarp.UnsignedMessage
unsignedWarpMessageID ids.ID
// signedWarpMsg *avalancheWarp.Message
chainAWSClient, chainBWSClient ethclient.Client
chainID = big.NewInt(99999)
payload = []byte{1, 2, 3}
payload = []byte{}
txSigner = types.LatestSignerForChainID(chainID)
)

Expand Down Expand Up @@ -278,14 +288,22 @@ var _ = ginkgo.Describe("[Relayer]", ginkgo.Ordered, func() {
gomega.Expect(err).Should(gomega.BeNil())

log.Info("Subscribing to new heads")
newHeads := make(chan *types.Header, 10)
sub, err := chainAWSClient.SubscribeNewHead(ctx, newHeads)
// newHeadsA := make(chan *types.Header, 10)
// sub, err := chainAWSClient.SubscribeNewHead(ctx, newHeadsA)
// gomega.Expect(err).Should(gomega.BeNil())
// defer sub.Unsubscribe()

newHeadsB := make(chan *types.Header, 10)
sub, err := chainBWSClient.SubscribeNewHead(ctx, newHeadsB)
gomega.Expect(err).Should(gomega.BeNil())
defer sub.Unsubscribe()

startingNonce, err := chainAWSClient.NonceAt(ctx, fundedAddress, nil)
gomega.Expect(err).Should(gomega.BeNil())

payload, err = teleporter.PackTeleporterMessage(common.Hash(blockchainIDB), teleporterMessage)
gomega.Expect(err).Should(gomega.BeNil())

packedInput, err := warp.PackSendWarpMessage(warp.SendWarpMessageInput{
DestinationChainID: common.Hash(blockchainIDB),
DestinationAddress: fundedAddress,
Expand All @@ -304,16 +322,16 @@ var _ = ginkgo.Describe("[Relayer]", ginkgo.Ordered, func() {
})
signedTx, err := types.SignTx(tx, txSigner, fundedKey)
gomega.Expect(err).Should(gomega.BeNil())
log.Info("Sending sendWarpMessage transaction", "txHash", signedTx.Hash())
log.Info("Sending sendWarpMessage transaction", "destinationChainID", blockchainIDB, "txHash", signedTx.Hash())
err = chainAWSClient.SendTransaction(ctx, signedTx)
gomega.Expect(err).Should(gomega.BeNil())

log.Info("Waiting for new block confirmation")
newHead := <-newHeads
newHead := <-newHeadsB
blockHash := newHead.Hash()

log.Info("Fetching relevant warp logs from the newly produced block")
logs, err := chainAWSClient.FilterLogs(ctx, interfaces.FilterQuery{
logs, err := chainBWSClient.FilterLogs(ctx, interfaces.FilterQuery{
BlockHash: &blockHash,
Addresses: []common.Address{warp.Module.Address},
})
Expand All @@ -335,22 +353,22 @@ var _ = ginkgo.Describe("[Relayer]", ginkgo.Ordered, func() {
// Loop over each client on chain A to ensure they all have time to accept the block.
// Note: if we did not confirm this here, the next stage could be racy since it assumes every node
// has accepted the block.
for i, uri := range chainAURIs {
chainAWSURI := toWebsocketURI(uri, blockchainIDA.String())
log.Info("Creating ethclient for blockchainA", "wsURI", chainAWSURI)
client, err := ethclient.Dial(chainAWSURI)
gomega.Expect(err).Should(gomega.BeNil())

// Loop until each node has advanced to >= the height of the block that emitted the warp log
for {
block, err := client.BlockByNumber(ctx, nil)
gomega.Expect(err).Should(gomega.BeNil())
if block.NumberU64() >= newHead.Number.Uint64() {
log.Info("client accepted the block containing SendWarpMessage", "client", i, "height", block.NumberU64())
break
}
}
}
// for i, uri := range chainAURIs {
// chainAWSURI := toWebsocketURI(uri, blockchainIDA.String())
// log.Info("Creating ethclient for blockchainA", "wsURI", chainAWSURI)
// client, err := ethclient.Dial(chainAWSURI)
// gomega.Expect(err).Should(gomega.BeNil())

// // Loop until each node has advanced to >= the height of the block that emitted the warp log
// for {
// block, err := client.BlockByNumber(ctx, nil)
// gomega.Expect(err).Should(gomega.BeNil())
// if block.NumberU64() >= newHead.Number.Uint64() {
// log.Info("client accepted the block containing SendWarpMessage", "client", i, "height", block.NumberU64())
// break
// }
// }
// }
})
})

Expand Down

0 comments on commit a84cb32

Please sign in to comment.