From 2f0cb2569ffe989cd0afcc85ed124c305f173515 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 10 Jul 2017 22:57:18 +0200 Subject: [PATCH] Add prefixStore (state space) --- stack/prefixstore.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 stack/prefixstore.go diff --git a/stack/prefixstore.go b/stack/prefixstore.go new file mode 100644 index 000000000..8592ba321 --- /dev/null +++ b/stack/prefixstore.go @@ -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} +}