-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_helpers.go
171 lines (142 loc) · 5.51 KB
/
test_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package app
import (
"encoding/json"
"time"
"cosmossdk.io/math"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
cmtProto "github.com/cometbft/cometbft/proto/tendermint/types"
cmtTypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"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/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
// Auth
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
// Bank
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
// Global
globalTypes "github.com/KYVENetwork/chain/x/global/types"
// Staking
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
// Team
teamTypes "github.com/KYVENetwork/chain/x/team/types"
)
// DefaultConsensusParams ...
var DefaultConsensusParams = &cmtProto.ConsensusParams{
Block: &cmtProto.BlockParams{
MaxBytes: 200000,
MaxGas: -1, // no limit
},
Evidence: &cmtProto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
Validator: &cmtProto.ValidatorParams{
PubKeyTypes: []string{
cmtTypes.ABCIPubKeyTypeEd25519,
},
},
}
type EmptyAppOptions struct{}
func (ao EmptyAppOptions) Get(_ string) interface{} { return nil }
func DefaultGenesisWithValSet(codec codec.Codec) map[string]json.RawMessage {
bondingDenom := globalTypes.Denom
// Generate a new validator.
key, _ := mock.NewPV().GetPubKey()
validator := cmtTypes.NewValidator(key, 1)
publicKey, _ := cryptoCodec.FromTmPubKeyInterface(validator.PubKey)
publicKeyAny, _ := codecTypes.NewAnyWithValue(publicKey)
validators := []stakingTypes.Validator{
{
OperatorAddress: sdk.ValAddress(validator.Address).String(),
ConsensusPubkey: publicKeyAny,
Jailed: false,
Status: stakingTypes.Bonded,
Tokens: sdk.DefaultPowerReduction,
DelegatorShares: sdk.OneDec(),
Description: stakingTypes.Description{},
UnbondingHeight: 0,
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingTypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: math.ZeroInt(),
},
}
// Generate a new delegator.
delegatorKey := secp256k1.GenPrivKey()
delegator := authTypes.NewBaseAccount(
delegatorKey.PubKey().Address().Bytes(), delegatorKey.PubKey(), 0, 0,
)
delegations := []stakingTypes.Delegation{
stakingTypes.NewDelegation(delegator.GetAddress(), validator.Address.Bytes(), sdk.OneDec()),
}
// Default genesis state.
config := MakeEncodingConfig()
genesisState := ModuleBasics.DefaultGenesis(config.Marshaler)
// Update x/auth state.
authGenesis := authTypes.NewGenesisState(authTypes.DefaultParams(), []authTypes.GenesisAccount{delegator})
genesisState[authTypes.ModuleName] = codec.MustMarshalJSON(authGenesis)
// Update x/bank state.
bondedCoins := sdk.NewCoins(sdk.NewCoin(bondingDenom, sdk.DefaultPowerReduction))
teamCoins := sdk.NewCoins(sdk.NewInt64Coin(globalTypes.Denom, int64(teamTypes.TEAM_ALLOCATION)))
bankGenesis := bankTypes.NewGenesisState(bankTypes.DefaultGenesisState().Params, []bankTypes.Balance{
{
Address: authTypes.NewModuleAddress(stakingTypes.BondedPoolName).String(),
Coins: bondedCoins,
},
{
Address: authTypes.NewModuleAddress(teamTypes.ModuleName).String(),
Coins: teamCoins,
},
}, bondedCoins.Add(sdk.NewInt64Coin(globalTypes.Denom, int64(teamTypes.TEAM_ALLOCATION))), []bankTypes.Metadata{}, []bankTypes.SendEnabled{})
genesisState[bankTypes.ModuleName] = codec.MustMarshalJSON(bankGenesis)
// Update x/staking state.
stakingParams := stakingTypes.DefaultParams()
stakingParams.BondDenom = bondingDenom
stakingGenesis := stakingTypes.NewGenesisState(stakingParams, validators, delegations)
genesisState[stakingTypes.ModuleName] = codec.MustMarshalJSON(stakingGenesis)
// Return.
return genesisState
}
// Setup initializes a new App.
func Setup() *App {
db := dbm.NewMemDB()
// config := MakeEncodingConfig()
setPrefixes("kyve")
// app := NewKYVEApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, config, EmptyAppOptions{})
app := NewKYVEApp(log.NewNopLogger(), db, nil, true, EmptyAppOptions{}, baseapp.SetChainID("kyve-test"))
// init chain must be called to stop deliverState from being nil
genesisState := DefaultGenesisWithValSet(app.AppCodec())
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
// Initialize the chain
app.InitChain(
abci.RequestInitChain{
ChainId: "kyve-test",
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
return app
}
func setPrefixes(accountAddressPrefix string) {
// Set prefixes
accountPubKeyPrefix := accountAddressPrefix + "pub"
validatorAddressPrefix := accountAddressPrefix + "valoper"
validatorPubKeyPrefix := accountAddressPrefix + "valoperpub"
consNodeAddressPrefix := accountAddressPrefix + "valcons"
consNodePubKeyPrefix := accountAddressPrefix + "valconspub"
// Set and seal config
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(accountAddressPrefix, accountPubKeyPrefix)
config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)
}