Skip to content

Commit

Permalink
add submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Sep 13, 2018
1 parent 9850ab8 commit 5f7310d
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 5 deletions.
95 changes: 90 additions & 5 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ type BaseApp struct {
// Volatile
// checkState is set on initialization and reset on Commit.
// deliverState is set in InitChain and BeginBlock and cleared on Commit.
// See methods setCheckState and setDeliverState.
checkState *state // for CheckTx
deliverState *state // for DeliverTx
// See methods app.setCheckState and setDeliverState.
checkState *state // for CheckTx
checkParams sdk.ConsensusParams
deliverState *state // for DeliverTx
deliverParams sdk.ConsensusParams

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

// flag for sealing
Expand Down Expand Up @@ -213,6 +216,16 @@ func (app *BaseApp) setCheckState(header abci.Header) {
}
}

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()
app.deliverState = &state{
Expand All @@ -221,6 +234,16 @@ func (app *BaseApp) setDeliverState(header abci.Header) {
}
}

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

func (app *BaseApp) getDeliverContext() sdk.Context {
return app.deliverState.ctx.WithConsensusParams(app.deliverParams)
}

//______________________________________________________________________________

// ABCI
Expand All @@ -247,12 +270,14 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
// Initialize the deliver state and check state with ChainID and run initChain
app.setDeliverState(abci.Header{ChainID: req.ChainId})
app.setDeliverParams(req.ConsensusParams)
app.setCheckState(abci.Header{ChainID: req.ChainId})
app.setCheckParams(req.ConsensusParams)

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

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

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

// set the signed validators for addition to context in deliverTx
Expand Down Expand Up @@ -509,6 +534,12 @@ func (app *BaseApp) getContextForAnte(mode runTxMode, txBytes []byte) (ctx sdk.C
if mode == runTxModeDeliver {
ctx = ctx.WithSigningValidators(app.signedValidators)
}

params := ctx.ConsensusParams()
if params.TxSize.MaxGas != 0 {
ctx = ctx.WithGasMeter(sdk.NewGasMeter(params.TxSize.MaxGas))
}

return
}

Expand Down Expand Up @@ -654,6 +685,50 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
return
}

// Copied from tendermint/tendermint/types/params.go
func updateConsensusParams(params *abci.ConsensusParams, updates *abci.ConsensusParams) (res *abci.ConsensusParams) {
res = new(abci.ConsensusParams)
res.BlockSize = new(abci.BlockSize)
res.TxSize = new(abci.TxSize)
res.BlockGossip = new(abci.BlockGossip)

if updates == nil {
return
}

if updates.BlockSize != nil {
if updates.BlockSize.MaxBytes > 0 {
res.BlockSize.MaxBytes = updates.BlockSize.MaxBytes
} else {
res.BlockSize.MaxBytes = params.BlockSize.MaxBytes
}
if updates.BlockSize.MaxTxs > 0 {
res.BlockSize.MaxTxs = updates.BlockSize.MaxTxs
} else {
res.BlockSize.MaxTxs = params.BlockSize.MaxTxs
}
if updates.BlockSize.MaxGas > 0 {
res.BlockSize.MaxGas = updates.BlockSize.MaxGas
} else {
res.BlockSize.MaxGas = params.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
}
}
return
}

// EndBlock implements the ABCI application interface.
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
if app.deliverState.ms.TracingEnabled() {
Expand All @@ -664,6 +739,16 @@ 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)

if res.ConsensusParamUpdates != nil {
// Update consensus parameter
newparams := updateConsensusParams(params, res.ConsensusParamUpdates)
app.setDeliverParams(newparams)
}

return
}

Expand Down
2 changes: 2 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ func TestDeliverTx(t *testing.T) {
codec := wire.NewCodec()
registerTestCodec(codec)

app.InitChain(abci.RequestInitChain{})

nBlocks := 3
txPerHeight := 5
for blockN := 0; blockN < nBlocks; blockN++ {
Expand Down
34 changes: 34 additions & 0 deletions types/consensus_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package types
import (
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
)
// Reusing tmtype
type ConsensusParams tmtypes.ConsensusParams
// 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) {
// Manually set nil members to empty value
if abciparams == nil {
abciparams = &abci.ConsensusParams{
BlockSize: &abci.BlockSize{},
TxSize: &abci.TxSize{},
BlockGossip: &abci.BlockGossip{},
}
} else {
if abciparams.BlockSize == nil {
abciparams.BlockSize = &abci.BlockSize{}
}
if abciparams.TxSize == nil {
abciparams.TxSize = &abci.TxSize{}
}
if abciparams.BlockGossip == nil {
abciparams.BlockGossip = &abci.BlockGossip{}
}
}
*params = ConsensusParams(tmtypes.PB2TM.ConsensusParams(abciparams))
}
6 changes: 6 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func (c Context) BlockHeader() abci.Header {
func (c Context) BlockHeight() int64 {
return c.Value(contextKeyBlockHeight).(int64)
}
func (c Context) ConsensusParams() ConsensusParams {
return c.Value(contextKeyConsensusParams).(ConsensusParams)
}
func (c Context) ChainID() string {
return c.Value(contextKeyChainID).(string)
}
Expand Down Expand Up @@ -187,6 +190,9 @@ func (c Context) WithBlockHeader(header abci.Header) Context {
func (c Context) WithBlockHeight(height int64) Context {
return c.withValue(contextKeyBlockHeight, height)
}
func (c Context) WithConsensusParams(params ConsensusParams) Context {
return c.withValue(contextKeyConsensusParams, params)
}
func (c Context) WithChainID(chainID string) Context {
return c.withValue(contextKeyChainID, chainID)
}
Expand Down
22 changes: 22 additions & 0 deletions x/params/circuit/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package circuit

import (
sdk "github.com/cosmos/cosmos-sdk/types"
params "github.com/cosmos/cosmos-sdk/x/params/space"
)

// NewAnteHandler returns an AnteHandler that checks
// whether msg type is circuit brake or not
func NewAnteHandler(space params.Space) sdk.AnteHandler {
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, sdk.Result, bool) {
for _, msg := range tx.GetMsgs() {
key := CircuitBrakeKey(msg.Type())
var brake bool
space.GetIfExists(ctx, key, &brake)
if brake {
return ctx, sdk.ErrUnauthorized("msg type circuit brake").Result(), true
}
}
return ctx, sdk.Result{}, false
}
}
58 changes: 58 additions & 0 deletions x/params/circuit/ante_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package circuit

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/space"
)

type tx []sdk.Msg

func (tx tx) GetMsgs() []sdk.Msg { return tx }

type msg struct{}

func (msg) ValidateBasic() sdk.Error { return nil }

func (msg) GetSignBytes() []byte { return nil }

func (msg) GetSigners() []sdk.AccAddress { return nil }

type msg1 struct{ msg }

func (msg1) Type() string { return "msg1" }

type msg2 struct{ msg }

func (msg2) Type() string { return "msg2" }

func TestAnteHandler(t *testing.T) {
msg1key := space.NewKey("msg1")
msg2key := space.NewKey("msg2")

ctx, space, _ := space.DefaultTestComponents(t)

data := GenesisState{
CircuitBrakeTypes: []string{"msg2"},
}

InitGenesis(ctx, space, data)

ante := NewAnteHandler(space)

_, _, abort := ante(ctx, tx{msg1{}}, false)
require.False(t, abort)
_, _, abort = ante(ctx, tx{msg2{}}, false)
require.True(t, abort)

space.Set(ctx, msg1key, true)
space.Set(ctx, msg2key, false)

_, _, abort = ante(ctx, tx{msg1{}}, false)
require.True(t, abort)
_, _, abort = ante(ctx, tx{msg2{}}, false)
require.False(t, abort)
}
18 changes: 18 additions & 0 deletions x/params/circuit/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package circuit

import (
sdk "github.com/cosmos/cosmos-sdk/types"
params "github.com/cosmos/cosmos-sdk/x/params/space"
)

// GenesisState defines initial circuit brake msg types(usually empty)
type GenesisState struct {
CircuitBrakeTypes []string `json:"circuit-brake-types"`
}

// InitGenesis spaces activated types to param space
func InitGenesis(ctx sdk.Context, space params.Space, data GenesisState) {
for _, ty := range data.CircuitBrakeTypes {
space.Set(ctx, CircuitBrakeKey(ty), true)
}
}
13 changes: 13 additions & 0 deletions x/params/circuit/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package circuit

import (
params "github.com/cosmos/cosmos-sdk/x/params/space"
)

// Default parameter namespace
const (
DefaultParamSpace = "circuit"
)

// CircuitBrakeKey - returns key for boolean flag indicating circuit brake
func CircuitBrakeKey(msgtype string) params.Key { return params.NewKey(msgtype) }
44 changes: 44 additions & 0 deletions x/params/consensus/endblocker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package consensus

import (
abci "github.com/tendermint/tendermint/abci/types"

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

params "github.com/cosmos/cosmos-sdk/x/params/space"
)

// EndBlock returns consensus parameters set in the block
func EndBlock(ctx sdk.Context, space params.Space) (updates *abci.ConsensusParams) {
updates = &abci.ConsensusParams{
BlockSize: new(abci.BlockSize),
TxSize: new(abci.TxSize),
BlockGossip: new(abci.BlockGossip),
}

if space.Modified(ctx, blockMaxBytesKey) {
space.Get(ctx, blockMaxBytesKey, &updates.BlockSize.MaxBytes)
}

if space.Modified(ctx, blockMaxTxsKey) {
space.Get(ctx, blockMaxTxsKey, &updates.BlockSize.MaxTxs)
}

if space.Modified(ctx, blockMaxGasKey) {
space.Get(ctx, blockMaxGasKey, &updates.BlockSize.MaxGas)
}

if space.Modified(ctx, txMaxBytesKey) {
space.Get(ctx, txMaxBytesKey, &updates.TxSize.MaxBytes)
}

if space.Modified(ctx, txMaxGasKey) {
space.Get(ctx, txMaxGasKey, &updates.TxSize.MaxGas)
}

if space.Modified(ctx, blockPartSizeBytesKey) {
space.Get(ctx, blockPartSizeBytesKey, &updates.BlockGossip.BlockPartSizeBytes)
}

return
}
Loading

0 comments on commit 5f7310d

Please sign in to comment.