Skip to content

Commit

Permalink
Merge pull request #1772: Default Parameter from Genesis
Browse files Browse the repository at this point in the history
add InitGenesis

fix lint

add immutablestore

Merge pull request #1775: ConsensusParams in BaseApp

add endblocker in params

in progress

fix errors

fix gaiadebug/hack.go

finalize rebase to transientstore branch

fix error

fix lint

finalize merging

fix synerror

in progress

in progress

add gasconfig in params, restructure

fix errors

add tests
  • Loading branch information
mossid committed Aug 14, 2018
1 parent 0adbd60 commit ae602f3
Show file tree
Hide file tree
Showing 32 changed files with 1,054 additions and 703 deletions.
50 changes: 49 additions & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type BaseApp struct {

// flag for sealing
sealed bool
// Consensus parameters are set by (Request)InitChain and (Response)EndBlock
consensusParams *abci.ConsensusParams
}

var _ abci.Application = (*BaseApp)(nil)
Expand Down Expand Up @@ -181,6 +183,9 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {

// NewContext returns a new Context with the correct store, the given header, and nil txBytes.
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
if app.consensusParams == nil {

}
if isCheckTx {
return sdk.NewContext(app.checkState.ms, header, true, app.Logger)
}
Expand All @@ -200,7 +205,7 @@ func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.Logger),
ctx: sdk.NewContext(ms, header, true, app.Logger).WithConsensusParams(app.consensusParams),
}
}

Expand Down Expand Up @@ -240,6 +245,8 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
app.setDeliverState(abci.Header{ChainID: req.ChainId})
app.setCheckState(abci.Header{ChainID: req.ChainId})

app.consensusParams = req.ConsensusParams

if app.initChainer == nil {
return
}
Expand Down Expand Up @@ -602,6 +609,43 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
return
}

// Copied from tendermint/tendermint/types/params.go
func (app *BaseApp) updateConsensusParams(updates *abci.ConsensusParams) {
if updates == nil {
return
}

params := app.consensusParams

// we must defensively consider any structs may be nil
// XXX: it's cast city over here. It's ok because we only do int32->int
// but still, watch it champ.
if updates.BlockSize != nil {
if updates.BlockSize.MaxBytes > 0 {
params.BlockSize.MaxBytes = updates.BlockSize.MaxBytes
}
if updates.BlockSize.MaxTxs > 0 {
params.BlockSize.MaxTxs = updates.BlockSize.MaxTxs
}
if updates.BlockSize.MaxGas > 0 {
params.BlockSize.MaxGas = updates.BlockSize.MaxGas
}
}
if updates.TxSize != nil {
if updates.TxSize.MaxBytes > 0 {
params.TxSize.MaxBytes = updates.TxSize.MaxBytes
}
if updates.TxSize.MaxGas > 0 {
params.TxSize.MaxGas = updates.TxSize.MaxGas
}
}
if updates.BlockGossip != nil {
if updates.BlockGossip.BlockPartSizeBytes > 0 {
params.BlockGossip.BlockPartSizeBytes = updates.BlockGossip.BlockPartSizeBytes
}
}
}

// EndBlock implements the ABCI application interface.
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
if app.deliverState.ms.TracingEnabled() {
Expand All @@ -612,6 +656,10 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc
res = app.endBlocker(app.deliverState.ctx, req)
}

if res.ConsensusParamUpdates != nil {
app.updateConsensusParams(res.ConsensusParamUpdates)
}

return
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
// add handlers
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams, nil)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper.Setter(), app.coinKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace))
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection)
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Getter(), app.RegisterCodespace(slashing.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.SubStore(slashing.DefaultParamSpace), app.RegisterCodespace(slashing.DefaultCodespace))

// register message routes
app.Router().
Expand Down Expand Up @@ -187,6 +187,11 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci

gov.InitGenesis(ctx, app.govKeeper, gov.DefaultGenesisState())

err = slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.SlashingData)
if err != nil {
panic(err)
}

return abci.ResponseInitChain{
Validators: validators,
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/db"
Expand All @@ -21,8 +22,9 @@ func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error {
}

genesisState := GenesisState{
Accounts: genaccs,
StakeData: stake.DefaultGenesisState(),
Accounts: genaccs,
StakeData: stake.DefaultGenesisState(),
SlashingData: slashing.HubDefaultGenesisState(),
}

stateBytes, err := wire.MarshalJSONIndent(gapp.cdc, genesisState)
Expand Down
13 changes: 9 additions & 4 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"

"github.com/spf13/pflag"
Expand All @@ -30,8 +31,9 @@ var (

// State to Unmarshal
type GenesisState struct {
Accounts []GenesisAccount `json:"accounts"`
StakeData stake.GenesisState `json:"stake"`
Accounts []GenesisAccount `json:"accounts"`
StakeData stake.GenesisState `json:"stake"`
SlashingData slashing.GenesisState `json:"slashing"`
}

// GenesisAccount doesn't need pubkey or sequence
Expand Down Expand Up @@ -167,6 +169,8 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState
// start with the default staking genesis state
stakeData := stake.DefaultGenesisState()

slashingData := slashing.HubDefaultGenesisState()

// get genesis flag account information
genaccs := make([]GenesisAccount, len(appGenTxs))
for i, appGenTx := range appGenTxs {
Expand Down Expand Up @@ -214,8 +218,9 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState

// create the final app state
genesisState = GenesisState{
Accounts: genaccs,
StakeData: stakeData,
Accounts: genaccs,
StakeData: stakeData,
SlashingData: slashingData,
}
return
}
Expand Down
14 changes: 11 additions & 3 deletions cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type GaiaApp struct {
keyStake *sdk.KVStoreKey
keySlashing *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
tkeyParams *sdk.TransientStoreKey

// Manage getting and setting accounts
accountMapper auth.AccountMapper
Expand Down Expand Up @@ -163,6 +164,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
keyStake: sdk.NewKVStoreKey("stake"),
keySlashing: sdk.NewKVStoreKey("slashing"),
keyParams: sdk.NewKVStoreKey("params"),
tkeyParams: sdk.NewTransientStoreKey("params"),
}

// define the accountMapper
Expand All @@ -175,9 +177,9 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
// add handlers
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams, nil)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Getter(), app.RegisterCodespace(slashing.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.SubStore(slashing.DefaultParamSpace), app.RegisterCodespace(slashing.DefaultCodespace))

// register message routes
app.Router().
Expand All @@ -190,7 +192,8 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keySlashing)
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keySlashing, app.keyParams)
app.MountStore(app.tkeyParams, sdk.StoreTypeTransient)
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
Expand Down Expand Up @@ -257,6 +260,11 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 // return sdk.ErrGenesisParse("").TraceCause(err, "")
}

err = slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.SlashingData)
if err != nil {
panic(err)
}

return abci.ResponseInitChain{
Validators: validators,
}
Expand Down
44 changes: 44 additions & 0 deletions store/immutablestore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package store

import (
dbm "github.com/tendermint/tendermint/libs/db"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type ImmutableStore struct {
dbStoreAdapter

initialized bool
commitID CommitID
}

func NewImmutableStore() *ImmutableStore {
return &ImmutableStore{
dbStoreAdapter: dbStoreAdapter{dbm.NewMemDB()},
}
}

func (is *ImmutableStore) Commit() CommitID {
if !is.initialized {
panic("Commit() on not initialized ImmutableStore")
}
is.commitID.Version += 1
return is.commitID
}

func (is *ImmutableStore) LastCommitID() CommitID {
return is.commitID
}

func (is *ImmutableStore) SetPruning(pruning sdk.PruningStrategy) {
return
}

func (is *ImmutableStore) Set(key, value []byte) {
panic("Set() called on ImmutableStore")
}

func (is *ImmutableStore) Delete(key []byte) {
panic("Delete() called on ImmutableStore")
}
20 changes: 18 additions & 2 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, logger log.Lo
c = c.WithLogger(logger)
c = c.WithSigningValidators(nil)
c = c.WithGasMeter(NewInfiniteGasMeter())
c = c.WithKVGasConfig(DefaultKVGasConfig())
c = c.WithTransientGasConfig(DefaultTransientGasConfig())
return c
}

Expand All @@ -69,12 +71,12 @@ func (c Context) Value(key interface{}) interface{} {

// KVStore fetches a KVStore from the MultiStore.
func (c Context) KVStore(key StoreKey) KVStore {
return c.multiStore().GetKVStore(key).Gas(c.GasMeter(), cachedDefaultGasConfig)
return c.multiStore().GetKVStore(key).Gas(c.GasMeter(), c.KVGasConfig())
}

// TransientStore fetches a TransientStore from the MultiStore.
func (c Context) TransientStore(key StoreKey) KVStore {
return c.multiStore().GetKVStore(key).Gas(c.GasMeter(), cachedTransientGasConfig)
return c.multiStore().GetKVStore(key).Gas(c.GasMeter(), c.TransientGasConfig())
}

//----------------------------------------
Expand Down Expand Up @@ -135,6 +137,8 @@ const (
contextKeyLogger
contextKeySigningValidators
contextKeyGasMeter
contextKeyKVGasConfig
contextKeyTransientGasConfig
)

// NOTE: Do not expose MultiStore.
Expand Down Expand Up @@ -169,6 +173,12 @@ func (c Context) SigningValidators() []abci.SigningValidator {
func (c Context) GasMeter() GasMeter {
return c.Value(contextKeyGasMeter).(GasMeter)
}
func (c Context) KVGasConfig() GasConfig {
return c.Value(contextKeyKVGasConfig).(GasConfig)
}
func (c Context) TransientGasConfig() GasConfig {
return c.Value(contextKeyTransientGasConfig).(GasConfig)
}
func (c Context) WithMultiStore(ms MultiStore) Context {
return c.withValue(contextKeyMultiStore, ms)
}
Expand Down Expand Up @@ -201,6 +211,12 @@ func (c Context) WithSigningValidators(SigningValidators []abci.SigningValidator
func (c Context) WithGasMeter(meter GasMeter) Context {
return c.withValue(contextKeyGasMeter, meter)
}
func (c Context) WithKVGasConfig(config GasConfig) Context {
return c.withValue(contextKeyKVGasConfig, config)
}
func (c Context) WithTransientGasConfig(config GasConfig) Context {
return c.withValue(contextKeyTransientGasConfig, config)
}

// Cache the multistore and return a new cached context. The cached context is
// written to the context when writeCache is called.
Expand Down
11 changes: 3 additions & 8 deletions types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ type GasConfig struct {
ValueCostPerByte Gas
}

var (
cachedDefaultGasConfig = DefaultGasConfig()
cachedTransientGasConfig = TransientGasConfig()
)

// Default gas config for KVStores
func DefaultGasConfig() GasConfig {
func DefaultKVGasConfig() GasConfig {
return GasConfig{
HasCost: 10,
ReadCostFlat: 10,
Expand All @@ -87,7 +82,7 @@ func DefaultGasConfig() GasConfig {
}

// Default gas config for TransientStores
func TransientGasConfig() GasConfig {
func DefaultTransientGasConfig() GasConfig {
// TODO: define gasconfig for transient stores
return DefaultGasConfig()
return DefaultKVGasConfig()
}
24 changes: 24 additions & 0 deletions types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ const (
StoreTypeDB
StoreTypeIAVL
StoreTypeTransient
StoreTypeImmutable
)

//----------------------------------------
Expand Down Expand Up @@ -329,6 +330,29 @@ func (key *TransientStoreKey) String() string {
return fmt.Sprintf("TransientStoreKey{%p, %s}", key, key.name)
}

// ImmutableStoreKey is used for indexing transient stores in a MultiStore
type ImmutableStoreKey struct {
name string
}

// Constructs new ImmutableStoreKey
// Must return a pointer according to the ocap principle
func NewImmutableStoreKey(name string) *ImmutableStoreKey {
return &ImmutableStoreKey{
name: name,
}
}

// Implements StoreKey
func (key *ImmutableStoreKey) Name() string {
return key.name
}

// Implements StoreKey
func (key *ImmutableStoreKey) String() string {
return fmt.Sprintf("ImmutableStoreKey{%p, %s}", key, key.name)
}

//----------------------------------------

// key-value result for iterator queries
Expand Down
Loading

0 comments on commit ae602f3

Please sign in to comment.