github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/kvstorefortrie_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package protocol 7 8 import ( 9 "bytes" 10 "context" 11 "encoding/hex" 12 "testing" 13 14 "github.com/iotexproject/iotex-core/state" 15 "github.com/stretchr/testify/require" 16 ) 17 18 type inMemStateManager struct { 19 store map[string]map[string][]byte 20 } 21 22 func newInMemStateManager() *inMemStateManager { 23 return &inMemStateManager{ 24 store: map[string]map[string][]byte{}, 25 } 26 } 27 28 func (imsm *inMemStateManager) Height() (uint64, error) { 29 return 0, nil 30 } 31 32 func (imsm *inMemStateManager) State(s interface{}, opts ...StateOption) (uint64, error) { 33 cfg, err := CreateStateConfig(opts...) 34 if err != nil { 35 return 0, err 36 } 37 value, ok := imsm.store[cfg.Namespace][hex.EncodeToString(cfg.Key)] 38 if !ok { 39 return 0, state.ErrStateNotExist 40 } 41 state.Deserialize(s, value) 42 return 0, nil 43 } 44 45 func (imsm *inMemStateManager) States(...StateOption) (uint64, state.Iterator, error) { 46 return 0, nil, nil 47 } 48 49 func (imsm *inMemStateManager) ReadView(string) (interface{}, error) { 50 return nil, nil 51 } 52 53 func (imsm *inMemStateManager) Snapshot() int { 54 return 0 55 } 56 57 func (imsm *inMemStateManager) Revert(int) error { 58 return nil 59 } 60 61 func (imsm *inMemStateManager) PutState(s interface{}, opts ...StateOption) (uint64, error) { 62 cfg, err := CreateStateConfig(opts...) 63 if err != nil { 64 return 0, err 65 } 66 value, err := state.Serialize(s) 67 if err != nil { 68 return 0, err 69 } 70 if _, ok := imsm.store[cfg.Namespace]; !ok { 71 imsm.store[cfg.Namespace] = map[string][]byte{} 72 } 73 imsm.store[cfg.Namespace][hex.EncodeToString(cfg.Key)] = value 74 75 return 0, nil 76 } 77 78 func (imsm *inMemStateManager) DelState(opts ...StateOption) (uint64, error) { 79 cfg, err := CreateStateConfig(opts...) 80 if err != nil { 81 return 0, err 82 } 83 if _, ok := imsm.store[cfg.Namespace][hex.EncodeToString(cfg.Key)]; !ok { 84 return 0, state.ErrStateNotExist 85 } 86 delete(imsm.store[cfg.Namespace], hex.EncodeToString(cfg.Key)) 87 88 return 0, nil 89 } 90 91 func (imsm *inMemStateManager) WriteView(string, interface{}) error { 92 return nil 93 } 94 95 func (imsm *inMemStateManager) ProtocolDirty(string) bool { 96 return false 97 } 98 99 func (imsm *inMemStateManager) Load(string, string, interface{}) error { 100 return nil 101 } 102 103 func (imsm *inMemStateManager) Unload(string, string, interface{}) error { 104 return nil 105 } 106 107 func (imsm *inMemStateManager) Reset() { 108 } 109 110 func TestKVStoreForTrie(t *testing.T) { 111 require := require.New(t) 112 ns := "namespace" 113 key := []byte("key") 114 value := SerializableBytes("value") 115 sm := newInMemStateManager() 116 kvstore := NewKVStoreForTrieWithStateManager(ns, sm) 117 require.NoError(kvstore.Start(context.Background())) 118 require.NoError(kvstore.Stop(context.Background())) 119 _, err := kvstore.Get(key) 120 require.Error(err) 121 require.NoError(kvstore.Delete(key)) 122 require.NoError(kvstore.Put(key, value)) 123 fromStore, err := kvstore.Get(key) 124 require.NoError(err) 125 require.True(bytes.Equal(fromStore, value)) 126 require.NoError(kvstore.Delete(key)) 127 _, err = kvstore.Get(key) 128 require.Error(err) 129 require.NoError(kvstore.Put(key, value)) 130 kvstore = NewKVStoreForTrieWithStateReader(ns, sm) 131 require.NoError(kvstore.Start(context.Background())) 132 require.NoError(kvstore.Stop(context.Background())) 133 require.Error(kvstore.Put(key, value), "not implemented") 134 require.Error(kvstore.Delete(key), "not implemented") 135 _, err = kvstore.Get([]byte("not exist")) 136 require.Error(err) 137 fromStore, err = kvstore.Get(key) 138 require.NoError(err) 139 require.True(bytes.Equal(fromStore, value)) 140 141 }