github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/privacyenabledstate/optimization_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package privacyenabledstate 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping" 13 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/mock" 14 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestMetadataHintCorrectness(t *testing.T) { 19 bookkeepingTestEnv := bookkeeping.NewTestEnv(t) 20 defer bookkeepingTestEnv.Cleanup() 21 bookkeeper := bookkeepingTestEnv.TestProvider.GetDBHandle("ledger1", bookkeeping.MetadataPresenceIndicator) 22 23 metadataHint := newMetadataHint(bookkeeper) 24 assert.False(t, metadataHint.metadataEverUsedFor("ns1")) 25 26 updates := NewUpdateBatch() 27 updates.PubUpdates.PutValAndMetadata("ns1", "key", []byte("value"), []byte("metadata"), version.NewHeight(1, 1)) 28 updates.PubUpdates.PutValAndMetadata("ns2", "key", []byte("value"), []byte("metadata"), version.NewHeight(1, 2)) 29 updates.PubUpdates.PutValAndMetadata("ns3", "key", []byte("value"), nil, version.NewHeight(1, 3)) 30 updates.HashUpdates.PutValAndMetadata("ns1_pvt", "key", "coll", []byte("value"), []byte("metadata"), version.NewHeight(1, 1)) 31 updates.HashUpdates.PutValAndMetadata("ns2_pvt", "key", "coll", []byte("value"), []byte("metadata"), version.NewHeight(1, 3)) 32 updates.HashUpdates.PutValAndMetadata("ns3_pvt", "key", "coll", []byte("value"), nil, version.NewHeight(1, 3)) 33 metadataHint.setMetadataUsedFlag(updates) 34 35 t.Run("MetadataAddedInCurrentSession", func(t *testing.T) { 36 assert.True(t, metadataHint.metadataEverUsedFor("ns1")) 37 assert.True(t, metadataHint.metadataEverUsedFor("ns2")) 38 assert.True(t, metadataHint.metadataEverUsedFor("ns1_pvt")) 39 assert.True(t, metadataHint.metadataEverUsedFor("ns2_pvt")) 40 assert.False(t, metadataHint.metadataEverUsedFor("ns3")) 41 assert.False(t, metadataHint.metadataEverUsedFor("ns4")) 42 }) 43 44 t.Run("MetadataFromPersistence", func(t *testing.T) { 45 metadataHintFromPersistence := newMetadataHint(bookkeeper) 46 assert.True(t, metadataHintFromPersistence.metadataEverUsedFor("ns1")) 47 assert.True(t, metadataHintFromPersistence.metadataEverUsedFor("ns2")) 48 assert.True(t, metadataHintFromPersistence.metadataEverUsedFor("ns1_pvt")) 49 assert.True(t, metadataHintFromPersistence.metadataEverUsedFor("ns2_pvt")) 50 assert.False(t, metadataHintFromPersistence.metadataEverUsedFor("ns3")) 51 assert.False(t, metadataHintFromPersistence.metadataEverUsedFor("ns4")) 52 }) 53 } 54 55 func TestMetadataHintOptimizationSkippingGoingToDB(t *testing.T) { 56 bookkeepingTestEnv := bookkeeping.NewTestEnv(t) 57 defer bookkeepingTestEnv.Cleanup() 58 bookkeeper := bookkeepingTestEnv.TestProvider.GetDBHandle("ledger1", bookkeeping.MetadataPresenceIndicator) 59 60 mockVersionedDB := &mock.VersionedDB{} 61 db, err := NewCommonStorageDB(mockVersionedDB, "testledger", newMetadataHint(bookkeeper)) 62 assert.NoError(t, err) 63 updates := NewUpdateBatch() 64 updates.PubUpdates.PutValAndMetadata("ns1", "key", []byte("value"), []byte("metadata"), version.NewHeight(1, 1)) 65 updates.PubUpdates.PutValAndMetadata("ns2", "key", []byte("value"), nil, version.NewHeight(1, 2)) 66 db.ApplyPrivacyAwareUpdates(updates, version.NewHeight(1, 3)) 67 68 db.GetStateMetadata("ns1", "randomkey") 69 assert.Equal(t, 1, mockVersionedDB.GetStateCallCount()) 70 db.GetPrivateDataMetadataByHash("ns1", "randomColl", []byte("randomKeyhash")) 71 assert.Equal(t, 2, mockVersionedDB.GetStateCallCount()) 72 73 db.GetStateMetadata("ns2", "randomkey") 74 db.GetPrivateDataMetadataByHash("ns2", "randomColl", []byte("randomKeyhash")) 75 assert.Equal(t, 2, mockVersionedDB.GetStateCallCount()) 76 77 db.GetStateMetadata("randomeNs", "randomkey") 78 db.GetPrivateDataMetadataByHash("randomeNs", "randomColl", []byte("randomKeyhash")) 79 assert.Equal(t, 2, mockVersionedDB.GetStateCallCount()) 80 }