Skip to content

Commit

Permalink
Merge pull request #139 from ava-labs/e2e-basic-send-receive-ginkgo
Browse files Browse the repository at this point in the history
e2e test case: basic send *and receive*
  • Loading branch information
feuGeneA authored Nov 22, 2023
2 parents e78983f + 67650ae commit f2f5694
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 62 deletions.
4 changes: 4 additions & 0 deletions tests/add_fee_amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ func AddFeeAmount(network network.Network) {
subnetAInfo.TeleporterMessenger.CheckRelayerRewardAmount(&bind.CallOpts{}, fundedAddress, mockTokenAddress)
Expect(err).Should(BeNil())
Expect(amount).Should(Equal(additionalFeeAmount.Add(additionalFeeAmount, initFeeAmount)))

utils.RedeemRelayerRewardsAndConfirm(
ctx, subnetAInfo, mockToken, mockTokenAddress, fundedKey, amount,
)
}
61 changes: 0 additions & 61 deletions tests/basic_one_way_send.go

This file was deleted.

119 changes: 119 additions & 0 deletions tests/basic_send_receive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package tests

import (
"context"
"math/big"

"github.com/ava-labs/subnet-evm/accounts/abi/bind"
teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/go/Teleporter/TeleporterMessenger"
"github.com/ava-labs/teleporter/tests/network"
"github.com/ava-labs/teleporter/tests/utils"
localUtils "github.com/ava-labs/teleporter/tests/utils/local-network-utils"
"github.com/ethereum/go-ethereum/common"
. "github.com/onsi/gomega"
)

func BasicSendReceiveGinkgo() {
BasicSendReceive(&network.LocalNetwork{})
}

// Tests basic one-way send from Subnet A to Subnet B and vice versa
func BasicSendReceive(network network.Network) {
var (
teleporterMessageID *big.Int
)

subnets := network.GetSubnetsInfo()
subnetAInfo := subnets[0]
subnetBInfo := subnets[1]
teleporterContractAddress := network.GetTeleporterContractAddress()
fundedAddress, fundedKey := network.GetFundedAccountInfo()

//
// Send a transaction to Subnet A to issue a Warp Message from the Teleporter contract to Subnet B
//
ctx := context.Background()

feeAmount := big.NewInt(1)
feeTokenAddress, feeToken := localUtils.DeployExampleERC20(
ctx,
fundedKey,
subnetAInfo,
)
localUtils.ExampleERC20Approve(
ctx,
feeToken,
teleporterContractAddress,
big.NewInt(0).Mul(big.NewInt(1e18),
big.NewInt(10)),
subnetAInfo,
fundedKey,
)

sendCrossChainMessageInput := teleportermessenger.TeleporterMessageInput{
DestinationChainID: subnetBInfo.BlockchainID,
DestinationAddress: fundedAddress,
FeeInfo: teleportermessenger.TeleporterFeeInfo{
FeeTokenAddress: feeTokenAddress,
Amount: feeAmount,
},
RequiredGasLimit: big.NewInt(1),
AllowedRelayerAddresses: []common.Address{},
Message: []byte{1, 2, 3, 4},
}

receipt, teleporterMessageID := utils.SendCrossChainMessageAndWaitForAcceptance(
ctx,
subnetAInfo,
subnetBInfo,
sendCrossChainMessageInput,
fundedKey,
)

//
// Relay the message to the destination
//

network.RelayMessage(ctx, receipt, subnetAInfo, subnetBInfo, true)

//
// Check Teleporter message received on the destination
//
delivered, err := subnetBInfo.TeleporterMessenger.MessageReceived(
&bind.CallOpts{}, subnetAInfo.BlockchainID, teleporterMessageID,
)
Expect(err).Should(BeNil())
Expect(delivered).Should(BeTrue())

//
// Send a transaction to Subnet B to issue a Warp Message from the Teleporter contract to Subnet A
//

sendCrossChainMessageInput.DestinationChainID = subnetAInfo.BlockchainID
sendCrossChainMessageInput.FeeInfo.Amount = big.NewInt(0)
receipt, teleporterMessageID = utils.SendCrossChainMessageAndWaitForAcceptance(
ctx,
subnetBInfo,
subnetAInfo,
sendCrossChainMessageInput,
fundedKey,
)

//
// Relay the message to the destination
//
network.RelayMessage(ctx, receipt, subnetBInfo, subnetAInfo, true)

//
// Check Teleporter message received on the destination
//
delivered, err = subnetAInfo.TeleporterMessenger.MessageReceived(
&bind.CallOpts{}, subnetBInfo.BlockchainID, teleporterMessageID,
)
Expect(err).Should(BeNil())
Expect(delivered).Should(BeTrue())

utils.RedeemRelayerRewardsAndConfirm(
ctx, subnetAInfo, feeToken, feeTokenAddress, fundedKey, feeAmount,
)
}
2 changes: 1 addition & 1 deletion tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = ginkgo.Describe("[Teleporter integration tests]", func() {
ginkgo.It("ERC20 bridge multihop", ERC20BridgeMultihopGinkgo)

// Teleporter tests
ginkgo.It("Send a message from Subnet A to Subnet B", BasicOneWaySendGinkgo)
ginkgo.It("Send a message from Subnet A to Subnet B, and one from B to A", BasicSendReceiveGinkgo)
ginkgo.It("Deliver to the wrong chain", DeliverToWrongChainGinkgo)
ginkgo.It("Deliver to non-existent contract", DeliverToNonExistentContractGinkgo) // TODO: fix
ginkgo.It("Retry successful execution", RetrySuccessfulExecutionGinkgo)
Expand Down
44 changes: 44 additions & 0 deletions tests/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ava-labs/subnet-evm/params"
predicateutils "github.com/ava-labs/subnet-evm/predicate"
"github.com/ava-labs/subnet-evm/x/warp"
exampleerc20 "github.com/ava-labs/teleporter/abi-bindings/go/Mocks/ExampleERC20"
teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/go/Teleporter/TeleporterMessenger"
gasUtils "github.com/ava-labs/teleporter/utils/gas-utils"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -159,6 +160,49 @@ func RetryMessageExecutionAndWaitForAcceptance(
return receipt
}

func RedeemRelayerRewardsAndConfirm(
ctx context.Context,
subnet SubnetTestInfo,
feeToken *exampleerc20.ExampleERC20,
feeTokenAddress common.Address,
relayerKey *ecdsa.PrivateKey,
expectedAmount *big.Int,
) *types.Receipt {
relayerAddress := crypto.PubkeyToAddress(relayerKey.PublicKey)

balanceBeforeRedemption, err := feeToken.BalanceOf(
&bind.CallOpts{}, relayerAddress,
)
Expect(err).Should(BeNil())

tx_opts, err := bind.NewKeyedTransactorWithChainID(
relayerKey, subnet.ChainIDInt,
)
Expect(err).Should(BeNil())
transaction, err := subnet.TeleporterMessenger.RedeemRelayerRewards(
tx_opts, feeTokenAddress,
)
Expect(err).Should(BeNil())
receipt, err := bind.WaitMined(ctx, subnet.ChainRPCClient, transaction)
Expect(err).Should(BeNil())
Expect(receipt.Status).Should(Equal(types.ReceiptStatusSuccessful))

balanceAfterRedemption, err := feeToken.BalanceOf(
&bind.CallOpts{}, relayerAddress,
)
Expect(err).Should(BeNil())

Expect(balanceAfterRedemption).Should(
Equal(
big.NewInt(0).Add(
balanceBeforeRedemption, expectedAmount,
),
),
)

return receipt
}

func SendSpecifiedReceiptsAndWaitForAcceptance(
ctx context.Context,
originChainID ids.ID,
Expand Down

0 comments on commit f2f5694

Please sign in to comment.