Start separating middleware and handler

This commit is contained in:
Ethan Frey 2017-06-27 19:22:25 +02:00
parent 52f7a47ef2
commit eed1bf753d
3 changed files with 50 additions and 13 deletions

View File

@ -9,11 +9,31 @@ import (
"github.com/tendermint/basecoin/types"
)
type Named interface {
Name() string
}
type Checker interface {
CheckTx(ctx Context, store types.KVStore, tx Tx) (Result, error)
}
type Deliver interface {
DeliverTx(ctx Context, store types.KVStore, tx Tx) (Result, error)
}
type CheckerMiddle interface {
CheckTx(ctx Context, store types.KVStore, tx Tx, next Checker) (Result, error)
}
type DeliverMiddle interface {
DeliverTx(ctx Context, store types.KVStore, tx Tx, next Deliver) (Result, error)
}
// Handler is anything that processes a transaction
type Handler interface {
CheckTx(ctx Context, store types.KVStore, tx Tx) (Result, error)
DeliverTx(ctx Context, store types.KVStore, tx Tx) (Result, error)
Checker
Deliver
Named
// TODO: flesh these out as well
// SetOption(store types.KVStore, key, value string) (log string)
// InitChain(store types.KVStore, vals []*abci.Validator)
@ -21,12 +41,15 @@ type Handler interface {
// EndBlock(store types.KVStore, height uint64) abci.ResponseEndBlock
}
// different apps to authorize
const (
Sigs = "sigs"
IBC = "ibc"
Role = "role"
)
// 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
Named
}
// TODO: handle this in some secure way, only certain apps can add permissions
type Permission struct {

View File

@ -7,6 +7,10 @@ import (
"github.com/tendermint/basecoin/types"
)
const (
NameFee = "fee"
)
type AccountChecker interface {
// Get amount checks the current amount
GetAmount(store types.KVStore, addr []byte) (types.Coins, error)
@ -26,6 +30,10 @@ func (h SimpleFeeHandler) Next() basecoin.Handler {
return h.Inner
}
func (_ SimpleFeeHandler) Name() string {
return NameFee
}
var _ basecoin.Handler = SimpleFeeHandler{}
// Yes, I know refactor a bit... really too late already
@ -41,7 +49,7 @@ func (h SimpleFeeHandler) CheckTx(ctx basecoin.Context, store types.KVStore, tx
return res, errors.InsufficientFees()
}
if !ctx.HasPermission(Sigs, feeTx.Payer) {
if !ctx.HasPermission(NameSigs, feeTx.Payer) {
return res, errors.Unauthorized()
}
@ -64,7 +72,7 @@ func (h SimpleFeeHandler) DeliverTx(ctx basecoin.Context, store types.KVStore, t
return res, errors.InsufficientFees()
}
if !ctx.HasPermission(Sigs, feeTx.Payer) {
if !ctx.HasPermission(NameSigs, feeTx.Payer) {
return res, errors.Unauthorized()
}

View File

@ -9,13 +9,19 @@ import (
)
// app name for auth
const Sigs = "sigs"
const (
NameSigs = "sigs"
)
type SignedHandler struct {
AllowMultiSig bool
Inner basecoin.Handler
}
func (_ SignedHandler) Name() string {
return NameSigs
}
func (h SignedHandler) Next() basecoin.Handler {
return h.Inner
}
@ -65,7 +71,7 @@ func (h SignedHandler) DeliverTx(ctx basecoin.Context, store types.KVStore, tx b
func addSigners(ctx basecoin.Context, sigs []crypto.PubKey) basecoin.Context {
perms := make([]basecoin.Permission, len(sigs))
for i, s := range sigs {
perms[i] = basecoin.Permission{App: Sigs, Address: s.Address()}
perms[i] = basecoin.Permission{App: NameSigs, Address: s.Address()}
}
// add the signers to the context and continue