forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normal_operations.go
89 lines (77 loc) · 2.99 KB
/
normal_operations.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
package integration
import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
consumertypes "github.com/cosmos/interchain-security/v4/x/ccv/consumer/types"
ccvtypes "github.com/cosmos/interchain-security/v4/x/ccv/types"
)
// Tests the tracking of historical info in the context of new blocks being committed
func (k CCVTestSuite) TestHistoricalInfo() { //nolint:govet // this is a test so we can copy locks
consumerKeeper := k.consumerApp.GetConsumerKeeper()
cCtx := k.consumerChain.GetContext
// save init consumer valset length
initValsetLen := len(consumerKeeper.GetAllCCValidator(cCtx()))
// save current block height
initHeight := cCtx().BlockHeight()
// define an utility function that creates a new cross-chain validator
// and then call track historical info in the next block
createVal := func(k CCVTestSuite) { //nolint:govet // this is a test so we can copy locks
// add new validator to consumer states
pk := ed25519.GenPrivKey().PubKey()
cVal, err := consumertypes.NewCCValidator(pk.Address(), int64(1), pk)
k.Require().NoError(err)
consumerKeeper.SetCCValidator(k.consumerChain.GetContext(), cVal)
// commit block in order to call TrackHistoricalInfo
k.consumerChain.NextBlock()
}
// testsetup create 2 validators and then call track historical info with header block height
// increased by HistoricalEntries in order to prune the historical info less or equal to the current block height
// Note that historical info containing the created validators are stored during the next block BeginBlocker
// and thus are indexed with the respective block heights InitHeight+1 and InitHeight+2
testSetup := []func(CCVTestSuite){
createVal,
createVal,
func(k CCVTestSuite) { //nolint:govet // this is a test so we can copy locks
historicalEntries := k.consumerApp.GetConsumerKeeper().GetHistoricalEntries(k.consumerCtx())
newHeight := k.consumerChain.GetContext().BlockHeight() + historicalEntries
header := tmproto.Header{
ChainID: "HelloChain",
Height: newHeight,
}
ctx := k.consumerChain.GetContext().WithBlockHeader(header)
consumerKeeper.TrackHistoricalInfo(ctx)
},
}
for _, ts := range testSetup {
ts(k) //nolint:govet // this is a test so we can copy locks
}
// test cases verify that historical info entries are pruned when their height
// is below CurrentHeight - HistoricalEntries, and check that their valset gets updated
testCases := []struct {
height int64
found bool
expLen int
}{
{
height: initHeight + 1,
found: false,
expLen: 0,
},
{
height: initHeight + 2,
found: false,
expLen: 0,
},
{
height: initHeight + ccvtypes.DefaultHistoricalEntries + 2,
found: true,
expLen: initValsetLen + 2,
},
}
for _, tc := range testCases {
cCtx().WithBlockHeight(tc.height)
hi, found := consumerKeeper.GetHistoricalInfo(cCtx().WithBlockHeight(tc.height), tc.height)
k.Require().Equal(tc.found, found)
k.Require().Len(hi.Valset, tc.expLen)
}
}