/
helpers.go
133 lines (117 loc) · 4.26 KB
/
helpers.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ututil
import (
"crypto/rand"
"encoding/binary"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tmdb "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/interchain-security/x/types"
)
// Parameters needed to instantiate an in-memory keeper
type InMemKeeperParams struct {
Cdc *codec.ProtoCodec
StoreKey *storetypes.KVStoreKey
ParamsSubspace *paramstypes.Subspace
Ctx sdk.Context
}
// NewInMemKeeperParams instantiates in-memory keeper params with default values
func NewInMemKeeperParams(tb testing.TB) InMemKeeperParams {
tb.Helper()
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(tb, stateStore.LoadLatestVersion())
registry := codectypes.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry) // Public key implementation registered here
cdc := codec.NewProtoCodec(registry)
paramsSubspace := paramstypes.NewSubspace(cdc,
codec.NewLegacyAmino(),
storeKey,
memStoreKey,
paramstypes.ModuleName,
)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
return InMemKeeperParams{
Cdc: cdc,
StoreKey: storeKey,
ParamsSubspace: ¶msSubspace,
Ctx: ctx,
}
}
// A struct holding pointers to any mocked external keeper needed for provider/consumer keeper setup.
type MockedKeepers struct {
*MockScopedKeeper
*MockChannelKeeper
*MockPortKeeper
*MockConnectionKeeper
*MockClientKeeper
*MockStakingKeeper
*MockSlashingKeeper
*MockAccountKeeper
*MockBankKeeper
*MockIBCTransferKeeper
*MockIBCCoreKeeper
*MockEvidenceKeeper
*MockDistributionKeeper
}
// NewMockedKeepers instantiates a struct with pointers to properly instantiated mocked keepers.
func NewMockedKeepers(ctrl *gomock.Controller) MockedKeepers {
return MockedKeepers{
MockScopedKeeper: NewMockScopedKeeper(ctrl),
MockChannelKeeper: NewMockChannelKeeper(ctrl),
MockPortKeeper: NewMockPortKeeper(ctrl),
MockConnectionKeeper: NewMockConnectionKeeper(ctrl),
MockClientKeeper: NewMockClientKeeper(ctrl),
MockStakingKeeper: NewMockStakingKeeper(ctrl),
MockSlashingKeeper: NewMockSlashingKeeper(ctrl),
MockAccountKeeper: NewMockAccountKeeper(ctrl),
MockBankKeeper: NewMockBankKeeper(ctrl),
MockIBCTransferKeeper: NewMockIBCTransferKeeper(ctrl),
MockIBCCoreKeeper: NewMockIBCCoreKeeper(ctrl),
MockEvidenceKeeper: NewMockEvidenceKeeper(ctrl),
MockDistributionKeeper: NewMockDistributionKeeper(ctrl),
}
}
type PrivateKey struct {
PrivKey cryptotypes.PrivKey
}
// Obtains slash packet data with a newly generated key, and randomized field values
func GetNewSlashPacketData() types.SlashPacketData {
b1 := make([]byte, 8)
_, _ = rand.Read(b1)
b2 := make([]byte, 8)
_, _ = rand.Read(b2)
b3 := make([]byte, 8)
_, _ = rand.Read(b3)
return types.SlashPacketData{
Validator: abci.Validator{
Address: ed25519.GenPrivKey().PubKey().Address(),
Power: int64(binary.BigEndian.Uint64(b1)),
},
ValsetUpdateId: binary.BigEndian.Uint64(b2),
Infraction: stakingtypes.Infraction(binary.BigEndian.Uint64(b2) % 3),
}
}
// Obtains vsc matured packet data with a newly generated key
func GetNewVSCMaturedPacketData() types.VSCMaturedPacketData {
b := make([]byte, 8)
_, _ = rand.Read(b)
return types.VSCMaturedPacketData{ValsetUpdateId: binary.BigEndian.Uint64(b)}
}