Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R [Staging PR 2/3] genesis generalization #4128

Closed
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ import (
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
txbuilder "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
bankrest "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
distrrest "github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
"github.com/cosmos/cosmos-sdk/x/gov"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
"github.com/cosmos/cosmos-sdk/x/mint"
mintrest "github.com/cosmos/cosmos-sdk/x/mint/client/rest"
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingrest "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
Expand Down Expand Up @@ -281,39 +283,51 @@ func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress
require.NoError(t, err)

// add some tokens to init accounts
stakingDataBz := genesisState.Modules[staking.ModuleName]
var stakingData staking.GenesisState
cdc.MustUnmarshalJSON(stakingDataBz, &stakingData)
for _, addr := range initAddrs {
accAuth := auth.NewBaseAccountWithAddress(addr)
accTokens := sdk.TokensFromTendermintPower(100)
accAuth.Coins = sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, accTokens)}
acc := gapp.NewGenesisAccount(&accAuth)
genesisState.Accounts = append(genesisState.Accounts, acc)
genesisState.StakingData.Pool.NotBondedTokens = genesisState.StakingData.Pool.NotBondedTokens.Add(accTokens)
stakingData.Pool.NotBondedTokens = stakingData.Pool.NotBondedTokens.Add(accTokens)
}
stakingDataBz = cdc.MustMarshalJSON(stakingData)
genesisState.Modules[staking.ModuleName] = stakingDataBz

// mint genesis (none set within genesisState)
mintData := mint.DefaultGenesisState()
inflationMin := sdk.ZeroDec()
if minting {
inflationMin = sdk.MustNewDecFromStr("10000.0")
genesisState.MintData.Params.InflationMax = sdk.MustNewDecFromStr("15000.0")
mintData.Params.InflationMax = sdk.MustNewDecFromStr("15000.0")
} else {
genesisState.MintData.Params.InflationMax = inflationMin
mintData.Params.InflationMax = inflationMin
}
genesisState.MintData.Minter.Inflation = inflationMin
genesisState.MintData.Params.InflationMin = inflationMin
mintData.Minter.Inflation = inflationMin
mintData.Params.InflationMin = inflationMin
mintDataBz := cdc.MustMarshalJSON(mintData)
genesisState.Modules[mint.ModuleName] = mintDataBz

// initialize crisis data
genesisState.CrisisData.ConstantFee = sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000)
crisisDataBz := genesisState.Modules[crisis.ModuleName]
var crisisData crisis.GenesisState
cdc.MustUnmarshalJSON(crisisDataBz, &crisisData)
crisisData.ConstantFee = sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000)
crisisDataBz = cdc.MustMarshalJSON(crisisData)
genesisState.Modules[crisis.ModuleName] = crisisDataBz

// double check inflation is set according to the minting boolean flag
if minting {
require.Equal(t, sdk.MustNewDecFromStr("15000.0"),
genesisState.MintData.Params.InflationMax)
require.Equal(t, sdk.MustNewDecFromStr("10000.0"), genesisState.MintData.Minter.Inflation)
require.Equal(t, sdk.MustNewDecFromStr("10000.0"),
genesisState.MintData.Params.InflationMin)
require.Equal(t, sdk.MustNewDecFromStr("15000.0"), mintData.Params.InflationMax)
require.Equal(t, sdk.MustNewDecFromStr("10000.0"), mintData.Minter.Inflation)
require.Equal(t, sdk.MustNewDecFromStr("10000.0"), mintData.Params.InflationMin)
} else {
require.Equal(t, sdk.ZeroDec(), genesisState.MintData.Params.InflationMax)
require.Equal(t, sdk.ZeroDec(), genesisState.MintData.Minter.Inflation)
require.Equal(t, sdk.ZeroDec(), genesisState.MintData.Params.InflationMin)
require.Equal(t, sdk.ZeroDec(), mintData.Params.InflationMax)
require.Equal(t, sdk.ZeroDec(), mintData.Minter.Inflation)
require.Equal(t, sdk.ZeroDec(), mintData.Params.InflationMin)
}

appState, err := codec.MarshalJSONIndent(cdc, genesisState)
Expand Down
106 changes: 49 additions & 57 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,34 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking"
)

const (
appName = "GaiaApp"
)
const appName = "GaiaApp"

// default home directories for expected binaries
var (
DefaultCLIHome = os.ExpandEnv("$HOME/.gaiacli")
// default home directories for gaiacli
DefaultCLIHome = os.ExpandEnv("$HOME/.gaiacli")

// default home directories for gaiad
DefaultNodeHome = os.ExpandEnv("$HOME/.gaiad")

// The ModuleBasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
mbm sdk.ModuleBasicManager
)

func init() {
mbm = sdk.NewModuleBasicManager(
auth.AppModuleBasic{},
bank.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
)
}

// Extended ABCI application
type GaiaApp struct {
*bam.BaseApp
Expand Down Expand Up @@ -71,25 +89,9 @@ type GaiaApp struct {
mm *sdk.ModuleManager
}

// custom tx codec
func MakeCodec() *codec.Codec {
var cdc = codec.New()
bank.RegisterCodec(cdc)
staking.RegisterCodec(cdc)
distr.RegisterCodec(cdc)
slashing.RegisterCodec(cdc)
gov.RegisterCodec(cdc)
auth.RegisterCodec(cdc)
crisis.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}

// NewGaiaApp returns a reference to an initialized GaiaApp.
func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
invCheckPeriod uint,
baseAppOptions ...func(*bam.BaseApp)) *GaiaApp {
invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp)) *GaiaApp {

cdc := MakeCodec()

Expand Down Expand Up @@ -142,29 +144,28 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
app.bankKeeper, app.feeCollectionKeeper)

// register the staking hooks
// NOTE: The stakingKeeper above is passed by reference, so that it can be
// modified like below:
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.stakingKeeper = *stakingKeeper.SetHooks(
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks()))

app.mm = sdk.NewModuleManager(
auth.NewAppModule(app.accountKeeper),
auth.NewAppModule(app.accountKeeper, app.feeCollectionKeeper),
bank.NewAppModule(app.bankKeeper, app.accountKeeper),
crisis.NewAppModule(app.crisisKeeper, app.Logger()),
distr.NewAppModule(app.distrKeeper),
gov.NewAppModule(app.govKeeper),
mint.NewAppModule(app.mintKeeper),
slashing.NewAppModule(app.slashingKeeper),
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
staking.NewAppModule(app.stakingKeeper, app.feeCollectionKeeper, app.distrKeeper, app.accountKeeper),
)

// NOTE: during begin block slashing happens after distr.BeginBlocker so
// that there is nothing left over in the validator fee pool, so as to keep
// the CanWithdrawInvariant invariant.
// TODO: slashing should really happen at EndBlocker.
app.mm.SetOrderBeginBlockers(mint.ModuleName, distr.ModuleName, slashing.ModuleName)
app.mm.SetOrderEndBlockers(gov.ModuleName, staking.ModuleName)
app.mm.SetOrderInitGenesis(crisis.ModuleName) // XXX this only is for invariant checks right now
app.mm.SetOrderInitGenesis(distr.ModuleName, staking.ModuleName, auth.ModuleName, bank.ModuleName,
slashing.ModuleName, gov.ModuleName, mint.ModuleName, crisis.ModuleName)
app.mm.RegisterInvariants(&app.crisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())

Expand All @@ -183,7 +184,6 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
cmn.Exit(err.Error())
}
}

return app
}

Expand All @@ -208,35 +208,27 @@ func (app *GaiaApp) initFromGenesisState(ctx sdk.Context, genesisState GenesisSt
app.accountKeeper.SetAccount(ctx, acc)
}

// initialize distribution (must happen before staking)
distr.InitGenesis(ctx, app.distrKeeper, genesisState.DistrData)

// load the initial staking information
validators, err := staking.InitGenesis(ctx, app.stakingKeeper, genesisState.StakingData)
if err != nil {
panic(err)
}

// initialize module-specific stores
auth.InitGenesis(ctx, app.accountKeeper, app.feeCollectionKeeper, genesisState.AuthData)
bank.InitGenesis(ctx, app.bankKeeper, genesisState.BankData)
slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.SlashingData, genesisState.StakingData.Validators.ToSDKValidators())
gov.InitGenesis(ctx, app.govKeeper, genesisState.GovData)
mint.InitGenesis(ctx, app.mintKeeper, genesisState.MintData)
crisis.InitGenesis(ctx, app.crisisKeeper, genesisState.CrisisData)
// initialize modules
validators := app.mm.InitGenesis(ctx, genesisState.Modules)

// validate genesis state
if err := GaiaValidateGenesisState(genesisState); err != nil {
panic(err)
}

if len(genesisState.GenTxs) > 0 {
validators = app.deliverGenTxs(ctx, genesisState.GenTxs, app.BaseApp.DeliverTx)
}

return validators
}

// GaiaValidateGenesisState ensures that the genesis state obeys the expected invariants
func GaiaValidateGenesisState(genesisState GenesisState) error {
if err := validateGenesisStateAccounts(genesisState.Accounts); err != nil {
return err
}
return mbm.ValidateGenesis(genesisState.Modules)
}

type deliverTxFn func([]byte) abci.ResponseDeliverTx

// TODO move this genTx functionality to staking
Expand All @@ -255,20 +247,10 @@ func (app *GaiaApp) deliverGenTxs(ctx sdk.Context, genTxs []json.RawMessage, del

// custom logic for gaia initialization
func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {

var genesisState GenesisState
app.cdc.MustUnmarshalJSON(req.AppStateBytes, &genesisState)
validators := app.initFromGenesisState(ctx, genesisState)

// XXX this is a temporary partial init-genesis implementation to allow for
// an invariance check for the crisis module
ctx = app.NewContext(false, abci.Header{Height: app.LastBlockHeight() + 1})
dummyGenesisData := make(map[string]json.RawMessage)
_, err := app.mm.InitGenesis(ctx, dummyGenesisData)
if err != nil {
panic(err)
}

return abci.ResponseInitChain{
Validators: validators,
}
Expand All @@ -278,3 +260,13 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
func (app *GaiaApp) LoadHeight(height int64) error {
return app.LoadVersion(height, app.keyMain)
}

//_______________________________
// custom tx codec
func MakeCodec() *codec.Codec {
var cdc = codec.New()
mbm.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}
20 changes: 1 addition & 19 deletions cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ import (
"os"
"testing"

"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"

abci "github.com/tendermint/tendermint/abci/types"
)
Expand All @@ -28,17 +20,7 @@ func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error {
genaccs[i] = NewGenesisAccount(acc)
}

genesisState := NewGenesisState(
genaccs,
auth.DefaultGenesisState(),
bank.DefaultGenesisState(),
staking.DefaultGenesisState(),
mint.DefaultGenesisState(),
distr.DefaultGenesisState(),
gov.DefaultGenesisState(),
crisis.DefaultGenesisState(),
slashing.DefaultGenesisState(),
)
genesisState := NewGenesisState(genaccs, mbm.DefaultGenesis(), nil)

stateBytes, err := codec.MarshalJSONIndent(gapp.cdc, genesisState)
if err != nil {
Expand Down
15 changes: 2 additions & 13 deletions cmd/gaia/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
)
Expand All @@ -41,14 +36,8 @@ func (app *GaiaApp) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteLis

genState := NewGenesisState(
accounts,
auth.ExportGenesis(ctx, app.accountKeeper, app.feeCollectionKeeper),
bank.ExportGenesis(ctx, app.bankKeeper),
staking.ExportGenesis(ctx, app.stakingKeeper),
mint.ExportGenesis(ctx, app.mintKeeper),
distr.ExportGenesis(ctx, app.distrKeeper),
gov.ExportGenesis(ctx, app.govKeeper),
crisis.ExportGenesis(ctx, app.crisisKeeper),
slashing.ExportGenesis(ctx, app.slashingKeeper),
app.mm.ExportGenesis(ctx),
nil,
)
appState, err = codec.MarshalJSONIndent(app.cdc, genState)
if err != nil {
Expand Down
Loading