From 904de3a8f14cd1e6a599f89d32ba4961cd2ce501 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 2 Oct 2019 18:22:45 +0700 Subject: [PATCH 1/5] Merge gen script to one gen-genesis --- internal/tools/gen-genesis/main.go | 163 +++++++++++++++++++ internal/tools/gen_cosmos_account/main.go | 68 -------- internal/tools/gen_genesis_validator/main.go | 90 ---------- internal/tools/gen_tm_identity/main.go | 53 ------ 4 files changed, 163 insertions(+), 211 deletions(-) create mode 100644 internal/tools/gen-genesis/main.go delete mode 100644 internal/tools/gen_cosmos_account/main.go delete mode 100644 internal/tools/gen_genesis_validator/main.go delete mode 100644 internal/tools/gen_tm_identity/main.go diff --git a/internal/tools/gen-genesis/main.go b/internal/tools/gen-genesis/main.go new file mode 100644 index 000000000..113a7870b --- /dev/null +++ b/internal/tools/gen-genesis/main.go @@ -0,0 +1,163 @@ +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "log" + "math/rand" + "os" + "path/filepath" + "strings" + "time" + + "github.com/cosmos/cosmos-sdk/codec" + sdktypes "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + bip39 "github.com/cosmos/go-bip39" + "github.com/mesg-foundation/engine/cosmos" + "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/p2p" + "github.com/tendermint/tendermint/privval" +) + +const mnemonicEntropySize = 256 + +const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + +var names = [...]string{ + "bob", + "alice", + "eve", + "bell", + "brown", + "beethoven", + "curie", + "copernicus", + "chopin", + "euler", + "gauss", + "postel", + "liskov", + "boole", + "dijkstra", + "knuth", +} + +func randompassword() string { + b := make([]byte, 16) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + return string(b) +} + +var ( + vno = flag.Int("vno", 1, "validator numbers") + chainid = flag.String("chain-id", "mesg-chain", "chain id") + kbpath = flag.String("co-kbpath", ".", "cosmos key base path") + tmpath = flag.String("tm-path", ".", "tendermint config path") + gentxfilepath = flag.String("gentx-filepath", "genesistx.json", "genesis transaction file path") + peersfilepath = flag.String("peers-filepath", "peers", "peers file path") +) + +func main() { + rand.Seed(time.Now().UnixNano()) + flag.Parse() + + passwords := make([]string, *vno) + for i := 0; i < *vno; i++ { + passwords[i] = randompassword() + } + + kb, err := cosmos.NewKeybase(*kbpath) + if err != nil { + log.Fatalln("creating keybase error:", err) + } + + cdc := codec.New() + codec.RegisterCrypto(cdc) + sdktypes.RegisterCodec(cdc) + stakingtypes.RegisterCodec(cdc) + + msgs := []sdktypes.Msg{} + peers := []string{} + for i := 0; i < *vno; i++ { + // read entropy seed straight from crypto.Rand and convert to mnemonic + entropySeed, err := bip39.NewEntropy(mnemonicEntropySize) + if err != nil { + log.Fatalln("new entropy error:", err) + } + + mnemonic, err := bip39.NewMnemonic(entropySeed) + if err != nil { + log.Fatalln("new mnemonic error:", err) + } + + acc, err := kb.CreateAccount(names[i], mnemonic, "", passwords[i], 0, 0) + if err != nil { + log.Fatalln("create account error:", err) + } + + if err := os.MkdirAll(filepath.Join(*tmpath, names[i], "config"), 0755); err != nil { + log.Fatalln("mkdir tm config error:", err) + } + + if err := os.MkdirAll(filepath.Join(*tmpath, names[i], "data"), 0755); err != nil { + log.Fatalln("mkdir tm data error:", err) + } + + cfg := config.DefaultConfig() + cfg.SetRoot(filepath.Join(*tmpath, names[i])) + + nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) + if err != nil { + log.Fatalln("load/gen node key error:", err) + } + + fmt.Printf("Validator #%d:\nNode ID: %s\nName: %s\nPassword: %s\nAddress: %s\nMnemonic: %s\nPeer address: %s@%s:26656\n\n", i+1, nodeKey.ID(), names[i], passwords[i], acc.GetAddress(), mnemonic, nodeKey.ID(), names[i]) + peers = append(peers, fmt.Sprintf("%s@%s:26656", nodeKey.ID(), names[i])) + + me := privval.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()) + msgs = append(msgs, stakingtypes.NewMsgCreateValidator( + sdktypes.ValAddress(acc.GetAddress()), + me.GetPubKey(), + sdktypes.NewCoin(sdktypes.DefaultBondDenom, sdktypes.TokensFromConsensusPower(100)), + stakingtypes.Description{ + Moniker: acc.GetName(), + Details: "init-validator", + }, + stakingtypes.NewCommissionRates( + sdktypes.ZeroDec(), + sdktypes.ZeroDec(), + sdktypes.ZeroDec(), + ), + sdktypes.NewInt(1), + )) + } + + // generate genesis transaction + b := cosmos.NewTxBuilder(cdc, 0, 0, kb, *chainid) + signedMsg, err := b.BuildSignMsg(msgs) + if err != nil { + log.Fatalln("build sign msg error:", err) + } + // test to sign with only 1 validator + stdTx := authtypes.NewStdTx(signedMsg.Msgs, signedMsg.Fee, []authtypes.StdSignature{}, signedMsg.Memo) + for i := 0; i < *vno; i++ { + stdTx, err = b.SignStdTx(names[i], passwords[i], stdTx, true) + if err != nil { + log.Fatalln("sign msg create validator error:", err) + } + } + validatorTx := string(cdc.MustMarshalJSON(stdTx)) + if err := ioutil.WriteFile(*gentxfilepath, []byte(validatorTx), 0644); err != nil { + log.Fatalln("error during writing genesis tx file:", err) + } + + // peers file + if err := ioutil.WriteFile(*peersfilepath, []byte(strings.Join(peers, ",")), 0644); err != nil { + log.Fatalln("error during writing peers file:", err) + } +} diff --git a/internal/tools/gen_cosmos_account/main.go b/internal/tools/gen_cosmos_account/main.go deleted file mode 100644 index bebfb9e2d..000000000 --- a/internal/tools/gen_cosmos_account/main.go +++ /dev/null @@ -1,68 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "log" - - bip39 "github.com/cosmos/go-bip39" - "github.com/mesg-foundation/engine/cosmos" - "github.com/tendermint/tendermint/crypto/secp256k1" -) - -const mnemonicEntropySize = 256 - -func main() { - name := flag.String("name", "", "") - password := flag.String("password", "", "") - kbpath := flag.String("kbpath", "", "") - flag.Parse() - - if *name == "" { - log.Fatalln("name required") - } - - if *password == "" { - log.Fatalln("password required") - } - - if *kbpath == "" { - log.Fatalln("kbpath required") - } - - kb, err := cosmos.NewKeybase(*kbpath) - if err != nil { - log.Fatalln("creating keybase error:", err) - } - - // if account exists do not create it - if info, _ := kb.Get(*name); info != nil { - return - } - - // read entropy seed straight from crypto.Rand and convert to mnemonic - entropySeed, err := bip39.NewEntropy(mnemonicEntropySize) - if err != nil { - log.Fatalln(err) - } - - mnemonic, err := bip39.NewMnemonic(entropySeed) - if err != nil { - log.Fatalln(err) - } - - acc, err := kb.CreateAccount(*name, mnemonic, "", *password, 0, 0) - if err != nil { - log.Fatalln(err) - } - pubkey := acc.GetPubKey().(secp256k1.PubKeySecp256k1) - - fmt.Printf(`MESG_COSMOS_ACCOUNT_PUBKEY=%X -MESG_COSMOS_ACCOUNT_ADDRESS=%s -MESG_COSMOS_ACCOUNT_MNEMONIC="%s" -`, - pubkey[:], - acc.GetAddress(), - mnemonic, - ) -} diff --git a/internal/tools/gen_genesis_validator/main.go b/internal/tools/gen_genesis_validator/main.go deleted file mode 100644 index 597ffd8da..000000000 --- a/internal/tools/gen_genesis_validator/main.go +++ /dev/null @@ -1,90 +0,0 @@ -package main - -import ( - "encoding/hex" - "flag" - "fmt" - "log" - - "github.com/cosmos/cosmos-sdk/codec" - sdktypes "github.com/cosmos/cosmos-sdk/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/mesg-foundation/engine/cosmos" - "github.com/tendermint/tendermint/crypto/ed25519" -) - -func main() { - name := flag.String("name", "", "") - password := flag.String("password", "", "") - chainid := flag.String("chain-id", "", "") - pubkey := flag.String("pubkey", "", "") - kbpath := flag.String("kbpath", "", "") - flag.Parse() - - if *name == "" { - log.Fatalln("flag name required") - } - - if *password == "" { - log.Fatalln("flag password required") - } - - if *chainid == "" { - log.Fatalln("flag chain-id required") - } - - if *pubkey == "" { - log.Fatalln("flag pubkey required") - } - - if *kbpath == "" { - log.Fatalln("flag kbpath required") - } - - dec, err := hex.DecodeString(*pubkey) - if err != nil { - log.Fatalln("validator public key decode error:", err) - } - - var validatorPubKey ed25519.PubKeyEd25519 - copy(validatorPubKey[:], dec) - - kb, err := cosmos.NewKeybase(*kbpath) - if err != nil { - log.Fatalln("creating keybase error:", err) - } - - // fetch account - account, err := kb.Get(*name) - if err != nil { - log.Fatalln("getting user from keybase error:", err) - } - - msg := stakingtypes.NewMsgCreateValidator( - sdktypes.ValAddress(account.GetAddress()), - validatorPubKey, - sdktypes.NewCoin(sdktypes.DefaultBondDenom, sdktypes.TokensFromConsensusPower(100)), - stakingtypes.Description{ - Moniker: *name, - Details: "create-first-validator", - }, - stakingtypes.NewCommissionRates( - sdktypes.ZeroDec(), - sdktypes.ZeroDec(), - sdktypes.ZeroDec(), - ), - sdktypes.NewInt(1), - ) - - cdc := codec.New() - codec.RegisterCrypto(cdc) - sdktypes.RegisterCodec(cdc) - stakingtypes.RegisterCodec(cdc) - - tx, err := cosmos.NewTxBuilder(cdc, 0, 0, kb, *chainid).BuildAndSignStdTx(msg, *name, *password) - if err != nil { - log.Fatalln("sign msg create validator error:", err) - } - - fmt.Printf("MESG_COSMOS_GENESISVALIDATORTX='%s'\n", string(cdc.MustMarshalJSON(tx))) -} diff --git a/internal/tools/gen_tm_identity/main.go b/internal/tools/gen_tm_identity/main.go deleted file mode 100644 index eb8cf5bb2..000000000 --- a/internal/tools/gen_tm_identity/main.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "os" - "path/filepath" - - "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/crypto/ed25519" - "github.com/tendermint/tendermint/p2p" - "github.com/tendermint/tendermint/privval" -) - -func main() { - path := flag.String("path", "", "") - name := flag.String("name", "", "") - port := flag.String("port", "", "") - - flag.Parse() - - if path == nil || *path == "" { - panic("path is required") - } - - if name == nil || *name == "" { - panic("name is required") - } - - if port == nil || *port == "" { - panic("port is required") - } - - if err := os.MkdirAll(filepath.Join(*path, "config"), 0755); err != nil { - panic(err) - } - if err := os.MkdirAll(filepath.Join(*path, "data"), 0755); err != nil { - panic(err) - } - - cfg := config.DefaultConfig() - cfg.SetRoot(*path) - - nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) - if err != nil { - panic(err) - } - fmt.Printf("NODE_PUBKEY=%s@%s:%s\n", nodeKey.ID(), *name, *port) - - me := privval.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()) - pk := me.GetPubKey().(ed25519.PubKeyEd25519) - fmt.Printf("VALIDATOR_PUBKEY=%X\n", pk[:]) -} From b4a147681615f699e1828b3a00494d4562024ee0 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 2 Oct 2019 18:23:29 +0700 Subject: [PATCH 2/5] Update dev script to use new gen-genesis script --- .gitignore | 5 ++-- dev | 67 ++++++++++++++++++++-------------------------- sdk/account/sdk.go | 2 ++ 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index ac5e30899..14f2ea36b 100644 --- a/.gitignore +++ b/.gitignore @@ -23,9 +23,8 @@ node_modules vendor tmp-systemservices -# tendermint/cosmos -.tendermint-validator/ -.cosmos-keybase/ +# genesis +.genesis # gogo extension gogo/ diff --git a/dev b/dev index 828a0c941..34708024f 100755 --- a/dev +++ b/dev @@ -2,57 +2,46 @@ set -e -DIR=$(pwd) - -# Flags -if [[ $* == *--exp* ]]; then - EXPERIMENTAL_FLAGS+=" --experimental" -fi - # mesg config variables -MESG_NAME=${MESG_NAME:-"engine"} +MESG_NAME=${MESG_NAME:-"bob"} MESG_PATH=${MESG_PATH:-"$HOME/.mesg"} MESG_LOG_FORMAT=${MESG_LOG_FORMAT:-"text"} MESG_LOG_FORCECOLORS=${MESG_LOG_FORCECOLORS:-"false"} MESG_LOG_LEVEL=${MESG_LOG_LEVEL:-"debug"} MESG_SERVER_PORT=${MESG_SERVER_PORT:-"50052"} +MESG_VALIDATOR_NUMBER=${MESG_VALIDATOR_NUMBER:-"1"} + # network to connect multiple engine instances -TENDERMINT_NETWORK="mesg-tendermint" -TENDERMINT_HOME_CONFIG="$MESG_PATH/tendermint" -TENDERMINT_NETWORK_ALIAS=${TENDERMINT_NETWORK_ALIAS:-$MESG_NAME} -TENDERMINT_VALIDATOR_NAME=${TENDERMINT_VALIDATOR_NAME:-"validator"} -TENDERMINT_VALIDATOR_PORT=${TENDERMINT_VALIDATOR_PORT:-"26656"} -TENDERMINT_VALIDATOR_PATH=${TENDERMINT_VALIDATOR_PATH:-".tendermint-validator"} +MESG_TENDERMINT_NETWORK="mesg-tendermint" +MESG_TENDERMINT_HOME_CONFIG="$MESG_PATH/tendermint" +MESG_TENDERMINT_VALIDATOR_PORT=${MESG_TENDERMINT_VALIDATOR_PORT:-"26656"} # to delete or should be passed to engine config to start multiple port at the same time maybe +MESG_TENDERMINT_VALIDATOR_PATH=${MESG_TENDERMINT_VALIDATOR_PATH:-".genesis/tendermint-validator"} MESG_COSMOS_HOME_CONFIG="$MESG_PATH/cosmos" MESG_COSMOS_CHAINID=${MESG_COSMOS_CHAINID:-"mesg-chain"} -MESG_COSMOS_GENESISACCOUNT_NAME=${MESG_COSMOS_GENESISACCOUNT_NAME:-"bob"} -MESG_COSMOS_GENESISACCOUNT_PASSWORD=${MESG_COSMOS_GENESISACCOUNT_PASSWORD:-"pass"} -MESG_COSMOS_KEYBASE_PATH=${MESG_COSMOS_KEYBASE_PATH:-".cosmos-keybase"} +MESG_COSMOS_KEYBASE_PATH=${MESG_COSMOS_KEYBASE_PATH:-".genesis/cosmos-keybase"} MESG_COSMOS_GENESISTIME=${MESG_COSMOS_GENESISTIME:-"2019-01-01T00:00:00Z"} +MESG_COSMSOS_GENESISTXFILEPATH=${MESG_COSMSOS_GENESISTXFILEPATH:-".genesis/genesistx.json"} +MESG_COSMSOS_PEERSFILEPATH=${MESG_COSMSOS_PEERSFILEPATH:-".genesis/peers"} -# Init and read validator identity -eval $(go run internal/tools/gen_cosmos_account/main.go -name=$MESG_COSMOS_GENESISACCOUNT_NAME -password=$MESG_COSMOS_GENESISACCOUNT_PASSWORD -kbpath=$MESG_COSMOS_KEYBASE_PATH) -echo "Created genenis account mnemonic (if created): $MESG_COSMOS_ACCOUNT_MNEMONIC" - -eval $(go run internal/tools/gen_tm_identity/main.go -name=$TENDERMINT_VALIDATOR_NAME -port=$TENDERMINT_VALIDATOR_PORT -path=$TENDERMINT_VALIDATOR_PATH) -MESG_COSMOS_VALIDATORPUBKEY=${MESG_COSMOS_VALIDATORPUBKEY:-$VALIDATOR_PUBKEY} - -eval $(go run internal/tools/gen_genesis_validator/main.go -name=$MESG_COSMOS_GENESISACCOUNT_NAME -password=$MESG_COSMOS_GENESISACCOUNT_PASSWORD -kbpath=$MESG_COSMOS_KEYBASE_PATH --chain-id $MESG_COSMOS_CHAINID --pubkey $MESG_COSMOS_VALIDATORPUBKEY) - -MESG_TENDERMINT_P2P_PERSISTENTPEERS=${MESG_TENDERMINT_P2P_PERSISTENTPEERS:-$NODE_PUBKEY} +if [[ $* == *--gen-genesis* ]]; then + echo "Generating new genesis..." + go run internal/tools/gen-genesis/main.go --co-kbpath $MESG_COSMOS_KEYBASE_PATH --tm-path $MESG_TENDERMINT_VALIDATOR_PATH --chain-id $MESG_COSMOS_CHAINID --gentx-filepath $MESG_COSMSOS_GENESISTXFILEPATH --peers-filepath $MESG_COSMSOS_PEERSFILEPATH --vno $MESG_VALIDATOR_NUMBER +fi +MESG_COSMOS_GENESISVALIDATORTX=$(cat $MESG_COSMSOS_GENESISTXFILEPATH) +MESG_TENDERMINT_P2P_PERSISTENTPEERS=$(cat $MESG_COSMSOS_PEERSFILEPATH) # Setup the validator private keys if [[ $* == *--validator* ]]; then - echo "Validator flag passed. Copy validator private keys to $TENDERMINT_HOME_CONFIG" - mkdir -p $TENDERMINT_HOME_CONFIG - rsync -a $TENDERMINT_VALIDATOR_PATH/ $TENDERMINT_HOME_CONFIG - TENDERMINT_NETWORK_ALIAS=$TENDERMINT_VALIDATOR_NAME - TENDERMINT_VALIDATOR_PORT_PUBLISH="--publish $TENDERMINT_VALIDATOR_PORT:$TENDERMINT_VALIDATOR_PORT" + echo "Validator flag passed. Copy validator private keys to $MESG_TENDERMINT_HOME_CONFIG" + mkdir -p $MESG_TENDERMINT_HOME_CONFIG + rsync -a $MESG_TENDERMINT_VALIDATOR_PATH/$MESG_NAME/ $MESG_TENDERMINT_HOME_CONFIG + MESG_TENDERMINT_VALIDATOR_PORT_PUBLISH="--publish $MESG_TENDERMINT_VALIDATOR_PORT:26656" fi +# TODO: to remove when account importation is implemented if [[ $* == *--genesis_validator ]]; then echo "Genesis validator flag passed. Copy genesis account private keys to $MESG_COSMOS_HOME_CONFIG" mkdir -p $MESG_COSMOS_HOME_CONFIG @@ -67,7 +56,7 @@ function onexit { echo -e "\nshutting down, please wait..." docker_service_remove "$MESG_NAME" docker_network_remove "$MESG_NAME" - docker_network_remove "$TENDERMINT_NETWORK" + docker_network_remove "$MESG_TENDERMINT_NETWORK" } function docker_service_remove { @@ -131,10 +120,12 @@ if ! docker_network_exist "$MESG_NAME"; then docker_network_create "$MESG_NAME" fi -if ! docker_network_exist "$TENDERMINT_NETWORK"; then - docker_network_create "$TENDERMINT_NETWORK" +if ! docker_network_exist "$MESG_TENDERMINT_NETWORK"; then + docker_network_create "$MESG_TENDERMINT_NETWORK" fi + + echo "create docker service: " docker service create \ --name $MESG_NAME \ @@ -148,15 +139,15 @@ docker service create \ --env MESG_TENDERMINT_P2P_PERSISTENTPEERS=$MESG_TENDERMINT_P2P_PERSISTENTPEERS \ --env MESG_TENDERMINT_P2P_EXTERNALADDRESS=$MESG_TENDERMINT_P2P_EXTERNALADDRESS \ --env MESG_COSMOS_CHAINID=$MESG_COSMOS_CHAINID \ - --env MESG_COSMOS_VALIDATORPUBKEY=$MESG_COSMOS_VALIDATORPUBKEY \ --env MESG_COSMOS_GENESISVALIDATORTX=$MESG_COSMOS_GENESISVALIDATORTX \ --env MESG_COSMOS_GENESISTIME=$MESG_COSMOS_GENESISTIME \ --mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \ --mount type=bind,source=$MESG_PATH,destination=/root/.mesg \ --network $MESG_NAME \ - --network name=$TENDERMINT_NETWORK,alias=$TENDERMINT_NETWORK_ALIAS \ + --network name=$MESG_TENDERMINT_NETWORK \ --publish $MESG_SERVER_PORT:50052 \ - $TENDERMINT_VALIDATOR_PORT_PUBLISH \ + $MESG_TENDERMINT_VALIDATOR_PORT_PUBLISH \ mesg/engine:$VERSION ./engine $EXPERIMENTAL_FLAGS + docker service logs --follow --raw $MESG_NAME diff --git a/sdk/account/sdk.go b/sdk/account/sdk.go index 65a34507d..bbe0af555 100644 --- a/sdk/account/sdk.go +++ b/sdk/account/sdk.go @@ -21,6 +21,8 @@ func NewSDK(kb *cosmos.Keybase) *SDK { // Create generates a new mnemonic and its associated account from a name and password. func (s *SDK) Create(name, password string) (address *account.Account, mnemonic string, err error) { // TODO: should throw error if name already exist + // acc, err := kb.Get(name) + // if keyerror.IsErrKeyNotFound(err) { mnemonic, err = s.kb.NewMnemonic() if err != nil { return nil, "", err From 27b0fb8d6d6fa2046ecb0abaa8b897fc4d75d473 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 2 Oct 2019 18:24:10 +0700 Subject: [PATCH 3/5] Generate account for each validators of the genesis transaction --- cosmos/node.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cosmos/node.go b/cosmos/node.go index ffae0213b..53b987ad1 100644 --- a/cosmos/node.go +++ b/cosmos/node.go @@ -48,15 +48,18 @@ func NewNode(app *App, cfg *tmconfig.Config, ccfg *config.CosmosConfig) (*node.N } func createAppState(defaultGenesisŚtate map[string]json.RawMessage, cdc *codec.Codec, signedStdTx authtypes.StdTx) (map[string]json.RawMessage, error) { - signers := signedStdTx.GetSigners() - address := signers[len(signers)-1] - stakes := sdktypes.NewCoin(sdktypes.DefaultBondDenom, sdktypes.NewInt(100000000)) - genAcc := genaccounts.NewGenesisAccountRaw(address, sdktypes.NewCoins(stakes), sdktypes.NewCoins(), 0, 0, "", "") - if err := genAcc.Validate(); err != nil { - return nil, err + genAccs := []genaccounts.GenesisAccount{} + + for _, signer := range signedStdTx.GetSigners() { + stakes := sdktypes.NewCoin(sdktypes.DefaultBondDenom, sdktypes.NewInt(100000000)) + genAcc := genaccounts.NewGenesisAccountRaw(signer, sdktypes.NewCoins(stakes), sdktypes.NewCoins(), 0, 0, "", "") + if err := genAcc.Validate(); err != nil { + return nil, err + } + genAccs = append(genAccs, genAcc) } - genstate, err := cdc.MarshalJSON(genaccounts.GenesisState([]genaccounts.GenesisAccount{genAcc})) + genstate, err := cdc.MarshalJSON(genaccounts.GenesisState(genAccs)) if err != nil { return nil, err } From e030d00dc5ef5bfd2b5f0eb7bd2242e8a3777157 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 2 Oct 2019 18:24:39 +0700 Subject: [PATCH 4/5] Remove ValidatorPubKey from config --- config/config.go | 33 ++++----------------------------- config/config_test.go | 4 ---- 2 files changed, 4 insertions(+), 33 deletions(-) diff --git a/config/config.go b/config/config.go index 66b5b6e54..2df8379fd 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,6 @@ package config import ( - "encoding/hex" "fmt" "os" "path/filepath" @@ -16,7 +15,6 @@ import ( homedir "github.com/mitchellh/go-homedir" "github.com/sirupsen/logrus" tmconfig "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/crypto/ed25519" "gopkg.in/go-playground/validator.v9" ) @@ -61,11 +59,10 @@ type Config struct { // CosmosConfig is the struct to hold cosmos related configs. type CosmosConfig struct { - Path string `validate:"required"` - ChainID string `validate:"required"` - GenesisTime time.Time `validate:"required"` - GenesisValidatorTx StdTx `validate:"required"` - ValidatorPubKey PubKeyEd25519 `validate:"required"` + Path string `validate:"required"` + ChainID string `validate:"required"` + GenesisTime time.Time `validate:"required"` + GenesisValidatorTx StdTx `validate:"required"` } // Default creates a new config with default values. @@ -158,28 +155,6 @@ func (c *Config) Validate() error { return validator.New().Struct(c) } -// PubKeyEd25519 is type used to parse value provided by envconfig. -type PubKeyEd25519 ed25519.PubKeyEd25519 - -// Decode parses string value as hex ed25519 key. -func (key *PubKeyEd25519) Decode(value string) error { - if value == "" { - return fmt.Errorf("validator public key is empty") - } - - dec, err := hex.DecodeString(value) - if err != nil { - return fmt.Errorf("validator public key decode error: %s", err) - } - - if len(dec) != ed25519.PubKeyEd25519Size { - return fmt.Errorf("validator public key %s has invalid size", value) - } - - copy(key[:], dec) - return nil -} - // StdTx is type used to parse cosmos tx value provided by envconfig. type StdTx authtypes.StdTx diff --git a/config/config_test.go b/config/config_test.go index 5dd5e945f..edbd9499e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -7,7 +7,6 @@ import ( homedir "github.com/mitchellh/go-homedir" "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/crypto/ed25519" ) func TestDefaultValue(t *testing.T) { @@ -31,7 +30,6 @@ func TestLoad(t *testing.T) { "MESG_LOG_LEVEL": "", "MESG_LOG_FORCECOLORS": "", "MESG_TENDERMINT_P2P_PERSISTENTPEERS": "", - "MESG_COSMOS_VALIDATORPUBKEY": "", "MESG_COSMOS_GENESISVALIDATORTX": "", } for key := range snapsnot { @@ -48,7 +46,6 @@ func TestLoad(t *testing.T) { os.Setenv("MESG_LOG_LEVEL", "test_log_level") os.Setenv("MESG_LOG_FORCECOLORS", "true") os.Setenv("MESG_TENDERMINT_P2P_PERSISTENTPEERS", "localhost") - os.Setenv("MESG_COSMOS_VALIDATORPUBKEY", "0000000000000000000000000000000000000000000000000000000000000001") os.Setenv("MESG_COSMOS_GENESISVALIDATORTX", `{"msg":[{"type":"cosmos-sdk/MsgCreateValidator","value":{"description":{"moniker":"bob","identity":"","website":"","details":"create-first-validator"},"commission":{"rate":"0.000000000000000000","max_rate":"0.000000000000000000","max_change_rate":"0.000000000000000000"},"min_self_delegation":"1","delegator_address":"cosmos1eav92wlgjjwkutl0s0u7nvdgc4m06zxtzdgwc0","validator_address":"cosmosvaloper1eav92wlgjjwkutl0s0u7nvdgc4m06zxt8eum5u","pubkey":"cosmosvalconspub1zcjduepqq0a87y3pur6vvzyp99t92me2zyxywz46kyq5vt7x2n4g987acmxszqey9p","value":{"denom":"stake","amount":"100000000"}}}],"fee":{"amount":[],"gas":"200000"},"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"A/p/EiHg9MYIgSlWVW8qEQxHCrqxAUYvxlTqgp/dxs3c"},"signature":"ehLZyLIARYDfYSolk9GtFb48g/5Mp33yHoIVuZZ2p78UkVABIKnFkfteaTqR0R06dTfbtcl2uTMhku0eT35Org=="}],"memo":"memo"}`) c, _ := Default() @@ -58,7 +55,6 @@ func TestLoad(t *testing.T) { require.Equal(t, "test_log_level", c.Log.Level) require.Equal(t, true, c.Log.ForceColors) require.Equal(t, "localhost", c.Tendermint.P2P.PersistentPeers) - require.Equal(t, "PubKeyEd25519{0000000000000000000000000000000000000000000000000000000000000001}", ed25519.PubKeyEd25519(c.Cosmos.ValidatorPubKey).String()) require.Equal(t, "memo", c.Cosmos.GenesisValidatorTx.Memo) require.Equal(t, uint64(200000), c.Cosmos.GenesisValidatorTx.Fee.Gas) } From 28a3650b89878565724b96ac4500228e7b5e62dc Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 2 Oct 2019 18:30:32 +0700 Subject: [PATCH 5/5] rename dev script flag genesis_validator to genesis_account --- dev | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dev b/dev index 34708024f..d5f688bd2 100755 --- a/dev +++ b/dev @@ -26,7 +26,7 @@ MESG_COSMSOS_GENESISTXFILEPATH=${MESG_COSMSOS_GENESISTXFILEPATH:-".genesis/genes MESG_COSMSOS_PEERSFILEPATH=${MESG_COSMSOS_PEERSFILEPATH:-".genesis/peers"} if [[ $* == *--gen-genesis* ]]; then - echo "Generating new genesis..." + echo "Flag --gen-genesis passed. Generating new genesis..." go run internal/tools/gen-genesis/main.go --co-kbpath $MESG_COSMOS_KEYBASE_PATH --tm-path $MESG_TENDERMINT_VALIDATOR_PATH --chain-id $MESG_COSMOS_CHAINID --gentx-filepath $MESG_COSMSOS_GENESISTXFILEPATH --peers-filepath $MESG_COSMSOS_PEERSFILEPATH --vno $MESG_VALIDATOR_NUMBER fi @@ -35,15 +35,15 @@ MESG_TENDERMINT_P2P_PERSISTENTPEERS=$(cat $MESG_COSMSOS_PEERSFILEPATH) # Setup the validator private keys if [[ $* == *--validator* ]]; then - echo "Validator flag passed. Copy validator private keys to $MESG_TENDERMINT_HOME_CONFIG" + echo "Flag --validator passed. Copy validator private keys to $MESG_TENDERMINT_HOME_CONFIG" mkdir -p $MESG_TENDERMINT_HOME_CONFIG rsync -a $MESG_TENDERMINT_VALIDATOR_PATH/$MESG_NAME/ $MESG_TENDERMINT_HOME_CONFIG MESG_TENDERMINT_VALIDATOR_PORT_PUBLISH="--publish $MESG_TENDERMINT_VALIDATOR_PORT:26656" fi # TODO: to remove when account importation is implemented -if [[ $* == *--genesis_validator ]]; then - echo "Genesis validator flag passed. Copy genesis account private keys to $MESG_COSMOS_HOME_CONFIG" +if [[ $* == *--genesis_account ]]; then + echo "Flag --genesis_account passed. Copy genesis account private keys to $MESG_COSMOS_HOME_CONFIG" mkdir -p $MESG_COSMOS_HOME_CONFIG rsync -a $MESG_COSMOS_KEYBASE_PATH/ $MESG_COSMOS_HOME_CONFIG fi