github.com/ewagmig/fabric@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/pvtstatepurgemgmt/expiry_schedule_builder_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package pvtstatepurgemgmt 8 9 import ( 10 "testing" 11 12 "github.com/davecgh/go-spew/spew" 13 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/privacyenabledstate" 14 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version" 15 btltestutil "github.com/hyperledger/fabric/core/ledger/pvtdatapolicy/testutil" 16 "github.com/hyperledger/fabric/core/ledger/util" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func TestBuildExpirySchedule(t *testing.T) { 21 btlPolicy := btltestutil.SampleBTLPolicy( 22 map[[2]string]uint64{ 23 {"ns1", "coll1"}: 1, 24 {"ns1", "coll2"}: 2, 25 {"ns2", "coll3"}: 3, 26 {"ns3", "coll4"}: 0, 27 }, 28 ) 29 updates := privacyenabledstate.NewUpdateBatch() 30 updates.PubUpdates.Put("ns1", "pubkey1", []byte("pubvalue1"), version.NewHeight(1, 1)) 31 putPvtAndHashUpdates(t, updates, "ns1", "coll1", "pvtkey1", []byte("pvtvalue1"), version.NewHeight(1, 1)) 32 putPvtAndHashUpdates(t, updates, "ns1", "coll2", "pvtkey2", []byte("pvtvalue2"), version.NewHeight(2, 1)) 33 putPvtAndHashUpdates(t, updates, "ns2", "coll3", "pvtkey3", []byte("pvtvalue3"), version.NewHeight(3, 1)) 34 putPvtAndHashUpdates(t, updates, "ns3", "coll4", "pvtkey4", []byte("pvtvalue4"), version.NewHeight(4, 1)) 35 36 listExpinfo, err := buildExpirySchedule(btlPolicy, updates.PvtUpdates, updates.HashUpdates) 37 assert.NoError(t, err) 38 t.Logf("listExpinfo=%s", spew.Sdump(listExpinfo)) 39 40 pvtdataKeys1 := newPvtdataKeys() 41 pvtdataKeys1.add("ns1", "coll1", "pvtkey1", util.ComputeStringHash("pvtkey1")) 42 43 pvtdataKeys2 := newPvtdataKeys() 44 pvtdataKeys2.add("ns1", "coll2", "pvtkey2", util.ComputeStringHash("pvtkey2")) 45 46 pvtdataKeys3 := newPvtdataKeys() 47 pvtdataKeys3.add("ns2", "coll3", "pvtkey3", util.ComputeStringHash("pvtkey3")) 48 49 expectedListExpInfo := []*expiryInfo{ 50 {expiryInfoKey: &expiryInfoKey{expiryBlk: 3, committingBlk: 1}, pvtdataKeys: pvtdataKeys1}, 51 {expiryInfoKey: &expiryInfoKey{expiryBlk: 5, committingBlk: 2}, pvtdataKeys: pvtdataKeys2}, 52 {expiryInfoKey: &expiryInfoKey{expiryBlk: 7, committingBlk: 3}, pvtdataKeys: pvtdataKeys3}, 53 } 54 55 assert.Len(t, listExpinfo, 3) 56 assert.ElementsMatch(t, expectedListExpInfo, listExpinfo) 57 } 58 59 func TestBuildExpiryScheduleWithMissingPvtdata(t *testing.T) { 60 btlPolicy := btltestutil.SampleBTLPolicy( 61 map[[2]string]uint64{ 62 {"ns1", "coll1"}: 1, 63 {"ns1", "coll2"}: 2, 64 {"ns2", "coll3"}: 3, 65 {"ns3", "coll4"}: 0, 66 {"ns3", "coll5"}: 20, 67 }, 68 ) 69 70 updates := privacyenabledstate.NewUpdateBatch() 71 72 // This update should appear in the expiry schedule with both the key and the hash 73 putPvtAndHashUpdates(t, updates, "ns1", "coll1", "pvtkey1", []byte("pvtvalue1"), version.NewHeight(50, 1)) 74 75 // This update should appear in the expiry schedule with only the key-hash 76 putHashUpdates(updates, "ns1", "coll2", "pvtkey2", []byte("pvtvalue2"), version.NewHeight(50, 2)) 77 78 // This update should appear in the expiry schedule with only the key-hash 79 putHashUpdates(updates, "ns2", "coll3", "pvtkey3", []byte("pvtvalue3"), version.NewHeight(50, 3)) 80 81 // this update is not expectd to appear in the expiry schdule as this collection is configured to expire - 'never' 82 putPvtAndHashUpdates(t, updates, "ns3", "coll4", "pvtkey4", []byte("pvtvalue4"), version.NewHeight(50, 4)) 83 84 // the following two updates are not expected to appear in the expiry schdule as they are deletes 85 deletePvtAndHashUpdates(t, updates, "ns3", "coll5", "pvtkey5", version.NewHeight(50, 5)) 86 deleteHashUpdates(updates, "ns3", "coll5", "pvtkey6", version.NewHeight(50, 6)) 87 88 listExpinfo, err := buildExpirySchedule(btlPolicy, updates.PvtUpdates, updates.HashUpdates) 89 assert.NoError(t, err) 90 t.Logf("listExpinfo=%s", spew.Sdump(listExpinfo)) 91 92 pvtdataKeys1 := newPvtdataKeys() 93 pvtdataKeys1.add("ns1", "coll1", "pvtkey1", util.ComputeStringHash("pvtkey1")) 94 pvtdataKeys2 := newPvtdataKeys() 95 pvtdataKeys2.add("ns1", "coll2", "", util.ComputeStringHash("pvtkey2")) 96 pvtdataKeys3 := newPvtdataKeys() 97 pvtdataKeys3.add("ns2", "coll3", "", util.ComputeStringHash("pvtkey3")) 98 99 expectedListExpInfo := []*expiryInfo{ 100 {expiryInfoKey: &expiryInfoKey{expiryBlk: 52, committingBlk: 50}, pvtdataKeys: pvtdataKeys1}, 101 {expiryInfoKey: &expiryInfoKey{expiryBlk: 53, committingBlk: 50}, pvtdataKeys: pvtdataKeys2}, 102 {expiryInfoKey: &expiryInfoKey{expiryBlk: 54, committingBlk: 50}, pvtdataKeys: pvtdataKeys3}, 103 } 104 105 assert.Len(t, listExpinfo, 3) 106 assert.ElementsMatch(t, expectedListExpInfo, listExpinfo) 107 } 108 109 func putPvtAndHashUpdates(t *testing.T, updates *privacyenabledstate.UpdateBatch, ns, coll, key string, value []byte, ver *version.Height) { 110 putPvtUpdates(updates, ns, coll, key, value, ver) 111 putHashUpdates(updates, ns, coll, key, value, ver) 112 } 113 114 func deletePvtAndHashUpdates(t *testing.T, updates *privacyenabledstate.UpdateBatch, ns, coll, key string, ver *version.Height) { 115 updates.PvtUpdates.Delete(ns, coll, key, ver) 116 deleteHashUpdates(updates, ns, coll, key, ver) 117 } 118 119 func putHashUpdates(updates *privacyenabledstate.UpdateBatch, ns, coll, key string, value []byte, ver *version.Height) { 120 updates.HashUpdates.Put(ns, coll, util.ComputeStringHash(key), util.ComputeHash(value), ver) 121 } 122 123 func putPvtUpdates(updates *privacyenabledstate.UpdateBatch, ns, coll, key string, value []byte, ver *version.Height) { 124 updates.PvtUpdates.Put(ns, coll, key, value, ver) 125 } 126 127 func deleteHashUpdates(updates *privacyenabledstate.UpdateBatch, ns, coll, key string, ver *version.Height) { 128 updates.HashUpdates.Delete(ns, coll, util.ComputeStringHash(key), ver) 129 }