Skip to content

Commit

Permalink
#92 Populate state with random addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed Dec 26, 2018
1 parent ff4e726 commit c908471
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 36 deletions.
4 changes: 0 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@ func (app *CyberdApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) ab
panic(err)
}

// sort by account number to maintain consistency
sort.Slice(genesisState.Accounts, func(i, j int) bool {
return genesisState.Accounts[i].AccountNumber < genesisState.Accounts[j].AccountNumber
})
// load the accounts
for _, gacc := range genesisState.Accounts {
acc := gacc.ToAccount()
Expand Down
40 changes: 12 additions & 28 deletions app/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,29 @@ func NewGenesisState(

// nolint
type GenesisAccount struct {
Name string `json:"name"`
Address sdk.AccAddress `json:"address"`
Coins sdk.Coins `json:"coins"`
Sequence uint64 `json:"sequence_number"`
AccountNumber uint64 `json:"account_number"`
Address sdk.AccAddress `json:"addr"`
Amount int64 `json:"amt"`
}

func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount {
return GenesisAccount{
Address: acc.Address,
Coins: acc.Coins,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
Address: acc.Address,
Amount: acc.Coins.AmountOf(coin.CBD).Int64(),
}
}

func NewGenesisAccountI(acc auth.Account) GenesisAccount {
return GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
AccountNumber: acc.GetAccountNumber(),
Sequence: acc.GetSequence(),
Address: acc.GetAddress(),
Amount: acc.GetCoins().AmountOf(coin.CBD).Int64(),
}
}

// convert GenesisAccount to auth.BaseAccount
func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) {
return &auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins.Sort(),
AccountNumber: ga.AccountNumber,
Sequence: ga.Sequence,
Address: ga.Address,
Coins: sdk.Coins{sdk.NewInt64Coin(coin.CBD, ga.Amount)},
}
}

Expand Down Expand Up @@ -111,12 +102,8 @@ func CyberdAppGenState(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []
}

for _, acc := range genesisState.Accounts {
for _, c := range acc.Coins {
if c.Denom == coin.CBD {
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.
Add(sdk.NewDecFromInt(c.Amount)) // increase the supply
}
}
// increase the supply
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDec(acc.Amount))
}
genesisState.StakeData = stakeData
genesisState.GenTxs = appGenTxs
Expand All @@ -138,9 +125,6 @@ func NewDefaultGenesisState() GenesisState {
}

// CyberdValidateGenesisState ensures that the genesis state obeys the expected invariants
// TODO: No validators are both bonded and jailed (#2088)
// TODO: Error if there is a duplicate validator (#1708)
// TODO: Ensure all state machine parameters are in genesis (#1704)
func CyberdValidateGenesisState(genesisState GenesisState) (err error) {
err = validateGenesisStateAccounts(genesisState.Accounts)
if err != nil {
Expand Down Expand Up @@ -247,9 +231,9 @@ func CollectStdTxs(cdc *codec.Codec, moniker string, genTxsDir string, genDoc tm
return appGenTxs, persistentPeers, fmt.Errorf(
"account %v not in genesis.json: %+v", addr, addrMap)
}
if acc.Coins.AmountOf(msg.Delegation.Denom).LT(msg.Delegation.Amount) {
if sdk.NewInt(acc.Amount).LT(msg.Delegation.Amount) {
err = fmt.Errorf("insufficient fund for the delegation: %s < %s",
acc.Coins.AmountOf(msg.Delegation.Denom), msg.Delegation.Amount)
acc.Amount, msg.Delegation.Amount)
}

// exclude itself from persistent peers
Expand Down
File renamed without changes.
81 changes: 81 additions & 0 deletions daemon/init/generate_accs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package init

import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/go-bip39"
"github.com/cybercongress/cyberd/app"
. "github.com/cybercongress/cyberd/app/genesis"
"sync"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/common"
)

// Used for development purposes.
// Takes about 4min to generated 250kk genesis accs.
func GenerateAccountsCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "add-random-genesis-accounts [count]",
Short: "Add randoms accounts to genesis.json. Used to populate tests chain during performance testing.",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {

config := ctx.Config
config.SetRoot(viper.GetString(cli.HomeFlag))

count, ok := sdk.NewIntFromString(args[0])
if !ok {
return fmt.Errorf("failed to parse accs count: %s", args[0])
}

genFile := config.GenesisFile()
if !common.FileExists(genFile) {
return fmt.Errorf("%s does not exist, run `cyberd init` first", genFile)
}

genDoc, err := loadGenesisDoc(cdc, genFile)
if err != nil {
return err
}

var appState GenesisState
if err = cdc.UnmarshalJSON(genDoc.AppState, &appState); err != nil {
return err
}

amount := int64(10)
kb := client.MockKeyBase()
addresses := make([]GenesisAccount, count.Int64())

var wg sync.WaitGroup
wg.Add(int(count.Int64()))
for i := int64(0); i < count.Int64(); i++ {
go func(position int64) {
defer wg.Done()
entropySeed, _ := bip39.NewEntropy(256)
mnemonic, _ := bip39.NewMnemonic(entropySeed[:])
info, _ := kb.CreateKey(string(position), mnemonic, "")
addresses[position] = GenesisAccount{Address: info.GetAddress(), Amount: amount}
}(i)
}
wg.Wait()

appState.Accounts = append(appState.Accounts, addresses...)
appStateJSON, err := cdc.MarshalJSON(appState)
if err != nil {
return err
}

return ExportGenesisFile(genFile, genDoc.ChainID, nil, appStateJSON)
},
}

cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory")
return cmd
}
4 changes: 1 addition & 3 deletions daemon/init/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ func initTestnet(config *cfg.Config, cdc *codec.Codec) error {

accs = append(accs, GenesisAccount{
Address: addr,
Coins: sdk.Coins{
sdk.NewInt64Coin("CBD", 100),
},
Amount: 100,
})

msg := stake.NewMsgCreateValidator(
Expand Down
1 change: 1 addition & 0 deletions daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func main() {

rootCmd.AddCommand(initCyberd.InitCmd(ctx, cdc))
rootCmd.AddCommand(initCyberd.GenerateAccountCmd())
rootCmd.AddCommand(initCyberd.GenerateAccountsCmd(ctx, cdc))
rootCmd.AddCommand(initCyberd.CollectGenTxsCmd(ctx, cdc))
rootCmd.AddCommand(initCyberd.TestnetFilesCmd(ctx, cdc))
rootCmd.AddCommand(initCyberd.GenTxCmd(ctx, cdc))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/btcsuite/btcd v0.0.0-20180903232927-cff30e1d23fc // indirect
github.com/btcsuite/btcutil v0.0.0-20180706230648-ab6388e0c60a // indirect
github.com/cosmos/cosmos-sdk v0.29.0
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 // indirect
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8
github.com/fortytw2/leaktest v1.2.0 // indirect
github.com/go-kit/kit v0.7.0 // indirect
github.com/go-logfmt/logfmt v0.3.0 // indirect
Expand Down

0 comments on commit c908471

Please sign in to comment.