Skip to content

Commit

Permalink
Merge pull request #1781 from mesg-foundation/feature/staking-token
Browse files Browse the repository at this point in the history
Switch back to default staking coins of cosmos
  • Loading branch information
antho1404 authored Apr 10, 2020
2 parents cd23aac + 7b895a3 commit d60e919
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 85 deletions.
18 changes: 3 additions & 15 deletions cmd/mesg-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/mesg-foundation/engine/app"
"github.com/mesg-foundation/engine/config"
"github.com/mesg-foundation/engine/cosmos"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -32,18 +31,8 @@ func main() {
// Instantiate the codec for the command line application
cdc := app.MakeCodec()

// Read in the configuration file for the sdk
// TODO: change back when more refactor is done
// config := sdk.GetConfig()
// config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub)
// config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
// config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
// config.Seal()
cfg, err := config.New()
if err != nil {
panic(err)
}
cosmos.CustomizeConfig(cfg)
// init the config of cosmos
cosmos.InitConfig()

rootCmd := &cobra.Command{
Use: version.ClientName,
Expand Down Expand Up @@ -76,8 +65,7 @@ func main() {
// Add flags and prefix all env exposed with MESG
executor := cli.PrepareMainCmd(rootCmd, "MESG", app.DefaultCLIHome)

err = executor.Execute()
if err != nil {
if err := executor.Execute(); err != nil {
fmt.Printf("Failed executing CLI command: %s, exiting...\n", err)
os.Exit(1)
}
Expand Down
17 changes: 3 additions & 14 deletions cmd/mesg-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/mesg-foundation/engine/app"
"github.com/mesg-foundation/engine/config"
"github.com/mesg-foundation/engine/cosmos"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -33,17 +32,8 @@ var invCheckPeriod uint
func main() {
cdc := app.MakeCodec()

// TODO: put back when more refactor is done
// config := sdk.GetConfig()
// config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub)
// config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
// config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
// config.Seal()
cfg, err := config.New()
if err != nil {
panic(err)
}
cosmos.CustomizeConfig(cfg)
// init the config of cosmos
cosmos.InitConfig()

ctx := server.NewDefaultContext()
cobra.EnableCommandSorting = false
Expand Down Expand Up @@ -73,8 +63,7 @@ func main() {
executor := cli.PrepareBaseCmd(rootCmd, "AU", app.DefaultNodeHome)
rootCmd.PersistentFlags().UintVar(&invCheckPeriod, flagInvCheckPeriod,
0, "Assert registered invariants every N blocks")
err = executor.Execute()
if err != nil {
if err := executor.Execute(); err != nil {
panic(err)
}
}
Expand Down
16 changes: 3 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,11 @@ type Config struct {

// Minimum gas prices for transactions.
MinGasPrices string `validate:"required,deccoins"`

// Token name to use in the staking module.
StakeTokenDenom string `validate:"required"`

// Power reduction between the staking token and the voting power on tendermint.
PowerReduction int64 `validate:"required"`
}

DevGenesis struct {
ChainID string `validate:"required"`
InitialBalances string `validate:"required,coins"`
ValidatorDelegationCoin string `validate:"required,coin"`
ChainID string `validate:"required"`
InitialBalances string `validate:"required,coins"`
}

Account struct {
Expand Down Expand Up @@ -120,12 +113,9 @@ func defaultConfig() (*Config, error) {

c.Cosmos.RelativePath = "cosmos"
c.Cosmos.MinGasPrices = "1.0atto"
c.Cosmos.StakeTokenDenom = "atto"
c.Cosmos.PowerReduction = 18

c.DevGenesis.ChainID = "mesg-dev-chain"
c.DevGenesis.InitialBalances = "250000000000000000000000000atto" // 250 000 000 * 10^18
c.DevGenesis.ValidatorDelegationCoin = "1000000000000000000000000atto" // 1 000 000 * 10^18
c.DevGenesis.InitialBalances = "250000000000000000000000000atto,100000000stake" // 250 000 000 * 10^18 atto + 100,000,000 stake

c.Account.Name = "engine"
c.Account.Password = "pass"
Expand Down
7 changes: 5 additions & 2 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ func loadOrGenDevGenesis(cdc *codec.Codec, kb *cosmos.Keybase, cfg *config.Confi
"nodeID": validator.NodeID,
"peer": fmt.Sprintf("%s@%s:26656", validator.NodeID, validator.Name),
}).Warnln("Validator")
return cosmos.GenGenesis(cdc, kb, app.NewDefaultGenesisState(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.DevGenesis.ValidatorDelegationCoin, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator})
return cosmos.GenGenesis(cdc, kb, app.NewDefaultGenesisState(), cfg.DevGenesis.ChainID, cfg.DevGenesis.InitialBalances, cfg.Tendermint.Config.GenesisFile(), []cosmos.GenesisValidator{validator})
}

//nolint:gocyclo
func main() {
xrand.SeedInit()

// init the config of cosmos
cosmos.InitConfig()

// load engine config
cfg, err := config.New()
if err != nil {
logrus.WithField("module", "main").Fatalln(err)
}
cosmos.CustomizeConfig(cfg)

// init logger.
logger.Init(cfg.Log.Format, cfg.Log.Level, cfg.Log.ForceColors)
Expand Down
22 changes: 2 additions & 20 deletions cosmos/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package cosmos

import (
"math/big"

sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/mesg-foundation/engine/config"
)

func init() {
// InitConfig sets the bech32 prefix and HDPath to cosmos config.
func InitConfig() {
// See github.com/cosmos/cosmos-sdk/types/address.go
const (
// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address
Expand All @@ -34,18 +31,3 @@ func init() {
cfg.SetCoinType(config.CosmosCoinType)
cfg.Seal()
}

// CustomizeConfig customizes the cosmos application like addresses prefixes and coin type
func CustomizeConfig(engineCfg *config.Config) {
// From github.com/cosmos/cosmos-sdk/types/staking.go
// Set the power reduction from token to voting power to 10^18 (number of decimal of the token).
sdktypes.PowerReduction = sdktypes.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(engineCfg.Cosmos.PowerReduction), nil))

// From github.com/cosmos/cosmos-sdk/x/staking/types/params.go
// Override default genesis state of staking module to set the bond denom
staking.DefaultGenesisState = func() staking.GenesisState {
state := stakingtypes.DefaultGenesisState()
state.Params.BondDenom = engineCfg.Cosmos.StakeTokenDenom
return state
}
}
12 changes: 4 additions & 8 deletions cosmos/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ func LoadGenesis(genesisFile string) (*tmtypes.GenesisDoc, error) {
}

// GenGenesis generates a new genesis and save it.
func GenGenesis(cdc *codec.Codec, kb *Keybase, defaultGenesisŚtate map[string]json.RawMessage, chainID, initialBalances, validatorDelegationCoin, genesisFile string, validators []GenesisValidator) (*tmtypes.GenesisDoc, error) {
valDelCoin, err := sdktypes.ParseCoin(validatorDelegationCoin)
if err != nil {
return nil, err
}
func GenGenesis(cdc *codec.Codec, 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
Expand All @@ -67,7 +63,7 @@ func GenGenesis(cdc *codec.Codec, kb *Keybase, defaultGenesisŚtate map[string]j
return nil, err
}
// generate msg to add this validator
msgs = append(msgs, genCreateValidatorMsg(acc.GetAddress(), validator.Name, valDelCoin, validator.ValPubKey))
msgs = append(msgs, genCreateValidatorMsg(acc.GetAddress(), validator.Name, validator.ValPubKey))
}
// generate genesis transaction
accNumber := uint64(0)
Expand Down Expand Up @@ -147,11 +143,11 @@ func genGenesisAppState(cdc *codec.Codec, defaultGenesisŚtate map[string]json.R
return genutil.SetGenTxsInAppGenesisState(cdc, defaultGenesisŚtate, []authtypes.StdTx{signedStdTx})
}

func genCreateValidatorMsg(accAddress sdktypes.AccAddress, accName string, validatorDelegationCoin sdktypes.Coin, valPubKey crypto.PubKey) stakingtypes.MsgCreateValidator {
func genCreateValidatorMsg(accAddress sdktypes.AccAddress, accName string, valPubKey crypto.PubKey) stakingtypes.MsgCreateValidator {
return stakingtypes.NewMsgCreateValidator(
sdktypes.ValAddress(accAddress),
valPubKey,
validatorDelegationCoin,
sdktypes.NewCoin(sdktypes.DefaultBondDenom, sdktypes.TokensFromConsensusPower(100)),
stakingtypes.Description{
Moniker: accName,
Details: "init-validator",
Expand Down
23 changes: 11 additions & 12 deletions cosmos/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ func TestGenesis(t *testing.T) {
require.NoError(t, err)
// variables
var (
chainID = "test-chainID"
initialBalances = "100amesg"
validatorDelegationCoin = "1amesg"
name = "name"
password = "pass"
privValidatorKeyFile = filepath.Join(path, "privValidatorKeyFile.json")
privValidatorStateFile = filepath.Join(path, "privValidatorStateFile.json")
nodeKeyFile = filepath.Join(path, "nodeKeyFile.json")
genesisPath = filepath.Join(path, "genesis.json")
validators = []GenesisValidator{}
defaultGenesisState = map[string]json.RawMessage{}
chainID = "test-chainID"
initialBalances = "100amesg"
name = "name"
password = "pass"
privValidatorKeyFile = filepath.Join(path, "privValidatorKeyFile.json")
privValidatorStateFile = filepath.Join(path, "privValidatorStateFile.json")
nodeKeyFile = filepath.Join(path, "nodeKeyFile.json")
genesisPath = filepath.Join(path, "genesis.json")
validators = []GenesisValidator{}
defaultGenesisState = map[string]json.RawMessage{}
)
// init account
mnemonic, _ := kb.NewMnemonic()
Expand All @@ -63,7 +62,7 @@ func TestGenesis(t *testing.T) {
require.False(t, GenesisExist(genesisPath))
})
t.Run("generate genesis", func(t *testing.T) {
genesis, err := GenGenesis(cdc, kb, defaultGenesisState, chainID, initialBalances, validatorDelegationCoin, genesisPath, validators)
genesis, err := GenGenesis(cdc, kb, defaultGenesisState, chainID, initialBalances, genesisPath, validators)
require.NoError(t, err)
require.NotEmpty(t, genesis)
})
Expand Down
4 changes: 3 additions & 1 deletion e2e/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ func TestAPI(t *testing.T) {
t.Skip()
}

// init the config of cosmos
cosmos.InitConfig()

// init config
var err error
cfg, err = config.New()
require.NoError(t, err)
minExecutionPrice, err = sdk.ParseCoins(cfg.DefaultExecutionPrice)
require.NoError(t, err)
cosmos.CustomizeConfig(cfg)

// change and recreate cosmos relative path because CI dir permissions
cfg.Cosmos.RelativePath = "e2e.cosmos"
Expand Down

0 comments on commit d60e919

Please sign in to comment.