Add prefixStore (state space)

This commit is contained in:
Ethan Frey 2017-07-10 22:57:18 +02:00
parent fbdec34ea6
commit 2f0cb2569f
1 changed files with 31 additions and 0 deletions

31
stack/prefixstore.go Normal file
View File

@ -0,0 +1,31 @@
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) {
key = append(key, p.prefix...)
p.store.Set(key, value)
}
func (p prefixStore) Get(key []byte) (value []byte) {
key = append(key, p.prefix...)
return p.store.Get(key)
}
// stateSpace will unwrap any prefixStore and then add the prefix
func stateSpace(store state.KVStore, app string) state.KVStore {
// unwrap one-level if wrapped
if pstore, ok := store.(prefixStore); ok {
store = pstore.store
}
// wrap it with the prefix
prefix := append([]byte(app), byte(0))
return prefixStore{prefix, store}
}