cosmos-sdk/handler.go

164 lines
4.4 KiB
Go
Raw Normal View History

2017-06-01 11:01:19 -07:00
package basecoin
import (
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/tmlibs/log"
2017-06-01 11:01:19 -07:00
2017-07-06 05:23:38 -07:00
"github.com/tendermint/basecoin/state"
2017-06-01 11:01:19 -07:00
)
// Handler is anything that processes a transaction
type Handler interface {
2017-07-30 14:26:25 -07:00
// Checker verifies there are valid fees and estimates work
Checker
2017-07-30 14:26:25 -07:00
// Deliver performs the tx once it makes it in the block
Deliver
2017-07-30 14:26:25 -07:00
// InitStater sets state from the genesis file
2017-07-30 09:57:48 -07:00
InitStater
2017-07-30 14:26:25 -07:00
// InitValidater sets the initial validator set
InitValidater
// Named ensures there is a name for the item
Named
2017-07-30 09:57:48 -07:00
// TODO????
// BeginBlock(store state.SimpleDB, hash []byte, header *abci.Header)
}
2017-07-30 14:26:25 -07:00
// Named ensures there is a name for the item
type Named interface {
Name() string
}
2017-07-30 14:26:25 -07:00
// Checker verifies there are valid fees and estimates work
type Checker interface {
2017-07-29 17:22:45 -07:00
CheckTx(ctx Context, store state.SimpleDB, tx Tx) (CheckResult, error)
}
2017-08-04 04:50:55 -07:00
// CheckerFunc (like http.HandlerFunc) is a shortcut for making wrappers
2017-07-29 17:22:45 -07:00
type CheckerFunc func(Context, state.SimpleDB, Tx) (CheckResult, error)
2017-07-29 17:22:45 -07:00
func (c CheckerFunc) CheckTx(ctx Context, store state.SimpleDB, tx Tx) (CheckResult, error) {
return c(ctx, store, tx)
}
2017-07-30 14:26:25 -07:00
// Deliver performs the tx once it makes it in the block
type Deliver interface {
2017-07-29 17:22:45 -07:00
DeliverTx(ctx Context, store state.SimpleDB, tx Tx) (DeliverResult, error)
}
2017-08-04 04:50:55 -07:00
// DeliverFunc (like http.HandlerFunc) is a shortcut for making wrappers
2017-07-29 17:22:45 -07:00
type DeliverFunc func(Context, state.SimpleDB, Tx) (DeliverResult, error)
2017-07-29 17:22:45 -07:00
func (c DeliverFunc) DeliverTx(ctx Context, store state.SimpleDB, tx Tx) (DeliverResult, error) {
return c(ctx, store, tx)
}
2017-07-30 14:26:25 -07:00
// InitStater sets state from the genesis file
2017-07-30 09:57:48 -07:00
type InitStater interface {
InitState(l log.Logger, store state.SimpleDB, module, key, value string) (string, error)
}
2017-08-04 04:50:55 -07:00
// InitStateFunc (like http.HandlerFunc) is a shortcut for making wrappers
2017-07-30 09:57:48 -07:00
type InitStateFunc func(log.Logger, state.SimpleDB, string, string, string) (string, error)
2017-07-30 09:57:48 -07:00
func (c InitStateFunc) InitState(l log.Logger, store state.SimpleDB, module, key, value string) (string, error) {
return c(l, store, module, key, value)
2017-06-01 11:01:19 -07:00
}
2017-07-30 14:26:25 -07:00
// InitValidater sets the initial validator set
type InitValidater interface {
InitValidate(log log.Logger, store state.SimpleDB, vals []*abci.Validator)
}
2017-08-04 04:50:55 -07:00
// InitValidateFunc (like http.HandlerFunc) is a shortcut for making wrappers
2017-07-30 14:26:25 -07:00
type InitValidateFunc func(log.Logger, state.SimpleDB, []*abci.Validator)
func (c InitValidateFunc) InitValidate(l log.Logger, store state.SimpleDB, vals []*abci.Validator) {
c(l, store, vals)
}
//---------- results and some wrappers --------
2017-08-04 04:50:55 -07:00
// Result is a common interface of CheckResult and GetResult
type Result interface {
GetData() data.Bytes
2017-08-04 04:50:55 -07:00
GetLog() string
}
func ToABCI(r Result) abci.Result {
return abci.Result{
Data: r.GetData(),
Log: r.GetLog(),
}
}
2017-07-29 17:22:45 -07:00
// CheckResult captures any non-error abci result
2017-06-01 11:01:19 -07:00
// to make sure people use error for error cases
2017-07-29 17:22:45 -07:00
type CheckResult struct {
2017-07-30 14:51:57 -07:00
Data data.Bytes
Log string
// GasAllocated is the maximum units of work we allow this tx to perform
GasAllocated uint64
2017-07-30 14:51:57 -07:00
// GasPayment is the total fees for this tx (or other source of payment)
GasPayment uint64
2017-06-01 11:01:19 -07:00
}
// NewCheck sets the gas used and the response data but no more info
// these are the most common info needed to be set by the Handler
func NewCheck(gasAllocated uint64, log string) CheckResult {
return CheckResult{
GasAllocated: gasAllocated,
Log: log,
}
}
2017-08-04 04:50:55 -07:00
var _ Result = CheckResult{}
2017-07-29 17:22:45 -07:00
func (r CheckResult) GetData() data.Bytes {
return r.Data
}
2017-08-04 04:50:55 -07:00
func (r CheckResult) GetLog() string {
return r.Log
}
2017-07-29 17:22:45 -07:00
// DeliverResult captures any non-error abci result
// to make sure people use error for error cases
type DeliverResult struct {
Data data.Bytes
Log string
Diff []*abci.Validator
GasUsed uint64
2017-07-29 17:22:45 -07:00
}
2017-08-04 04:50:55 -07:00
var _ Result = DeliverResult{}
func (r DeliverResult) GetData() data.Bytes {
return r.Data
}
2017-08-04 04:50:55 -07:00
func (r DeliverResult) GetLog() string {
return r.Log
}
// placeholders
// holders
type NopCheck struct{}
2017-07-29 17:22:45 -07:00
func (_ NopCheck) CheckTx(Context, state.SimpleDB, Tx) (r CheckResult, e error) { return }
type NopDeliver struct{}
2017-07-29 17:22:45 -07:00
func (_ NopDeliver) DeliverTx(Context, state.SimpleDB, Tx) (r DeliverResult, e error) { return }
2017-07-30 14:26:25 -07:00
type NopInitState struct{}
2017-07-30 14:26:25 -07:00
func (_ NopInitState) InitState(log.Logger, state.SimpleDB, string, string, string) (string, error) {
return "", nil
}
2017-07-30 14:26:25 -07:00
type NopInitValidate struct{}
func (_ NopInitValidate) InitValidate(log log.Logger, store state.SimpleDB, vals []*abci.Validator) {}