Skip to content

Commit

Permalink
Merge pull request #1391 from mesg-foundation/feature/generate-genesi…
Browse files Browse the repository at this point in the history
…s-script

Improve generate genesis script
  • Loading branch information
Nicolas Mahé authored Oct 3, 2019
2 parents ea72c6d + 28a3650 commit 7e3a35a
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 294 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ node_modules
vendor
tmp-systemservices

# tendermint/cosmos
.tendermint-validator/
.cosmos-keybase/
# genesis
.genesis

# gogo extension
gogo/
33 changes: 4 additions & 29 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"encoding/hex"
"fmt"
"os"
"path/filepath"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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)
}
Expand Down
17 changes: 10 additions & 7 deletions cosmos/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
71 changes: 31 additions & 40 deletions dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,48 @@

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 "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

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 "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

if [[ $* == *--genesis_validator ]]; then
echo "Genesis validator flag passed. Copy genesis account private keys to $MESG_COSMOS_HOME_CONFIG"
# TODO: to remove when account importation is implemented
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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 \
Expand All @@ -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
Loading

0 comments on commit 7e3a35a

Please sign in to comment.