From eed1bf753db9574f53f0a8091672a5ae645ab4ee Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 27 Jun 2017 19:22:25 +0200 Subject: [PATCH] Start separating middleware and handler --- handler.go | 41 ++++++++++++++++++++++++++++++++--------- handlers/base.go | 12 ++++++++++-- handlers/sigs.go | 10 ++++++++-- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/handler.go b/handler.go index cbc88d4fed..acebaa4981 100644 --- a/handler.go +++ b/handler.go @@ -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 { diff --git a/handlers/base.go b/handlers/base.go index 99a9ce08ad..26ca282a79 100644 --- a/handlers/base.go +++ b/handlers/base.go @@ -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() } diff --git a/handlers/sigs.go b/handlers/sigs.go index 28baf8f8a1..50d80636e5 100644 --- a/handlers/sigs.go +++ b/handlers/sigs.go @@ -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