From ade42e74b7d3b6b700e9de05b3d95f34b67f345a Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 26 Apr 2018 00:49:53 -0400 Subject: [PATCH] better init test --- cmd/gaia/app/genesis.go | 82 ++++++++++++++++++------------------ cmd/gaia/app/genesis_test.go | 33 +++++++++++++++ server/init.go | 74 ++++++++++++++++---------------- server/init_test.go | 15 ++++++- 4 files changed, 125 insertions(+), 79 deletions(-) create mode 100644 cmd/gaia/app/genesis_test.go diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 66fd1f98d9..513430ec9a 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -65,8 +65,8 @@ func GaiaAppInit() server.AppInit { return server.AppInit{ FlagsAppGenState: fsAppGenState, FlagsAppGenTx: fsAppGenTx, - AppGenState: GaiaAppGenState, AppGenTx: GaiaAppGenTx, + AppGenState: GaiaAppGenState, } } @@ -77,7 +77,45 @@ type GaiaGenTx struct { PubKey crypto.PubKey `json:"pub_key"` } -// power given to validators in gaia init functions +// Generate a gaia genesis transaction +func GaiaAppGenTx(cdc *wire.Codec, pk crypto.PubKey) ( + appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { + + var addr sdk.Address + var secret string + clientRoot := viper.GetString(flagClientHome) + overwrite := viper.GetBool(flagOWK) + name := viper.GetString(flagName) + addr, secret, err = server.GenerateSaveCoinKey(clientRoot, name, "1234567890", overwrite) + if err != nil { + return + } + + var bz []byte + gaiaGenTx := GaiaGenTx{ + Name: name, + Address: addr, + PubKey: pk, + } + bz, err = wire.MarshalJSONIndent(cdc, gaiaGenTx) + if err != nil { + return + } + appGenTx = json.RawMessage(bz) + + mm := map[string]string{"secret": secret} + bz, err = cdc.MarshalJSON(mm) + if err != nil { + return + } + cliPrint = json.RawMessage(bz) + + validator = tmtypes.GenesisValidator{ + PubKey: pk, + Power: freeFermionVal, + } + return +} // Create the core parameters for genesis initialization for gaia // note that the pubkey input is this machines pubkey @@ -133,43 +171,3 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState jso appState, err = wire.MarshalJSONIndent(cdc, genesisState) return } - -// Generate a gaia genesis transaction -func GaiaAppGenTx(cdc *wire.Codec, pk crypto.PubKey) ( - appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { - - var addr sdk.Address - var secret string - clientRoot := viper.GetString(flagClientHome) - overwrite := viper.GetBool(flagOWK) - name := viper.GetString(flagName) - addr, secret, err = server.GenerateSaveCoinKey(clientRoot, name, "1234567890", overwrite) - if err != nil { - return - } - - var bz []byte - gaiaGenTx := GaiaGenTx{ - Name: name, - Address: addr, - PubKey: pk, - } - bz, err = wire.MarshalJSONIndent(cdc, gaiaGenTx) - if err != nil { - return - } - appGenTx = json.RawMessage(bz) - - mm := map[string]string{"secret": secret} - bz, err = cdc.MarshalJSON(mm) - if err != nil { - return - } - cliPrint = json.RawMessage(bz) - - validator = tmtypes.GenesisValidator{ - PubKey: pk, - Power: freeFermionVal, - } - return -} diff --git a/cmd/gaia/app/genesis_test.go b/cmd/gaia/app/genesis_test.go new file mode 100644 index 0000000000..94bcdd0df4 --- /dev/null +++ b/cmd/gaia/app/genesis_test.go @@ -0,0 +1,33 @@ +package app + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/stretchr/testify/assert" + crypto "github.com/tendermint/go-crypto" +) + +func TestToAccount(t *testing.T) { + priv = crypto.GenPrivKeyEd25519() + addr = priv.PubKey().Address() + authAcc := auth.NewBaseAccountWithAddress(addr) + genAcc := NewGenesisAccount(authAcc) + assert.Equal(t, authAcc, genAcc.ToAccount()) +} + +func TestGaiaAppGenTx(t *testing.T) { + cdc := MakeCodec() + + //TODO test that key overwrite flags work / no overwrites if set off + //TODO test validator created has provided pubkey + //TODO test the account created has the correct pubkey +} + +func TestGaiaAppGenState(t *testing.T) { + cdc := MakeCodec() + + // TODO test must provide at least genesis transaction + // TODO test with both one and two genesis transactions: + // TODO correct: genesis account created, canididates created, pool token variance +} diff --git a/server/init.go b/server/init.go index d23d78b818..af735f0927 100644 --- a/server/init.go +++ b/server/init.go @@ -58,7 +58,7 @@ func GenTxCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command { return err } nodeID := string(nodeKey.ID()) - pubKey := ReadOrCreatePrivValidator(config) + pubKey := readOrCreatePrivValidator(config) appGenTx, cliPrint, validator, err := appInit.AppGenTx(cdc, pubKey) if err != nil { @@ -126,7 +126,7 @@ func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command { return err } nodeID := string(nodeKey.ID()) - pubKey := ReadOrCreatePrivValidator(config) + pubKey := readOrCreatePrivValidator(config) chainID := viper.GetString(flagChainID) if chainID == "" { @@ -247,7 +247,7 @@ func processGenTxs(genTxsDir string, cdc *wire.Codec, appInit AppInit) ( //________________________________________________________________________________________ // read of create the private key file for this config -func ReadOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey { +func readOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey { // private validator privValFile := tmConfig.PrivValidatorFile() var privValidator *pvm.FilePV @@ -297,21 +297,21 @@ type AppInit struct { FlagsAppGenState *pflag.FlagSet FlagsAppGenTx *pflag.FlagSet - // AppGenState creates the core parameters initialization. It takes in a - // pubkey meant to represent the pubkey of the validator of this machine. - AppGenState func(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) - // create the application genesis tx AppGenTx func(cdc *wire.Codec, pk crypto.PubKey) ( appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) + + // AppGenState creates the core parameters initialization. It takes in a + // pubkey meant to represent the pubkey of the validator of this machine. + AppGenState func(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) } //_____________________________________________________________________ // simple default application init var DefaultAppInit = AppInit{ - AppGenState: SimpleAppGenState, AppGenTx: SimpleAppGenTx, + AppGenState: SimpleAppGenState, } // simple genesis tx @@ -319,34 +319,6 @@ type SimpleGenTx struct { Addr sdk.Address `json:"addr"` } -// create the genesis app state -func SimpleAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { - - if len(appGenTxs) != 1 { - err = errors.New("must provide a single genesis transaction") - return - } - - var genTx SimpleGenTx - err = cdc.UnmarshalJSON(appGenTxs[0], &genTx) - if err != nil { - return - } - - appState = json.RawMessage(fmt.Sprintf(`{ - "accounts": [{ - "address": "%s", - "coins": [ - { - "denom": "mycoin", - "amount": 9007199254740992 - } - ] - }] -}`, genTx.Addr.String())) - return -} - // Generate a genesis transaction func SimpleAppGenTx(cdc *wire.Codec, pk crypto.PubKey) ( appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) { @@ -380,6 +352,36 @@ func SimpleAppGenTx(cdc *wire.Codec, pk crypto.PubKey) ( return } +// create the genesis app state +func SimpleAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { + + if len(appGenTxs) != 1 { + err = errors.New("must provide a single genesis transaction") + return + } + + var genTx SimpleGenTx + err = cdc.UnmarshalJSON(appGenTxs[0], &genTx) + if err != nil { + return + } + + appState = json.RawMessage(fmt.Sprintf(`{ + "accounts": [{ + "address": "%s", + "coins": [ + { + "denom": "mycoin", + "amount": 9007199254740992 + } + ] + }] +}`, genTx.Addr.String())) + return +} + +//___________________________________________________________________________________________ + // GenerateCoinKey returns the address of a public key, along with the secret // phrase to recover the private key. func GenerateCoinKey() (sdk.Address, string, error) { diff --git a/server/init_test.go b/server/init_test.go index 1bdf0a085d..eca5295052 100644 --- a/server/init_test.go +++ b/server/init_test.go @@ -12,7 +12,8 @@ import ( tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" ) -func TestInit(t *testing.T) { +// TODO update +func TestInitCmd(t *testing.T) { defer setupViper(t)() logger := log.NewNopLogger() @@ -28,3 +29,15 @@ func TestInit(t *testing.T) { err = cmd.RunE(nil, nil) require.NoError(t, err) } + +func TestGenTxCmd(t *testing.T) { + // TODO +} + +func TestSimpleAppGenTx(t *testing.T) { + // TODO +} + +func TestSimpleAppGenState(t *testing.T) { + // TODO +}