Moved the handlers from stack into modules

This commit is contained in:
Ethan Frey 2017-07-06 16:00:54 +02:00
parent 768427dcc0
commit a047e210fa
18 changed files with 104 additions and 72 deletions

View File

@ -11,6 +11,8 @@ import (
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
"github.com/tendermint/basecoin/errors" "github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/base"
"github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/stack"
sm "github.com/tendermint/basecoin/state" sm "github.com/tendermint/basecoin/state"
@ -50,7 +52,12 @@ func DefaultHandler() basecoin.Handler {
// use the default stack // use the default stack
h := coin.NewHandler() h := coin.NewHandler()
d := stack.NewDispatcher(stack.WrapHandler(h)) d := stack.NewDispatcher(stack.WrapHandler(h))
return stack.NewDefault().Use(d) return stack.New(
base.Logger{},
stack.Recovery{},
auth.Signatures{},
base.Chain{},
).Use(d)
} }
// GetState - XXX For testing, not thread safe! // GetState - XXX For testing, not thread safe!

View File

@ -10,8 +10,8 @@ import (
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs" "github.com/tendermint/basecoin/txs"
wire "github.com/tendermint/go-wire" wire "github.com/tendermint/go-wire"
@ -87,7 +87,7 @@ func getBalance(key basecoin.Actor, state state.KVStore) (coin.Coins, error) {
} }
func getAddr(addr []byte, state state.KVStore) (coin.Coins, error) { func getAddr(addr []byte, state state.KVStore) (coin.Coins, error) {
actor := stack.SigPerm(addr) actor := auth.SigPerm(addr)
return getBalance(actor, state) return getBalance(actor, state)
} }

View File

@ -13,8 +13,8 @@ import (
txcmd "github.com/tendermint/light-client/commands/txs" txcmd "github.com/tendermint/light-client/commands/txs"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/txs" "github.com/tendermint/basecoin/txs"
) )
@ -82,7 +82,7 @@ func readSendTxFlags() (tx basecoin.Tx, err error) {
if err != nil { if err != nil {
return tx, err return tx, err
} }
toAddr := stack.SigPerm(to) toAddr := auth.SigPerm(to)
toAddr.ChainID = chain toAddr.ChainID = chain
// //parse the fee and amounts into coin types // //parse the fee and amounts into coin types
@ -102,7 +102,7 @@ func readSendTxFlags() (tx basecoin.Tx, err error) {
var fromAddr basecoin.Actor var fromAddr basecoin.Actor
signer := txcmd.GetSigner() signer := txcmd.GetSigner()
if !signer.Empty() { if !signer.Empty() {
fromAddr = stack.SigPerm(signer.Address()) fromAddr = auth.SigPerm(signer.Address())
} }
// craft the inputs and outputs // craft the inputs and outputs

View File

@ -11,8 +11,8 @@ import (
proofcmd "github.com/tendermint/light-client/commands/proofs" proofcmd "github.com/tendermint/light-client/commands/proofs"
"github.com/tendermint/light-client/proofs" "github.com/tendermint/light-client/proofs"
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack"
) )
// AccountQueryCmd - command to query an account // AccountQueryCmd - command to query an account
@ -27,7 +27,7 @@ func doAccountQuery(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
key := coin.NewAccountant("").MakeKey(stack.SigPerm(addr)) key := coin.NewAccountant("").MakeKey(auth.SigPerm(addr))
acc := coin.Account{} acc := coin.Account{}
proof, err := proofcmd.GetAndParseAppProof(key, &acc) proof, err := proofcmd.GetAndParseAppProof(key, &acc)

View File

@ -8,6 +8,8 @@ import (
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors" "github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/base"
"github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
@ -95,7 +97,12 @@ func NewHandler() basecoin.Handler {
stack.WrapHandler(coin), stack.WrapHandler(coin),
counter, counter,
) )
return stack.NewDefault().Use(dispatcher) return stack.New(
base.Logger{},
stack.Recovery{},
auth.Signatures{},
base.Chain{},
).Use(dispatcher)
} }
// Handler the counter transaction processing handler // Handler the counter transaction processing handler
@ -134,7 +141,7 @@ func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoi
// handle coin movement.... like, actually decrement the other account // handle coin movement.... like, actually decrement the other account
if !ctr.Fee.IsZero() { if !ctr.Fee.IsZero() {
// take the coins and put them in out account! // take the coins and put them in out account!
senders := ctx.GetPermissions("", stack.NameSigs) senders := ctx.GetPermissions("", auth.NameSigs)
if len(senders) == 0 { if len(senders) == 0 {
return res, errors.ErrMissingSignature() return res, errors.ErrMissingSignature()
} }

View File

@ -1,28 +1,33 @@
package stack package auth
import ( import (
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors" "github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
) )
// app name for auth //nolint
const ( const (
NameSigs = "sigs" NameSigs = "sigs"
) )
// Signatures parses out go-crypto signatures and adds permissions to the
// context for use inside the application
type Signatures struct { type Signatures struct {
PassOption stack.PassOption
} }
func (_ Signatures) Name() string { // Name of the module - fulfills Middleware interface
func (Signatures) Name() string {
return NameSigs return NameSigs
} }
var _ Middleware = Signatures{} var _ stack.Middleware = Signatures{}
// SigPerm takes the binary address from PubKey.Address and makes it an Actor
func SigPerm(addr []byte) basecoin.Actor { func SigPerm(addr []byte) basecoin.Actor {
return basecoin.NewActor(NameSigs, addr) return basecoin.NewActor(NameSigs, addr)
} }
@ -33,7 +38,8 @@ type Signed interface {
Signers() ([]crypto.PubKey, error) Signers() ([]crypto.PubKey, error)
} }
func (h Signatures) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { // CheckTx verifies the signatures are correct - fulfills Middlware interface
func (Signatures) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
sigs, tnext, err := getSigners(tx) sigs, tnext, err := getSigners(tx)
if err != nil { if err != nil {
return res, err return res, err
@ -42,7 +48,8 @@ func (h Signatures) CheckTx(ctx basecoin.Context, store state.KVStore, tx baseco
return next.CheckTx(ctx2, store, tnext) return next.CheckTx(ctx2, store, tnext)
} }
func (h Signatures) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { // DeliverTx verifies the signatures are correct - fulfills Middlware interface
func (Signatures) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
sigs, tnext, err := getSigners(tx) sigs, tnext, err := getSigners(tx)
if err != nil { if err != nil {
return res, err return res, err

View File

@ -1,4 +1,4 @@
package stack package auth
import ( import (
"strconv" "strconv"
@ -10,6 +10,7 @@ import (
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs" "github.com/tendermint/basecoin/txs"
) )
@ -18,7 +19,7 @@ func TestSignatureChecks(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// generic args // generic args
ctx := NewContext("test-chain", log.NewNopLogger()) ctx := stack.NewContext("test-chain", log.NewNopLogger())
store := state.NewMemKVStore() store := state.NewMemKVStore()
raw := txs.NewRaw([]byte{1, 2, 3, 4}) raw := txs.NewRaw([]byte{1, 2, 3, 4})
@ -56,11 +57,10 @@ func TestSignatureChecks(t *testing.T) {
idx := strconv.Itoa(i) idx := strconv.Itoa(i)
// make the stack check for the given permission // make the stack check for the given permission
app := New( app := stack.New(
Recovery{}, // we need this so panics turn to errors
Signatures{}, Signatures{},
CheckMiddleware{Required: tc.check}, stack.CheckMiddleware{Required: tc.check},
).Use(OKHandler{}) ).Use(stack.OKHandler{})
var tx basecoin.Tx var tx basecoin.Tx
// this does the signing as needed // this does the signing as needed
@ -80,11 +80,8 @@ func TestSignatureChecks(t *testing.T) {
tx = otx.Wrap() tx = otx.Wrap()
} }
// this will trivial expose the printing error...
// _, err := app.CheckTx(ctx, store, raw)
_, err := app.CheckTx(ctx, store, tx) _, err := app.CheckTx(ctx, store, tx)
if tc.valid { if tc.valid {
// TODO: why doen't tmerror print properly???
assert.Nil(err, "%d: %+v", i, err) assert.Nil(err, "%d: %+v", i, err)
} else { } else {
assert.NotNil(err, idx) assert.NotNil(err, idx)

View File

@ -1,27 +1,31 @@
package stack package base
import ( import (
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors" "github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs" "github.com/tendermint/basecoin/txs"
) )
//nolint
const ( const (
NameChain = "chan" NameChain = "chan"
) )
// Chain enforces that this tx was bound to the named chain // Chain enforces that this tx was bound to the named chain
type Chain struct { type Chain struct {
PassOption stack.PassOption
} }
func (_ Chain) Name() string { // Name of the module - fulfills Middleware interface
return NameRecovery func (Chain) Name() string {
return NameChain
} }
var _ Middleware = Chain{} var _ stack.Middleware = Chain{}
// CheckTx makes sure we are on the proper chain - fulfills Middlware interface
func (c Chain) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { func (c Chain) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
stx, err := c.checkChain(ctx.ChainID(), tx) stx, err := c.checkChain(ctx.ChainID(), tx)
if err != nil { if err != nil {
@ -30,6 +34,7 @@ func (c Chain) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx
return next.CheckTx(ctx, store, stx) return next.CheckTx(ctx, store, stx)
} }
// DeliverTx makes sure we are on the proper chain - fulfills Middlware interface
func (c Chain) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { func (c Chain) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
stx, err := c.checkChain(ctx.ChainID(), tx) stx, err := c.checkChain(ctx.ChainID(), tx)
if err != nil { if err != nil {

View File

@ -1,4 +1,4 @@
package stack package base
import ( import (
"strconv" "strconv"
@ -9,6 +9,7 @@ import (
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs" "github.com/tendermint/basecoin/txs"
) )
@ -30,12 +31,12 @@ func TestChain(t *testing.T) {
} }
// generic args here... // generic args here...
ctx := NewContext(chainID, log.NewNopLogger()) ctx := stack.NewContext(chainID, log.NewNopLogger())
store := state.NewMemKVStore() store := state.NewMemKVStore()
// build the stack // build the stack
ok := OKHandler{Log: msg} ok := stack.OKHandler{Log: msg}
app := New(Chain{}).Use(ok) app := stack.New(Chain{}).Use(ok)
for idx, tc := range cases { for idx, tc := range cases {
i := strconv.Itoa(idx) i := strconv.Itoa(idx)

View File

@ -1,4 +1,4 @@
package stack package base
import ( import (
"time" "time"
@ -6,9 +6,11 @@ import (
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
) )
// nolint
const ( const (
NameLogger = "lggr" NameLogger = "lggr"
) )
@ -16,13 +18,15 @@ const (
// Logger catches any panics and returns them as errors instead // Logger catches any panics and returns them as errors instead
type Logger struct{} type Logger struct{}
func (_ Logger) Name() string { // Name of the module - fulfills Middleware interface
func (Logger) Name() string {
return NameLogger return NameLogger
} }
var _ Middleware = Logger{} var _ stack.Middleware = Logger{}
func (_ Logger) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { // CheckTx logs time and result - fulfills Middlware interface
func (Logger) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
start := time.Now() start := time.Now()
res, err = next.CheckTx(ctx, store, tx) res, err = next.CheckTx(ctx, store, tx)
delta := time.Now().Sub(start) delta := time.Now().Sub(start)
@ -36,7 +40,8 @@ func (_ Logger) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.T
return return
} }
func (_ Logger) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { // DeliverTx logs time and result - fulfills Middlware interface
func (Logger) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
start := time.Now() start := time.Now()
res, err = next.DeliverTx(ctx, store, tx) res, err = next.DeliverTx(ctx, store, tx)
delta := time.Now().Sub(start) delta := time.Now().Sub(start)
@ -50,7 +55,8 @@ func (_ Logger) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin
return return
} }
func (_ Logger) SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (string, error) { // SetOption logs time and result - fulfills Middlware interface
func (Logger) SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (string, error) {
start := time.Now() start := time.Now()
res, err := next.SetOption(l, store, module, key, value) res, err := next.SetOption(l, store, module, key, value)
delta := time.Now().Sub(start) delta := time.Now().Sub(start)

View File

@ -1,4 +1,4 @@
package stack package base
import ( import (
"strings" "strings"
@ -7,32 +7,38 @@ import (
"github.com/tendermint/go-wire/data" "github.com/tendermint/go-wire/data"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/txs" "github.com/tendermint/basecoin/txs"
) )
//nolint
const ( const (
NameMultiplexer = "mplx" NameMultiplexer = "mplx"
) )
// Multiplexer grabs a MultiTx and sends them sequentially down the line
type Multiplexer struct { type Multiplexer struct {
PassOption stack.PassOption
} }
func (_ Multiplexer) Name() string { // Name of the module - fulfills Middleware interface
func (Multiplexer) Name() string {
return NameMultiplexer return NameMultiplexer
} }
var _ Middleware = Multiplexer{} var _ stack.Middleware = Multiplexer{}
func (_ Multiplexer) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { // CheckTx splits the input tx and checks them all - fulfills Middlware interface
func (Multiplexer) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok { if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok {
return runAll(ctx, store, mtx.Txs, next.CheckTx) return runAll(ctx, store, mtx.Txs, next.CheckTx)
} }
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
func (_ Multiplexer) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { // DeliverTx splits the input tx and checks them all - fulfills Middlware interface
func (Multiplexer) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok { if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok {
return runAll(ctx, store, mtx.Txs, next.DeliverTx) return runAll(ctx, store, mtx.Txs, next.DeliverTx)
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors" "github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
) )
@ -98,7 +98,7 @@ func (h Handler) SetOption(l log.Logger, store state.KVStore, module, key, value
return "", ErrInvalidAddress() return "", ErrInvalidAddress()
} }
// this sets the permission for a public key signature, use that app // this sets the permission for a public key signature, use that app
actor := stack.SigPerm(addr) actor := auth.SigPerm(addr)
err = storeAccount(store, h.MakeKey(actor), acc.ToAccount()) err = storeAccount(store, h.MakeKey(actor), acc.ToAccount())
if err != nil { if err != nil {
return "", err return "", err

View File

@ -11,6 +11,7 @@ import (
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
) )
@ -172,7 +173,7 @@ func TestSetOption(t *testing.T) {
// some sample settings // some sample settings
pk := crypto.GenPrivKeySecp256k1().Wrap() pk := crypto.GenPrivKeySecp256k1().Wrap()
addr := pk.PubKey().Address() addr := pk.PubKey().Address()
actor := basecoin.Actor{App: stack.NameSigs, Address: addr} actor := auth.SigPerm(addr)
someCoins := Coins{{"atom", 123}} someCoins := Coins{{"atom", 123}}
otherCoins := Coins{{"eth", 11}} otherCoins := Coins{{"eth", 11}}

View File

@ -1,11 +1,11 @@
package coin package coin
import ( import (
"github.com/tendermint/basecoin/modules/auth"
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire/data" "github.com/tendermint/go-wire/data"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/stack"
) )
// AccountWithKey is a helper for tests, that includes and account // AccountWithKey is a helper for tests, that includes and account
@ -31,7 +31,7 @@ func (a *AccountWithKey) Address() []byte {
// Actor returns the basecoin actor associated with this account // Actor returns the basecoin actor associated with this account
func (a *AccountWithKey) Actor() basecoin.Actor { func (a *AccountWithKey) Actor() basecoin.Actor {
return stack.SigPerm(a.Key.PubKey().Address()) return auth.SigPerm(a.Key.PubKey().Address())
} }
// MakeOption returns a string to use with SetOption to initialize this account // MakeOption returns a string to use with SetOption to initialize this account

View File

@ -60,20 +60,6 @@ func New(middlewares ...Middleware) *Stack {
} }
} }
// NewDefault sets up the common middlewares before your custom stack.
//
// This is logger, recovery, signature, and chain
func NewDefault(middlewares ...Middleware) *Stack {
mids := []Middleware{
Logger{},
Recovery{},
Signatures{},
Chain{},
}
mids = append(mids, middlewares...)
return New(mids...)
}
// Use sets the final handler for the stack and prepares it for use // Use sets the final handler for the stack and prepares it for use
func (s *Stack) Use(handler basecoin.Handler) *Stack { func (s *Stack) Use(handler basecoin.Handler) *Stack {
if handler == nil { if handler == nil {

View File

@ -15,6 +15,10 @@ import (
"github.com/tendermint/basecoin/txs" "github.com/tendermint/basecoin/txs"
) )
const (
nameSigner = "signer"
)
func TestPermissionSandbox(t *testing.T) { func TestPermissionSandbox(t *testing.T) {
require := require.New(t) require := require.New(t)
@ -28,7 +32,7 @@ func TestPermissionSandbox(t *testing.T) {
// test cases to make sure permissioning is solid // test cases to make sure permissioning is solid
grantee := basecoin.Actor{App: NameGrant, Address: []byte{1}} grantee := basecoin.Actor{App: NameGrant, Address: []byte{1}}
grantee2 := basecoin.Actor{App: NameGrant, Address: []byte{2}} grantee2 := basecoin.Actor{App: NameGrant, Address: []byte{2}}
signer := basecoin.Actor{App: NameSigs, Address: []byte{1}} signer := basecoin.Actor{App: nameSigner, Address: []byte{1}}
cases := []struct { cases := []struct {
grant basecoin.Actor grant basecoin.Actor
require basecoin.Actor require basecoin.Actor

View File

@ -10,6 +10,7 @@ import (
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
) )
// nolint
const ( const (
NameRecovery = "rcvr" NameRecovery = "rcvr"
) )
@ -17,13 +18,15 @@ const (
// Recovery catches any panics and returns them as errors instead // Recovery catches any panics and returns them as errors instead
type Recovery struct{} type Recovery struct{}
func (_ Recovery) Name() string { // Name of the module - fulfills Middleware interface
func (Recovery) Name() string {
return NameRecovery return NameRecovery
} }
var _ Middleware = Recovery{} var _ Middleware = Recovery{}
func (_ Recovery) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { // CheckTx catches any panic and converts to error - fulfills Middlware interface
func (Recovery) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
err = normalizePanic(r) err = normalizePanic(r)
@ -32,7 +35,8 @@ func (_ Recovery) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
func (_ Recovery) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { // DeliverTx catches any panic and converts to error - fulfills Middlware interface
func (Recovery) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
err = normalizePanic(r) err = normalizePanic(r)
@ -41,7 +45,8 @@ func (_ Recovery) DeliverTx(ctx basecoin.Context, store state.KVStore, tx baseco
return next.DeliverTx(ctx, store, tx) return next.DeliverTx(ctx, store, tx)
} }
func (_ Recovery) SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (log string, err error) { // SetOption catches any panic and converts to error - fulfills Middlware interface
func (Recovery) SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (log string, err error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
err = normalizePanic(r) err = normalizePanic(r)

View File

@ -27,7 +27,7 @@ func TestRecovery(t *testing.T) {
expected string // expected text in panic expected string // expected text in panic
}{ }{
{"buzz", nil, "buzz"}, {"buzz", nil, "buzz"},
{"", errors.New("owa!"), "owa!"}, {"", errors.New("some text"), "some text"},
{"text", errors.New("error"), "error"}, {"text", errors.New("error"), "error"},
} }