Skip to content

Commit 8942694

Browse files
committed
refactor(cmd/testnetify): add multi-account support
Signed-off-by: Artur Troian <[email protected]>
1 parent 4ab1cd9 commit 8942694

File tree

4 files changed

+78
-35
lines changed

4 files changed

+78
-35
lines changed

app/export.go

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

188188
_ = iter.Close()
189189

190-
_, _ = app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
190+
_, err := app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
191+
if err != nil {
192+
panic(err)
193+
}
191194

192195
/* Handle slashing state. */
193196

app/testnet.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ type TestnetValidator struct {
2323
OperatorAddress sdk.ValAddress
2424
ConsensusAddress sdk.ConsAddress
2525
ConsensusPubKey *types.Any
26+
Status stakingtypes.BondStatus
2627
Moniker string
2728
Commission stakingtypes.Commission
2829
MinSelfDelegation sdk.Int
30+
Tokens sdk.Int
31+
DelegatorShares sdk.Dec
2932
}
3033

3134
type TestnetUpgrade struct {
@@ -42,8 +45,13 @@ type TestnetGovConfig struct {
4245
} `json:"voting_params,omitempty"`
4346
}
4447

48+
type TestnetAccount struct {
49+
Address sdk.AccAddress `json:"address"`
50+
Balances []sdk.Coin `json:"balances"`
51+
}
52+
4553
type TestnetConfig struct {
46-
Accounts []sdk.AccAddress
54+
Accounts []TestnetAccount
4755
Validators []TestnetValidator
4856
Gov TestnetGovConfig
4957
Upgrade TestnetUpgrade
@@ -137,27 +145,21 @@ func InitAkashAppForTestnet(
137145
OperatorAddress: val.OperatorAddress.String(),
138146
ConsensusPubkey: val.ConsensusPubKey,
139147
Jailed: false,
140-
Status: stakingtypes.Bonded,
141-
Tokens: sdk.NewInt(900000000000000),
142-
DelegatorShares: sdk.MustNewDecFromStr("10000000"),
148+
Status: val.Status,
149+
Tokens: val.Tokens,
150+
DelegatorShares: val.DelegatorShares,
143151
Description: stakingtypes.Description{
144-
Moniker: "Testnet Validator",
145-
},
146-
Commission: stakingtypes.Commission{
147-
CommissionRates: stakingtypes.CommissionRates{
148-
Rate: sdk.MustNewDecFromStr("0.05"),
149-
MaxRate: sdk.MustNewDecFromStr("0.1"),
150-
MaxChangeRate: sdk.MustNewDecFromStr("0.05"),
151-
},
152+
Moniker: val.Moniker,
152153
},
153-
MinSelfDelegation: sdk.OneInt(),
154+
Commission: val.Commission,
155+
MinSelfDelegation: val.MinSelfDelegation,
154156
}
155157

156158
// Add our validator to power and last validators store
157159
app.Keepers.Cosmos.Staking.SetValidator(ctx, newVal)
158160
err = app.Keepers.Cosmos.Staking.SetValidatorByConsAddr(ctx, newVal)
159161
if err != nil {
160-
return nil
162+
panic(err)
161163
}
162164

163165
app.Keepers.Cosmos.Staking.SetValidatorByPowerIndex(ctx, newVal)
@@ -189,7 +191,10 @@ func InitAkashAppForTestnet(
189191
Tombstoned: false,
190192
}
191193

192-
_, _ = app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
194+
_, err = app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
195+
if err != nil {
196+
panic(err)
197+
}
193198

194199
app.Keepers.Cosmos.Slashing.SetValidatorSigningInfo(ctx, newConsAddr, newValidatorSigningInfo)
195200
}
@@ -208,19 +213,25 @@ func InitAkashAppForTestnet(
208213
// BANK
209214
//
210215

211-
defaultCoins := sdk.NewCoins(
212-
sdk.NewInt64Coin("uakt", 1000000000000),
213-
sdk.NewInt64Coin("ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84", 1000000000000), // axlUSDC
214-
)
215-
216216
for _, account := range tcfg.Accounts {
217-
err := app.Keepers.Cosmos.Bank.MintCoins(ctx, minttypes.ModuleName, defaultCoins)
217+
var coins sdk.Coins
218+
219+
if len(account.Balances) > 0 {
220+
coins = sdk.NewCoins(account.Balances...)
221+
} else {
222+
coins = sdk.NewCoins(
223+
sdk.NewInt64Coin("uakt", 1000000000000),
224+
sdk.NewInt64Coin("ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84", 1000000000000), // axlUSDC
225+
)
226+
}
227+
228+
err := app.Keepers.Cosmos.Bank.MintCoins(ctx, minttypes.ModuleName, coins)
218229
if err != nil {
219-
return nil
230+
panic(err)
220231
}
221-
err = app.Keepers.Cosmos.Bank.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, account, defaultCoins)
232+
err = app.Keepers.Cosmos.Bank.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, account.Address, coins)
222233
if err != nil {
223-
return nil
234+
panic(err)
224235
}
225236
}
226237

cmd/akash/cmd/testnetify/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,24 @@ type ConsAddress struct {
4343
type TestnetValidator struct {
4444
Moniker string `json:"moniker"`
4545
Operator AccAddress `json:"operator"`
46-
Bonded bool `json:"bonded"`
46+
Status stakingtypes.BondStatus `json:"status"`
4747
Commission stakingtypes.Commission `json:"commission"`
4848
MinSelfDelegation sdk.Int `json:"min_self_delegation"`
4949
Home string `json:"home"`
50-
51-
privValidator *pvm.FilePV
52-
pubKey crypto.PubKey
53-
validatorAddress crypto.Address
54-
consAddress sdk.ConsAddress
50+
Tokens *sdk.Int `json:"tokens"`
51+
DelegatorShares *sdk.Dec `json:"delegator_shares"`
52+
privValidator *pvm.FilePV
53+
pubKey crypto.PubKey
54+
validatorAddress crypto.Address
55+
consAddress sdk.ConsAddress
5556
}
5657

5758
type TestnetValidators []TestnetValidator
5859

5960
type TestnetConfig struct {
6061
ChainID string `json:"chain_id"`
6162
Validators TestnetValidators `json:"validators"`
62-
Accounts []sdk.AccAddress `json:"accounts"`
63+
Accounts []akash.TestnetAccount `json:"accounts"`
6364
Gov akash.TestnetGovConfig `json:"gov"`
6465
upgrade akash.TestnetUpgrade
6566
}

cmd/akash/cmd/testnetify/testnetify.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testnetify
22

33
import (
44
"bufio"
5+
"encoding/hex"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -13,6 +14,7 @@ import (
1314

1415
"github.com/cosmos/cosmos-sdk/client"
1516
"github.com/cosmos/cosmos-sdk/client/flags"
17+
sdk "github.com/cosmos/cosmos-sdk/types"
1618
"github.com/spf13/cobra"
1719
"github.com/tendermint/tendermint/crypto/tmhash"
1820

@@ -114,6 +116,29 @@ you want to test the upgrade handler itself.
114116
return err
115117
}
116118

119+
for i, acc := range cfg.Accounts {
120+
if len(acc.Balances) > 0 {
121+
cfg.Accounts[i].Balances = sdk.NewCoins(acc.Balances...)
122+
} else {
123+
cfg.Accounts[i].Balances = sdk.NewCoins(
124+
sdk.NewInt64Coin("uakt", 1000000000000),
125+
sdk.NewInt64Coin("ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84", 1000000000000), // axlUSDC
126+
)
127+
}
128+
}
129+
130+
for i, val := range cfg.Validators {
131+
if val.Tokens == nil {
132+
tk := sdk.NewInt(900000000000000)
133+
cfg.Validators[i].Tokens = &tk
134+
}
135+
136+
if val.DelegatorShares == nil {
137+
shares := sdk.MustNewDecFromStr("10000000")
138+
cfg.Validators[i].DelegatorShares = &shares
139+
}
140+
}
141+
117142
sctx.Logger.Info(fmt.Sprintf("loaded config from %s", cfgFilePath))
118143

119144
if name, _ := cmd.Flags().GetString(KeyTestnetTriggerUpgrade); name != "" {
@@ -296,7 +321,7 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
296321
return nil, node.ErrSaveGenesisDocHash{Err: err}
297322
}
298323

299-
state, stateStore, _, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genDocProvider, "")
324+
state, stateStore, _, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genDocProvider, hex.EncodeToString(updatedChecksum))
300325
if err != nil {
301326
return nil, err
302327
}
@@ -346,6 +371,9 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
346371
Moniker: val.Moniker,
347372
Commission: val.Commission,
348373
MinSelfDelegation: val.MinSelfDelegation,
374+
Tokens: *val.Tokens,
375+
DelegatorShares: *val.DelegatorShares,
376+
Status: val.Status,
349377
})
350378

351379
tcfg.Validators[i].privValidator = privValidator
@@ -450,12 +478,12 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
450478
newValidators = append(newValidators, &cmttypes.Validator{
451479
Address: val.validatorAddress,
452480
PubKey: val.pubKey,
453-
VotingPower: 900000000000000,
481+
VotingPower: val.Tokens.Int64(),
454482
})
455483
}
456484

457485
// Replace all valSets in state to be the valSet with just our validator.
458-
// and set the very first validator as proposer
486+
// and set the very first validator as a proposer
459487
newValSet := &cmttypes.ValidatorSet{
460488
Validators: newValidators,
461489
Proposer: newValidators[0],

0 commit comments

Comments
 (0)