Skip to content

Commit ade8437

Browse files
authored
Merge pull request #44 from oraichain/dang/test-ibchooks
Test: Add interchain test for ibc hooks
2 parents a1c744c + 33e354e commit ade8437

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ test-system: install
158158
ictest-basic:
159159
cd tests/interchaintest && go test -race -v -run TestStartOrai .
160160

161+
# Executes basic chain tests via interchaintest
162+
ictest-ibchooks:
163+
cd tests/interchaintest && go test -race -v -run TestIbcHooks .
164+
161165
###############################################################################
162166
### Linting ###
163167
###############################################################################
132 KB
Binary file not shown.
+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package interchaintest
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"cosmossdk.io/math"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
11+
"github.com/strangelove-ventures/interchaintest/v8"
12+
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
13+
"github.com/strangelove-ventures/interchaintest/v8/ibc"
14+
"github.com/strangelove-ventures/interchaintest/v8/relayer"
15+
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
16+
"github.com/strangelove-ventures/interchaintest/v8/testutil"
17+
"github.com/stretchr/testify/require"
18+
"go.uber.org/zap/zaptest"
19+
)
20+
21+
// TestStartOrai is a basic test to assert that spinning up a Orai network with 1 validator works properly.
22+
func TestIbcHooks(t *testing.T) {
23+
if testing.Short() {
24+
t.Skip()
25+
}
26+
27+
t.Parallel()
28+
29+
ctx := context.Background()
30+
31+
numVals := 1
32+
numFullNodes := 1
33+
34+
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
35+
{
36+
Name: "orai",
37+
ChainConfig: oraiConfig,
38+
NumValidators: &numVals,
39+
NumFullNodes: &numFullNodes,
40+
},
41+
{
42+
Name: "gaia",
43+
Version: GaiaImageVersion,
44+
NumValidators: &numVals,
45+
NumFullNodes: &numFullNodes,
46+
},
47+
})
48+
49+
// Get chains from the chain factory
50+
chains, err := cf.Chains(t.Name())
51+
require.NoError(t, err)
52+
53+
orai, gaia := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)
54+
55+
// Create relayer factory to utilize the go-relayer
56+
client, network := interchaintest.DockerSetup(t)
57+
r := interchaintest.NewBuiltinRelayerFactory(
58+
ibc.CosmosRly,
59+
zaptest.NewLogger(t),
60+
relayer.CustomDockerImage(IBCRelayerImage, IBCRelayerVersion, "100:1000"),
61+
).Build(t, client, network)
62+
63+
ic := interchaintest.NewInterchain().
64+
AddChain(orai).
65+
AddChain(gaia).
66+
AddRelayer(r, "rly").
67+
AddLink(interchaintest.InterchainLink{
68+
Chain1: orai,
69+
Chain2: gaia,
70+
Relayer: r,
71+
Path: pathOraiGaia,
72+
})
73+
74+
rep := testreporter.NewNopReporter()
75+
eRep := rep.RelayerExecReporter(t)
76+
77+
err = ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
78+
TestName: t.Name(),
79+
Client: client,
80+
NetworkID: network,
81+
SkipPathCreation: false,
82+
83+
// This can be used to write to the block database which will index all block data e.g. txs, msgs, events, etc.
84+
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
85+
})
86+
require.NoError(t, err)
87+
88+
t.Cleanup(func() {
89+
_ = ic.Close()
90+
})
91+
92+
// Start the relayer
93+
require.NoError(t, r.StartRelayer(ctx, eRep, pathOraiGaia))
94+
t.Cleanup(
95+
func() {
96+
err := r.StopRelayer(ctx, eRep)
97+
if err != nil {
98+
panic(fmt.Errorf("an error occurred while stopping the relayer: %s", err))
99+
}
100+
},
101+
)
102+
103+
channel, err := ibc.GetTransferChannel(ctx, r, eRep, orai.Config().ChainID, gaia.Config().ChainID)
104+
require.NoError(t, err)
105+
106+
// Create some user accounts on both chains
107+
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), genesisWalletAmount, orai, gaia)
108+
109+
// Wait a few blocks for relayer to start and for user accounts to be created
110+
err = testutil.WaitForBlocks(ctx, 5, orai, gaia)
111+
require.NoError(t, err)
112+
113+
// Get our Bech32 encoded user addresses
114+
oraiUser, gaiaUser := users[0], users[1]
115+
116+
oraiUserAddress := sdk.MustBech32ifyAddressBytes(orai.Config().Bech32Prefix, oraiUser.Address())
117+
gaiaUserAddr := sdk.MustBech32ifyAddressBytes(gaia.Config().Bech32Prefix, gaiaUser.Address())
118+
119+
_ = oraiUserAddress
120+
_ = gaiaUserAddr
121+
122+
// Store and instantiate contract on Orai chain
123+
counterContractID, err := orai.StoreContract(ctx, oraiUser.KeyName(), "./bytecode/counter.wasm")
124+
require.NoError(t, err)
125+
126+
initMsg := "{\"count\": 0}"
127+
contractAddress, err := orai.InstantiateContract(ctx, oraiUser.KeyName(), counterContractID, initMsg, true)
128+
require.NoError(t, err)
129+
130+
// Get stake denom on orai
131+
gaiaDenom := transfertypes.GetPrefixedDenom(channel.Counterparty.PortID, channel.Counterparty.ChannelID, gaia.Config().Denom) //transfer/channel-0/uatom
132+
gaiaIBCDenom := transfertypes.ParseDenomTrace(gaiaDenom).IBCDenom() // ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2
133+
134+
// check contract address balance
135+
balances, err := orai.BankQueryBalance(ctx, contractAddress, gaiaIBCDenom)
136+
require.NoError(t, err)
137+
require.Equal(t, math.NewInt(0), balances)
138+
139+
// send ibc transaction to execite the contract
140+
transfer := ibc.WalletAmount{
141+
Address: contractAddress,
142+
Denom: gaia.Config().Denom,
143+
Amount: amountToSend,
144+
}
145+
memo := fmt.Sprintf("{\"wasm\":{\"contract\":\"%s\",\"msg\": {\"increment\": {}} }}", contractAddress)
146+
transferTx, err := gaia.SendIBCTransfer(ctx, channel.Counterparty.ChannelID, gaiaUserAddr, transfer, ibc.TransferOptions{Memo: memo})
147+
require.NoError(t, err)
148+
149+
// waiting for ACK -> transfer successfully
150+
gaiaHeight, err := gaia.Height(ctx)
151+
require.NoError(t, err)
152+
_, err = testutil.PollForAck(ctx, gaia, gaiaHeight-5, gaiaHeight+25, transferTx.Packet)
153+
require.NoError(t, err)
154+
155+
// check new balances
156+
balances, err = orai.BankQueryBalance(ctx, contractAddress, gaiaIBCDenom)
157+
require.NoError(t, err)
158+
require.Equal(t, amountToSend, balances)
159+
160+
// check contract
161+
var res GetCountResponse
162+
err = orai.QueryContract(ctx, contractAddress, QueryMsg{GetCount: &GetCountQuery{}}, &res)
163+
require.NoError(t, err)
164+
require.Equal(t, uint64(1), res.Data.Count)
165+
}

tests/interchaintest/setup.go

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ var (
5252
}
5353
genesisWalletAmount = math.NewInt(100_000_000_000)
5454
amountToSend = math.NewInt(1_000_000_000)
55+
56+
pathOraiGaia = "orai-gaia"
5557
)
5658

5759
// oraiEncoding registers the Orai specific module codecs so that the associated types and msgs
@@ -91,6 +93,9 @@ func modifyGenesisShortProposals(
9193
if err := dyno.Set(g, votingPeriod, "app_state", "gov", "params", "voting_period"); err != nil {
9294
return nil, fmt.Errorf("failed to set voting period in genesis json: %w", err)
9395
}
96+
if err := dyno.Set(g, "1", "initial_height"); err != nil {
97+
return nil, fmt.Errorf("failed to set initial height in genesis json: %w", err)
98+
}
9499
if err := dyno.Set(g, maxDepositPeriod, "app_state", "gov", "params", "max_deposit_period"); err != nil {
95100
return nil, fmt.Errorf("failed to set voting period in genesis json: %w", err)
96101
}

tests/interchaintest/types.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package interchaintest
2+
3+
type QueryMsg struct {
4+
// IBC Hooks
5+
GetCount *GetCountQuery `json:"get_count,omitempty"`
6+
}
7+
8+
type GetCountQuery struct{}
9+
10+
type GetCountResponse struct {
11+
Data CountObj `json: "data"`
12+
}
13+
14+
type CountObj struct {
15+
Count uint64 `json:"count"`
16+
}

0 commit comments

Comments
 (0)