-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathinterchain_security_test.go
118 lines (98 loc) · 3.81 KB
/
interchain_security_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package integration
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
appConsumer "github.com/cosmos/interchain-security/v5/app/consumer"
"github.com/cosmos/interchain-security/v5/tests/integration"
icstestingutils "github.com/cosmos/interchain-security/v5/testutil/ibc_testing"
"github.com/cosmos/interchain-security/v5/x/ccv/types"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/gaia/v19/ante"
gaiaApp "github.com/cosmos/gaia/v19/app"
)
var ccvSuite *integration.CCVTestSuite
func init() {
// Pass in concrete app types that implement the interfaces defined in https://github.com/cosmos/interchain-security/testutil/integration/interfaces.go
// IMPORTANT: the concrete app types passed in as type parameters here must match the
// concrete app types returned by the relevant app initers.
ccvSuite = integration.NewCCVTestSuite[*gaiaApp.GaiaApp, *appConsumer.App](
// Pass in ibctesting.AppIniters for gaia (provider) and consumer.
GaiaAppIniterRandomDir, icstestingutils.ConsumerAppIniter, []string{})
ante.UseFeeMarketDecorator = false
}
func TestCCVTestSuite(t *testing.T) {
// Run tests
suite.Run(t, ccvSuite)
}
func TestICSEpochs(t *testing.T) {
// a bit hacky but cannot be called
// in SetupTest() since it requires `t`
ccvSuite.SetT(t)
ccvSuite.SetupTest()
providerKeeper := app.GetProviderKeeper()
stakingKeeper := app.StakingKeeper
provCtx := ccvSuite.GetProviderChain().GetContext()
delegateFn := func(ctx sdk.Context) {
delAddr := ccvSuite.GetProviderChain().SenderAccount.GetAddress()
consAddr := sdk.ConsAddress(ccvSuite.GetProviderChain().Vals.Validators[0].Address)
validator, err := stakingKeeper.ValidatorByConsAddr(ctx, consAddr)
require.NoError(t, err)
_, err = stakingKeeper.Delegate(
ctx,
delAddr,
math.NewInt(1000000),
stakingtypes.Unbonded,
validator.(stakingtypes.Validator),
true,
)
require.NoError(t, err)
}
getVSCPacketsFn := func() []types.ValidatorSetChangePacketData {
return providerKeeper.GetPendingVSCPackets(provCtx, ccvSuite.GetCCVPath().EndpointA.Chain.ChainID)
}
nextEpoch := func(ctx sdk.Context) sdk.Context {
blockPerEpochs := providerKeeper.GetBlocksPerEpoch(ctx)
for {
if ctx.BlockHeight()%blockPerEpochs == 0 {
return ctx
}
ccvSuite.GetProviderChain().NextBlock()
ctx = ccvSuite.GetProviderChain().GetContext()
}
}
// Bond some tokens on provider to change validator powers
delegateFn(provCtx)
// VSCPacket should only be created at the end of the current epoch
require.Empty(t, getVSCPacketsFn())
provCtx = nextEpoch(provCtx)
// Expect to create a VSC packet
// without sending it since CCV channel isn't established
_, err := app.EndBlocker(provCtx)
require.NoError(t, err)
require.NotEmpty(t, getVSCPacketsFn())
// Expect the VSC packet to send after setting up the CCV channel
ccvSuite.SetupCCVChannel(ccvSuite.GetCCVPath())
require.Empty(t, getVSCPacketsFn())
// Expect VSC Packet to be committed
require.Len(t, ccvSuite.GetProviderChain().App.GetIBCKeeper().ChannelKeeper.GetAllPacketCommitmentsAtChannel(
provCtx,
ccvSuite.GetCCVPath().EndpointB.ChannelConfig.PortID,
ccvSuite.GetCCVPath().EndpointB.ChannelID,
), 1)
// Bond some tokens on provider to change validator powers
delegateFn(provCtx)
// Second VSCPacket should only be created at the end of the current epoch
require.Empty(t, getVSCPacketsFn())
provCtx = nextEpoch(provCtx)
_, err = app.EndBlocker(provCtx)
require.NoError(t, err)
// Expect second VSC Packet to be committed
require.Len(t, ccvSuite.GetProviderChain().App.GetIBCKeeper().ChannelKeeper.GetAllPacketCommitmentsAtChannel(
provCtx,
ccvSuite.GetCCVPath().EndpointB.ChannelConfig.PortID,
ccvSuite.GetCCVPath().EndpointB.ChannelID,
), 2)
}