Skip to content

Commit 36bde6a

Browse files
committed
add block time test
1 parent 61a577d commit 36bde6a

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ ictest-gasfees:
298298
@echo "Running gas fees e2e test"
299299
@cd interchaintest && go test -race -v -run TestGasFees .
300300

301+
ictest-blocktime:
302+
@echo "Running block time configuration e2e test"
303+
@cd interchaintest && go test -race -v -run TestBlockTimeConfiguration .
304+
301305
###############################################################################
302306
### testnet ###
303307
###############################################################################
@@ -349,7 +353,8 @@ help:
349353
@echo " ictest-basic : Basic end-to-end test"
350354
@echo " ictest-ibc : IBC end-to-end test"
351355
@echo " ictest-token-transfer : Token transfer between users end-to-end test"
352-
@echo " ictest-gasfees : Gas fees end-to-end test"
356+
@echo " ictest-gasfees : Gas fees end-to-end test"
357+
@echo " ictest-blocktime : Block time configuration end-to-end test"
353358
@echo " generate-webapp : Create a new webapp template"
354359

355360
.PHONY: help

interchaintest/blocktime_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"cosmossdk.io/math"
9+
"github.com/strangelove-ventures/interchaintest/v8"
10+
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
11+
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
12+
"github.com/strangelove-ventures/interchaintest/v8/testutil"
13+
"github.com/stretchr/testify/require"
14+
"go.uber.org/zap/zaptest"
15+
)
16+
17+
// TestBlockTimeConfiguration verifies that the blockchain respects a custom block time parameter.
18+
// This test ensures that:
19+
// 1. The chain can be properly initialized with a custom block time
20+
// 2. Blocks are produced at approximately the configured interval
21+
// 3. The actual block production time is close to the configured block time
22+
func TestBlockTimeConfiguration(t *testing.T) {
23+
ctx := context.Background()
24+
rep := testreporter.NewNopReporter()
25+
eRep := rep.RelayerExecReporter(t)
26+
27+
// Set up Docker environment for the test
28+
client, network := interchaintest.DockerSetup(t)
29+
30+
// Set a custom block time (2 seconds instead of default)
31+
customBlockTime := 2 * time.Second
32+
33+
// Create a modified chain spec with custom block time
34+
// Note: We need to modify the genesis to set the block time
35+
chainSpec := DefaultChainSpec
36+
37+
// Create a copy of the default genesis and add our custom block time
38+
genesisKVs := make([]cosmos.GenesisKV, len(DefaultGenesis))
39+
copy(genesisKVs, DefaultGenesis)
40+
41+
// Add the block time configuration to the genesis
42+
genesisKVs = append(genesisKVs,
43+
cosmos.NewGenesisKV("consensus.params.block.time_iota_ms", "2000")) // 2000ms = 2s
44+
45+
// Set the modified genesis
46+
chainSpec.ModifyGenesis = cosmos.ModifyGenesis(genesisKVs)
47+
48+
// Create a chain factory with our modified chain specification
49+
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
50+
&chainSpec,
51+
})
52+
53+
// Initialize the chains from the factory
54+
chains, err := cf.Chains(t.Name())
55+
require.NoError(t, err)
56+
57+
// Get the first chain from the list (we only have one in this test)
58+
chain := chains[0].(*cosmos.CosmosChain)
59+
60+
// Setup Interchain environment with our single chain
61+
ic := interchaintest.NewInterchain().
62+
AddChain(chain)
63+
64+
// Build the interchain environment
65+
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
66+
TestName: t.Name(),
67+
Client: client,
68+
NetworkID: network,
69+
SkipPathCreation: true,
70+
}))
71+
72+
// Clean up resources when the test completes
73+
t.Cleanup(func() {
74+
_ = ic.Close()
75+
})
76+
77+
// Create and fund a test user with 10 million tokens
78+
amt := math.NewInt(10_000_000)
79+
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", amt, chain)
80+
user := users[0]
81+
82+
// Verify that the user was properly funded with the expected amount
83+
bal, err := chain.BankQueryBalance(ctx, user.FormattedAddress(), chain.Config().Denom)
84+
require.NoError(t, err)
85+
require.EqualValues(t, amt, bal)
86+
87+
// Get the current block height
88+
height, err := chain.Height(ctx)
89+
require.NoError(t, err)
90+
startHeight := height
91+
92+
// Record the start time
93+
startTime := time.Now()
94+
95+
// Wait for a specific number of blocks to be produced
96+
numBlocksToWait := 5
97+
err = testutil.WaitForBlocks(ctx, numBlocksToWait, chain)
98+
require.NoError(t, err)
99+
100+
// Get the new block height
101+
height, err = chain.Height(ctx)
102+
require.NoError(t, err)
103+
endHeight := height
104+
105+
// Calculate the elapsed time
106+
elapsedTime := time.Since(startTime)
107+
108+
// Calculate the actual number of blocks produced
109+
blocksProduced := endHeight - startHeight
110+
111+
// Calculate the average time per block
112+
avgTimePerBlock := elapsedTime / time.Duration(blocksProduced)
113+
114+
// Verify that the average block time is close to the configured block time
115+
// Allow for a 50% margin of error to account for network delays and other factors
116+
maxAllowedDeviation := time.Duration(float64(customBlockTime) * 0.5)
117+
118+
t.Logf("Configured block time: %v", customBlockTime)
119+
t.Logf("Actual average block time: %v", avgTimePerBlock)
120+
t.Logf("Blocks produced: %d", blocksProduced)
121+
t.Logf("Elapsed time: %v", elapsedTime)
122+
123+
// Check if the average block time is within the acceptable range
124+
require.InDelta(t, customBlockTime.Seconds(), avgTimePerBlock.Seconds(),
125+
maxAllowedDeviation.Seconds(),
126+
"Average block time (%v) is not close enough to configured block time (%v)",
127+
avgTimePerBlock, customBlockTime)
128+
}

0 commit comments

Comments
 (0)