Skip to content

Commit

Permalink
test:wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
djm07073 committed Feb 14, 2025
1 parent 0bc0c0f commit 26962a8
Show file tree
Hide file tree
Showing 3 changed files with 352 additions and 101 deletions.
105 changes: 4 additions & 101 deletions integration-tests/erc721_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,23 @@ package test

import (
"math/big"
"testing"
"time"

"github.com/stretchr/testify/suite"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"

nfttransferkeeper "github.com/initia-labs/initia/x/ibc/nft-transfer/keeper"
nfttransfertypes "github.com/initia-labs/initia/x/ibc/nft-transfer/types"
ibctesting "github.com/initia-labs/initia/x/ibc/testing"

minievmapp "github.com/initia-labs/minievm/app"
evmkeeper "github.com/initia-labs/minievm/x/evm/keeper"
evmtypes "github.com/initia-labs/minievm/x/evm/types"

"github.com/ethereum/go-ethereum/common"
)

type KeeperTestSuite struct {
suite.Suite

coordinator *ibctesting.Coordinator

// testing chains used for convenience and readability
// MinievmAppChain is the chain used by the testing suite
chainA *ibctesting.TestChain
// MinievmAppChain is the chain used by the testing suite
chainB *ibctesting.TestChain
// InitiaAppChain is the chain used by the testing suite
chainC *ibctesting.TestChain

queryClient nfttransfertypes.QueryClient
}

func getMinitiaApp(chain *ibctesting.TestChain) *minievmapp.MinitiaApp {
return chain.App.(*minievmapp.MinitiaApp)
}

const (
TrustingPeriod time.Duration = time.Hour * 24 * 7 * 2 / 3
UnbondingPeriod time.Duration = time.Hour * 24 * 7
)

func (suite *KeeperTestSuite) convertAppToMApp(chain *ibctesting.TestChain) {
genAccs := make([]authtypes.GenesisAccount, len(chain.SenderAccounts))
genBals := make([]banktypes.Balance, len(chain.SenderAccounts))
for i, acc := range chain.SenderAccounts {
genAccs[i] = acc.SenderAccount.(*authtypes.BaseAccount)
amount, ok := math.NewIntFromString("10000000000000000000")
suite.Require().True(ok)

// add sender account
balance := banktypes.Balance{
Address: genAccs[i].GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)),
}
genBals[i] = balance
}

miniApp := minievmapp.SetupWithGenesisAccounts(chain.Vals.Copy(), genAccs, genBals...)
baseapp.SetChainID(chain.ChainID)(miniApp.GetBaseApp())
chain.App = miniApp
chain.Codec = miniApp.AppCodec()
chain.TxConfig = miniApp.TxConfig()

chain.CurrentHeader = cmtproto.Header{
ChainID: chain.ChainID,
Height: chain.App.LastBlockHeight() + 1,
AppHash: chain.App.LastCommitID().Hash,
Time: chain.CurrentHeader.Time,
ValidatorsHash: chain.Vals.Hash(),
NextValidatorsHash: chain.NextVals.Hash(),
ProposerAddress: chain.CurrentHeader.ProposerAddress,
}
}

func (suite *KeeperTestSuite) SetupTest() {
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 3)
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2))
suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(3))

suite.convertAppToMApp(suite.chainA)
suite.convertAppToMApp(suite.chainB)
suite.convertAppToMApp(suite.chainC)

queryHelper := baseapp.NewQueryServerTestHelper(suite.chainA.GetContext(), getMinitiaApp(suite.chainA).InterfaceRegistry())
nfttransfertypes.RegisterQueryServer(queryHelper, nfttransferkeeper.NewQueryServerImpl(getMinitiaApp(suite.chainA).NftTransferKeeper))
suite.queryClient = nfttransfertypes.NewQueryClient(queryHelper)
}

func NewTransferPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path {
func NewNftTransferPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path {
path := ibctesting.NewPath(chainA, chainB)
path.EndpointA.ChannelConfig.PortID = nfttransfertypes.PortID
path.EndpointB.ChannelConfig.PortID = nfttransfertypes.PortID
Expand All @@ -116,10 +31,6 @@ func NewTransferPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path {
return path
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (suite *KeeperTestSuite) CreateNftClass(
endpoint *ibctesting.Endpoint,
name, uri string,
Expand Down Expand Up @@ -214,7 +125,7 @@ func (suite *KeeperTestSuite) TestSendAndReceive() {
// pathB2C.EndpointA is ChainB endpoint (source of path)`
// pathB2C.EndpointB is ChainC endpoint (destination of path)
// pathA2B.EndpointB.Chain.SenderAccount is same with receiver account of pathA2B before testing`
pathA2B := NewTransferPath(suite.chainA, suite.chainB)
pathA2B := NewNftTransferPath(suite.chainA, suite.chainB)
suite.Run("transfer forward A->B", func() {
{
suite.coordinator.SetupConnections(pathA2B)
Expand Down Expand Up @@ -247,7 +158,7 @@ func (suite *KeeperTestSuite) TestSendAndReceive() {
})

// transfer from chainB to chainC
pathB2C := NewTransferPath(suite.chainB, suite.chainC)
pathB2C := NewNftTransferPath(suite.chainB, suite.chainC)
suite.Run("transfer forward B->C", func() {
{
suite.coordinator.SetupConnections(pathB2C)
Expand Down Expand Up @@ -368,17 +279,9 @@ func (suite *KeeperTestSuite) receiverNft(
suite.Require().NoError(err)

// get proof of packet commitment from chainA
err = toEndpoint.UpdateClient()
err = commitRecvPacket(fromEndpoint, toEndpoint, packet)
suite.Require().NoError(err)

packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
proof, proofHeight := fromEndpoint.QueryProof(packetKey)

recvMsg := channeltypes.NewMsgRecvPacket(
packet, proof, proofHeight, toEndpoint.Chain.SenderAccount.GetAddress().String())
_, err = toEndpoint.Chain.SendMsgs(recvMsg)
suite.Require().NoError(err) // message committed

var classId string

isAwayFromOrigin := nfttransfertypes.SenderChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.GetClassId())
Expand Down
122 changes: 122 additions & 0 deletions integration-tests/testsuite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package test

import (
"testing"
"time"

"github.com/stretchr/testify/suite"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"

nfttransferkeeper "github.com/initia-labs/initia/x/ibc/nft-transfer/keeper"
nfttransfertypes "github.com/initia-labs/initia/x/ibc/nft-transfer/types"
ibctesting "github.com/initia-labs/initia/x/ibc/testing"

minievmapp "github.com/initia-labs/minievm/app"
)

type KeeperTestSuite struct {
suite.Suite

coordinator *ibctesting.Coordinator

// testing chains used for convenience and readability
// MinievmAppChain is the chain used by the testing suite
chainA *ibctesting.TestChain
// MinievmAppChain is the chain used by the testing suite
chainB *ibctesting.TestChain
// InitiaAppChain is the chain used by the testing suite
chainC *ibctesting.TestChain

queryClient nfttransfertypes.QueryClient
}

func getMinitiaApp(chain *ibctesting.TestChain) *minievmapp.MinitiaApp {
return chain.App.(*minievmapp.MinitiaApp)
}

const (
TrustingPeriod time.Duration = time.Hour * 24 * 7 * 2 / 3
UnbondingPeriod time.Duration = time.Hour * 24 * 7
)

func (suite *KeeperTestSuite) convertAppToMApp(chain *ibctesting.TestChain) {
genAccs := make([]authtypes.GenesisAccount, len(chain.SenderAccounts))
genBals := make([]banktypes.Balance, len(chain.SenderAccounts))
for i, acc := range chain.SenderAccounts {
genAccs[i] = acc.SenderAccount.(*authtypes.BaseAccount)
amount, ok := math.NewIntFromString("10000000000000000000")
suite.Require().True(ok)

// add sender account
balance := banktypes.Balance{
Address: genAccs[i].GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)),
}
genBals[i] = balance
}

miniApp := minievmapp.SetupWithGenesisAccounts(chain.Vals.Copy(), genAccs, genBals...)
baseapp.SetChainID(chain.ChainID)(miniApp.GetBaseApp())
chain.App = miniApp
chain.Codec = miniApp.AppCodec()
chain.TxConfig = miniApp.TxConfig()

chain.CurrentHeader = cmtproto.Header{
ChainID: chain.ChainID,
Height: chain.App.LastBlockHeight() + 1,
AppHash: chain.App.LastCommitID().Hash,
Time: chain.CurrentHeader.Time,
ValidatorsHash: chain.Vals.Hash(),
NextValidatorsHash: chain.NextVals.Hash(),
ProposerAddress: chain.CurrentHeader.ProposerAddress,
}
}

func (suite *KeeperTestSuite) SetupTest() {
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 3)
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2))
suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(3))

suite.convertAppToMApp(suite.chainA)
suite.convertAppToMApp(suite.chainB)
suite.convertAppToMApp(suite.chainC)

queryHelper := baseapp.NewQueryServerTestHelper(suite.chainA.GetContext(), getMinitiaApp(suite.chainA).InterfaceRegistry())
nfttransfertypes.RegisterQueryServer(queryHelper, nfttransferkeeper.NewQueryServerImpl(getMinitiaApp(suite.chainA).NftTransferKeeper))
suite.queryClient = nfttransfertypes.NewQueryClient(queryHelper)
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func commitRecvPacket(fromEndpoint *ibctesting.Endpoint, toEndpoint *ibctesting.Endpoint, packet channeltypes.Packet) error {
err := toEndpoint.UpdateClient()
if err != nil {
return err
}

packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
proof, proofHeight := fromEndpoint.QueryProof(packetKey)

recvMsg := channeltypes.NewMsgRecvPacket(
packet, proof, proofHeight, toEndpoint.Chain.SenderAccount.GetAddress().String())
_, err = toEndpoint.Chain.SendMsgs(recvMsg)
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 26962a8

Please sign in to comment.