Skip to content

Commit

Permalink
fix consensusparams
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Sep 13, 2018
1 parent 5f7310d commit cef7fb4
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 83 deletions.
60 changes: 28 additions & 32 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ type BaseApp struct {
// checkState is set on initialization and reset on Commit.
// deliverState is set in InitChain and BeginBlock and cleared on Commit.
// See methods app.setCheckState and setDeliverState.
checkState *state // for CheckTx
checkParams sdk.ConsensusParams
deliverState *state // for DeliverTx
deliverParams sdk.ConsensusParams
checkState *state // for CheckTx
deliverState *state // for DeliverTx
checkConsensusParams sdk.ConsensusParams
deliverConsensusParams sdk.ConsensusParams

signedValidators []abci.SigningValidator // absent validators from begin block

Expand Down Expand Up @@ -210,38 +210,32 @@ func (st *state) CacheMultiStore() sdk.CacheMultiStore {

func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
ctx := sdk.NewContext(ms, header, true, app.Logger)
ctx = ctx.WithConsensusParams(app.checkConsensusParams)

app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.Logger),
ctx: ctx,
}
}

func (app *BaseApp) setCheckParams(params *abci.ConsensusParams) {
var sdkparams sdk.ConsensusParams
(&sdkparams).FromABCI(params)
app.checkParams = sdkparams
}

func (app *BaseApp) getCheckContext() sdk.Context {
return app.checkState.ctx.WithConsensusParams(app.checkParams)
}

func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore()
ctx := sdk.NewContext(ms, header, false, app.Logger)
ctx = ctx.WithConsensusParams(app.deliverConsensusParams)

app.deliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, app.Logger),
ctx: ctx,
}
}

func (app *BaseApp) setDeliverParams(params *abci.ConsensusParams) {
var sdkparams sdk.ConsensusParams
(&sdkparams).FromABCI(params)
app.deliverParams = sdkparams
func (app *BaseApp) setCheckConsensusParams(params sdk.ConsensusParams) {
app.checkConsensusParams = params
}

func (app *BaseApp) getDeliverContext() sdk.Context {
return app.deliverState.ctx.WithConsensusParams(app.deliverParams)
func (app *BaseApp) setDeliverConsensusParams(params sdk.ConsensusParams) {
app.deliverConsensusParams = params
}

//______________________________________________________________________________
Expand All @@ -268,16 +262,18 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp
// Implements ABCI
// InitChain runs the initialization logic directly on the CommitMultiStore and commits it.
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
// Initialize the deliver state and check state with ChainID and run initChain
// Initialize the deliver state and check state
// with ChainID
app.setDeliverState(abci.Header{ChainID: req.ChainId})
app.setDeliverParams(req.ConsensusParams)
app.setCheckState(abci.Header{ChainID: req.ChainId})
app.setCheckParams(req.ConsensusParams)
app.setDeliverConsensusParams(sdk.ReadConsensusParams(req.ConsensusParams))
app.setCheckConsensusParams(sdk.ReadConsensusParams(req.ConsensusParams))

// Run initChain
if app.initChainer == nil {
return
}
res = app.initChainer(app.getDeliverContext(), req)
res = app.initChainer(app.deliverState.ctx, req)

// NOTE: we don't commit, but BeginBlock for block 1
// starts from this deliverState
Expand Down Expand Up @@ -448,9 +444,8 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
}

if app.beginBlocker != nil {
res = app.beginBlocker(app.getDeliverContext(), req)
res = app.beginBlocker(app.deliverState.ctx, req)
}

// set the signed validators for addition to context in deliverTx
// TODO: communicate this result to the address to pubkey map in slashing
app.signedValidators = req.LastCommitInfo.GetValidators()
Expand Down Expand Up @@ -621,6 +616,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
// meter so we initialize upfront.
var gasWanted int64
var msCache sdk.CacheMultiStore

ctx := app.getContextForAnte(mode, txBytes)
ctx = app.initializeContext(ctx, mode)

Expand Down Expand Up @@ -739,14 +735,14 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc
res = app.endBlocker(app.deliverState.ctx, req)
}

// Use consensus parameter from last block
params := app.getDeliverContext().ConsensusParams().ToABCI()
app.setCheckParams(params)
app.setCheckConsensusParams(app.deliverConsensusParams)

if res.ConsensusParamUpdates != nil {
// Update consensus parameter
// Will be applied at BeginBlock for DeliverState
params := app.deliverConsensusParams.ToABCI()
newparams := updateConsensusParams(params, res.ConsensusParamUpdates)
app.setDeliverParams(newparams)
app.setDeliverConsensusParams(sdk.ReadConsensusParams(newparams))
}

return
Expand Down
22 changes: 16 additions & 6 deletions types/consensus_params.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package types
import (

import (
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
)
// Reusing tmtype

// Reusing tmtype
type ConsensusParams tmtypes.ConsensusParams
// Convert ConsensusParams to ABCI type

// Convert ConsensusParams to ABCI type
func (params ConsensusParams) ToABCI() *abci.ConsensusParams {
inner := tmtypes.ConsensusParams(params)
return tmtypes.TM2PB.ConsensusParams(&inner)
}
// Load ConsensusParams from ABCI type
func (params *ConsensusParams) FromABCI(abciparams *abci.ConsensusParams) {

// Load ConsensusParams from ABCI type
func (params *ConsensusParams) FromABCI(abciparams *abci.ConsensusParams) *ConsensusParams {
// Manually set nil members to empty value
if abciparams == nil {
abciparams = &abci.ConsensusParams{
Expand All @@ -30,5 +34,11 @@ func (params *ConsensusParams) FromABCI(abciparams *abci.ConsensusParams) {
abciparams.BlockGossip = &abci.BlockGossip{}
}
}
*params = ConsensusParams(tmtypes.PB2TM.ConsensusParams(abciparams))
*params = ConsensusParams(tmtypes.PB2TM.ConsensusParams(abciparams))
return params
}

func ReadConsensusParams(abciparams *abci.ConsensusParams) (params ConsensusParams) {
(&params).FromABCI(abciparams)
return
}
6 changes: 3 additions & 3 deletions x/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const (
)

// nolint - Paramstore key constructor
func ParamStoreKeyDepositProcedure() params.Key { return params.NewKey([]byte("depositprocedure")) }
func ParamStoreKeyVotingProcedure() params.Key { return params.NewKey([]byte("votingprocedure")) }
func ParamStoreKeyTallyingProcedure() params.Key { return params.NewKey([]byte("tallyingprocedure")) }
func ParamStoreKeyDepositProcedure() params.Key { return params.NewKey("depositprocedure") }
func ParamStoreKeyVotingProcedure() params.Key { return params.NewKey("votingprocedure") }
func ParamStoreKeyTallyingProcedure() params.Key { return params.NewKey("tallyingprocedure") }

// Cached parameter store keys
var (
Expand Down
14 changes: 8 additions & 6 deletions x/params/consensus/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const (
)

// nolint - Key generators for parameter access
func BlockMaxBytesKey() params.Key { return params.NewKey("BlockSize", "MaxBytes") }
func BlockMaxTxsKey() params.Key { return params.NewKey("BlockSize", "MaxTxs") }
func BlockMaxGasKey() params.Key { return params.NewKey("BlockSize", "MaxGas") }
func TxMaxBytesKey() params.Key { return params.NewKey("TxSize", "MaxBytes") }
func TxMaxGasKey() params.Key { return params.NewKey("TxSize", "MaxGas") }
func BlockPartSizeBytesKey() params.Key { return params.NewKey("BlockGossip", "PartSizeBytes") }
func BlockMaxBytesKey() params.Key { return params.NewKey("BlockSize", "MaxBytes") }
func BlockMaxTxsKey() params.Key { return params.NewKey("BlockSize", "MaxTxs") }
func BlockMaxGasKey() params.Key { return params.NewKey("BlockSize", "MaxGas") }
func TxMaxBytesKey() params.Key { return params.NewKey("TxSize", "MaxBytes") }
func TxMaxGasKey() params.Key { return params.NewKey("TxSize", "MaxGas") }
func BlockPartSizeBytesKey() params.Key {
return params.NewKey("BlockGossip", "PartSizeBytes")
}

// Cached parameter keys
var (
Expand Down
36 changes: 18 additions & 18 deletions x/params/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func TestKeeper(t *testing.T) {
key Key
param int64
}{
{NewKey([]byte("key1")), 10},
{NewKey([]byte("key2")), 55},
{NewKey([]byte("key3")), 182},
{NewKey([]byte("key4")), 17582},
{NewKey([]byte("key5")), 2768554},
{NewKey([]byte("space1"), []byte("key1")), 1157279},
{NewKey([]byte("space1"), []byte("key2")), 9058701},
{NewKey("key1"), 10},
{NewKey("key2"), 55},
{NewKey("key3"), 182},
{NewKey("key4"), 17582},
{NewKey("key5"), 2768554},
{NewKey("space1", "key1"), 1157279},
{NewKey("space1", "key2"), 9058701},
}

skey := sdk.NewKVStoreKey("test")
Expand Down Expand Up @@ -96,14 +96,14 @@ func TestGet(t *testing.T) {
zero interface{}
ptr interface{}
}{
{NewKey([]byte("string")), "test", "", new(string)},
{NewKey([]byte("bool")), true, false, new(bool)},
{NewKey([]byte("int16")), int16(1), int16(0), new(int16)},
{NewKey([]byte("int32")), int32(1), int32(0), new(int32)},
{NewKey([]byte("int64")), int64(1), int64(0), new(int64)},
{NewKey([]byte("uint16")), uint16(1), uint16(0), new(uint16)},
{NewKey([]byte("uint32")), uint32(1), uint32(0), new(uint32)},
{NewKey([]byte("uint64")), uint64(1), uint64(0), new(uint64)},
{NewKey("string"), "test", "", new(string)},
{NewKey("bool"), true, false, new(bool)},
{NewKey("int16"), int16(1), int16(0), new(int16)},
{NewKey("int32"), int32(1), int32(0), new(int32)},
{NewKey("int64"), int64(1), int64(0), new(int64)},
{NewKey("uint16"), uint16(1), uint16(0), new(uint16)},
{NewKey("uint32"), uint32(1), uint32(0), new(uint32)},
{NewKey("uint64"), uint64(1), uint64(0), new(uint64)},
/*
{NewKey("int"), sdk.NewInt(1), *new(sdk.Int), new(sdk.Int)},
{NewKey("uint"), sdk.NewUint(1), *new(sdk.Uint), new(sdk.Uint)},
Expand All @@ -116,17 +116,17 @@ func TestGet(t *testing.T) {
}

for _, kv := range kvs {
require.NotPanics(t, func() { space.GetIfExists(ctx, NewKey([]byte("invalid")), kv.ptr) })
require.NotPanics(t, func() { space.GetIfExists(ctx, NewKey("invalid"), kv.ptr) })
require.Equal(t, kv.zero, reflect.ValueOf(kv.ptr).Elem().Interface())
require.Panics(t, func() { space.Get(ctx, NewKey([]byte("invalid")), kv.ptr) })
require.Panics(t, func() { space.Get(ctx, NewKey("invalid"), kv.ptr) })
require.Equal(t, kv.zero, reflect.ValueOf(kv.ptr).Elem().Interface())

require.NotPanics(t, func() { space.GetIfExists(ctx, kv.key, kv.ptr) })
require.Equal(t, kv.param, reflect.ValueOf(kv.ptr).Elem().Interface())
require.NotPanics(t, func() { space.Get(ctx, kv.key, kv.ptr) })
require.Equal(t, kv.param, reflect.ValueOf(kv.ptr).Elem().Interface())

require.Panics(t, func() { space.Get(ctx, NewKey([]byte("invalid")), kv.ptr) })
require.Panics(t, func() { space.Get(ctx, NewKey("invalid"), kv.ptr) })
require.Equal(t, kv.param, reflect.ValueOf(kv.ptr).Elem().Interface())

require.Panics(t, func() { space.Get(ctx, kv.key, nil) })
Expand Down
2 changes: 1 addition & 1 deletion x/params/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type KeyFieldPairs = space.KeyFieldPairs
type ParamStruct = space.ParamStruct

// nolint - reexport
func NewKey(keys ...[]byte) Key {
func NewKey(keys ...string) Key {
return space.NewKey(keys...)
}
func UnmarshalParamsFromMap(m map[string][]byte, cdc *wire.Codec, ps space.ParamStruct) error {
Expand Down
6 changes: 3 additions & 3 deletions x/params/space/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Key struct {

// Appending two keys with '/' as separator
// Checks alphanumericity
func (k Key) Append(keys ...[]byte) (res Key) {
func (k Key) Append(keys ...string) (res Key) {
res.s = make([]byte, len(k.s))
copy(res.s, k.s)

Expand All @@ -27,11 +27,11 @@ func (k Key) Append(keys ...[]byte) (res Key) {
}

// NewKey constructs a key from a list of strings
func NewKey(keys ...[]byte) (res Key) {
func NewKey(keys ...string) (res Key) {
if len(keys) < 1 {
panic("length of parameter keys must not be zero")
}
res = Key{keys[0]}
res = Key{[]byte(keys[0])}

return res.Append(keys[1:]...)
}
Expand Down
14 changes: 7 additions & 7 deletions x/slashing/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ const (
)

// nolint - Key generators for parameter access
func MaxEvidenceAgeKey() params.Key { return params.NewKey([]byte("MaxEvidenceAge")) }
func SignedBlocksWindowKey() params.Key { return params.NewKey([]byte("SignedBlocksWindow")) }
func MinSignedPerWindowKey() params.Key { return params.NewKey([]byte("MinSignedPerWindow")) }
func MaxEvidenceAgeKey() params.Key { return params.NewKey("MaxEvidenceAge") }
func SignedBlocksWindowKey() params.Key { return params.NewKey("SignedBlocksWindow") }
func MinSignedPerWindowKey() params.Key { return params.NewKey("MinSignedPerWindow") }
func DoubleSignUnbondDurationKey() params.Key {
return params.NewKey([]byte("DoubleSignUnbondDuration"))
return params.NewKey("DoubleSignUnbondDuration")
}
func DowntimeUnbondDurationKey() params.Key { return params.NewKey([]byte("DowntimeUnbondDuration")) }
func SlashFractionDoubleSignKey() params.Key { return params.NewKey([]byte("SlashFractionDoubleSign")) }
func SlashFractionDowntimeKey() params.Key { return params.NewKey([]byte("SlashFractionDowntime")) }
func DowntimeUnbondDurationKey() params.Key { return params.NewKey("DowntimeUnbondDuration") }
func SlashFractionDoubleSignKey() params.Key { return params.NewKey("SlashFractionDoubleSign") }
func SlashFractionDowntimeKey() params.Key { return params.NewKey("SlashFractionDowntime") }

// Cached parameter keys
var (
Expand Down
14 changes: 7 additions & 7 deletions x/stake/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
const defaultUnbondingTime time.Duration = 60 * 60 * 24 * 3 * time.Second

// nolint - Key generators for parameter access
func KeyInflationRateChange() params.Key { return params.NewKey([]byte("InflationRateChange")) }
func KeyInflationMax() params.Key { return params.NewKey([]byte("InflationMax")) }
func KeyInflationMin() params.Key { return params.NewKey([]byte("InflationMin")) }
func KeyGoalBonded() params.Key { return params.NewKey([]byte("GoalBonded")) }
func KeyUnbondingTime() params.Key { return params.NewKey([]byte("UnbondingTime")) }
func KeyMaxValidators() params.Key { return params.NewKey([]byte("MaxValidators")) }
func KeyBondDenom() params.Key { return params.NewKey([]byte("BondDenom")) }
func KeyInflationRateChange() params.Key { return params.NewKey("InflationRateChange") }
func KeyInflationMax() params.Key { return params.NewKey("InflationMax") }
func KeyInflationMin() params.Key { return params.NewKey("InflationMin") }
func KeyGoalBonded() params.Key { return params.NewKey("GoalBonded") }
func KeyUnbondingTime() params.Key { return params.NewKey("UnbondingTime") }
func KeyMaxValidators() params.Key { return params.NewKey("MaxValidators") }
func KeyBondDenom() params.Key { return params.NewKey("BondDenom") }

// Params defines the high level settings for staking
type Params struct {
Expand Down

0 comments on commit cef7fb4

Please sign in to comment.