Skip to content

Commit 7a1e86a

Browse files
committed
refactor(cmd/testnetify): improve multi-validator test environment
Signed-off-by: Artur Troian <[email protected]>
1 parent 4111097 commit 7a1e86a

File tree

9 files changed

+336
-107
lines changed

9 files changed

+336
-107
lines changed

app/export.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ func (app *AkashApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
254254

255255
_ = iter.Close()
256256

257-
_, _ = app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
257+
_, err = app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
258+
if err != nil {
259+
panic(err)
260+
}
258261

259262
/* Handle slashing state. */
260263

app/testnet.go

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ import (
2626
utypes "pkg.akt.dev/node/upgrades/types"
2727
)
2828

29+
type TestnetDelegation struct {
30+
Address sdk.AccAddress `json:"address"`
31+
Amount sdk.Coin `json:"amount"`
32+
}
33+
2934
type TestnetValidator struct {
3035
OperatorAddress sdk.Address
3136
ConsensusAddress sdk.ConsAddress
3237
ConsensusPubKey *types.Any
38+
Status stakingtypes.BondStatus
3339
Moniker string
3440
Commission stakingtypes.Commission
3541
MinSelfDelegation sdkmath.Int
42+
Delegations []TestnetDelegation
3643
}
3744

3845
type TestnetGov struct {
@@ -44,8 +51,13 @@ type TestnetUpgrade struct {
4451
Name string
4552
}
4653

54+
type TestnetAccount struct {
55+
Address sdk.AccAddress `json:"address"`
56+
Balances []sdk.Coin `json:"balances"`
57+
}
58+
4759
type TestnetConfig struct {
48-
Accounts []sdk.AccAddress
60+
Accounts []TestnetAccount
4961
Validators []TestnetValidator
5062
Gov TestnetGov
5163
Upgrade TestnetUpgrade
@@ -81,14 +93,14 @@ func InitAkashAppForTestnet(
8193
stakingStore := ctx.KVStore(stakingKey)
8294
iterator, err := app.Keepers.Cosmos.Staking.ValidatorsPowerStoreIterator(ctx)
8395
if err != nil {
84-
return nil
96+
panic(err.Error())
8597
}
8698
for ; iterator.Valid(); iterator.Next() {
8799
stakingStore.Delete(iterator.Key())
88100
}
89101
_ = iterator.Close()
90102

91-
// Remove all validators from last validators store
103+
// Remove all validators from the last validators store
92104
iterator, err = app.Keepers.Cosmos.Staking.LastValidatorsIterator(ctx)
93105
if err != nil {
94106
return nil
@@ -98,7 +110,7 @@ func InitAkashAppForTestnet(
98110
}
99111
_ = iterator.Close()
100112

101-
// Remove all validators from validator store
113+
// Remove all validators from the validator store
102114
iterator = storetypes.KVStorePrefixIterator(stakingStore, stakingtypes.ValidatorsKey)
103115
for ; iterator.Valid(); iterator.Next() {
104116
stakingStore.Delete(iterator.Key())
@@ -112,14 +124,29 @@ func InitAkashAppForTestnet(
112124
}
113125
_ = iterator.Close()
114126

127+
// BANK
128+
//
129+
130+
// Fund localakash accounts
131+
for _, account := range tcfg.Accounts {
132+
err := app.Keepers.Cosmos.Bank.MintCoins(ctx, minttypes.ModuleName, account.Balances)
133+
if err != nil {
134+
panic(err.Error())
135+
}
136+
err = app.Keepers.Cosmos.Bank.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, account.Address, account.Balances)
137+
if err != nil {
138+
panic(err.Error())
139+
}
140+
}
141+
115142
for _, val := range tcfg.Validators {
116143
_, bz, err := bech32.DecodeAndConvert(val.OperatorAddress.String())
117144
if err != nil {
118-
return nil
145+
panic(err.Error())
119146
}
120147
bech32Addr, err := bech32.ConvertAndEncode("akashvaloper", bz)
121148
if err != nil {
122-
return nil
149+
panic(err.Error())
123150
}
124151

125152
// Create Validator struct for our new validator.
@@ -128,41 +155,35 @@ func InitAkashAppForTestnet(
128155
ConsensusPubkey: val.ConsensusPubKey,
129156
Jailed: false,
130157
Status: stakingtypes.Bonded,
131-
Tokens: sdkmath.NewInt(900000000000000),
132-
DelegatorShares: sdkmath.LegacyMustNewDecFromStr("10000000"),
158+
Tokens: sdkmath.NewInt(0),
159+
DelegatorShares: sdkmath.LegacyMustNewDecFromStr("0"),
133160
Description: stakingtypes.Description{
134-
Moniker: "Testnet Validator",
135-
},
136-
Commission: stakingtypes.Commission{
137-
CommissionRates: stakingtypes.CommissionRates{
138-
Rate: sdkmath.LegacyMustNewDecFromStr("0.05"),
139-
MaxRate: sdkmath.LegacyMustNewDecFromStr("0.1"),
140-
MaxChangeRate: sdkmath.LegacyMustNewDecFromStr("0.05"),
141-
},
161+
Moniker: val.Moniker,
142162
},
143-
MinSelfDelegation: sdkmath.OneInt(),
163+
Commission: val.Commission,
164+
MinSelfDelegation: val.MinSelfDelegation,
144165
}
145166

146167
// Add our validator to power and last validators store
147168
err = app.Keepers.Cosmos.Staking.SetValidator(ctx, newVal)
148169
if err != nil {
149-
return nil
170+
panic(err.Error())
150171
}
151172
err = app.Keepers.Cosmos.Staking.SetValidatorByConsAddr(ctx, newVal)
152173
if err != nil {
153-
return nil
174+
panic(err.Error())
154175
}
155176
err = app.Keepers.Cosmos.Staking.SetValidatorByPowerIndex(ctx, newVal)
156177
if err != nil {
157-
return nil
178+
panic(err.Error())
158179
}
159180
valAddr, err := sdk.ValAddressFromBech32(newVal.GetOperator())
160181
if err != nil {
161-
return nil
182+
panic(err.Error())
162183
}
163184
err = app.Keepers.Cosmos.Staking.SetLastValidatorPower(ctx, valAddr, 0)
164185
if err != nil {
165-
return nil
186+
panic(err.Error())
166187
}
167188
if err := app.Keepers.Cosmos.Staking.Hooks().AfterValidatorCreated(ctx, valAddr); err != nil {
168189
panic(err)
@@ -174,23 +195,23 @@ func InitAkashAppForTestnet(
174195
// Initialize records for this validator across all distribution stores
175196
valAddr, err = sdk.ValAddressFromBech32(newVal.GetOperator())
176197
if err != nil {
177-
return nil
198+
panic(err.Error())
178199
}
179200
err = app.Keepers.Cosmos.Distr.SetValidatorHistoricalRewards(ctx, valAddr, 0, distrtypes.NewValidatorHistoricalRewards(sdk.DecCoins{}, 1))
180201
if err != nil {
181-
return nil
202+
panic(err.Error())
182203
}
183204
err = app.Keepers.Cosmos.Distr.SetValidatorCurrentRewards(ctx, valAddr, distrtypes.NewValidatorCurrentRewards(sdk.DecCoins{}, 1))
184205
if err != nil {
185-
return nil
206+
panic(err.Error())
186207
}
187208
err = app.Keepers.Cosmos.Distr.SetValidatorAccumulatedCommission(ctx, valAddr, distrtypes.InitialValidatorAccumulatedCommission())
188209
if err != nil {
189-
return nil
210+
panic(err.Error())
190211
}
191212
err = app.Keepers.Cosmos.Distr.SetValidatorOutstandingRewards(ctx, valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: sdk.DecCoins{}})
192213
if err != nil {
193-
return nil
214+
panic(err.Error())
194215
}
195216

196217
// SLASHING
@@ -205,7 +226,19 @@ func InitAkashAppForTestnet(
205226
}
206227
err = app.Keepers.Cosmos.Slashing.SetValidatorSigningInfo(ctx, newConsAddr, newValidatorSigningInfo)
207228
if err != nil {
208-
return nil
229+
panic(err.Error())
230+
}
231+
232+
for _, del := range val.Delegations {
233+
vl, err := app.Keepers.Cosmos.Staking.GetValidator(ctx, valAddr)
234+
if err != nil {
235+
panic(err.Error())
236+
}
237+
238+
_, err = app.Keepers.Cosmos.Staking.Delegate(ctx, del.Address, del.Amount.Amount, stakingtypes.Unbonded, vl, true)
239+
if err != nil {
240+
panic(err)
241+
}
209242
}
210243
}
211244
//
@@ -217,7 +250,7 @@ func InitAkashAppForTestnet(
217250

218251
govParams, err := app.Keepers.Cosmos.Gov.Params.Get(ctx)
219252
if err != nil {
220-
return nil
253+
panic(err.Error())
221254
}
222255
govParams.ExpeditedVotingPeriod = &tcfg.Gov.ExpeditedVotePeriod
223256
govParams.VotingPeriod = &tcfg.Gov.VotePeriod
@@ -226,27 +259,7 @@ func InitAkashAppForTestnet(
226259

227260
err = app.Keepers.Cosmos.Gov.Params.Set(ctx, govParams)
228261
if err != nil {
229-
return nil
230-
}
231-
232-
// BANK
233-
//
234-
235-
defaultCoins := sdk.NewCoins(
236-
sdk.NewInt64Coin("uakt", 1000000000000),
237-
sdk.NewInt64Coin("ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84", 1000000000000), // axlUSDC
238-
)
239-
240-
// Fund localakash accounts
241-
for _, account := range tcfg.Accounts {
242-
err := app.Keepers.Cosmos.Bank.MintCoins(ctx, minttypes.ModuleName, defaultCoins)
243-
if err != nil {
244-
return nil
245-
}
246-
err = app.Keepers.Cosmos.Bank.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, account, defaultCoins)
247-
if err != nil {
248-
return nil
249-
}
262+
panic(err.Error())
250263
}
251264

252265
// UPGRADE

cmd/akash/cmd/testnetify/config.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ type ConsAddress struct {
4242
}
4343

4444
type TestnetValidator struct {
45-
Moniker string `json:"moniker"`
46-
Operator AccAddress `json:"operator"`
47-
Bonded bool `json:"bonded"`
48-
Commission stakingtypes.Commission `json:"commission"`
49-
MinSelfDelegation sdkmath.Int `json:"min_self_delegation"`
50-
Home string `json:"home"`
45+
Moniker string `json:"moniker"`
46+
Operator AccAddress `json:"operator"`
47+
Status stakingtypes.BondStatus `json:"status"`
48+
Commission stakingtypes.Commission `json:"commission"`
49+
MinSelfDelegation sdkmath.Int `json:"min_self_delegation"`
50+
Home string `json:"home"`
51+
Delegations []akash.TestnetDelegation `json:"delegations"`
5152

5253
privValidator *pvm.FilePV
5354
pubKey crypto.PubKey
@@ -58,10 +59,10 @@ type TestnetValidator struct {
5859
type TestnetValidators []TestnetValidator
5960

6061
type TestnetConfig struct {
61-
ChainID string `json:"chain_id"`
62-
Validators TestnetValidators `json:"validators"`
63-
Accounts []sdk.AccAddress `json:"accounts"`
64-
Gov akash.TestnetGov `json:"gov"`
62+
ChainID string `json:"chain_id"`
63+
Validators TestnetValidators `json:"validators"`
64+
Accounts []akash.TestnetAccount `json:"accounts"`
65+
Gov akash.TestnetGov `json:"gov"`
6566
upgrade akash.TestnetUpgrade
6667
}
6768

cmd/akash/cmd/testnetify/testnetify.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testnetify
33
import (
44
"bufio"
55
"context"
6+
"encoding/hex"
67
"encoding/json"
78
"fmt"
89
"io"
@@ -34,6 +35,7 @@ import (
3435
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
3536
"github.com/cosmos/cosmos-sdk/server/types"
3637
"github.com/cosmos/cosmos-sdk/telemetry"
38+
sdktypes "github.com/cosmos/cosmos-sdk/types"
3739
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
3840

3941
cflags "pkg.akt.dev/go/cli/flags"
@@ -111,6 +113,17 @@ you want to test the upgrade handler itself.
111113
return err
112114
}
113115

116+
for i, acc := range cfg.Accounts {
117+
if len(acc.Balances) > 0 {
118+
cfg.Accounts[i].Balances = sdktypes.NewCoins(acc.Balances...)
119+
} else {
120+
cfg.Accounts[i].Balances = sdktypes.NewCoins(
121+
sdktypes.NewInt64Coin("uakt", 10000000000000),
122+
sdktypes.NewInt64Coin("ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84", 1000000000000), // axlUSDC
123+
)
124+
}
125+
}
126+
114127
sctx.Logger.Info(fmt.Sprintf("loaded config from %s", cfgFilePath))
115128

116129
if name, _ := cmd.Flags().GetString(cflags.KeyTestnetTriggerUpgrade); name != "" {
@@ -328,7 +341,7 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
328341
DiscardABCIResponses: config.Storage.DiscardABCIResponses,
329342
})
330343

331-
state, genDoc, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genDocProvider, "")
344+
state, genDoc, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genDocProvider, hex.EncodeToString(updatedChecksum))
332345
if err != nil {
333346
return nil, err
334347
}
@@ -375,9 +388,11 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
375388
OperatorAddress: val.Operator,
376389
ConsensusAddress: pubKey.Address().Bytes(),
377390
ConsensusPubKey: consensusPubkey,
391+
Status: val.Status,
378392
Moniker: val.Moniker,
379393
Commission: val.Commission,
380394
MinSelfDelegation: val.MinSelfDelegation,
395+
Delegations: val.Delegations,
381396
})
382397

383398
tcfg.Validators[i].privValidator = privValidator
@@ -480,10 +495,16 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
480495
Signature: voteProto.Signature,
481496
})
482497

498+
var vp int64
499+
500+
for _, del := range val.Delegations {
501+
vp += del.Amount.Amount.Quo(sdktypes.DefaultPowerReduction).Int64()
502+
}
503+
483504
newValidators = append(newValidators, &cmttypes.Validator{
484505
Address: val.validatorAddress,
485506
PubKey: val.pubKey,
486-
VotingPower: 900000000000000,
507+
VotingPower: vp,
487508
})
488509
}
489510

0 commit comments

Comments
 (0)