-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
544 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.