Skip to content

Commit

Permalink
test case with real genesis data
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia committed Aug 11, 2020
1 parent 8084428 commit f7aecc5
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 8 deletions.
2 changes: 1 addition & 1 deletion x/genutil/client/cli/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ $ %s migrate v0.36 /path/to/genesis.json --chain-id=cosmoshub-3 --genesis-time=2
return errors.Wrap(err, "failed to sort JSON genesis doc")
}

fmt.Println(string(sortedBz))
cmd.Println(string(sortedBz))
return nil
},
}
Expand Down
71 changes: 64 additions & 7 deletions x/genutil/client/cli/migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package cli
package cli_test

import (
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"

abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
Expand All @@ -17,22 +27,30 @@ import (
"github.com/cosmos/cosmos-sdk/tests"
)

// custom tx codec
func makeCodec() *codec.Codec {
var cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}

func setupCmd(genesisTime string, chainID string) *cobra.Command {
c := &cobra.Command{
Use: "c",
Args: cobra.ArbitraryArgs,
Run: func(_ *cobra.Command, args []string) {},
}

c.Flags().String(flagGenesisTime, genesisTime, "")
c.Flags().String(flagChainID, chainID, "")
c.Flags().String("genesis-time", genesisTime, "")
c.Flags().String("chain-id", chainID, "")

return c
}

func TestGetMigrationCallback(t *testing.T) {
for _, version := range GetMigrationVersions() {
require.NotNil(t, GetMigrationCallback(version))
for _, version := range genutilcli.GetMigrationVersions() {
require.NotNil(t, genutilcli.GetMigrationCallback(version))
}
}

Expand All @@ -52,13 +70,52 @@ func TestMigrateGenesis(t *testing.T) {
defer cleanup()

// Reject if we dont' have the right parameters or genesis does not exists
require.Error(t, MigrateGenesisCmd(ctx, cdc).RunE(nil, []string{target, genesisPath}))
require.Error(t, genutilcli.MigrateGenesisCmd(ctx, cdc).RunE(nil, []string{target, genesisPath}))

// Noop migration with minimal genesis
emptyGenesis := []byte(`{"chain_id":"test","app_state":{}}`)
err = ioutil.WriteFile(genesisPath, emptyGenesis, 0600)
require.Nil(t, err)
cmd := setupCmd("", "test2")
require.NoError(t, MigrateGenesisCmd(ctx, cdc).RunE(cmd, []string{target, genesisPath}))
require.NoError(t, genutilcli.MigrateGenesisCmd(ctx, cdc).RunE(cmd, []string{target, genesisPath}))
// Every migration function shuold tests its own module separately
}

func TestMigrateCommercioGenesisData(t *testing.T) {
home, cleanup := tests.NewTestCaseDir(t)
t.Cleanup(cleanup)
viper.Set(cli.HomeFlag, home)
viper.Set(flags.FlagName, "moniker")
logger := log.NewNopLogger()
cfg, err := tcmd.ParseConfig()
require.Nil(t, err)
ctx := server.NewContext(cfg, logger)
cdc := makeCodec()

genesisPath := filepath.Join("testdata", "commercio-genesis.json")
require.NoError(t, err)
target := "v0.39"

config := sdk.GetConfig()
config.SetBech32PrefixForAccount("did:com:", "did:com:pub")
config.Seal()

// Migration with minimal genesis
cmd := setupCmd("2020-08-10T09:52:06.576222474Z", "test2")
_, newGenesisStream, _ := tests.ApplyMockIO(cmd)
require.NoError(t, genutilcli.MigrateGenesisCmd(ctx, cdc).RunE(cmd, []string{target, genesisPath}))

db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, 0)
newGenesisBytes := newGenesisStream.Bytes()

// Initialize the chain
require.NotPanics(t, func() {
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: newGenesisBytes,
},
)
})
}
244 changes: 244 additions & 0 deletions x/genutil/client/cli/testdata/commercio-genesis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"genesis_time": "2020-08-06T09:52:06.576222474Z",
"chain_id": "testnet",
"consensus_params": {
"block": {
"max_bytes": "22020096",
"max_gas": "-1",
"time_iota_ms": "1000"
},
"evidence": {
"max_age_num_blocks": "100000",
"max_age_duration": "172800000000000"
},
"validator": {
"pub_key_types": [
"ed25519"
]
}
},
"app_hash": "",
"app_state": {
"distribution": {
"params": {
"community_tax": "0.020000000000000000",
"base_proposer_reward": "0.010000000000000000",
"bonus_proposer_reward": "0.040000000000000000",
"withdraw_addr_enabled": true
},
"fee_pool": {
"community_pool": []
},
"delegator_withdraw_infos": [],
"previous_proposer": "",
"outstanding_rewards": [],
"validator_accumulated_commissions": [],
"validator_historical_rewards": [],
"validator_current_rewards": [],
"delegator_starting_infos": [],
"validator_slash_events": []
},
"pricefeed": {
"oracles": [
"did:com:1r93fsdxfthckptsz0wush4cx08t8c2fz4jhruw"
],
"assets": [
"ucommercio"
],
"raw_prices": [
{
"oracle": "did:com:1r93fsdxfthckptsz0wush4cx08t8c2fz4jhruw",
"price": {
"asset_name": "ucommercio",
"value": "1.000000000000000000",
"expiry": "100000000"
},
"created": "0"
}
],
"denom_blacklist": [
"uccc"
]
},
"docs": {
"documents": null,
"receipts": null,
"supported_metadata_schemes": null,
"trusted_metadata_schema_proposers": null
},
"accreditations": {
"liquidity_pool_amount": [],
"invites": null,
"trusted_service_providers": [
"did:com:1r93fsdxfthckptsz0wush4cx08t8c2fz4jhruw",
"did:com:1zlr8yn5plplvf39zr7laj05e8dm8mqd7ljznhf"
],
"stable_credits_denom": "uccc",
"memberships": [
{
"owner": "did:com:1r93fsdxfthckptsz0wush4cx08t8c2fz4jhruw",
"membership_type": "black"
}
]
},
"params": null,
"vbr": {
"pool_amount": [
{
"denom": "ucommercio",
"amount": "12500000000000.000000000000000000"
}
],
"yearly_pool_amount": null,
"year_number": "0"
},
"commerciomint": {
"positions": [],
"pool_amount": [],
"credits_denom": "uccc",
"collateral_rate": "2.000000000000000000"
},
"slashing": {
"params": {
"signed_blocks_window": "100",
"min_signed_per_window": "0.500000000000000000",
"downtime_jail_duration": "600000000000",
"slash_fraction_double_sign": "0.050000000000000000",
"slash_fraction_downtime": "0.010000000000000000"
},
"signing_infos": {},
"missed_blocks": {}
},
"creditrisk": {
"pool": []
},
"bank": {
"send_enabled": true,
"blocked_accounts": []
},
"id": {
"did_documents": null,
"power_up_requests": null
},
"crisis": {
"constant_fee": {
"denom": "ucommercio",
"amount": "1000"
}
},
"staking": {
"params": {
"unbonding_time": "1814400000000000",
"max_validators": 100,
"max_entries": 7,
"historical_entries": 0,
"bond_denom": "ucommercio"
},
"last_total_power": "0",
"last_validator_powers": null,
"validators": null,
"delegations": null,
"unbonding_delegations": null,
"redelegations": null,
"exported": false
},
"government": {
"government_address": "did:com:1r93fsdxfthckptsz0wush4cx08t8c2fz4jhruw",
"tumbler_address": "did:com:1ej6229ezfdn443ry72l964nle6ra0sc5sstyy3"
},
"supply": {
"supply": []
},
"genutil": {
"gentxs": [
{
"type": "cosmos-sdk/StdTx",
"value": {
"msg": [
{
"type": "cosmos-sdk/MsgCreateValidator",
"value": {
"description": {
"moniker": "testchain",
"identity": "",
"website": "",
"security_contact": "",
"details": ""
},
"commission": {
"rate": "0.100000000000000000",
"max_rate": "0.200000000000000000",
"max_change_rate": "0.010000000000000000"
},
"min_self_delegation": "1",
"delegator_address": "did:com:1r93fsdxfthckptsz0wush4cx08t8c2fz4jhruw",
"validator_address": "did:com:valoper1r93fsdxfthckptsz0wush4cx08t8c2fzk3w93a",
"pubkey": "did:com:valconspub1zcjduepqt3k7lh6qledmay447gnfl3ns4ejjwrzqk9n6ydcmr5clka5yvjlsdhcpag",
"value": {
"denom": "ucommercio",
"amount": "100000000"
}
}
}
],
"fee": {
"amount": [],
"gas": "200000"
},
"signatures": [
{
"pub_key": {
"type": "tendermint/PubKeySecp256k1",
"value": "A0ZymN/QXBkfyxQJjfxvjReNhZjHjdaKjBqmAs945S4f"
},
"signature": "oqLK+EycP7E5Ikaacltz5WI8eg1mJQOKUNEKQa6AQIlqBN9oYX4BKlMXSNfbu8Y3JlUNTDYNfOJKA2oSw6nssw=="
}
],
"memo": "[email protected]:26656"
}
}
]
},
"auth": {
"params": {
"max_memo_characters": "256",
"tx_sig_limit": "7",
"tx_size_cost_per_byte": "10",
"sig_verify_cost_ed25519": "590",
"sig_verify_cost_secp256k1": "1000"
},
"accounts": [
{
"type": "cosmos-sdk/Account",
"value": {
"address": "did:com:1r93fsdxfthckptsz0wush4cx08t8c2fz4jhruw",
"coins": [
{
"denom": "ucommercio",
"amount": "10000000000000"
}
],
"public_key": "",
"account_number": 0,
"sequence": 0
}
},
{
"type": "cosmos-sdk/Account",
"value": {
"address": "did:com:1zlr8yn5plplvf39zr7laj05e8dm8mqd7ljznhf",
"coins": [
{
"denom": "uccc",
"amount": "10000000000"
}
],
"public_key": "",
"account_number": 0,
"sequence": 0
}
}
]
}
}
}

0 comments on commit f7aecc5

Please sign in to comment.