2017-07-06 20:37:45 -07:00
|
|
|
//nolint
|
2017-06-29 06:40:26 -07:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
2017-07-03 09:10:46 -07:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
|
|
|
|
2017-06-29 06:40:26 -07:00
|
|
|
"github.com/tendermint/basecoin"
|
2017-07-06 05:23:38 -07:00
|
|
|
"github.com/tendermint/basecoin/state"
|
2017-06-29 06:40:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Middleware is anything that wraps another handler to enhance functionality.
|
|
|
|
//
|
|
|
|
// You can use utilities in handlers to construct them, the interfaces
|
|
|
|
// are exposed in the top-level package to avoid import loops.
|
|
|
|
type Middleware interface {
|
|
|
|
CheckerMiddle
|
|
|
|
DeliverMiddle
|
2017-07-30 09:57:48 -07:00
|
|
|
InitStateMiddle
|
2017-06-29 06:40:26 -07:00
|
|
|
basecoin.Named
|
|
|
|
}
|
2017-07-03 09:10:46 -07:00
|
|
|
|
|
|
|
type CheckerMiddle interface {
|
2017-07-26 16:55:05 -07:00
|
|
|
CheckTx(ctx basecoin.Context, store state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
tx basecoin.Tx, next basecoin.Checker) (basecoin.CheckResult, error)
|
2017-07-03 09:10:46 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
type CheckerMiddleFunc func(basecoin.Context, state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
basecoin.Tx, basecoin.Checker) (basecoin.CheckResult, error)
|
2017-07-03 09:10:46 -07:00
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
func (c CheckerMiddleFunc) CheckTx(ctx basecoin.Context, store state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
tx basecoin.Tx, next basecoin.Checker) (basecoin.CheckResult, error) {
|
2017-07-03 09:10:46 -07:00
|
|
|
return c(ctx, store, tx, next)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeliverMiddle interface {
|
2017-07-26 16:55:05 -07:00
|
|
|
DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx,
|
2017-07-30 08:45:08 -07:00
|
|
|
next basecoin.Deliver) (basecoin.DeliverResult, error)
|
2017-07-03 09:10:46 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
type DeliverMiddleFunc func(basecoin.Context, state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
basecoin.Tx, basecoin.Deliver) (basecoin.DeliverResult, error)
|
2017-07-03 09:10:46 -07:00
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
func (d DeliverMiddleFunc) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
tx basecoin.Tx, next basecoin.Deliver) (basecoin.DeliverResult, error) {
|
2017-07-03 09:10:46 -07:00
|
|
|
return d(ctx, store, tx, next)
|
|
|
|
}
|
|
|
|
|
2017-07-30 09:57:48 -07:00
|
|
|
type InitStateMiddle interface {
|
|
|
|
InitState(l log.Logger, store state.SimpleDB, module,
|
|
|
|
key, value string, next basecoin.InitStater) (string, error)
|
2017-07-03 09:10:46 -07:00
|
|
|
}
|
|
|
|
|
2017-07-30 09:57:48 -07:00
|
|
|
type InitStateMiddleFunc func(log.Logger, state.SimpleDB,
|
|
|
|
string, string, string, basecoin.InitStater) (string, error)
|
2017-07-03 09:10:46 -07:00
|
|
|
|
2017-07-30 09:57:48 -07:00
|
|
|
func (c InitStateMiddleFunc) InitState(l log.Logger, store state.SimpleDB,
|
|
|
|
module, key, value string, next basecoin.InitStater) (string, error) {
|
2017-07-04 03:22:06 -07:00
|
|
|
return c(l, store, module, key, value, next)
|
2017-07-03 09:10:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// holders
|
|
|
|
type PassCheck struct{}
|
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
func (_ PassCheck) CheckTx(ctx basecoin.Context, store state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
tx basecoin.Tx, next basecoin.Checker) (basecoin.CheckResult, error) {
|
2017-07-03 09:10:46 -07:00
|
|
|
return next.CheckTx(ctx, store, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PassDeliver struct{}
|
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
func (_ PassDeliver) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
tx basecoin.Tx, next basecoin.Deliver) (basecoin.DeliverResult, error) {
|
2017-07-03 09:10:46 -07:00
|
|
|
return next.DeliverTx(ctx, store, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PassOption struct{}
|
|
|
|
|
2017-07-30 09:57:48 -07:00
|
|
|
func (_ PassOption) InitState(l log.Logger, store state.SimpleDB, module,
|
|
|
|
key, value string, next basecoin.InitStater) (string, error) {
|
|
|
|
return next.InitState(l, store, module, key, value)
|
2017-07-04 03:22:06 -07:00
|
|
|
}
|
|
|
|
|
2017-07-04 05:47:46 -07:00
|
|
|
type NopOption struct{}
|
|
|
|
|
2017-07-30 09:57:48 -07:00
|
|
|
func (_ NopOption) InitState(l log.Logger, store state.SimpleDB, module,
|
|
|
|
key, value string, next basecoin.InitStater) (string, error) {
|
2017-07-04 05:47:46 -07:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 03:22:06 -07:00
|
|
|
// Dispatchable is like middleware, except the meaning of "next" is different.
|
|
|
|
// Whereas in the middleware, it is the next handler that we should pass the same tx into,
|
|
|
|
// for dispatchers, it is a dispatcher, which it can use to
|
|
|
|
type Dispatchable interface {
|
|
|
|
Middleware
|
|
|
|
AssertDispatcher()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapHandler turns a basecoin.Handler into a Dispatchable interface
|
|
|
|
func WrapHandler(h basecoin.Handler) Dispatchable {
|
|
|
|
return wrapped{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrapped struct {
|
|
|
|
h basecoin.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Dispatchable = wrapped{}
|
|
|
|
|
|
|
|
func (w wrapped) AssertDispatcher() {}
|
|
|
|
|
|
|
|
func (w wrapped) Name() string {
|
|
|
|
return w.h.Name()
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
func (w wrapped) CheckTx(ctx basecoin.Context, store state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
tx basecoin.Tx, _ basecoin.Checker) (basecoin.CheckResult, error) {
|
2017-07-04 03:22:06 -07:00
|
|
|
return w.h.CheckTx(ctx, store, tx)
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:55:05 -07:00
|
|
|
func (w wrapped) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
|
2017-07-30 08:45:08 -07:00
|
|
|
tx basecoin.Tx, _ basecoin.Deliver) (basecoin.DeliverResult, error) {
|
2017-07-04 03:22:06 -07:00
|
|
|
return w.h.DeliverTx(ctx, store, tx)
|
|
|
|
}
|
|
|
|
|
2017-07-30 09:57:48 -07:00
|
|
|
func (w wrapped) InitState(l log.Logger, store state.SimpleDB,
|
|
|
|
module, key, value string, _ basecoin.InitStater) (string, error) {
|
|
|
|
return w.h.InitState(l, store, module, key, value)
|
2017-07-03 09:10:46 -07:00
|
|
|
}
|