Ported everything to SimpleDB interface

This commit is contained in:
Ethan Frey 2017-07-26 19:55:05 -04:00
parent f1785e312d
commit 51a29e4bb7
31 changed files with 216 additions and 143 deletions

View File

@ -78,7 +78,7 @@ func (app *Basecoin) GetChainID() string {
} }
// GetState is back... please kill me // GetState is back... please kill me
func (app *Basecoin) GetState() sm.KVStore { func (app *Basecoin) GetState() sm.SimpleDB {
return app.state.Append() return app.state.Append()
} }

View File

@ -100,13 +100,13 @@ func (at *appTest) reset() {
require.True(at.t, resabci.IsOK(), resabci) require.True(at.t, resabci.IsOK(), resabci)
} }
func getBalance(key basecoin.Actor, store state.KVStore) (coin.Coins, error) { func getBalance(key basecoin.Actor, store state.SimpleDB) (coin.Coins, error) {
cspace := stack.PrefixedStore(coin.NameCoin, store) cspace := stack.PrefixedStore(coin.NameCoin, store)
acct, err := coin.GetAccount(cspace, key) acct, err := coin.GetAccount(cspace, key)
return acct.Coins, err return acct.Coins, err
} }
func getAddr(addr []byte, state state.KVStore) (coin.Coins, error) { func getAddr(addr []byte, state state.SimpleDB) (coin.Coins, error) {
actor := auth.SigPerm(addr) actor := auth.SigPerm(addr)
return getBalance(actor, state) return getBalance(actor, state)
} }

View File

@ -125,13 +125,13 @@ func (Handler) Name() string {
func (Handler) AssertDispatcher() {} func (Handler) AssertDispatcher() {}
// CheckTx checks if the tx is properly structured // CheckTx checks if the tx is properly structured
func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, _ basecoin.Checker) (res basecoin.Result, err error) { func (h Handler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, _ basecoin.Checker) (res basecoin.Result, err error) {
_, err = checkTx(ctx, tx) _, err = checkTx(ctx, tx)
return return
} }
// DeliverTx executes the tx if valid // DeliverTx executes the tx if valid
func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, dispatch basecoin.Deliver) (res basecoin.Result, err error) { func (h Handler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, dispatch basecoin.Deliver) (res basecoin.Result, err error) {
ctr, err := checkTx(ctx, tx) ctr, err := checkTx(ctx, tx)
if err != nil { if err != nil {
return res, err return res, err
@ -203,7 +203,7 @@ func StateKey() []byte {
} }
// LoadState - retrieve the counter state from the store // LoadState - retrieve the counter state from the store
func LoadState(store state.KVStore) (state State, err error) { func LoadState(store state.SimpleDB) (state State, err error) {
bytes := store.Get(StateKey()) bytes := store.Get(StateKey())
if len(bytes) > 0 { if len(bytes) > 0 {
err = wire.ReadBinaryBytes(bytes, &state) err = wire.ReadBinaryBytes(bytes, &state)
@ -215,7 +215,7 @@ func LoadState(store state.KVStore) (state State, err error) {
} }
// SaveState - save the counter state to the provided store // SaveState - save the counter state to the provided store
func SaveState(store state.KVStore, state State) error { func SaveState(store state.SimpleDB, state State) error {
bytes := wire.BinaryBytes(state) bytes := wire.BinaryBytes(state)
store.Set(StateKey(), bytes) store.Set(StateKey(), bytes)
return nil return nil

6
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: 45eed61138603d4d03518ea822068cf32b45d0a219bb7f3b836e52129f2a3a2b hash: 45eed61138603d4d03518ea822068cf32b45d0a219bb7f3b836e52129f2a3a2b
updated: 2017-07-26T16:26:38.678294279-04:00 updated: 2017-07-26T19:44:39.753066441-04:00
imports: imports:
- name: github.com/bgentry/speakeasy - name: github.com/bgentry/speakeasy
version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd
@ -105,7 +105,7 @@ imports:
- leveldb/table - leveldb/table
- leveldb/util - leveldb/util
- name: github.com/tendermint/abci - name: github.com/tendermint/abci
version: 864d1f80b36b440bde030a5c18d8ac3aa8c2949d version: 7f5f48b6b9ec3964de4b07b6c3cd05d7c91aeee5
subpackages: subpackages:
- client - client
- example/dummy - example/dummy
@ -140,7 +140,7 @@ imports:
- certifiers/files - certifiers/files
- proofs - proofs
- name: github.com/tendermint/merkleeyes - name: github.com/tendermint/merkleeyes
version: fef0a1fc729f5b50c58a2dce04b4525aa06c2247 version: 0310013053953eef80def3619aeb1e3a3254f452
subpackages: subpackages:
- client - client
- iavl - iavl

View File

@ -15,9 +15,9 @@ type Handler interface {
SetOptioner SetOptioner
Named Named
// TODO: flesh these out as well // TODO: flesh these out as well
// InitChain(store state.KVStore, vals []*abci.Validator) // InitChain(store state.SimpleDB, vals []*abci.Validator)
// BeginBlock(store state.KVStore, hash []byte, header *abci.Header) // BeginBlock(store state.SimpleDB, hash []byte, header *abci.Header)
// EndBlock(store state.KVStore, height uint64) abci.ResponseEndBlock // EndBlock(store state.SimpleDB, height uint64) abci.ResponseEndBlock
} }
type Named interface { type Named interface {
@ -25,35 +25,35 @@ type Named interface {
} }
type Checker interface { type Checker interface {
CheckTx(ctx Context, store state.KVStore, tx Tx) (Result, error) CheckTx(ctx Context, store state.SimpleDB, tx Tx) (Result, error)
} }
// CheckerFunc (like http.HandlerFunc) is a shortcut for making wrapers // CheckerFunc (like http.HandlerFunc) is a shortcut for making wrapers
type CheckerFunc func(Context, state.KVStore, Tx) (Result, error) type CheckerFunc func(Context, state.SimpleDB, Tx) (Result, error)
func (c CheckerFunc) CheckTx(ctx Context, store state.KVStore, tx Tx) (Result, error) { func (c CheckerFunc) CheckTx(ctx Context, store state.SimpleDB, tx Tx) (Result, error) {
return c(ctx, store, tx) return c(ctx, store, tx)
} }
type Deliver interface { type Deliver interface {
DeliverTx(ctx Context, store state.KVStore, tx Tx) (Result, error) DeliverTx(ctx Context, store state.SimpleDB, tx Tx) (Result, error)
} }
// DeliverFunc (like http.HandlerFunc) is a shortcut for making wrapers // DeliverFunc (like http.HandlerFunc) is a shortcut for making wrapers
type DeliverFunc func(Context, state.KVStore, Tx) (Result, error) type DeliverFunc func(Context, state.SimpleDB, Tx) (Result, error)
func (c DeliverFunc) DeliverTx(ctx Context, store state.KVStore, tx Tx) (Result, error) { func (c DeliverFunc) DeliverTx(ctx Context, store state.SimpleDB, tx Tx) (Result, error) {
return c(ctx, store, tx) return c(ctx, store, tx)
} }
type SetOptioner interface { type SetOptioner interface {
SetOption(l log.Logger, store state.KVStore, module, key, value string) (string, error) SetOption(l log.Logger, store state.SimpleDB, module, key, value string) (string, error)
} }
// SetOptionFunc (like http.HandlerFunc) is a shortcut for making wrapers // SetOptionFunc (like http.HandlerFunc) is a shortcut for making wrapers
type SetOptionFunc func(log.Logger, state.KVStore, string, string, string) (string, error) type SetOptionFunc func(log.Logger, state.SimpleDB, string, string, string) (string, error)
func (c SetOptionFunc) SetOption(l log.Logger, store state.KVStore, module, key, value string) (string, error) { func (c SetOptionFunc) SetOption(l log.Logger, store state.SimpleDB, module, key, value string) (string, error) {
return c(l, store, module, key, value) return c(l, store, module, key, value)
} }
@ -75,14 +75,14 @@ func (r Result) ToABCI() abci.Result {
// holders // holders
type NopCheck struct{} type NopCheck struct{}
func (_ NopCheck) CheckTx(Context, state.KVStore, Tx) (r Result, e error) { return } func (_ NopCheck) CheckTx(Context, state.SimpleDB, Tx) (r Result, e error) { return }
type NopDeliver struct{} type NopDeliver struct{}
func (_ NopDeliver) DeliverTx(Context, state.KVStore, Tx) (r Result, e error) { return } func (_ NopDeliver) DeliverTx(Context, state.SimpleDB, Tx) (r Result, e error) { return }
type NopOption struct{} type NopOption struct{}
func (_ NopOption) SetOption(log.Logger, state.KVStore, string, string, string) (string, error) { func (_ NopOption) SetOption(log.Logger, state.SimpleDB, string, string, string) (string, error) {
return "", nil return "", nil
} }

View File

@ -39,7 +39,7 @@ type Signable interface {
} }
// CheckTx verifies the signatures are correct - fulfills Middlware interface // 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) { func (Signatures) CheckTx(ctx basecoin.Context, store state.SimpleDB, 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
@ -49,7 +49,7 @@ func (Signatures) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin
} }
// DeliverTx verifies the signatures are correct - fulfills Middlware interface // 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) { func (Signatures) DeliverTx(ctx basecoin.Context, store state.SimpleDB, 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

@ -24,7 +24,7 @@ func (Chain) Name() string {
var _ stack.Middleware = Chain{} var _ stack.Middleware = Chain{}
// CheckTx makes sure we are on the proper chain - fulfills Middlware interface // 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.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
stx, err := c.checkChainTx(ctx.ChainID(), ctx.BlockHeight(), tx) stx, err := c.checkChainTx(ctx.ChainID(), ctx.BlockHeight(), tx)
if err != nil { if err != nil {
return res, err return res, err
@ -33,7 +33,7 @@ func (c Chain) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx
} }
// DeliverTx makes sure we are on the proper chain - fulfills Middlware interface // 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.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
stx, err := c.checkChainTx(ctx.ChainID(), ctx.BlockHeight(), tx) stx, err := c.checkChainTx(ctx.ChainID(), ctx.BlockHeight(), tx)
if err != nil { if err != nil {
return res, err return res, err

View File

@ -26,7 +26,7 @@ func (Logger) Name() string {
var _ stack.Middleware = Logger{} var _ stack.Middleware = Logger{}
// CheckTx logs time and result - fulfills Middlware interface // 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) { func (Logger) CheckTx(ctx basecoin.Context, store state.SimpleDB, 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)
@ -41,7 +41,7 @@ func (Logger) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx,
} }
// DeliverTx logs time and result - fulfills Middlware interface // 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) { func (Logger) DeliverTx(ctx basecoin.Context, store state.SimpleDB, 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)
@ -56,7 +56,7 @@ func (Logger) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.T
} }
// SetOption logs time and result - fulfills Middlware interface // 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) { func (Logger) SetOption(l log.Logger, store state.SimpleDB, 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

@ -29,7 +29,7 @@ func (Multiplexer) Name() string {
var _ stack.Middleware = Multiplexer{} var _ stack.Middleware = Multiplexer{}
// CheckTx splits the input tx and checks them all - fulfills Middlware interface // 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) { func (Multiplexer) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
if mtx, ok := tx.Unwrap().(*MultiTx); ok { if mtx, ok := tx.Unwrap().(*MultiTx); ok {
return runAll(ctx, store, mtx.Txs, next.CheckTx) return runAll(ctx, store, mtx.Txs, next.CheckTx)
} }
@ -37,14 +37,14 @@ func (Multiplexer) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoi
} }
// DeliverTx splits the input tx and checks them all - fulfills Middlware interface // 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) { func (Multiplexer) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
if mtx, ok := tx.Unwrap().(*MultiTx); ok { if mtx, ok := tx.Unwrap().(*MultiTx); ok {
return runAll(ctx, store, mtx.Txs, next.DeliverTx) return runAll(ctx, store, mtx.Txs, next.DeliverTx)
} }
return next.DeliverTx(ctx, store, tx) return next.DeliverTx(ctx, store, tx)
} }
func runAll(ctx basecoin.Context, store state.KVStore, txs []basecoin.Tx, next basecoin.CheckerFunc) (res basecoin.Result, err error) { func runAll(ctx basecoin.Context, store state.SimpleDB, txs []basecoin.Tx, next basecoin.CheckerFunc) (res basecoin.Result, err error) {
// store all results, unless anything errors // store all results, unless anything errors
rs := make([]basecoin.Result, len(txs)) rs := make([]basecoin.Result, len(txs))
for i, stx := range txs { for i, stx := range txs {

View File

@ -29,7 +29,7 @@ func (Handler) Name() string {
} }
// CheckTx checks if there is enough money in the account // CheckTx checks if there is enough money in the account
func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (h Handler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
send, err := checkTx(ctx, tx) send, err := checkTx(ctx, tx)
if err != nil { if err != nil {
return res, err return res, err
@ -48,7 +48,7 @@ func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.
} }
// DeliverTx moves the money // DeliverTx moves the money
func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (h Handler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
send, err := checkTx(ctx, tx) send, err := checkTx(ctx, tx)
if err != nil { if err != nil {
return res, err return res, err
@ -75,7 +75,7 @@ func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoi
} }
// SetOption - sets the genesis account balance // SetOption - sets the genesis account balance
func (h Handler) SetOption(l log.Logger, store state.KVStore, module, key, value string) (log string, err error) { func (h Handler) SetOption(l log.Logger, store state.SimpleDB, module, key, value string) (log string, err error) {
if module != NameCoin { if module != NameCoin {
return "", errors.ErrUnknownModule(module) return "", errors.ErrUnknownModule(module)
} }

View File

@ -11,7 +11,7 @@ import (
) )
// GetAccount - Get account from store and address // GetAccount - Get account from store and address
func GetAccount(store state.KVStore, addr basecoin.Actor) (Account, error) { func GetAccount(store state.SimpleDB, addr basecoin.Actor) (Account, error) {
acct, err := loadAccount(store, addr.Bytes()) acct, err := loadAccount(store, addr.Bytes())
// for empty accounts, don't return an error, but rather an empty account // for empty accounts, don't return an error, but rather an empty account
@ -22,13 +22,13 @@ func GetAccount(store state.KVStore, addr basecoin.Actor) (Account, error) {
} }
// CheckCoins makes sure there are funds, but doesn't change anything // CheckCoins makes sure there are funds, but doesn't change anything
func CheckCoins(store state.KVStore, addr basecoin.Actor, coins Coins) (Coins, error) { func CheckCoins(store state.SimpleDB, addr basecoin.Actor, coins Coins) (Coins, error) {
acct, err := updateCoins(store, addr, coins) acct, err := updateCoins(store, addr, coins)
return acct.Coins, err return acct.Coins, err
} }
// ChangeCoins changes the money, returns error if it would be negative // ChangeCoins changes the money, returns error if it would be negative
func ChangeCoins(store state.KVStore, addr basecoin.Actor, coins Coins) (Coins, error) { func ChangeCoins(store state.SimpleDB, addr basecoin.Actor, coins Coins) (Coins, error) {
acct, err := updateCoins(store, addr, coins) acct, err := updateCoins(store, addr, coins)
if err != nil { if err != nil {
return acct.Coins, err return acct.Coins, err
@ -41,7 +41,7 @@ func ChangeCoins(store state.KVStore, addr basecoin.Actor, coins Coins) (Coins,
// updateCoins will load the account, make all checks, and return the updated account. // updateCoins will load the account, make all checks, and return the updated account.
// //
// it doesn't save anything, that is up to you to decide (Check/Change Coins) // it doesn't save anything, that is up to you to decide (Check/Change Coins)
func updateCoins(store state.KVStore, addr basecoin.Actor, coins Coins) (acct Account, err error) { func updateCoins(store state.SimpleDB, addr basecoin.Actor, coins Coins) (acct Account, err error) {
acct, err = loadAccount(store, addr.Bytes()) acct, err = loadAccount(store, addr.Bytes())
// we can increase an empty account... // we can increase an empty account...
if IsNoAccountErr(err) && coins.IsPositive() { if IsNoAccountErr(err) && coins.IsPositive() {
@ -66,7 +66,7 @@ type Account struct {
Coins Coins `json:"coins"` Coins Coins `json:"coins"`
} }
func loadAccount(store state.KVStore, key []byte) (acct Account, err error) { func loadAccount(store state.SimpleDB, key []byte) (acct Account, err error) {
// fmt.Printf("load: %X\n", key) // fmt.Printf("load: %X\n", key)
data := store.Get(key) data := store.Get(key)
if len(data) == 0 { if len(data) == 0 {
@ -80,7 +80,7 @@ func loadAccount(store state.KVStore, key []byte) (acct Account, err error) {
return acct, nil return acct, nil
} }
func storeAccount(store state.KVStore, key []byte, acct Account) error { func storeAccount(store state.SimpleDB, key []byte, acct Account) error {
// fmt.Printf("store: %X\n", key) // fmt.Printf("store: %X\n", key)
bin := wire.BinaryBytes(acct) bin := wire.BinaryBytes(acct)
store.Set(key, bin) store.Set(key, bin)

View File

@ -45,16 +45,16 @@ func (SimpleFeeMiddleware) Name() string {
} }
// CheckTx - check the transaction // CheckTx - check the transaction
func (h SimpleFeeMiddleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { func (h SimpleFeeMiddleware) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
return h.doTx(ctx, store, tx, next.CheckTx) return h.doTx(ctx, store, tx, next.CheckTx)
} }
// DeliverTx - send the fee handler transaction // DeliverTx - send the fee handler transaction
func (h SimpleFeeMiddleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { func (h SimpleFeeMiddleware) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
return h.doTx(ctx, store, tx, next.DeliverTx) return h.doTx(ctx, store, tx, next.DeliverTx)
} }
func (h SimpleFeeMiddleware) doTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.CheckerFunc) (res basecoin.Result, err error) { func (h SimpleFeeMiddleware) doTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.CheckerFunc) (res basecoin.Result, err error) {
feeTx, ok := tx.Unwrap().(Fee) feeTx, ok := tx.Unwrap().(Fee)
if !ok { if !ok {
// the fee wrapper is not required if there is no minimum // the fee wrapper is not required if there is no minimum

View File

@ -24,7 +24,7 @@ func (ReplayCheck) Name() string {
var _ stack.Middleware = ReplayCheck{} var _ stack.Middleware = ReplayCheck{}
// CheckTx verifies tx is not being replayed - fulfills Middlware interface // CheckTx verifies tx is not being replayed - fulfills Middlware interface
func (r ReplayCheck) CheckTx(ctx basecoin.Context, store state.KVStore, func (r ReplayCheck) CheckTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
stx, err := r.checkIncrementNonceTx(ctx, store, tx) stx, err := r.checkIncrementNonceTx(ctx, store, tx)
@ -38,7 +38,7 @@ func (r ReplayCheck) CheckTx(ctx basecoin.Context, store state.KVStore,
// DeliverTx verifies tx is not being replayed - fulfills Middlware interface // DeliverTx verifies tx is not being replayed - fulfills Middlware interface
// NOTE It is okay to modify the sequence before running the wrapped TX because if the // NOTE It is okay to modify the sequence before running the wrapped TX because if the
// wrapped Tx fails, the state changes are not applied // wrapped Tx fails, the state changes are not applied
func (r ReplayCheck) DeliverTx(ctx basecoin.Context, store state.KVStore, func (r ReplayCheck) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
stx, err := r.checkIncrementNonceTx(ctx, store, tx) stx, err := r.checkIncrementNonceTx(ctx, store, tx)
@ -50,7 +50,7 @@ func (r ReplayCheck) DeliverTx(ctx basecoin.Context, store state.KVStore,
} }
// checkNonceTx varifies the nonce sequence, an increment sequence number // checkNonceTx varifies the nonce sequence, an increment sequence number
func (r ReplayCheck) checkIncrementNonceTx(ctx basecoin.Context, store state.KVStore, func (r ReplayCheck) checkIncrementNonceTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx) (basecoin.Tx, error) { tx basecoin.Tx) (basecoin.Tx, error) {
// make sure it is a the nonce Tx (Tx from this package) // make sure it is a the nonce Tx (Tx from this package)

View File

@ -9,7 +9,7 @@ import (
"github.com/tendermint/basecoin/state" "github.com/tendermint/basecoin/state"
) )
func getSeq(store state.KVStore, key []byte) (seq uint32, err error) { func getSeq(store state.SimpleDB, key []byte) (seq uint32, err error) {
data := store.Get(key) data := store.Get(key)
if len(data) == 0 { if len(data) == 0 {
//if the key is not stored, its a new key with a sequence of zero! //if the key is not stored, its a new key with a sequence of zero!
@ -23,7 +23,7 @@ func getSeq(store state.KVStore, key []byte) (seq uint32, err error) {
return seq, nil return seq, nil
} }
func setSeq(store state.KVStore, key []byte, seq uint32) error { func setSeq(store state.SimpleDB, key []byte, seq uint32) error {
bin := wire.BinaryBytes(seq) bin := wire.BinaryBytes(seq)
store.Set(key, bin) store.Set(key, bin)
return nil // real stores can return error... return nil // real stores can return error...

View File

@ -62,7 +62,7 @@ func (n Tx) ValidateBasic() error {
// and further increment the sequence number // and further increment the sequence number
// NOTE It is okay to modify the sequence before running the wrapped TX because if the // NOTE It is okay to modify the sequence before running the wrapped TX because if the
// wrapped Tx fails, the state changes are not applied // wrapped Tx fails, the state changes are not applied
func (n Tx) CheckIncrementSeq(ctx basecoin.Context, store state.KVStore) error { func (n Tx) CheckIncrementSeq(ctx basecoin.Context, store state.SimpleDB) error {
seqKey := n.getSeqKey() seqKey := n.getSeqKey()

View File

@ -27,7 +27,7 @@ func (Handler) Name() string {
} }
// CheckTx verifies if the transaction is properly formated // CheckTx verifies if the transaction is properly formated
func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (h Handler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
var cr CreateRoleTx var cr CreateRoleTx
cr, err = checkTx(ctx, tx) cr, err = checkTx(ctx, tx)
if err != nil { if err != nil {
@ -40,7 +40,7 @@ func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.
// DeliverTx tries to create a new role. // DeliverTx tries to create a new role.
// //
// Returns an error if the role already exists // Returns an error if the role already exists
func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (h Handler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
create, err := checkTx(ctx, tx) create, err := checkTx(ctx, tx)
if err != nil { if err != nil {
return res, err return res, err

View File

@ -27,7 +27,7 @@ func (Middleware) Name() string {
// CheckTx tries to assume the named role if requested. // CheckTx tries to assume the named role if requested.
// If no role is requested, do nothing. // If no role is requested, do nothing.
// If insufficient authority to assume the role, return error. // If insufficient authority to assume the role, return error.
func (m Middleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { func (m Middleware) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
// if this is not an AssumeRoleTx, then continue // if this is not an AssumeRoleTx, then continue
assume, ok := tx.Unwrap().(AssumeRoleTx) assume, ok := tx.Unwrap().(AssumeRoleTx)
if !ok { // this also breaks the recursion below if !ok { // this also breaks the recursion below
@ -46,7 +46,7 @@ func (m Middleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx baseco
// DeliverTx tries to assume the named role if requested. // DeliverTx tries to assume the named role if requested.
// If no role is requested, do nothing. // If no role is requested, do nothing.
// If insufficient authority to assume the role, return error. // If insufficient authority to assume the role, return error.
func (m Middleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { func (m Middleware) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
// if this is not an AssumeRoleTx, then continue // if this is not an AssumeRoleTx, then continue
assume, ok := tx.Unwrap().(AssumeRoleTx) assume, ok := tx.Unwrap().(AssumeRoleTx)
if !ok { // this also breaks the recursion below if !ok { // this also breaks the recursion below
@ -62,7 +62,7 @@ func (m Middleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx base
return m.DeliverTx(ctx, store, assume.Tx, next) return m.DeliverTx(ctx, store, assume.Tx, next)
} }
func assumeRole(ctx basecoin.Context, store state.KVStore, assume AssumeRoleTx) (basecoin.Context, error) { func assumeRole(ctx basecoin.Context, store state.SimpleDB, assume AssumeRoleTx) (basecoin.Context, error) {
err := assume.ValidateBasic() err := assume.ValidateBasic()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -17,7 +17,7 @@ import (
// shortcut for the lazy // shortcut for the lazy
type ba []basecoin.Actor type ba []basecoin.Actor
func createRole(app basecoin.Handler, store state.KVStore, func createRole(app basecoin.Handler, store state.SimpleDB,
name []byte, min uint32, sigs ...basecoin.Actor) (basecoin.Actor, error) { name []byte, min uint32, sigs ...basecoin.Actor) (basecoin.Actor, error) {
tx := roles.NewCreateRoleTx(name, min, sigs) tx := roles.NewCreateRoleTx(name, min, sigs)
ctx := stack.MockContext("foo", 1) ctx := stack.MockContext("foo", 1)

View File

@ -56,7 +56,7 @@ func (r Role) IsAuthorized(ctx basecoin.Context) bool {
return false return false
} }
func loadRole(store state.KVStore, key []byte) (role Role, err error) { func loadRole(store state.SimpleDB, key []byte) (role Role, err error) {
data := store.Get(key) data := store.Get(key)
if len(data) == 0 { if len(data) == 0 {
return role, ErrNoRole() return role, ErrNoRole()
@ -69,7 +69,7 @@ func loadRole(store state.KVStore, key []byte) (role Role, err error) {
return role, nil return role, nil
} }
func checkNoRole(store state.KVStore, key []byte) error { func checkNoRole(store state.SimpleDB, key []byte) error {
if _, err := loadRole(store, key); !IsNoRoleErr(err) { if _, err := loadRole(store, key); !IsNoRoleErr(err) {
return ErrRoleExists() return ErrRoleExists()
} }
@ -77,7 +77,7 @@ func checkNoRole(store state.KVStore, key []byte) error {
} }
// we only have create here, no update, since we don't allow update yet // we only have create here, no update, since we don't allow update yet
func createRole(store state.KVStore, key []byte, role Role) error { func createRole(store state.SimpleDB, key []byte, role Role) error {
if err := checkNoRole(store, key); err != nil { if err := checkNoRole(store, key); err != nil {
return err return err
} }

View File

@ -72,7 +72,7 @@ package ibc
// // GetSequenceNumber gets the sequence number for packets being sent from the src chain to the dst chain. // // GetSequenceNumber gets the sequence number for packets being sent from the src chain to the dst chain.
// // The sequence number counts how many packets have been sent. // // The sequence number counts how many packets have been sent.
// // The next packet must include the latest sequence number. // // The next packet must include the latest sequence number.
// func GetSequenceNumber(store state.KVStore, src, dst string) uint64 { // func GetSequenceNumber(store state.SimpleDB, src, dst string) uint64 {
// sequenceKey := toKey(_IBC, _EGRESS, src, dst) // sequenceKey := toKey(_IBC, _EGRESS, src, dst)
// seqBytes := store.Get(sequenceKey) // seqBytes := store.Get(sequenceKey)
// if seqBytes == nil { // if seqBytes == nil {
@ -86,14 +86,14 @@ package ibc
// } // }
// // SetSequenceNumber sets the sequence number for packets being sent from the src chain to the dst chain // // SetSequenceNumber sets the sequence number for packets being sent from the src chain to the dst chain
// func SetSequenceNumber(store state.KVStore, src, dst string, seq uint64) { // func SetSequenceNumber(store state.SimpleDB, src, dst string, seq uint64) {
// sequenceKey := toKey(_IBC, _EGRESS, src, dst) // sequenceKey := toKey(_IBC, _EGRESS, src, dst)
// store.Set(sequenceKey, []byte(strconv.FormatUint(seq, 10))) // store.Set(sequenceKey, []byte(strconv.FormatUint(seq, 10)))
// } // }
// // SaveNewIBCPacket creates an IBC packet with the given payload from the src chain to the dst chain // // SaveNewIBCPacket creates an IBC packet with the given payload from the src chain to the dst chain
// // using the correct sequence number. It also increments the sequence number by 1 // // using the correct sequence number. It also increments the sequence number by 1
// func SaveNewIBCPacket(state state.KVStore, src, dst string, payload Payload) { // func SaveNewIBCPacket(state state.SimpleDB, src, dst string, payload Payload) {
// // fetch sequence number and increment by 1 // // fetch sequence number and increment by 1
// seq := GetSequenceNumber(state, src, dst) // seq := GetSequenceNumber(state, src, dst)
// SetSequenceNumber(state, src, dst, seq+1) // SetSequenceNumber(state, src, dst, seq+1)
@ -104,7 +104,7 @@ package ibc
// save(state, packetKey, packet) // save(state, packetKey, packet)
// } // }
// func GetIBCPacket(state state.KVStore, src, dst string, seq uint64) (Packet, error) { // func GetIBCPacket(state state.SimpleDB, src, dst string, seq uint64) (Packet, error) {
// packetKey := toKey(_IBC, _EGRESS, src, dst, cmn.Fmt("%v", seq)) // packetKey := toKey(_IBC, _EGRESS, src, dst, cmn.Fmt("%v", seq))
// packetBytes := state.Get(packetKey) // packetBytes := state.Get(packetKey)
@ -251,11 +251,11 @@ package ibc
// return &IBCPlugin{} // return &IBCPlugin{}
// } // }
// func (ibc *IBCPlugin) SetOption(store state.KVStore, key string, value string) (log string) { // func (ibc *IBCPlugin) SetOption(store state.SimpleDB, key string, value string) (log string) {
// return "" // return ""
// } // }
// func (ibc *IBCPlugin) RunTx(store state.KVStore, ctx types.CallContext, txBytes []byte) (res abci.Result) { // func (ibc *IBCPlugin) RunTx(store state.SimpleDB, ctx types.CallContext, txBytes []byte) (res abci.Result) {
// // Decode tx // // Decode tx
// var tx IBCTx // var tx IBCTx
// err := wire.ReadBinaryBytes(txBytes, &tx) // err := wire.ReadBinaryBytes(txBytes, &tx)
@ -295,7 +295,7 @@ package ibc
// } // }
// type IBCStateMachine struct { // type IBCStateMachine struct {
// store state.KVStore // store state.SimpleDB
// ctx types.CallContext // ctx types.CallContext
// res abci.Result // res abci.Result
// } // }
@ -499,13 +499,13 @@ package ibc
// return // return
// } // }
// func (ibc *IBCPlugin) InitChain(store state.KVStore, vals []*abci.Validator) { // func (ibc *IBCPlugin) InitChain(store state.SimpleDB, vals []*abci.Validator) {
// } // }
// func (cp *IBCPlugin) BeginBlock(store state.KVStore, hash []byte, header *abci.Header) { // func (cp *IBCPlugin) BeginBlock(store state.SimpleDB, hash []byte, header *abci.Header) {
// } // }
// func (cp *IBCPlugin) EndBlock(store state.KVStore, height uint64) (res abci.ResponseEndBlock) { // func (cp *IBCPlugin) EndBlock(store state.SimpleDB, height uint64) (res abci.ResponseEndBlock) {
// return // return
// } // }
@ -513,7 +513,7 @@ package ibc
// // TODO: move to utils // // TODO: move to utils
// // Returns true if exists, false if nil. // // Returns true if exists, false if nil.
// func exists(store state.KVStore, key []byte) (exists bool) { // func exists(store state.SimpleDB, key []byte) (exists bool) {
// value := store.Get(key) // value := store.Get(key)
// return len(value) > 0 // return len(value) > 0
// } // }
@ -521,7 +521,7 @@ package ibc
// // Load bytes from store by reading value for key and read into ptr. // // Load bytes from store by reading value for key and read into ptr.
// // Returns true if exists, false if nil. // // Returns true if exists, false if nil.
// // Returns err if decoding error. // // Returns err if decoding error.
// func load(store state.KVStore, key []byte, ptr interface{}) (exists bool, err error) { // func load(store state.SimpleDB, key []byte, ptr interface{}) (exists bool, err error) {
// value := store.Get(key) // value := store.Get(key)
// if len(value) > 0 { // if len(value) > 0 {
// err = wire.ReadBinaryBytes(value, ptr) // err = wire.ReadBinaryBytes(value, ptr)
@ -537,7 +537,7 @@ package ibc
// } // }
// // Save bytes to store by writing obj's go-wire binary bytes. // // Save bytes to store by writing obj's go-wire binary bytes.
// func save(store state.KVStore, key []byte, obj interface{}) { // func save(store state.SimpleDB, key []byte, obj interface{}) {
// store.Set(key, wire.BinaryBytes(obj)) // store.Set(key, wire.BinaryBytes(obj))
// } // }

View File

@ -25,27 +25,27 @@ func (Checkpoint) Name() string {
var _ Middleware = Checkpoint{} var _ Middleware = Checkpoint{}
// CheckTx reverts all data changes if there was an error // CheckTx reverts all data changes if there was an error
func (c Checkpoint) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { func (c Checkpoint) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
if !c.OnCheck { if !c.OnCheck {
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
ps := state.NewKVCache(unwrap(store)) ps := store.Checkpoint()
res, err = next.CheckTx(ctx, ps, tx) res, err = next.CheckTx(ctx, ps, tx)
if err == nil { if err == nil {
ps.Sync() err = store.Commit(ps)
} }
return res, err return res, err
} }
// DeliverTx reverts all data changes if there was an error // DeliverTx reverts all data changes if there was an error
func (c Checkpoint) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { func (c Checkpoint) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
if !c.OnDeliver { if !c.OnDeliver {
return next.DeliverTx(ctx, store, tx) return next.DeliverTx(ctx, store, tx)
} }
ps := state.NewKVCache(unwrap(store)) ps := store.Checkpoint()
res, err = next.DeliverTx(ctx, ps, tx) res, err = next.DeliverTx(ctx, ps, tx)
if err == nil { if err == nil {
ps.Sync() err = store.Commit(ps)
} }
return res, err return res, err
} }

View File

@ -76,7 +76,7 @@ func withApp(ctx basecoin.Context, app string) basecoin.Context {
} }
func secureCheck(h basecoin.Checker, parent basecoin.Context) basecoin.Checker { func secureCheck(h basecoin.Checker, parent basecoin.Context) basecoin.Checker {
next := func(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { next := func(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
if !parent.IsParent(ctx) { if !parent.IsParent(ctx) {
return res, errors.New("Passing in non-child Context") return res, errors.New("Passing in non-child Context")
} }
@ -86,7 +86,7 @@ func secureCheck(h basecoin.Checker, parent basecoin.Context) basecoin.Checker {
} }
func secureDeliver(h basecoin.Deliver, parent basecoin.Context) basecoin.Deliver { func secureDeliver(h basecoin.Deliver, parent basecoin.Context) basecoin.Deliver {
next := func(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { next := func(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
if !parent.IsParent(ctx) { if !parent.IsParent(ctx) {
return res, errors.New("Passing in non-child Context") return res, errors.New("Passing in non-child Context")
} }

View File

@ -64,7 +64,7 @@ func (d *Dispatcher) Name() string {
// Tries to find a registered module (Dispatchable) based on the name of the tx. // Tries to find a registered module (Dispatchable) based on the name of the tx.
// The tx name (as registered with go-data) should be in the form `<module name>/XXXX`, // The tx name (as registered with go-data) should be in the form `<module name>/XXXX`,
// where `module name` must match the name of a dispatchable and XXX can be any string. // where `module name` must match the name of a dispatchable and XXX can be any string.
func (d *Dispatcher) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (d *Dispatcher) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
r, err := d.lookupTx(tx) r, err := d.lookupTx(tx)
if err != nil { if err != nil {
return res, err return res, err
@ -85,7 +85,7 @@ func (d *Dispatcher) CheckTx(ctx basecoin.Context, store state.KVStore, tx basec
// Tries to find a registered module (Dispatchable) based on the name of the tx. // Tries to find a registered module (Dispatchable) based on the name of the tx.
// The tx name (as registered with go-data) should be in the form `<module name>/XXXX`, // The tx name (as registered with go-data) should be in the form `<module name>/XXXX`,
// where `module name` must match the name of a dispatchable and XXX can be any string. // where `module name` must match the name of a dispatchable and XXX can be any string.
func (d *Dispatcher) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (d *Dispatcher) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
r, err := d.lookupTx(tx) r, err := d.lookupTx(tx)
if err != nil { if err != nil {
return res, err return res, err
@ -105,7 +105,7 @@ func (d *Dispatcher) DeliverTx(ctx basecoin.Context, store state.KVStore, tx bas
// //
// Tries to find a registered module (Dispatchable) based on the // Tries to find a registered module (Dispatchable) based on the
// module name from SetOption of the tx. // module name from SetOption of the tx.
func (d *Dispatcher) SetOption(l log.Logger, store state.KVStore, module, key, value string) (string, error) { func (d *Dispatcher) SetOption(l log.Logger, store state.SimpleDB, module, key, value string) (string, error) {
r, err := d.lookupModule(module) r, err := d.lookupModule(module)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -86,12 +86,12 @@ func (OKHandler) Name() string {
} }
// CheckTx always returns an empty success tx // CheckTx always returns an empty success tx
func (ok OKHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (ok OKHandler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
return basecoin.Result{Log: ok.Log}, nil return basecoin.Result{Log: ok.Log}, nil
} }
// DeliverTx always returns an empty success tx // DeliverTx always returns an empty success tx
func (ok OKHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (ok OKHandler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
return basecoin.Result{Log: ok.Log}, nil return basecoin.Result{Log: ok.Log}, nil
} }
@ -108,13 +108,13 @@ func (EchoHandler) Name() string {
} }
// CheckTx always returns an empty success tx // CheckTx always returns an empty success tx
func (EchoHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (EchoHandler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
data, err := data.ToWire(tx) data, err := data.ToWire(tx)
return basecoin.Result{Data: data}, err return basecoin.Result{Data: data}, err
} }
// DeliverTx always returns an empty success tx // DeliverTx always returns an empty success tx
func (EchoHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (EchoHandler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
data, err := data.ToWire(tx) data, err := data.ToWire(tx)
return basecoin.Result{Data: data}, err return basecoin.Result{Data: data}, err
} }
@ -133,12 +133,12 @@ func (FailHandler) Name() string {
} }
// CheckTx always returns the given error // CheckTx always returns the given error
func (f FailHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (f FailHandler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
return res, errors.Wrap(f.Err) return res, errors.Wrap(f.Err)
} }
// DeliverTx always returns the given error // DeliverTx always returns the given error
func (f FailHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (f FailHandler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
return res, errors.Wrap(f.Err) return res, errors.Wrap(f.Err)
} }
@ -157,7 +157,7 @@ func (PanicHandler) Name() string {
} }
// CheckTx always panics // CheckTx always panics
func (p PanicHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (p PanicHandler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
if p.Err != nil { if p.Err != nil {
panic(p.Err) panic(p.Err)
} }
@ -165,7 +165,7 @@ func (p PanicHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx base
} }
// DeliverTx always panics // DeliverTx always panics
func (p PanicHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (p PanicHandler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
if p.Err != nil { if p.Err != nil {
panic(p.Err) panic(p.Err)
} }
@ -185,7 +185,7 @@ func (CheckHandler) Name() string {
} }
// CheckTx verifies the permissions // CheckTx verifies the permissions
func (c CheckHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (c CheckHandler) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
check, ok := tx.Unwrap().(CheckTx) check, ok := tx.Unwrap().(CheckTx)
if !ok { if !ok {
return res, errors.ErrUnknownTxType(tx) return res, errors.ErrUnknownTxType(tx)
@ -200,7 +200,7 @@ func (c CheckHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx base
} }
// DeliverTx verifies the permissions // DeliverTx verifies the permissions
func (c CheckHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (c CheckHandler) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
// until something changes, just do the same as check // until something changes, just do the same as check
return c.CheckTx(ctx, store, tx) return c.CheckTx(ctx, store, tx)
} }

View File

@ -25,14 +25,14 @@ func (_ CheckMiddleware) Name() string {
return NameCheck return NameCheck
} }
func (p CheckMiddleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { func (p CheckMiddleware) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
if !ctx.HasPermission(p.Required) { if !ctx.HasPermission(p.Required) {
return res, errors.ErrUnauthorized() return res, errors.ErrUnauthorized()
} }
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
func (p CheckMiddleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { func (p CheckMiddleware) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
if !ctx.HasPermission(p.Required) { if !ctx.HasPermission(p.Required) {
return res, errors.ErrUnauthorized() return res, errors.ErrUnauthorized()
} }
@ -51,12 +51,12 @@ func (_ GrantMiddleware) Name() string {
return NameGrant return NameGrant
} }
func (g GrantMiddleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { func (g GrantMiddleware) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
ctx = ctx.WithPermissions(g.Auth) ctx = ctx.WithPermissions(g.Auth)
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
func (g GrantMiddleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { func (g GrantMiddleware) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
ctx = ctx.WithPermissions(g.Auth) ctx = ctx.WithPermissions(g.Auth)
return next.DeliverTx(ctx, store, tx) return next.DeliverTx(ctx, store, tx)
} }

View File

@ -20,40 +20,40 @@ type Middleware interface {
} }
type CheckerMiddle interface { type CheckerMiddle interface {
CheckTx(ctx basecoin.Context, store state.KVStore, CheckTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error)
} }
type CheckerMiddleFunc func(basecoin.Context, state.KVStore, type CheckerMiddleFunc func(basecoin.Context, state.SimpleDB,
basecoin.Tx, basecoin.Checker) (basecoin.Result, error) basecoin.Tx, basecoin.Checker) (basecoin.Result, error)
func (c CheckerMiddleFunc) CheckTx(ctx basecoin.Context, store state.KVStore, func (c CheckerMiddleFunc) CheckTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) { tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) {
return c(ctx, store, tx, next) return c(ctx, store, tx, next)
} }
type DeliverMiddle interface { type DeliverMiddle interface {
DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx,
next basecoin.Deliver) (basecoin.Result, error) next basecoin.Deliver) (basecoin.Result, error)
} }
type DeliverMiddleFunc func(basecoin.Context, state.KVStore, type DeliverMiddleFunc func(basecoin.Context, state.SimpleDB,
basecoin.Tx, basecoin.Deliver) (basecoin.Result, error) basecoin.Tx, basecoin.Deliver) (basecoin.Result, error)
func (d DeliverMiddleFunc) DeliverTx(ctx basecoin.Context, store state.KVStore, func (d DeliverMiddleFunc) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) { tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) {
return d(ctx, store, tx, next) return d(ctx, store, tx, next)
} }
type SetOptionMiddle interface { type SetOptionMiddle interface {
SetOption(l log.Logger, store state.KVStore, module, SetOption(l log.Logger, store state.SimpleDB, module,
key, value string, next basecoin.SetOptioner) (string, error) key, value string, next basecoin.SetOptioner) (string, error)
} }
type SetOptionMiddleFunc func(log.Logger, state.KVStore, type SetOptionMiddleFunc func(log.Logger, state.SimpleDB,
string, string, string, basecoin.SetOptioner) (string, error) string, string, string, basecoin.SetOptioner) (string, error)
func (c SetOptionMiddleFunc) SetOption(l log.Logger, store state.KVStore, func (c SetOptionMiddleFunc) SetOption(l log.Logger, store state.SimpleDB,
module, key, value string, next basecoin.SetOptioner) (string, error) { module, key, value string, next basecoin.SetOptioner) (string, error) {
return c(l, store, module, key, value, next) return c(l, store, module, key, value, next)
} }
@ -61,28 +61,28 @@ func (c SetOptionMiddleFunc) SetOption(l log.Logger, store state.KVStore,
// holders // holders
type PassCheck struct{} type PassCheck struct{}
func (_ PassCheck) CheckTx(ctx basecoin.Context, store state.KVStore, func (_ PassCheck) CheckTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) { tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) {
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
type PassDeliver struct{} type PassDeliver struct{}
func (_ PassDeliver) DeliverTx(ctx basecoin.Context, store state.KVStore, func (_ PassDeliver) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) { tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) {
return next.DeliverTx(ctx, store, tx) return next.DeliverTx(ctx, store, tx)
} }
type PassOption struct{} type PassOption struct{}
func (_ PassOption) SetOption(l log.Logger, store state.KVStore, module, func (_ PassOption) SetOption(l log.Logger, store state.SimpleDB, module,
key, value string, next basecoin.SetOptioner) (string, error) { key, value string, next basecoin.SetOptioner) (string, error) {
return next.SetOption(l, store, module, key, value) return next.SetOption(l, store, module, key, value)
} }
type NopOption struct{} type NopOption struct{}
func (_ NopOption) SetOption(l log.Logger, store state.KVStore, module, func (_ NopOption) SetOption(l log.Logger, store state.SimpleDB, module,
key, value string, next basecoin.SetOptioner) (string, error) { key, value string, next basecoin.SetOptioner) (string, error) {
return "", nil return "", nil
} }
@ -112,17 +112,17 @@ func (w wrapped) Name() string {
return w.h.Name() return w.h.Name()
} }
func (w wrapped) CheckTx(ctx basecoin.Context, store state.KVStore, func (w wrapped) CheckTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, _ basecoin.Checker) (basecoin.Result, error) { tx basecoin.Tx, _ basecoin.Checker) (basecoin.Result, error) {
return w.h.CheckTx(ctx, store, tx) return w.h.CheckTx(ctx, store, tx)
} }
func (w wrapped) DeliverTx(ctx basecoin.Context, store state.KVStore, func (w wrapped) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, _ basecoin.Deliver) (basecoin.Result, error) { tx basecoin.Tx, _ basecoin.Deliver) (basecoin.Result, error) {
return w.h.DeliverTx(ctx, store, tx) return w.h.DeliverTx(ctx, store, tx)
} }
func (w wrapped) SetOption(l log.Logger, store state.KVStore, func (w wrapped) SetOption(l log.Logger, store state.SimpleDB,
module, key, value string, _ basecoin.SetOptioner) (string, error) { module, key, value string, _ basecoin.SetOptioner) (string, error) {
return w.h.SetOption(l, store, module, key, value) return w.h.SetOption(l, store, module, key, value)
} }

View File

@ -22,7 +22,7 @@ func (m *middleware) Name() string {
} }
// CheckTx always returns an empty success tx // CheckTx always returns an empty success tx
func (m *middleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (basecoin.Result, error) { func (m *middleware) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (basecoin.Result, error) {
// make sure we pass in proper context to child // make sure we pass in proper context to child
next := secureCheck(m.next, ctx) next := secureCheck(m.next, ctx)
// set the permissions for this app // set the permissions for this app
@ -33,7 +33,7 @@ func (m *middleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basec
} }
// DeliverTx always returns an empty success tx // DeliverTx always returns an empty success tx
func (m *middleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (m *middleware) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx) (res basecoin.Result, err error) {
// make sure we pass in proper context to child // make sure we pass in proper context to child
next := secureDeliver(m.next, ctx) next := secureDeliver(m.next, ctx)
// set the permissions for this app // set the permissions for this app
@ -43,7 +43,7 @@ func (m *middleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx bas
return m.middleware.DeliverTx(ctx, store, tx, next) return m.middleware.DeliverTx(ctx, store, tx, next)
} }
func (m *middleware) SetOption(l log.Logger, store state.KVStore, module, key, value string) (string, error) { func (m *middleware) SetOption(l log.Logger, store state.SimpleDB, module, key, value string) (string, error) {
// set the namespace for the app // set the namespace for the app
store = stateSpace(store, m.Name()) store = stateSpace(store, m.Name())

View File

@ -1,13 +1,18 @@
package stack package stack
import "github.com/tendermint/basecoin/state" import (
"bytes"
"errors"
"github.com/tendermint/basecoin/state"
)
type prefixStore struct { type prefixStore struct {
prefix []byte prefix []byte
store state.KVStore store state.SimpleDB
} }
var _ state.KVStore = prefixStore{} var _ state.SimpleDB = prefixStore{}
func (p prefixStore) Set(key, value []byte) { func (p prefixStore) Set(key, value []byte) {
key = append(p.prefix, key...) key = append(p.prefix, key...)
@ -19,11 +24,78 @@ func (p prefixStore) Get(key []byte) (value []byte) {
return p.store.Get(key) return p.store.Get(key)
} }
func (p prefixStore) Has(key []byte) bool {
key = append(p.prefix, key...)
return p.store.Has(key)
}
func (p prefixStore) Remove(key []byte) (value []byte) {
key = append(p.prefix, key...)
return p.store.Remove(key)
}
func (p prefixStore) List(start, end []byte, limit int) []state.Model {
start = append(p.prefix, start...)
end = append(p.prefix, end...)
res := p.store.List(start, end, limit)
trim := len(p.prefix)
for i := range res {
res[i].Key = res[i].Key[trim:]
}
return res
}
func (p prefixStore) First(start, end []byte) state.Model {
start = append(p.prefix, start...)
end = append(p.prefix, end...)
res := p.store.First(start, end)
if len(res.Key) > 0 {
res.Key = res.Key[len(p.prefix):]
}
return res
}
func (p prefixStore) Last(start, end []byte) state.Model {
start = append(p.prefix, start...)
end = append(p.prefix, end...)
res := p.store.Last(start, end)
if len(res.Key) > 0 {
res.Key = res.Key[len(p.prefix):]
}
return res
}
func (p prefixStore) Checkpoint() state.SimpleDB {
return prefixStore{
prefix: p.prefix,
store: p.store.Checkpoint(),
}
}
func (p prefixStore) Commit(sub state.SimpleDB) error {
ps, ok := sub.(prefixStore)
if !ok {
return errors.New("Must commit prefixStore")
}
if !bytes.Equal(ps.prefix, p.prefix) {
return errors.New("Cannot commit sub-tx with different prefix")
}
// commit the wrapped data, don't worry about the prefix here
p.store.Commit(ps)
return nil
}
func (p prefixStore) Discard() {
p.store.Discard()
}
// stateSpace will unwrap any prefixStore and then add the prefix // stateSpace will unwrap any prefixStore and then add the prefix
// //
// this can be used by the middleware and dispatcher to isolate one space, // this can be used by the middleware and dispatcher to isolate one space,
// then unwrap and isolate another space // then unwrap and isolate another space
func stateSpace(store state.KVStore, app string) state.KVStore { func stateSpace(store state.SimpleDB, app string) state.SimpleDB {
// unwrap one-level if wrapped // unwrap one-level if wrapped
if pstore, ok := store.(prefixStore); ok { if pstore, ok := store.(prefixStore); ok {
store = pstore.store store = pstore.store
@ -31,7 +103,7 @@ func stateSpace(store state.KVStore, app string) state.KVStore {
return PrefixedStore(app, store) return PrefixedStore(app, store)
} }
func unwrap(store state.KVStore) state.KVStore { func unwrap(store state.SimpleDB) state.SimpleDB {
// unwrap one-level if wrapped // unwrap one-level if wrapped
if pstore, ok := store.(prefixStore); ok { if pstore, ok := store.(prefixStore); ok {
store = pstore.store store = pstore.store
@ -45,7 +117,7 @@ func unwrap(store state.KVStore) state.KVStore {
// This is useful for tests or utilities that have access to the global // This is useful for tests or utilities that have access to the global
// state to check individual app spaces. Individual apps should not be able // state to check individual app spaces. Individual apps should not be able
// to use this to read each other's space // to use this to read each other's space
func PrefixedStore(app string, store state.KVStore) state.KVStore { func PrefixedStore(app string, store state.SimpleDB) state.SimpleDB {
prefix := append([]byte(app), byte(0)) prefix := append([]byte(app), byte(0))
return prefixStore{prefix, store} return prefixStore{prefix, store}
} }

View File

@ -26,7 +26,7 @@ func (Recovery) Name() string {
var _ Middleware = Recovery{} var _ Middleware = Recovery{}
// CheckTx catches any panic and converts to error - fulfills Middlware interface // 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) { func (Recovery) CheckTx(ctx basecoin.Context, store state.SimpleDB, 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)
@ -36,7 +36,7 @@ func (Recovery) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.T
} }
// DeliverTx catches any panic and converts to error - fulfills Middlware interface // 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) { func (Recovery) DeliverTx(ctx basecoin.Context, store state.SimpleDB, 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)
@ -46,7 +46,7 @@ func (Recovery) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin
} }
// SetOption catches any panic and converts to error - fulfills Middlware interface // 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) { func (Recovery) SetOption(l log.Logger, store state.SimpleDB, 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

@ -23,19 +23,19 @@ var _ Middleware = writerMid{}
func (w writerMid) Name() string { return w.name } func (w writerMid) Name() string { return w.name }
func (w writerMid) CheckTx(ctx basecoin.Context, store state.KVStore, func (w writerMid) CheckTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) { tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) {
store.Set(w.key, w.value) store.Set(w.key, w.value)
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
func (w writerMid) DeliverTx(ctx basecoin.Context, store state.KVStore, func (w writerMid) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) { tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) {
store.Set(w.key, w.value) store.Set(w.key, w.value)
return next.DeliverTx(ctx, store, tx) return next.DeliverTx(ctx, store, tx)
} }
func (w writerMid) SetOption(l log.Logger, store state.KVStore, module, func (w writerMid) SetOption(l log.Logger, store state.SimpleDB, module,
key, value string, next basecoin.SetOptioner) (string, error) { key, value string, next basecoin.SetOptioner) (string, error) {
store.Set([]byte(key), []byte(value)) store.Set([]byte(key), []byte(value))
return next.SetOption(l, store, module, key, value) return next.SetOption(l, store, module, key, value)
@ -51,19 +51,19 @@ var _ basecoin.Handler = writerHand{}
func (w writerHand) Name() string { return w.name } func (w writerHand) Name() string { return w.name }
func (w writerHand) CheckTx(ctx basecoin.Context, store state.KVStore, func (w writerHand) CheckTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx) (basecoin.Result, error) { tx basecoin.Tx) (basecoin.Result, error) {
store.Set(w.key, w.value) store.Set(w.key, w.value)
return basecoin.Result{}, nil return basecoin.Result{}, nil
} }
func (w writerHand) DeliverTx(ctx basecoin.Context, store state.KVStore, func (w writerHand) DeliverTx(ctx basecoin.Context, store state.SimpleDB,
tx basecoin.Tx) (basecoin.Result, error) { tx basecoin.Tx) (basecoin.Result, error) {
store.Set(w.key, w.value) store.Set(w.key, w.value)
return basecoin.Result{}, nil return basecoin.Result{}, nil
} }
func (w writerHand) SetOption(l log.Logger, store state.KVStore, module, func (w writerHand) SetOption(l log.Logger, store state.SimpleDB, module,
key, value string) (string, error) { key, value string) (string, error) {
store.Set([]byte(key), []byte(value)) store.Set([]byte(key), []byte(value))
return "Success", nil return "Success", nil

View File

@ -1,27 +1,28 @@
package state package state
import ( import (
"io/ioutil"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tendermint/merkleeyes/iavl" "github.com/tendermint/merkleeyes/iavl"
// dbm "github.com/tendermint/tmlibs/db" dbm "github.com/tendermint/tmlibs/db"
) )
func GetDBs() []SimpleDB { func GetDBs() []SimpleDB {
// // tree with persistence.... // tree with persistence....
// tmpDir, err := ioutil.TempDir("", "state-tests") tmpDir, err := ioutil.TempDir("", "state-tests")
// if err != nil { if err != nil {
// panic(err) panic(err)
// } }
// db := dbm.NewDB("test-get-dbs", dbm.LevelDBBackendStr, tmpDir) db := dbm.NewDB("test-get-dbs", dbm.LevelDBBackendStr, tmpDir)
// persist := iavl.NewIAVLTree(500, db) persist := iavl.NewIAVLTree(500, db)
return []SimpleDB{ return []SimpleDB{
NewMemKVStore(), NewMemKVStore(),
NewBonsai(iavl.NewIAVLTree(0, nil)), NewBonsai(iavl.NewIAVLTree(0, nil)),
// NewBonsai(persist), NewBonsai(persist),
} }
} }