Skip to content

Commit e4d6cfa

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

File tree

4 files changed

+86
-45
lines changed

4 files changed

+86
-45
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: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ 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+
Delegations []TestnetDelegation
2931
}
3032

3133
type TestnetUpgrade struct {
@@ -42,8 +44,18 @@ type TestnetGovConfig struct {
4244
} `json:"voting_params,omitempty"`
4345
}
4446

47+
type TestnetAccount struct {
48+
Address sdk.AccAddress `json:"address"`
49+
Balances []sdk.Coin `json:"balances"`
50+
}
51+
52+
type TestnetDelegation struct {
53+
Address sdk.AccAddress `json:"address"`
54+
Amount sdk.Coin `json:"amount"`
55+
}
56+
4557
type TestnetConfig struct {
46-
Accounts []sdk.AccAddress
58+
Accounts []TestnetAccount
4759
Validators []TestnetValidator
4860
Gov TestnetGovConfig
4961
Upgrade TestnetUpgrade
@@ -131,33 +143,41 @@ func InitAkashAppForTestnet(
131143
panic(err)
132144
}
133145

146+
// BANK
147+
//
148+
149+
for _, account := range tcfg.Accounts {
150+
err := app.Keepers.Cosmos.Bank.MintCoins(ctx, minttypes.ModuleName, account.Balances)
151+
if err != nil {
152+
panic(err)
153+
}
154+
err = app.Keepers.Cosmos.Bank.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, account.Address, account.Balances)
155+
if err != nil {
156+
panic(err)
157+
}
158+
}
159+
134160
for _, val := range tcfg.Validators {
135161
// Create Validator struct for our new validator.
136162
newVal := stakingtypes.Validator{
137163
OperatorAddress: val.OperatorAddress.String(),
138164
ConsensusPubkey: val.ConsensusPubKey,
139165
Jailed: false,
140-
Status: stakingtypes.Bonded,
141-
Tokens: sdk.NewInt(900000000000000),
142-
DelegatorShares: sdk.MustNewDecFromStr("10000000"),
166+
Status: val.Status,
167+
Tokens: sdk.NewInt(0),
168+
DelegatorShares: sdk.MustNewDecFromStr("0"),
143169
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-
},
170+
Moniker: val.Moniker,
152171
},
153-
MinSelfDelegation: sdk.OneInt(),
172+
Commission: val.Commission,
173+
MinSelfDelegation: val.MinSelfDelegation,
154174
}
155175

156176
// Add our validator to power and last validators store
157177
app.Keepers.Cosmos.Staking.SetValidator(ctx, newVal)
158178
err = app.Keepers.Cosmos.Staking.SetValidatorByConsAddr(ctx, newVal)
159179
if err != nil {
160-
return nil
180+
panic(err)
161181
}
162182

163183
app.Keepers.Cosmos.Staking.SetValidatorByPowerIndex(ctx, newVal)
@@ -189,9 +209,24 @@ func InitAkashAppForTestnet(
189209
Tombstoned: false,
190210
}
191211

192-
_, _ = app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
212+
_, err = app.Keepers.Cosmos.Staking.ApplyAndReturnValidatorSetUpdates(ctx)
213+
if err != nil {
214+
panic(err)
215+
}
193216

194217
app.Keepers.Cosmos.Slashing.SetValidatorSigningInfo(ctx, newConsAddr, newValidatorSigningInfo)
218+
219+
for _, del := range val.Delegations {
220+
vl, found := app.Keepers.Cosmos.Staking.GetValidator(ctx, valAddr)
221+
if !found {
222+
panic("validator not found")
223+
}
224+
225+
_, err = app.Keepers.Cosmos.Staking.Delegate(ctx, del.Address, del.Amount.Amount, stakingtypes.Unbonded, vl, true)
226+
if err != nil {
227+
panic(err)
228+
}
229+
}
195230
}
196231

197232
//
@@ -205,25 +240,6 @@ func InitAkashAppForTestnet(
205240
voteParams.VotingPeriod = tcfg.Gov.VotingParams.VotingPeriod.Duration
206241
app.Keepers.Cosmos.Gov.SetVotingParams(ctx, voteParams)
207242

208-
// BANK
209-
//
210-
211-
defaultCoins := sdk.NewCoins(
212-
sdk.NewInt64Coin("uakt", 1000000000000),
213-
sdk.NewInt64Coin("ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84", 1000000000000), // axlUSDC
214-
)
215-
216-
for _, account := range tcfg.Accounts {
217-
err := app.Keepers.Cosmos.Bank.MintCoins(ctx, minttypes.ModuleName, defaultCoins)
218-
if err != nil {
219-
return nil
220-
}
221-
err = app.Keepers.Cosmos.Bank.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, account, defaultCoins)
222-
if err != nil {
223-
return nil
224-
}
225-
}
226-
227243
// UPGRADE
228244
//
229245
if tcfg.Upgrade.Name != "" {

cmd/akash/cmd/testnetify/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ type ConsAddress struct {
4141
}
4242

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

5152
privValidator *pvm.FilePV
5253
pubKey crypto.PubKey
@@ -59,7 +60,7 @@ type TestnetValidators []TestnetValidator
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: 24 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

@@ -29,6 +31,7 @@ import (
2931
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
3032
sdksrv "github.com/cosmos/cosmos-sdk/server"
3133
"github.com/cosmos/cosmos-sdk/server/types"
34+
sdktypes "github.com/cosmos/cosmos-sdk/types"
3235

3336
akash "github.com/akash-network/node/app"
3437
)
@@ -114,6 +117,17 @@ you want to test the upgrade handler itself.
114117
return err
115118
}
116119

120+
for i, acc := range cfg.Accounts {
121+
if len(acc.Balances) > 0 {
122+
cfg.Accounts[i].Balances = sdk.NewCoins(acc.Balances...)
123+
} else {
124+
cfg.Accounts[i].Balances = sdk.NewCoins(
125+
sdk.NewInt64Coin("uakt", 10000000000000),
126+
sdk.NewInt64Coin("ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84", 1000000000000), // axlUSDC
127+
)
128+
}
129+
}
130+
117131
sctx.Logger.Info(fmt.Sprintf("loaded config from %s", cfgFilePath))
118132

119133
if name, _ := cmd.Flags().GetString(KeyTestnetTriggerUpgrade); name != "" {
@@ -296,7 +310,7 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
296310
return nil, node.ErrSaveGenesisDocHash{Err: err}
297311
}
298312

299-
state, stateStore, _, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genDocProvider, "")
313+
state, stateStore, _, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genDocProvider, hex.EncodeToString(updatedChecksum))
300314
if err != nil {
301315
return nil, err
302316
}
@@ -346,6 +360,8 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
346360
Moniker: val.Moniker,
347361
Commission: val.Commission,
348362
MinSelfDelegation: val.MinSelfDelegation,
363+
Status: val.Status,
364+
Delegations: val.Delegations,
349365
})
350366

351367
tcfg.Validators[i].privValidator = privValidator
@@ -447,15 +463,20 @@ func testnetify(sctx *sdksrv.Context, tcfg TestnetConfig, testnetAppCreator type
447463
Signature: voteProto.Signature,
448464
})
449465

466+
var vp int64
467+
468+
for _, del := range val.Delegations {
469+
vp += del.Amount.Amount.Quo(sdktypes.DefaultPowerReduction).Int64()
470+
}
450471
newValidators = append(newValidators, &cmttypes.Validator{
451472
Address: val.validatorAddress,
452473
PubKey: val.pubKey,
453-
VotingPower: 900000000000000,
474+
VotingPower: vp,
454475
})
455476
}
456477

457478
// Replace all valSets in state to be the valSet with just our validator.
458-
// and set the very first validator as proposer
479+
// and set the very first validator as a proposer
459480
newValSet := &cmttypes.ValidatorSet{
460481
Validators: newValidators,
461482
Proposer: newValidators[0],

0 commit comments

Comments
 (0)