2020-03-04 16:41:13 +00:00
|
|
|
package ante
|
2020-03-04 14:35:16 +00:00
|
|
|
|
2020-03-04 16:41:13 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-03-04 19:16:27 +00:00
|
|
|
"github.com/kava-labs/kava/x/shutdown/keeper"
|
2020-03-04 16:41:13 +00:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CircuitBreakerDecorator needs to be combined with other standard decorators (from auth) to create the app's AnteHandler.
|
2020-03-04 14:35:16 +00:00
|
|
|
|
|
|
|
// CircuitBreakerDecorator errors if a tx contains a disallowed msg type
|
|
|
|
// Call next AnteHandler if all msgs are allowed
|
|
|
|
type CircuitBreakerDecorator struct {
|
2020-03-04 16:41:13 +00:00
|
|
|
cbk keeper.Keeper
|
2020-03-04 14:35:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 16:41:13 +00:00
|
|
|
func NewCircuitBreakerDecorator(cbk keeper.Keeper) CircuitBreakerDecorator {
|
2020-03-04 14:35:16 +00:00
|
|
|
return CircuitBreakerDecorator{
|
2020-03-04 16:41:13 +00:00
|
|
|
cbk: cbk,
|
2020-03-04 14:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cbd CircuitBreakerDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
|
|
|
|
|
|
|
// get msg route, error if not allowed
|
2020-03-04 16:41:13 +00:00
|
|
|
disallowedRoutes := cbd.cbk.GetMsgRoutes(ctx)
|
|
|
|
for _, m := range tx.GetMsgs() {
|
|
|
|
for _, r := range disallowedRoutes {
|
|
|
|
if r.Route == m.Route() && r.Msg == m.Type() {
|
2020-03-04 14:35:16 +00:00
|
|
|
return ctx, fmt.Errorf("route %s has been circuit broken, tx rejected", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|