From cfa8eade4a7f70fc083d1236f0deb08d04d9fcd4 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 12:01:31 +0700 Subject: [PATCH 01/10] Add min gas price to config --- config/config.go | 13 ++++++++++--- config/config_test.go | 3 +++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 76ae6706c..0465c47d7 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,7 @@ import ( "path/filepath" "time" + sdkcosmos "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/go-bip39" homedir "github.com/mitchellh/go-homedir" "github.com/sirupsen/logrus" @@ -45,6 +46,7 @@ type Config struct { Cosmos struct { RelativePath string `validate:"required"` + MinGasPrices string `validate:"required"` } DevGenesis struct { @@ -87,6 +89,7 @@ func defaultConfig() (*Config, error) { c.Tendermint.Config.Instrumentation.PrometheusListenAddr = "0.0.0.0:26660" c.Cosmos.RelativePath = "cosmos" + c.Cosmos.MinGasPrices = "1.0mesg" c.DevGenesis.ChainID = "mesg-dev-chain" @@ -156,13 +159,17 @@ func (c *Config) prepare() error { // validate checks values and return an error if any validation failed. func (c *Config) validate() error { if _, err := logrus.ParseLevel(c.Log.Level); err != nil { - return fmt.Errorf("config.Log.Level error: %w", err) + return fmt.Errorf("config log.level error: %w", err) } if c.Account.Mnemonic != "" && !bip39.IsMnemonicValid(c.Account.Mnemonic) { - return fmt.Errorf("config.Account.Mnemonic error: mnemonic is not valid") + return fmt.Errorf("config account.mnemonic error: mnemonic is not valid") } if err := c.Tendermint.Config.ValidateBasic(); err != nil { - return fmt.Errorf("config.Tendermint error: %w", err) + return fmt.Errorf("config tendermint error: %w", err) + } + + if _, err := sdkcosmos.ParseDecCoins(c.Cosmos.MinGasPrices); err != nil { + return fmt.Errorf("config cosmos.mingasprices error: %w", err) } return validator.New().Struct(c) diff --git a/config/config_test.go b/config/config_test.go index af51a7265..acd90ea2c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -60,6 +60,8 @@ log: forcecolors: true account: mnemonic: glimpse upon body vast economy give taxi yellow rabbit come click ranch chronic hammer sport near rotate charge lumber chicken cloud base thing forum +cosmos: + mingasprices: 2.0mesg tendermint: config: consensus: @@ -74,5 +76,6 @@ tendermint: require.Equal(t, "tcp://0.0.0.0:26657", c.Tendermint.Config.RPC.ListenAddress) require.Equal(t, tempPath, c.Path) require.Equal(t, "engine", c.Name) + require.Equal(t, "2.0mesg", c.Cosmos.MinGasPrices) }) } From 2b85941ff012da6237a1c26822b7974388b67fec Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 12:03:42 +0700 Subject: [PATCH 02/10] Pass min gas price from config to cosmos app --- core/main.go | 2 +- cosmos/appfactory.go | 4 ++-- internal/tools/gen-genesis/main.go | 8 +++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/main.go b/core/main.go index 86995f715..0bcc7389c 100644 --- a/core/main.go +++ b/core/main.go @@ -136,7 +136,7 @@ func main() { cosmos.CustomizeConfig() // TODO: rename NewAppFactory to something else - appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db) + appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db, cfg.Cosmos.MinGasPrices) // register the backend modules to the app factory. // TODO: this is a mandatory call so it should return a new types required by cosmos.NewApp diff --git a/cosmos/appfactory.go b/cosmos/appfactory.go index b6f2ef419..17318f6ce 100644 --- a/cosmos/appfactory.go +++ b/cosmos/appfactory.go @@ -27,12 +27,12 @@ type AppFactory struct { } // NewAppFactory returns a new AppFactory. -func NewAppFactory(logger log.Logger, db dbm.DB) *AppFactory { +func NewAppFactory(logger log.Logger, db dbm.DB, minGasPrices string) *AppFactory { sdk.RegisterCodec(codec.Codec) cosmoscodec.RegisterCrypto(codec.Codec) return &AppFactory{ - baseApp: bam.NewBaseApp("engine", logger, db, auth.DefaultTxDecoder(codec.Codec)), + baseApp: bam.NewBaseApp("engine", logger, db, auth.DefaultTxDecoder(codec.Codec), baseapp.SetMinGasPrices(minGasPrices)), modules: []module.AppModule{}, storeKeys: map[string]*sdk.KVStoreKey{ bam.MainStoreKey: sdk.NewKVStoreKey(bam.MainStoreKey), diff --git a/internal/tools/gen-genesis/main.go b/internal/tools/gen-genesis/main.go index 342c07d08..09468eb48 100644 --- a/internal/tools/gen-genesis/main.go +++ b/internal/tools/gen-genesis/main.go @@ -11,6 +11,7 @@ import ( "strings" "time" + enginecfg "github.com/mesg-foundation/engine/config" "github.com/mesg-foundation/engine/cosmos" "github.com/mesg-foundation/engine/logger" enginesdk "github.com/mesg-foundation/engine/sdk" @@ -55,7 +56,12 @@ func main() { if err != nil { logrus.Fatalln(err) } - appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db) + + ecfg, err := enginecfg.New() + if err != nil { + logrus.Fatalln(err) + } + appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db, ecfg.Cosmos.MinGasPrices) // register the backend modules to the app factory. enginesdk.NewBackend(appFactory) From 53468cb0de2b45f1e22d995182f5ef9d506922b9 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 12:44:06 +0700 Subject: [PATCH 03/10] Pass min gas price to txbuilder --- core/main.go | 5 +++-- cosmos/client.go | 30 +++++++++++++++++++----------- cosmos/genesis.go | 15 ++++++++++----- cosmos/genesis_test.go | 3 ++- cosmos/txbuilder.go | 6 +++--- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/core/main.go b/core/main.go index 0bcc7389c..8e9489b2c 100644 --- a/core/main.go +++ b/core/main.go @@ -105,7 +105,7 @@ func loadOrGenDevGenesis(app *cosmos.App, kb *cosmos.Keybase, cfg *config.Config "nodeID": validator.NodeID, "peer": fmt.Sprintf("%s@%s:26656", validator.NodeID, validator.Name), }).Warnln("Validator") - return cosmos.GenGenesis(kb, app.DefaultGenesis(), cfg.DevGenesis.ChainID, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) + return cosmos.GenGenesis(kb, app.DefaultGenesis(), cfg.DevGenesis.ChainID, cfg.Cosmos.MinGasPrices, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) } func main() { @@ -159,6 +159,7 @@ func main() { if err != nil { logrus.WithField("module", "main").Fatalln(err) } + logrus.WithField("address", acc.GetAddress().String()).Info("engine account") // load or gen genesis genesis, err := loadOrGenDevGenesis(app, kb, cfg) @@ -173,7 +174,7 @@ func main() { } // create cosmos client - client := cosmos.NewClient(node, kb, genesis.ChainID, cfg.Account.Name, cfg.Account.Password) + client := cosmos.NewClient(node, kb, genesis.ChainID, cfg.Account.Name, cfg.Account.Password, cfg.Cosmos.MinGasPrices) // init sdk sdk := enginesdk.New(client, kb, container, cfg.Name, strconv.Itoa(port), cfg.IpfsEndpoint) diff --git a/cosmos/client.go b/cosmos/client.go index 60c3a34ee..1e32977bb 100644 --- a/cosmos/client.go +++ b/cosmos/client.go @@ -10,6 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys" sdktypes "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" + authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/mesg-foundation/engine/codec" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/x/xreflect" @@ -24,10 +25,11 @@ import ( // Client is a tendermint client with helper functions. type Client struct { *rpcclient.Local - kb keys.Keybase - chainID string - accName string - accPassword string + kb keys.Keybase + chainID string + accName string + accPassword string + minGasPrices string // Local state acc auth.Account @@ -36,13 +38,14 @@ type Client struct { } // NewClient returns a rpc tendermint client. -func NewClient(node *node.Node, kb keys.Keybase, chainID, accName, accPassword string) *Client { +func NewClient(node *node.Node, kb keys.Keybase, chainID, accName, accPassword, minGasPrices string) *Client { return &Client{ - Local: rpcclient.NewLocal(node), - kb: kb, - chainID: chainID, - accName: accName, - accPassword: accPassword, + Local: rpcclient.NewLocal(node), + kb: kb, + chainID: chainID, + accName: accName, + accPassword: accPassword, + minGasPrices: minGasPrices, } } @@ -204,7 +207,12 @@ func (c *Client) sign(msg sdktypes.Msg) (tenderminttypes.Tx, error) { sequence := acc.GetSequence() acc.SetSequence(acc.GetSequence() + 1) - txBuilder := NewTxBuilder(sequence, c.kb, c.chainID) + minGasPrices, err := sdktypes.ParseDecCoins(c.minGasPrices) + if err != nil { + return nil, err + } + + txBuilder := NewTxBuilder(sequence, c.kb, c.chainID, minGasPrices) signedTx, err := txBuilder.BuildAndSignStdTx(msg, c.accName, c.accPassword) if err != nil { return nil, err diff --git a/cosmos/genesis.go b/cosmos/genesis.go index 5d911de3d..013555864 100644 --- a/cosmos/genesis.go +++ b/cosmos/genesis.go @@ -53,7 +53,7 @@ func LoadGenesis(genesisFile string) (*tmtypes.GenesisDoc, error) { } // GenGenesis generates a new genesis and save it. -func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, chainID string, genesisFile string, validators []GenesisValidator) (*tmtypes.GenesisDoc, error) { +func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, chainID, minGasPrices, genesisFile string, validators []GenesisValidator) (*tmtypes.GenesisDoc, error) { msgs := []sdktypes.Msg{} for _, validator := range validators { // get account @@ -66,7 +66,7 @@ func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, ch } // generate genesis transaction sequence := uint64(0) - b := NewTxBuilder(sequence, kb, chainID) + b := NewTxBuilder(sequence, kb, chainID, sdktypes.DecCoins{}) signedMsg, err := b.BuildSignMsg(msgs) if err != nil { return nil, err @@ -79,7 +79,11 @@ func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, ch } } // generate genesis - appState, err := genGenesisAppState(defaultGenesisŚtate, validatorTx) + minGasPricesP, err := sdktypes.ParseDecCoins(minGasPrices) + if err != nil { + return nil, err + } + appState, err := genGenesisAppState(defaultGenesisŚtate, validatorTx, minGasPricesP) if err != nil { return nil, err } @@ -108,11 +112,12 @@ func genGenesisDoc(appState map[string]json.RawMessage, chainID string, genesisT return genesis, genesis.ValidateAndComplete() } -func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx) (map[string]json.RawMessage, error) { +func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx, minGasPrices sdktypes.DecCoins) (map[string]json.RawMessage, error) { 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, "", "") + gasCoins, _ := minGasPrices.MulDec(sdktypes.NewDec(1000000000)).TruncateDecimal() + genAcc := genaccounts.NewGenesisAccountRaw(signer, gasCoins.Add(sdktypes.NewCoins(stakes)), sdktypes.NewCoins(), 0, 0, "", "") if err := genAcc.Validate(); err != nil { return nil, err } diff --git a/cosmos/genesis_test.go b/cosmos/genesis_test.go index d959d97c7..7299e8b63 100644 --- a/cosmos/genesis_test.go +++ b/cosmos/genesis_test.go @@ -28,6 +28,7 @@ func TestGenesis(t *testing.T) { // variables var ( chainID = "test-chainID" + minGasPrices = "1.0mesg" name = "name" password = "pass" privValidatorKeyFile = filepath.Join(path, "privValidatorKeyFile.json") @@ -57,7 +58,7 @@ func TestGenesis(t *testing.T) { require.False(t, GenesisExist(genesisPath)) }) t.Run("generate genesis", func(t *testing.T) { - genesis, err := GenGenesis(kb, defaultGenesisState, chainID, genesisPath, validators) + genesis, err := GenGenesis(kb, defaultGenesisState, chainID, minGasPrices, genesisPath, validators) require.NoError(t, err) require.NotEmpty(t, genesis) }) diff --git a/cosmos/txbuilder.go b/cosmos/txbuilder.go index 143478732..b0af831bc 100644 --- a/cosmos/txbuilder.go +++ b/cosmos/txbuilder.go @@ -16,19 +16,19 @@ type TxBuilder struct { } // NewTxBuilder returns a new initialized TxBuilder. -func NewTxBuilder(accSeq uint64, kb keys.Keybase, chainID string) TxBuilder { +func NewTxBuilder(accSeq uint64, kb keys.Keybase, chainID string, minGasPrices sdktypes.DecCoins) TxBuilder { return TxBuilder{ authtypes.NewTxBuilder( authutils.GetTxEncoder(codec.Codec), AccNumber, accSeq, - 1000000, + flags.DefaultGasLimit, flags.DefaultGasAdjustment, true, chainID, "", sdktypes.NewCoins(), - sdktypes.DecCoins{}, + minGasPrices, ).WithKeybase(kb), } } From afdf9146720a1f502f3d2cf27162d6a27f12b21a Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 12:44:31 +0700 Subject: [PATCH 04/10] Estimate the tx gas by simulating it. --- cosmos/client.go | 14 ++++++++++++++ cosmos/txbuilder.go | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/cosmos/client.go b/cosmos/client.go index 1e32977bb..877a4f909 100644 --- a/cosmos/client.go +++ b/cosmos/client.go @@ -213,6 +213,20 @@ func (c *Client) sign(msg sdktypes.Msg) (tenderminttypes.Tx, error) { } txBuilder := NewTxBuilder(sequence, c.kb, c.chainID, minGasPrices) + + // simulate tx to estimate the gas + if txBuilder.SimulateAndExecute() { + txBytes, err := txBuilder.BuildTxForSim([]sdktypes.Msg{msg}) + if err != nil { + return nil, err + } + _, adjusted, err := authutils.CalculateGas(c.QueryWithData, codec.Codec, txBytes, txBuilder.GasAdjustment()) + if err != nil { + return nil, err + } + txBuilder = txBuilder.WithGas(adjusted) + } + signedTx, err := txBuilder.BuildAndSignStdTx(msg, c.accName, c.accPassword) if err != nil { return nil, err diff --git a/cosmos/txbuilder.go b/cosmos/txbuilder.go index b0af831bc..85c016d11 100644 --- a/cosmos/txbuilder.go +++ b/cosmos/txbuilder.go @@ -33,6 +33,12 @@ func NewTxBuilder(accSeq uint64, kb keys.Keybase, chainID string, minGasPrices s } } +// WithGas returns a copy of the context with an updated gas. +func (b TxBuilder) WithGas(gas uint64) TxBuilder { + b.TxBuilder = b.TxBuilder.WithGas(gas) + return b +} + // BuildAndSignStdTx a signed transaction from a message. func (b TxBuilder) BuildAndSignStdTx(msg sdktypes.Msg, accountName, accountPassword string) (authtypes.StdTx, error) { signedMsg, err := b.BuildSignMsg([]sdktypes.Msg{msg}) From d428cd9336a11897344c27aded701ebf4c1762ae Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 12:58:21 +0700 Subject: [PATCH 05/10] fix config test --- config/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index acd90ea2c..30f8509a2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -61,7 +61,7 @@ log: account: mnemonic: glimpse upon body vast economy give taxi yellow rabbit come click ranch chronic hammer sport near rotate charge lumber chicken cloud base thing forum cosmos: - mingasprices: 2.0mesg + mingasprices: 2.0mesg tendermint: config: consensus: From 7a75349205462a116ee4dd5863ec4a572d13282e Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 13:10:27 +0700 Subject: [PATCH 06/10] fix gen-genesis script --- internal/tools/gen-genesis/main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/tools/gen-genesis/main.go b/internal/tools/gen-genesis/main.go index 09468eb48..3295ea68b 100644 --- a/internal/tools/gen-genesis/main.go +++ b/internal/tools/gen-genesis/main.go @@ -31,9 +31,10 @@ func randompassword() string { } var ( - validators = flag.String("validators", "engine", "list of validator names separated with a comma") - chainid = flag.String("chain-id", "mesg-chain", "chain id") - path = flag.String("path", ".genesis/", "genesis folder path") + validators = flag.String("validators", "engine", "list of validator names separated with a comma") + chainid = flag.String("chain-id", "mesg-chain", "chain id") + minGasPrices = flag.String("minGasPrices", "1.0mesg", "min gas price") + path = flag.String("path", ".genesis/", "genesis folder path") ) const ( @@ -122,7 +123,7 @@ func main() { } // generate and save genesis - _, err = cosmos.GenGenesis(kb, app.DefaultGenesis(), *chainid, filepath.Join(*path, "genesis.json"), vals) + _, err = cosmos.GenGenesis(kb, app.DefaultGenesis(), *chainid, *minGasPrices, filepath.Join(*path, "genesis.json"), vals) if err != nil { logrus.Fatalln(err) } From e125b7a0a60689074d8689f16702014c370e14bd Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 13:14:20 +0700 Subject: [PATCH 07/10] Set default min gas fee to 1attomesg --- config/config.go | 2 +- config/config_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 0465c47d7..3ccaf76fa 100644 --- a/config/config.go +++ b/config/config.go @@ -89,7 +89,7 @@ func defaultConfig() (*Config, error) { c.Tendermint.Config.Instrumentation.PrometheusListenAddr = "0.0.0.0:26660" c.Cosmos.RelativePath = "cosmos" - c.Cosmos.MinGasPrices = "1.0mesg" + c.Cosmos.MinGasPrices = "0.000000000000000001mesg" c.DevGenesis.ChainID = "mesg-dev-chain" diff --git a/config/config_test.go b/config/config_test.go index 30f8509a2..637f4fcdd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -61,7 +61,7 @@ log: account: mnemonic: glimpse upon body vast economy give taxi yellow rabbit come click ranch chronic hammer sport near rotate charge lumber chicken cloud base thing forum cosmos: - mingasprices: 2.0mesg + mingasprices: 2.0019294mesg tendermint: config: consensus: @@ -76,6 +76,6 @@ tendermint: require.Equal(t, "tcp://0.0.0.0:26657", c.Tendermint.Config.RPC.ListenAddress) require.Equal(t, tempPath, c.Path) require.Equal(t, "engine", c.Name) - require.Equal(t, "2.0mesg", c.Cosmos.MinGasPrices) + require.Equal(t, "2.0019294mesg", c.Cosmos.MinGasPrices) }) } From 2ccd096262bcdb55ee766ebb067020c074160e26 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 17:05:18 +0700 Subject: [PATCH 08/10] Switch to amesg denomination. Set gas fee to 1.0amesg. Add new dev genesis's initial balance parameter to config to give an initial balance. --- config/config.go | 11 +++++++---- core/main.go | 2 +- cosmos/genesis.go | 17 ++++++++--------- cosmos/genesis_test.go | 4 ++-- internal/tools/gen-genesis/main.go | 12 ++++++------ scripts/dev.sh | 2 +- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/config/config.go b/config/config.go index 3ccaf76fa..70c81477a 100644 --- a/config/config.go +++ b/config/config.go @@ -50,7 +50,8 @@ type Config struct { } DevGenesis struct { - ChainID string `validate:"required"` + ChainID string `validate:"required"` + InitialBalances string `validate:"required"` } Account struct { @@ -89,9 +90,10 @@ func defaultConfig() (*Config, error) { c.Tendermint.Config.Instrumentation.PrometheusListenAddr = "0.0.0.0:26660" c.Cosmos.RelativePath = "cosmos" - c.Cosmos.MinGasPrices = "0.000000000000000001mesg" + c.Cosmos.MinGasPrices = "1.0amesg" c.DevGenesis.ChainID = "mesg-dev-chain" + c.DevGenesis.InitialBalances = "1000000000000000000000000amesg" // 1 000 000 * 10^18 c.Account.Name = "engine" c.Account.Password = "pass" @@ -167,10 +169,11 @@ func (c *Config) validate() error { if err := c.Tendermint.Config.ValidateBasic(); err != nil { return fmt.Errorf("config tendermint error: %w", err) } - if _, err := sdkcosmos.ParseDecCoins(c.Cosmos.MinGasPrices); err != nil { return fmt.Errorf("config cosmos.mingasprices error: %w", err) } - + if _, err := sdkcosmos.ParseCoins(c.DevGenesis.InitialBalances); err != nil { + return fmt.Errorf("config devgenesis.initialbalances error: %w", err) + } return validator.New().Struct(c) } diff --git a/core/main.go b/core/main.go index 8e9489b2c..0c48866b0 100644 --- a/core/main.go +++ b/core/main.go @@ -105,7 +105,7 @@ func loadOrGenDevGenesis(app *cosmos.App, kb *cosmos.Keybase, cfg *config.Config "nodeID": validator.NodeID, "peer": fmt.Sprintf("%s@%s:26656", validator.NodeID, validator.Name), }).Warnln("Validator") - return cosmos.GenGenesis(kb, app.DefaultGenesis(), cfg.DevGenesis.ChainID, cfg.Cosmos.MinGasPrices, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) + return cosmos.GenGenesis(kb, app.DefaultGenesis(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator}) } func main() { diff --git a/cosmos/genesis.go b/cosmos/genesis.go index 013555864..793926a44 100644 --- a/cosmos/genesis.go +++ b/cosmos/genesis.go @@ -53,7 +53,7 @@ func LoadGenesis(genesisFile string) (*tmtypes.GenesisDoc, error) { } // GenGenesis generates a new genesis and save it. -func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, chainID, minGasPrices, genesisFile string, validators []GenesisValidator) (*tmtypes.GenesisDoc, error) { +func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, chainID, initialBalances, genesisFile string, validators []GenesisValidator) (*tmtypes.GenesisDoc, error) { msgs := []sdktypes.Msg{} for _, validator := range validators { // get account @@ -79,11 +79,7 @@ func GenGenesis(kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, ch } } // generate genesis - minGasPricesP, err := sdktypes.ParseDecCoins(minGasPrices) - if err != nil { - return nil, err - } - appState, err := genGenesisAppState(defaultGenesisŚtate, validatorTx, minGasPricesP) + appState, err := genGenesisAppState(defaultGenesisŚtate, validatorTx, initialBalances) if err != nil { return nil, err } @@ -112,12 +108,15 @@ func genGenesisDoc(appState map[string]json.RawMessage, chainID string, genesisT return genesis, genesis.ValidateAndComplete() } -func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx, minGasPrices sdktypes.DecCoins) (map[string]json.RawMessage, error) { +func genGenesisAppState(defaultGenesisŚtate map[string]json.RawMessage, signedStdTx authtypes.StdTx, initialBalances string) (map[string]json.RawMessage, error) { genAccs := []genaccounts.GenesisAccount{} for _, signer := range signedStdTx.GetSigners() { stakes := sdktypes.NewCoin(sdktypes.DefaultBondDenom, sdktypes.NewInt(100000000)) - gasCoins, _ := minGasPrices.MulDec(sdktypes.NewDec(1000000000)).TruncateDecimal() - genAcc := genaccounts.NewGenesisAccountRaw(signer, gasCoins.Add(sdktypes.NewCoins(stakes)), sdktypes.NewCoins(), 0, 0, "", "") + initialB, err := sdktypes.ParseCoin(initialBalances) + if err != nil { + return nil, err + } + genAcc := genaccounts.NewGenesisAccountRaw(signer, sdktypes.NewCoins(stakes, initialB), sdktypes.NewCoins(), 0, 0, "", "") if err := genAcc.Validate(); err != nil { return nil, err } diff --git a/cosmos/genesis_test.go b/cosmos/genesis_test.go index 7299e8b63..dfb2dd4d6 100644 --- a/cosmos/genesis_test.go +++ b/cosmos/genesis_test.go @@ -28,7 +28,7 @@ func TestGenesis(t *testing.T) { // variables var ( chainID = "test-chainID" - minGasPrices = "1.0mesg" + initialBalances = "1.0amesg" name = "name" password = "pass" privValidatorKeyFile = filepath.Join(path, "privValidatorKeyFile.json") @@ -58,7 +58,7 @@ func TestGenesis(t *testing.T) { require.False(t, GenesisExist(genesisPath)) }) t.Run("generate genesis", func(t *testing.T) { - genesis, err := GenGenesis(kb, defaultGenesisState, chainID, minGasPrices, genesisPath, validators) + genesis, err := GenGenesis(kb, defaultGenesisState, chainID, initialBalances, genesisPath, validators) require.NoError(t, err) require.NotEmpty(t, genesis) }) diff --git a/internal/tools/gen-genesis/main.go b/internal/tools/gen-genesis/main.go index 3295ea68b..ec38e6c85 100644 --- a/internal/tools/gen-genesis/main.go +++ b/internal/tools/gen-genesis/main.go @@ -31,10 +31,10 @@ func randompassword() string { } var ( - validators = flag.String("validators", "engine", "list of validator names separated with a comma") - chainid = flag.String("chain-id", "mesg-chain", "chain id") - minGasPrices = flag.String("minGasPrices", "1.0mesg", "min gas price") - path = flag.String("path", ".genesis/", "genesis folder path") + validators = flag.String("validators", "engine", "list of validator names separated with a comma") + chainid = flag.String("chain-id", "mesg-chain", "chain id") + initialBalances = flag.String("initialBalances", "1000000000amesg", "min gas price") + path = flag.String("path", ".genesis/", "genesis folder path") ) const ( @@ -62,7 +62,7 @@ func main() { if err != nil { logrus.Fatalln(err) } - appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db, ecfg.Cosmos.MinGasPrices) + appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db, ecfg.Cosmos.initialBalances) // register the backend modules to the app factory. enginesdk.NewBackend(appFactory) @@ -123,7 +123,7 @@ func main() { } // generate and save genesis - _, err = cosmos.GenGenesis(kb, app.DefaultGenesis(), *chainid, *minGasPrices, filepath.Join(*path, "genesis.json"), vals) + _, err = cosmos.GenGenesis(kb, app.DefaultGenesis(), *chainid, *initialBalances, filepath.Join(*path, "genesis.json"), vals) if err != nil { logrus.Fatalln(err) } diff --git a/scripts/dev.sh b/scripts/dev.sh index ac56dfea9..0f07e59ba 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -92,7 +92,7 @@ case $cmd in start_engine if ! $quiet; then trap onexit EXIT - docker service logs --tail 10000 --follow --raw $MESG_NAME + docker service logs --tail 1000 --follow --raw $MESG_NAME fi ;; stop) From 2031140ca403040c00c821bff19ec8fb2615e1a9 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 17:07:20 +0700 Subject: [PATCH 09/10] fix genesis test --- cosmos/genesis_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmos/genesis_test.go b/cosmos/genesis_test.go index dfb2dd4d6..0d38a1900 100644 --- a/cosmos/genesis_test.go +++ b/cosmos/genesis_test.go @@ -28,7 +28,7 @@ func TestGenesis(t *testing.T) { // variables var ( chainID = "test-chainID" - initialBalances = "1.0amesg" + initialBalances = "100amesg" name = "name" password = "pass" privValidatorKeyFile = filepath.Join(path, "privValidatorKeyFile.json") From 34d8d1f4ebfba17a72f67fefeb87f71b827f07a3 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Mon, 13 Jan 2020 17:11:28 +0700 Subject: [PATCH 10/10] fix merge --- cmd/mesg-cosmos-daemon/main.go | 2 +- cmd/mesg-cosmos/main.go | 2 +- internal/tools/gen-genesis/main.go | 137 ----------------------------- 3 files changed, 2 insertions(+), 139 deletions(-) delete mode 100644 internal/tools/gen-genesis/main.go diff --git a/cmd/mesg-cosmos-daemon/main.go b/cmd/mesg-cosmos-daemon/main.go index 0f60af617..8d9a09fcf 100644 --- a/cmd/mesg-cosmos-daemon/main.go +++ b/cmd/mesg-cosmos-daemon/main.go @@ -41,7 +41,7 @@ func main() { if err != nil { panic(err) } - appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db) + appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db, cfg.Cosmos.MinGasPrices) enginesdk.NewBackend(appFactory) app, err = cosmos.NewApp(appFactory) if err != nil { diff --git a/cmd/mesg-cosmos/main.go b/cmd/mesg-cosmos/main.go index 971221bea..f778659b8 100644 --- a/cmd/mesg-cosmos/main.go +++ b/cmd/mesg-cosmos/main.go @@ -39,7 +39,7 @@ func main() { if err != nil { panic(err) } - appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db) + appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db, cfg.Cosmos.MinGasPrices) enginesdk.NewBackend(appFactory) app, err = cosmos.NewApp(appFactory) if err != nil { diff --git a/internal/tools/gen-genesis/main.go b/internal/tools/gen-genesis/main.go deleted file mode 100644 index ec38e6c85..000000000 --- a/internal/tools/gen-genesis/main.go +++ /dev/null @@ -1,137 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io/ioutil" - "log" - "math/rand" - "os" - "path/filepath" - "strings" - "time" - - enginecfg "github.com/mesg-foundation/engine/config" - "github.com/mesg-foundation/engine/cosmos" - "github.com/mesg-foundation/engine/logger" - enginesdk "github.com/mesg-foundation/engine/sdk" - "github.com/sirupsen/logrus" - "github.com/tendermint/tendermint/config" - db "github.com/tendermint/tm-db" -) - -const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - -func randompassword() string { - b := make([]byte, 16) - for i := range b { - b[i] = letters[rand.Intn(len(letters))] - } - return string(b) -} - -var ( - validators = flag.String("validators", "engine", "list of validator names separated with a comma") - chainid = flag.String("chain-id", "mesg-chain", "chain id") - initialBalances = flag.String("initialBalances", "1000000000amesg", "min gas price") - path = flag.String("path", ".genesis/", "genesis folder path") -) - -const ( - tendermintPath = "tendermint" - cosmosPath = "cosmos" -) - -func main() { - rand.Seed(time.Now().UnixNano()) - flag.Parse() - - validatorNames := strings.Split(*validators, ",") - passwords := make([]string, len(validatorNames)) - for i := 0; i < len(validatorNames); i++ { - passwords[i] = randompassword() - } - - // init app factory - db, err := db.NewGoLevelDB("app", filepath.Join(*path, cosmosPath)) - if err != nil { - logrus.Fatalln(err) - } - - ecfg, err := enginecfg.New() - if err != nil { - logrus.Fatalln(err) - } - appFactory := cosmos.NewAppFactory(logger.TendermintLogger(), db, ecfg.Cosmos.initialBalances) - - // register the backend modules to the app factory. - enginesdk.NewBackend(appFactory) - - // init cosmos app - app, err := cosmos.NewApp(appFactory) - if err != nil { - logrus.Fatalln(err) - } - - // init keybase - kb, err := cosmos.NewKeybase(filepath.Join(*path, cosmosPath)) - if err != nil { - logrus.Fatalln(err) - } - - // create validators - vals := []cosmos.GenesisValidator{} - peers := []string{} - for i, valName := range validatorNames { - cfg := config.DefaultConfig() - cfg.SetRoot(filepath.Join(filepath.Join(*path, tendermintPath), valName)) - if err := os.MkdirAll(filepath.Dir(cfg.GenesisFile()), 0755); err != nil { - logrus.Fatalln(err) - } - if err := os.MkdirAll(filepath.Join(cfg.DBDir()), 0755); err != nil { - logrus.Fatalln(err) - } - mnemonic, err := kb.NewMnemonic() - if err != nil { - logrus.Fatalln(err) - } - acc, err := kb.CreateAccount(valName, mnemonic, "", passwords[i], cosmos.AccNumber, cosmos.AccIndex) - if err != nil { - logrus.Fatalln(err) - } - genVal, err := cosmos.NewGenesisValidator(kb, - valName, - passwords[i], - cfg.PrivValidatorKeyFile(), - cfg.PrivValidatorStateFile(), - cfg.NodeKeyFile(), - ) - if err != nil { - logrus.Fatalln(err) - } - vals = append(vals, genVal) - peer := fmt.Sprintf("%s@%s:26656", genVal.NodeID, genVal.Name) - logrus.WithFields(map[string]interface{}{ - "name": genVal.Name, - "address": acc.GetAddress().String, - "password": genVal.Password, - "mnemonic": mnemonic, - "nodeID": genVal.NodeID, - "peer": peer, - }).Infof("Validator #%d\n", i+1) - peers = append(peers, peer) - } - - // generate and save genesis - _, err = cosmos.GenGenesis(kb, app.DefaultGenesis(), *chainid, *initialBalances, filepath.Join(*path, "genesis.json"), vals) - if err != nil { - logrus.Fatalln(err) - } - - // save peers list - if err := ioutil.WriteFile(filepath.Join(*path, "peers.txt"), []byte(strings.Join(peers, ",")), 0644); err != nil { - log.Fatalln("error during writing peers file:", err) - } - - logrus.Infof("genesis created with success in folder %q\n", *path) -}