|
| 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 | + "fmt" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/davecgh/go-spew/spew" |
| 14 | + |
| 15 | + "github.com/hyperledger/fabric/common/ledger/testutil" |
| 16 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/privacyenabledstate" |
| 17 | + "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version" |
| 18 | + "github.com/hyperledger/fabric/core/ledger/pvtdatapolicy" |
| 19 | + "github.com/hyperledger/fabric/core/ledger/util" |
| 20 | + "github.com/spf13/viper" |
| 21 | +) |
| 22 | + |
| 23 | +func TestBuildExpirySchedule(t *testing.T) { |
| 24 | + ledgerid := "testledger-BuildExpirySchedule" |
| 25 | + viper.Set(fmt.Sprintf("ledger.pvtdata.btlpolicy.%s.ns1.coll1", ledgerid), 1) |
| 26 | + viper.Set(fmt.Sprintf("ledger.pvtdata.btlpolicy.%s.ns1.coll2", ledgerid), 2) |
| 27 | + viper.Set(fmt.Sprintf("ledger.pvtdata.btlpolicy.%s.ns2.coll3", ledgerid), 3) |
| 28 | + |
| 29 | + btlPolicy, _ := pvtdatapolicy.GetBTLPolicy(ledgerid) |
| 30 | + |
| 31 | + updates := privacyenabledstate.NewUpdateBatch() |
| 32 | + updates.PubUpdates.Put("ns1", "pubkey1", []byte("pubvalue1"), version.NewHeight(1, 1)) |
| 33 | + putPvtUpdates(t, updates, "ns1", "coll1", "pvtkey1", []byte("pvtvalue1"), version.NewHeight(1, 1)) |
| 34 | + putPvtUpdates(t, updates, "ns1", "coll2", "pvtkey2", []byte("pvtvalue2"), version.NewHeight(2, 1)) |
| 35 | + putPvtUpdates(t, updates, "ns2", "coll3", "pvtkey3", []byte("pvtvalue3"), version.NewHeight(3, 1)) |
| 36 | + putPvtUpdates(t, updates, "ns3", "coll4", "pvtkey4", []byte("pvtvalue4"), version.NewHeight(4, 1)) |
| 37 | + |
| 38 | + listExpinfo := buildExpirySchedule(btlPolicy, updates.PvtUpdates, updates.HashUpdates) |
| 39 | + t.Logf("listExpinfo=%s", spew.Sdump(listExpinfo)) |
| 40 | + |
| 41 | + pvtdataKeys1 := newPvtdataKeys() |
| 42 | + pvtdataKeys1.add("ns1", "coll1", "pvtkey1", util.ComputeStringHash("pvtkey1")) |
| 43 | + |
| 44 | + pvtdataKeys2 := newPvtdataKeys() |
| 45 | + pvtdataKeys2.add("ns1", "coll2", "pvtkey2", util.ComputeStringHash("pvtkey2")) |
| 46 | + |
| 47 | + pvtdataKeys3 := newPvtdataKeys() |
| 48 | + pvtdataKeys3.add("ns2", "coll3", "pvtkey3", util.ComputeStringHash("pvtkey3")) |
| 49 | + |
| 50 | + expectedListExpInfo := []*expiryInfo{ |
| 51 | + {expiryInfoKey: &expiryInfoKey{expiryBlk: 3, committingBlk: 1}, pvtdataKeys: pvtdataKeys1}, |
| 52 | + {expiryInfoKey: &expiryInfoKey{expiryBlk: 5, committingBlk: 2}, pvtdataKeys: pvtdataKeys2}, |
| 53 | + {expiryInfoKey: &expiryInfoKey{expiryBlk: 7, committingBlk: 3}, pvtdataKeys: pvtdataKeys3}, |
| 54 | + } |
| 55 | + |
| 56 | + testutil.AssertEquals(t, len(listExpinfo), 3) |
| 57 | + testutil.AssertContainsAll(t, listExpinfo, expectedListExpInfo) |
| 58 | +} |
| 59 | + |
| 60 | +func putPvtUpdates(t *testing.T, updates *privacyenabledstate.UpdateBatch, ns, coll, key string, value []byte, ver *version.Height) { |
| 61 | + updates.PvtUpdates.Put(ns, coll, key, value, ver) |
| 62 | + updates.HashUpdates.Put(ns, coll, util.ComputeStringHash(key), util.ComputeHash(value), ver) |
| 63 | +} |
| 64 | + |
| 65 | +func deletePvtUpdates(t *testing.T, updates *privacyenabledstate.UpdateBatch, ns, coll, key string, ver *version.Height) { |
| 66 | + updates.PvtUpdates.Delete(ns, coll, key, ver) |
| 67 | + updates.HashUpdates.Delete(ns, coll, util.ComputeStringHash(key), ver) |
| 68 | +} |
0 commit comments