/
store.go
47 lines (39 loc) · 1.1 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package kv
import (
"fmt"
"strings"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
)
type Store struct {
storetypes.KVStore
writeWhitelist map[string]struct{}
}
func NewStore(parent storetypes.KVStore, writeWhitelistKeys []string) storetypes.KVStore {
writeWhitelist := map[string]struct{}{}
for _, writeWhitelistKey := range writeWhitelistKeys {
writeWhitelist[writeWhitelistKey] = struct{}{}
}
return &Store{
KVStore: parent,
writeWhitelist: writeWhitelist,
}
}
func (store *Store) Set(key []byte, value []byte) {
store.validateKeyForWrite(key)
store.KVStore.Set(key, value)
}
func (store *Store) Delete(key []byte) {
store.validateKeyForWrite(key)
store.KVStore.Delete(key)
}
func (store *Store) validateKeyForWrite(key []byte) {
keyStr := string(key)
for whitelist := range store.writeWhitelist {
if strings.HasPrefix(keyStr, whitelist) {
return
}
}
// Panic since the Store interface does not return error on Set. Can be
// intercepted by calling routine through `err := recover()`
panic(fmt.Sprintf("Setting disallowed key %s in whitelisted KV store", keyStr))
}