github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/statedb/cache_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package statedb 8 9 import ( 10 "testing" 11 12 "github.com/VictoriaMetrics/fastcache" 13 "github.com/golang/protobuf/proto" 14 "github.com/stretchr/testify/require" 15 ) 16 17 var sysNamespaces = []string{"lscc", "_lifecycle"} 18 19 func TestNewCache(t *testing.T) { 20 cache := NewCache(32, sysNamespaces) 21 expectedCache := &Cache{ 22 sysCache: fastcache.New(64 * 1024 * 1024), 23 usrCache: fastcache.New(32 * 1024 * 1024), 24 sysNamespaces: sysNamespaces, 25 } 26 require.Equal(t, expectedCache, cache) 27 require.True(t, cache.Enabled("lscc")) 28 require.True(t, cache.Enabled("_lifecycle")) 29 require.True(t, cache.Enabled("xyz")) 30 31 cache = NewCache(0, sysNamespaces) 32 expectedCache = &Cache{ 33 sysCache: fastcache.New(64 * 1024 * 1024), 34 usrCache: nil, 35 sysNamespaces: sysNamespaces, 36 } 37 require.Equal(t, expectedCache, cache) 38 require.True(t, cache.Enabled("lscc")) 39 require.True(t, cache.Enabled("_lifecycle")) 40 require.False(t, cache.Enabled("xyz")) 41 } 42 43 func TestGetPutState(t *testing.T) { 44 cache := NewCache(32, sysNamespaces) 45 46 // test GetState 47 v, err := cache.GetState("ch1", "ns1", "k1") 48 require.NoError(t, err) 49 require.Nil(t, v) 50 51 // test PutState 52 expectedValue1 := &CacheValue{Value: []byte("value1")} 53 require.NoError(t, cache.PutState("ch1", "ns1", "k1", expectedValue1)) 54 55 v, err = cache.GetState("ch1", "ns1", "k1") 56 require.NoError(t, err) 57 require.True(t, proto.Equal(expectedValue1, v)) 58 } 59 60 func TestUpdateStates(t *testing.T) { 61 cache := NewCache(32, sysNamespaces) 62 63 // create states for three namespaces (ns1, ns2, ns3) 64 // each with two keys (k1, k2) 65 expectedValue1 := &CacheValue{Value: []byte("value1")} 66 require.NoError(t, cache.PutState("ch1", "ns1", "k1", expectedValue1)) 67 expectedValue2 := &CacheValue{Value: []byte("value2")} 68 require.NoError(t, cache.PutState("ch1", "ns1", "k2", expectedValue2)) 69 expectedValue3 := &CacheValue{Value: []byte("value3")} 70 require.NoError(t, cache.PutState("ch1", "ns2", "k1", expectedValue3)) 71 expectedValue4 := &CacheValue{Value: []byte("value4")} 72 require.NoError(t, cache.PutState("ch1", "ns2", "k2", expectedValue4)) 73 expectedValue5 := &CacheValue{Value: []byte("value5")} 74 require.NoError(t, cache.PutState("ch1", "ns3", "k1", expectedValue5)) 75 expectedValue6 := &CacheValue{Value: []byte("value6")} 76 require.NoError(t, cache.PutState("ch1", "ns3", "k2", expectedValue6)) 77 78 v1, err := cache.GetState("ch1", "ns1", "k1") 79 require.NoError(t, err) 80 require.True(t, proto.Equal(expectedValue1, v1)) 81 v2, err := cache.GetState("ch1", "ns1", "k2") 82 require.NoError(t, err) 83 require.True(t, proto.Equal(expectedValue2, v2)) 84 v3, err := cache.GetState("ch1", "ns2", "k1") 85 require.NoError(t, err) 86 require.True(t, proto.Equal(expectedValue3, v3)) 87 v4, err := cache.GetState("ch1", "ns2", "k2") 88 require.NoError(t, err) 89 require.True(t, proto.Equal(expectedValue4, v4)) 90 v5, err := cache.GetState("ch1", "ns3", "k1") 91 require.NoError(t, err) 92 require.True(t, proto.Equal(expectedValue5, v5)) 93 v6, err := cache.GetState("ch1", "ns3", "k2") 94 require.NoError(t, err) 95 require.True(t, proto.Equal(expectedValue6, v6)) 96 97 // delete (ns2, k1), (ns3, k1), and (ns3, k2) while updating others. 98 // nil value represents a delete operation. A new entry (ns3, k3) 99 // is also being passed but would not get added to the cache as the 100 // entry does not exist in cache already. 101 expectedValue7 := &CacheValue{Value: []byte("value7")} 102 expectedValue8 := &CacheValue{Value: []byte("value8")} 103 expectedValue9 := &CacheValue{Value: []byte("value9")} 104 expectedValue10 := &CacheValue{Value: []byte("value10")} 105 updates := CacheUpdates{ 106 "ns1": CacheKVs{ 107 "k1": expectedValue7, 108 "k2": expectedValue8, 109 }, 110 "ns2": CacheKVs{ 111 "k1": nil, 112 "k2": expectedValue9, 113 }, 114 "ns3": CacheKVs{ 115 "k1": nil, 116 "k2": nil, 117 "k3": expectedValue10, 118 }, 119 } 120 121 require.NoError(t, cache.UpdateStates("ch1", updates)) 122 123 v7, err := cache.GetState("ch1", "ns1", "k1") 124 require.NoError(t, err) 125 require.True(t, proto.Equal(expectedValue7, v7)) 126 127 v8, err := cache.GetState("ch1", "ns1", "k2") 128 require.NoError(t, err) 129 require.True(t, proto.Equal(expectedValue8, v8)) 130 131 v9, err := cache.GetState("ch1", "ns2", "k2") 132 require.NoError(t, err) 133 require.True(t, proto.Equal(expectedValue9, v9)) 134 135 v, err := cache.GetState("ch1", "ns2", "k1") 136 require.NoError(t, err) 137 require.Nil(t, v) 138 v, err = cache.GetState("ch1", "ns3", "k1") 139 require.NoError(t, err) 140 require.Nil(t, v) 141 v, err = cache.GetState("ch1", "ns3", "k2") 142 require.NoError(t, err) 143 require.Nil(t, v) 144 v, err = cache.GetState("ch1", "ns3", "k3") 145 require.NoError(t, err) 146 require.Nil(t, v) 147 } 148 149 func TestCacheReset(t *testing.T) { 150 cache := NewCache(32, sysNamespaces) 151 152 // create states for three namespaces (ns1, ns2, ns3) 153 // each with two keys (k1, k2) 154 expectedValue1 := &CacheValue{Value: []byte("value1")} 155 require.NoError(t, cache.PutState("ch1", "ns1", "k1", expectedValue1)) 156 157 expectedValue2 := &CacheValue{Value: []byte("value2")} 158 require.NoError(t, cache.PutState("ch1", "ns2", "k1", expectedValue2)) 159 160 expectedValue3 := &CacheValue{Value: []byte("value3")} 161 require.NoError(t, cache.PutState("ch1", "lscc", "k1", expectedValue3)) 162 163 v1, err := cache.GetState("ch1", "ns1", "k1") 164 require.NoError(t, err) 165 require.True(t, proto.Equal(expectedValue1, v1)) 166 167 v2, err := cache.GetState("ch1", "ns2", "k1") 168 require.NoError(t, err) 169 require.True(t, proto.Equal(expectedValue2, v2)) 170 171 v3, err := cache.GetState("ch1", "lscc", "k1") 172 require.NoError(t, err) 173 require.True(t, proto.Equal(expectedValue3, v3)) 174 175 cache.Reset() 176 177 v, err := cache.GetState("ch1", "ns1", "k1") 178 require.NoError(t, err) 179 require.Nil(t, v) 180 181 v, err = cache.GetState("ch1", "ns2", "k1") 182 require.NoError(t, err) 183 require.Nil(t, v) 184 185 v, err = cache.GetState("ch1", "lscc", "k1") 186 require.NoError(t, err) 187 require.Nil(t, v) 188 } 189 190 func TestCacheUpdates(t *testing.T) { 191 u := make(CacheUpdates) 192 u.Add("ns1", CacheKVs{ 193 "k1": &CacheValue{Value: []byte("v1")}, 194 "k2": &CacheValue{Value: []byte("v2")}, 195 }) 196 197 u.Add("ns1", CacheKVs{ 198 "k3": &CacheValue{Value: []byte("v1")}, 199 "k4": &CacheValue{Value: []byte("v2")}, 200 }) 201 202 u.Add("ns2", CacheKVs{ 203 "k1": &CacheValue{Value: []byte("v1")}, 204 "k2": &CacheValue{Value: []byte("v2")}, 205 }) 206 207 expectedCacheUpdates := CacheUpdates{ 208 "ns1": CacheKVs{ 209 "k1": &CacheValue{Value: []byte("v1")}, 210 "k2": &CacheValue{Value: []byte("v2")}, 211 "k3": &CacheValue{Value: []byte("v1")}, 212 "k4": &CacheValue{Value: []byte("v2")}, 213 }, 214 215 "ns2": CacheKVs{ 216 "k1": &CacheValue{Value: []byte("v1")}, 217 "k2": &CacheValue{Value: []byte("v2")}, 218 }, 219 } 220 221 require.Equal(t, expectedCacheUpdates, u) 222 }