cosmos-sdk/handler.go

101 lines
2.4 KiB
Go
Raw Normal View History

2017-06-01 11:01:19 -07:00
package basecoin
import (
"bytes"
2017-06-01 11:01:19 -07:00
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/basecoin/types"
)
type Named interface {
Name() string
}
type Checker interface {
2017-06-01 11:01:19 -07:00
CheckTx(ctx Context, store types.KVStore, tx Tx) (Result, error)
}
type Deliver interface {
2017-06-01 11:01:19 -07:00
DeliverTx(ctx Context, store types.KVStore, tx Tx) (Result, error)
}
2017-06-01 11:01:19 -07:00
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 {
Checker
Deliver
Named
2017-06-01 11:01:19 -07:00
// TODO: flesh these out as well
// SetOption(store types.KVStore, key, value string) (log string)
// InitChain(store types.KVStore, vals []*abci.Validator)
// BeginBlock(store types.KVStore, hash []byte, header *abci.Header)
// EndBlock(store types.KVStore, height uint64) abci.ResponseEndBlock
}
// 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 {
App string // Which app authorized this?
Address []byte // App-specific identifier
}
2017-06-01 11:01:19 -07:00
// TODO: Context is a place-holder, soon we add some request data here from the
// higher-levels (like tell an app who signed).
// Trust me, we will need it like CallContext now...
type Context struct {
perms []Permission
2017-06-01 11:01:19 -07:00
}
// TOTALLY insecure. will redo later, but you get the point
func (c Context) AddPermissions(perms ...Permission) Context {
2017-06-01 11:01:19 -07:00
return Context{
perms: append(c.perms, perms...),
2017-06-01 11:01:19 -07:00
}
}
func (c Context) HasPermission(app string, addr []byte) bool {
for _, p := range c.perms {
if app == p.App && bytes.Equal(addr, p.Address) {
return true
}
}
return false
}
// New should give a fresh context, and know what info makes sense to carry over
func (c Context) New() Context {
return Context{}
}
2017-06-01 11:01:19 -07:00
// Result captures any non-error abci result
// to make sure people use error for error cases
type Result struct {
Data data.Bytes
Log string
}
func (r Result) ToABCI() abci.Result {
return abci.Result{
Data: r.Data,
Log: r.Log,
}
}