Skip to content

Commit 7daa539

Browse files
committed
add comments to tests
1 parent d31b517 commit 7daa539

File tree

7 files changed

+208
-37
lines changed

7 files changed

+208
-37
lines changed

interchaintest/basic_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,64 @@ import (
1313
"go.uber.org/zap/zaptest"
1414
)
1515

16+
// TestBasicChain is a fundamental test that verifies the basic functionality of a single chain.
17+
// This test ensures that:
18+
// 1. The chain can be properly initialized and started
19+
// 2. The chain has the correct configuration (denom, chain ID, gas prices)
20+
// 3. Users can be created and funded with tokens
21+
// 4. The balance query functionality works correctly
1622
func TestBasicChain(t *testing.T) {
1723
ctx := context.Background()
1824
rep := testreporter.NewNopReporter()
1925
eRep := rep.RelayerExecReporter(t)
26+
// Set up Docker environment for the test
2027
client, network := interchaintest.DockerSetup(t)
2128

29+
// Create a chain factory with our default chain specification
2230
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
2331
&DefaultChainSpec,
2432
})
2533

34+
// Initialize the chains from the factory
2635
chains, err := cf.Chains(t.Name())
2736
require.NoError(t, err)
2837

38+
// Get the first chain from the list (we only have one in this test)
2939
chain := chains[0].(*cosmos.CosmosChain)
3040

31-
// Setup Interchain
41+
// Setup Interchain environment with our single chain
3242
ic := interchaintest.NewInterchain().
3343
AddChain(chain)
3444

45+
// Build the interchain environment
3546
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
3647
TestName: t.Name(),
3748
Client: client,
3849
NetworkID: network,
3950
SkipPathCreation: false,
4051
}))
52+
// Clean up resources when the test completes
4153
t.Cleanup(func() {
4254
_ = ic.Close()
4355
})
4456

57+
// Create and fund a test user with 10 million tokens
4558
amt := math.NewInt(10_000_000)
4659
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", amt,
4760
chain,
4861
)
4962
user := users[0]
5063

5164
t.Run("validate configuration", func(t *testing.T) {
52-
// Check that the chain is configured correctly
65+
// Check that the chain is configured correctly with the expected values
5366
require.Equal(t, chain.Config().Denom, "npush")
5467
require.Equal(t, chain.Config().ChainID, "localchain-1")
5568
require.Equal(t, chain.Config().GasPrices, "0npush")
5669
})
5770
t.Run("validate funding", func(t *testing.T) {
71+
// Verify that the user was properly funded with the expected amount
5872
bal, err := chain.BankQueryBalance(ctx, user.FormattedAddress(), chain.Config().Denom)
5973
require.NoError(t, err)
6074
require.EqualValues(t, amt, bal)
61-
6275
})
63-
6476
}

interchaintest/cosmwasm_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,121 @@ import (
1313
"go.uber.org/zap/zaptest"
1414
)
1515

16+
// GetCountResponse represents the response structure from the CosmWasm contract's get_count query
1617
type GetCountResponse struct {
1718
// {"data":{"count":0}}
1819
Data *GetCountObj `json:"data"`
1920
}
2021

22+
// GetCountObj holds the actual count value from the contract's state
2123
type GetCountObj struct {
2224
Count int64 `json:"count"`
2325
}
2426

27+
// TestCosmWasmIntegration verifies that CosmWasm smart contracts can be deployed and executed on the chain.
28+
// This test ensures that:
29+
// 1. A chain with CosmWasm support can be properly initialized
30+
// 2. A CosmWasm contract can be stored on the chain
31+
// 3. The contract can be instantiated with initial state
32+
// 4. Contract execution (state changes) works correctly
33+
// 5. Contract queries return the expected results
2534
func TestCosmWasmIntegration(t *testing.T) {
2635
t.Parallel()
2736
ctx := context.Background()
2837
rep := testreporter.NewNopReporter()
2938
eRep := rep.RelayerExecReporter(t)
39+
// Set up Docker environment for the test
3040
client, network := interchaintest.DockerSetup(t)
3141

42+
// Create a chain factory with our default chain specification
3243
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
3344
&DefaultChainSpec,
3445
})
3546

47+
// Initialize the chain from the factory
3648
chains, err := cf.Chains(t.Name())
3749
require.NoError(t, err)
3850

51+
// Get the chain from the list (we only have one in this test)
3952
chain := chains[0].(*cosmos.CosmosChain)
4053

41-
// Setup Interchain
54+
// Setup Interchain environment with our single chain
4255
ic := interchaintest.NewInterchain().
4356
AddChain(chain)
4457

58+
// Build the interchain environment
4559
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
4660
TestName: t.Name(),
4761
Client: client,
4862
NetworkID: network,
4963
SkipPathCreation: false,
5064
}))
65+
// Clean up resources when the test completes
5166
t.Cleanup(func() {
5267
_ = ic.Close()
5368
})
5469

70+
// Create and fund a test user
5571
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), GenesisFundsAmount, chain)
5672
user := users[0]
5773

74+
// Execute the standard CosmWasm test flow
5875
StdExecute(t, ctx, chain, user)
5976
}
6077

78+
// StdExecute performs a standard flow of CosmWasm operations:
79+
// 1. Uploads a contract to the chain
80+
// 2. Instantiates the contract with initial state
81+
// 3. Executes a transaction on the contract to increment the counter
82+
// 4. Queries the contract to verify the state change
6183
func StdExecute(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet) (contractAddr string) {
84+
// Upload and instantiate the contract with initial count of 0
6285
_, contractAddr = SetupContract(t, ctx, chain, user.KeyName(), "contracts/cw_template.wasm", `{"count":0}`)
86+
87+
// Execute the increment operation on the contract
6388
chain.ExecuteContract(ctx, user.KeyName(), contractAddr, `{"increment":{}}`, "--fees", "10000"+chain.Config().Denom)
6489

90+
// Query the contract to verify the count was incremented
6591
var res GetCountResponse
6692
err := SmartQueryString(t, ctx, chain, contractAddr, `{"get_count":{}}`, &res)
6793
require.NoError(t, err)
6894

95+
// Verify the count is now 1 after the increment operation
6996
require.Equal(t, int64(1), res.Data.Count)
7097

7198
return contractAddr
7299
}
73100

101+
// SmartQueryString performs a query on a CosmWasm contract and unmarshals the result into the provided response object
74102
func SmartQueryString(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, contractAddr, queryMsg string, res interface{}) error {
103+
// Convert the query string to a JSON map for the chain's QueryContract method
75104
var jsonMap map[string]interface{}
76105
if err := json.Unmarshal([]byte(queryMsg), &jsonMap); err != nil {
77106
t.Fatal(err)
78107
}
108+
// Execute the query and unmarshal the result into the provided response object
79109
err := chain.QueryContract(ctx, contractAddr, jsonMap, &res)
80110
return err
81111
}
82112

113+
// SetupContract uploads a CosmWasm contract to the chain and instantiates it
114+
// Returns the code ID and contract address
83115
func SetupContract(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, keyname string, fileLoc string, message string, extraFlags ...string) (codeId, contract string) {
116+
// Store the contract on the chain
84117
codeId, err := chain.StoreContract(ctx, keyname, fileLoc)
85118
if err != nil {
86119
t.Fatal(err)
87120
}
88121

122+
// Determine if we need to add the --no-admin flag
89123
needsNoAdminFlag := true
90124
for _, flag := range extraFlags {
91125
if flag == "--admin" {
92126
needsNoAdminFlag = false
93127
}
94128
}
95129

130+
// Instantiate the contract with the provided message
96131
contractAddr, err := chain.InstantiateContract(ctx, keyname, codeId, message, needsNoAdminFlag, extraFlags...)
97132
if err != nil {
98133
t.Fatal(err)

interchaintest/ibc_rate_limit_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import (
1616
"go.uber.org/zap/zaptest"
1717
)
1818

19+
// TestIBCRateLimit verifies that the IBC rate limiting functionality works correctly.
20+
// This test ensures that:
21+
// 1. Two chains can be properly initialized and connected via IBC
22+
// 2. The rate limit module can be configured to blacklist specific denominations
23+
// 3. Transfers of blacklisted denominations are properly rejected
24+
// 4. The error message correctly indicates that the denomination is blacklisted
1925
func TestIBCRateLimit(t *testing.T) {
2026
if testing.Short() {
2127
t.Skip()
@@ -25,68 +31,76 @@ func TestIBCRateLimit(t *testing.T) {
2531
ctx := context.Background()
2632
rep := testreporter.NewNopReporter()
2733
eRep := rep.RelayerExecReporter(t)
34+
// Set up Docker environment for the test
2835
client, network := interchaintest.DockerSetup(t)
2936

37+
// Configure the first chain with the chain's native denom blacklisted in the rate limit module
3038
cs := &DefaultChainSpec
3139
cs.ModifyGenesis = cosmos.ModifyGenesis([]cosmos.GenesisKV{cosmos.NewGenesisKV("app_state.ratelimit.blacklisted_denoms", []string{cs.Denom})})
3240

41+
// Create a chain factory with two chain specifications
3342
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
3443
cs,
3544
&SecondDefaultChainSpec,
3645
})
3746

47+
// Initialize the chains from the factory
3848
chains, err := cf.Chains(t.Name())
3949
require.NoError(t, err)
4050

51+
// Get references to both chains
4152
chain := chains[0].(*cosmos.CosmosChain)
4253
secondary := chains[1].(*cosmos.CosmosChain)
4354

44-
// Relayer Factory
55+
// Set up the relayer for IBC communication between chains
4556
r := interchaintest.NewBuiltinRelayerFactory(
4657
ibc.CosmosRly,
4758
zaptest.NewLogger(t, zaptest.Level(zapcore.DebugLevel)),
4859
interchaintestrelayer.CustomDockerImage(RelayerRepo, RelayerVersion, "100:1000"),
4960
interchaintestrelayer.StartupFlags("--processor", "events", "--block-history", "200"),
5061
).Build(t, client, network)
5162

63+
// Create the interchain environment with both chains and the relayer
5264
ic := interchaintest.NewInterchain().
5365
AddChain(chain).
5466
AddChain(secondary).
5567
AddRelayer(r, "relayer")
5668

69+
// Add an IBC link between the two chains
5770
ic = ic.AddLink(interchaintest.InterchainLink{
5871
Chain1: chain,
5972
Chain2: secondary,
6073
Relayer: r,
6174
Path: ibcPath,
6275
})
6376

64-
// Build interchain
77+
// Build the interchain environment
6578
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
6679
TestName: t.Name(),
6780
Client: client,
6881
NetworkID: network,
6982
SkipPathCreation: false,
7083
}))
7184

72-
// Create and Fund User Wallets
85+
// Create and fund test users on both chains
7386
fundAmount := math.NewInt(10_000_000)
7487
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", fundAmount, chain, secondary)
7588
userA, userB := users[0], users[1]
7689

90+
// Verify initial balance of userA
7791
userAInitial, err := chain.GetBalance(ctx, userA.FormattedAddress(), chain.Config().Denom)
7892
fmt.Println("userAInitial", userAInitial)
7993
require.NoError(t, err)
8094
require.True(t, userAInitial.Equal(fundAmount))
8195

82-
// Get Channel ID
96+
// Get the IBC channel ID for chainA
8397
aInfo, err := r.GetChannels(ctx, eRep, chain.Config().ChainID)
8498
require.NoError(t, err)
8599
aChannelID, err := getTransferChannel(aInfo)
86100
require.NoError(t, err)
87101
fmt.Println("aChannelID", aChannelID)
88102

89-
// Send Transaction
103+
// Prepare the IBC transfer from chainA to chainB
90104
amountToSend := math.NewInt(1_000_000)
91105
dstAddress := userB.FormattedAddress()
92106
transfer := ibc.WalletAmount{
@@ -95,7 +109,7 @@ func TestIBCRateLimit(t *testing.T) {
95109
Amount: amountToSend,
96110
}
97111

98-
// Validate transfer error occurs
112+
// Attempt the IBC transfer and verify it fails due to the blacklisted denom
99113
_, err = chain.SendIBCTransfer(ctx, aChannelID, userA.KeyName(), transfer, ibc.TransferOptions{})
100114
require.Error(t, err)
101115
require.Contains(t, err.Error(), "denom is blacklisted")

0 commit comments

Comments
 (0)