2017-07-10 13:57:18 -07:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import "github.com/tendermint/basecoin/state"
|
|
|
|
|
|
|
|
type prefixStore struct {
|
|
|
|
prefix []byte
|
|
|
|
store state.KVStore
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ state.KVStore = prefixStore{}
|
|
|
|
|
|
|
|
func (p prefixStore) Set(key, value []byte) {
|
2017-07-11 06:05:37 -07:00
|
|
|
key = append(p.prefix, key...)
|
2017-07-10 13:57:18 -07:00
|
|
|
p.store.Set(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p prefixStore) Get(key []byte) (value []byte) {
|
2017-07-11 06:05:37 -07:00
|
|
|
key = append(p.prefix, key...)
|
2017-07-10 13:57:18 -07:00
|
|
|
return p.store.Get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateSpace will unwrap any prefixStore and then add the prefix
|
2017-07-11 06:35:43 -07:00
|
|
|
//
|
|
|
|
// this can be used by the middleware and dispatcher to isolate one space,
|
|
|
|
// then unwrap and isolate another space
|
2017-07-10 13:57:18 -07:00
|
|
|
func stateSpace(store state.KVStore, app string) state.KVStore {
|
|
|
|
// unwrap one-level if wrapped
|
|
|
|
if pstore, ok := store.(prefixStore); ok {
|
|
|
|
store = pstore.store
|
|
|
|
}
|
2017-07-11 06:35:43 -07:00
|
|
|
return PrefixedStore(app, store)
|
2017-07-10 13:57:18 -07:00
|
|
|
}
|
2017-07-11 06:21:02 -07:00
|
|
|
|
2017-07-25 19:19:31 -07:00
|
|
|
func unwrap(store state.KVStore) state.KVStore {
|
|
|
|
// unwrap one-level if wrapped
|
|
|
|
if pstore, ok := store.(prefixStore); ok {
|
|
|
|
store = pstore.store
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
2017-07-11 06:35:43 -07:00
|
|
|
// PrefixedStore allows one to create an isolated state-space for a given
|
|
|
|
// app prefix, but it cannot easily be unwrapped
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// to use this to read each other's space
|
|
|
|
func PrefixedStore(app string, store state.KVStore) state.KVStore {
|
|
|
|
prefix := append([]byte(app), byte(0))
|
|
|
|
return prefixStore{prefix, store}
|
2017-07-11 06:21:02 -07:00
|
|
|
}
|
|
|
|
|
2017-07-11 06:35:43 -07:00
|
|
|
// PrefixedKey returns the absolute path to a given key in a particular
|
|
|
|
// app's state-space
|
2017-07-11 06:21:02 -07:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// to use this to read each other's space
|
|
|
|
func PrefixedKey(app string, key []byte) []byte {
|
2017-07-11 06:35:43 -07:00
|
|
|
prefix := append([]byte(app), byte(0))
|
|
|
|
return append(prefix, key...)
|
2017-07-11 06:21:02 -07:00
|
|
|
}
|