This commit is contained in:
Jae Kwon 2017-11-26 20:29:17 -08:00
parent 458fba22d3
commit 6e8e4331ea
4 changed files with 121 additions and 4 deletions

View File

@ -0,0 +1,76 @@
XXX Rename AppHandler to DefaultAppHandler.
XXX Register with a sdk.BaseApp instance to create Basecoin.
XXX Create TxParser in anotehr file.
package app
import (
wire "github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/store"
)
// AppHandler has no state for now, a more complex app could store state here
type AppHandler struct{}
func NewAppHandler() sdk.Handler {
return AppHandler{}
}
// DeliverTx applies the tx
func (h Handler) DeliverTx(ctx sdk.Context, store store.MultiStore,
msg interface{}) (res sdk.DeliverResult, err error) {
db := store.Get("main").(sdk.KVStore)
// Here we switch on which implementation of tx we use,
// and then take the appropriate action.
switch tx := tx.(type) {
case SendTx:
err = tx.ValidateBasic()
if err != nil {
break
}
db.Set(tx.Key, tx.Value)
res.Data = tx.Key
case RemoveTx:
err = tx.ValidateBasic()
if err != nil {
break
}
db.Remove(tx.Key)
res.Data = tx.Key
default:
err = errors.ErrInvalidFormat(TxWrapper{}, msg)
}
return
}
// CheckTx verifies if it is legit and returns info on how
// to prioritize it in the mempool
func (h Handler) CheckTx(ctx sdk.Context, store store.MultiStore,
msg interface{}) (res sdk.CheckResult, err error) {
// If we wanted to use the store,
// it would look the same (as DeliverTx)
// db := store.Get("main").(sdk.KVStore)
// Make sure it is something valid
tx, ok := msg.(Tx)
if !ok {
return res, errors.ErrInvalidFormat(TxWrapper{}, msg)
}
err = tx.ValidateBasic()
if err != nil {
return
}
// Now return the costs (these should have meaning in your app)
return sdk.CheckResult{
GasAllocated: 50,
GasPayment: 10,
}
}

View File

@ -57,6 +57,14 @@ func (c SDKContext) BlockHeader() tm.Header {
return c.Value(contextKeyBlockHeader).(tm.Header)
}
func (c SDKContext) BlockHeight() int64 {
return c.Value(contextKeyBlockHeight).(int64)
}
func (c SDKContext) ChainID() string {
return c.Value(contextKeyChainID).(string)
}
// Unexposed to prevent overriding.
func (c SDKContext) setBlockHeader(header tm.Header) SDKContext {
return c.WithValueSDK(contextKeyBlockHeader, header)

View File

@ -25,18 +25,18 @@ func Decorate(dec Decorator, next Handler) Handler {
/*
Helper to construct a decorated Handler from a stack of Decorators
(first-decorator-first-call as in Python @decorators) , w/ Handler provided
last for syntactic sugar of Stack().WithHandler()
last for syntactic sugar of ChainDecorators().WithHandler()
Usage:
handler := sdk.Stack(
handler := sdk.ChainDecorators(
decorator1,
decorator2,
...,
).WithHandler(myHandler)
*/
func Stack(decorators ...Decorator) stack {
func ChainDecorators(decorators ...Decorator) stack {
return stack{
decs: decorators,
}
@ -49,7 +49,7 @@ type stack struct {
// WithHandler sets the final handler for the stack and
// returns the decoratored Handler.
func (s *Stack) WithHandler(handler Handler) Handler {
func (s stack) WithHandler(handler Handler) Handler {
if handler == nil {
panic("WithHandler() requires a non-nil Handler")
}

33
x/auth/context.go Normal file
View File

@ -0,0 +1,33 @@
package auth
import (
sdk "github.com/cosmos/cosmos-sdk"
)
/*
Usage:
import "accounts"
var acc accounts.Account
accounts.SetAccount(ctx, acc)
acc2, ok := accounts.GetAccount(ctx)
*/
type contextKey int // local to the auth module
const (
// A context key of the Account variety
contextKeyAccount contextKey = iota
)
func SetAccount(ctx sdk.Context, account Account) sdk.Context {
return ctx.WithValue(contextKeyAccount, account)
}
func GetAccount(ctx sdk.Context) (Account, bool) {
return ctx.Value(contextKeyAccount).(Account)
}