github.com/ewagmig/fabric@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/privacyenabledstate/update_batch_bytes_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 proto "github.com/golang/protobuf/proto" 13 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestUpdateBatchBytesBuilderOnlyPublicWrites(t *testing.T) { 18 updateBatch := NewUpdateBatch() 19 updateBatch.PubUpdates.Put("ns1", "key1", []byte("value1"), version.NewHeight(1, 1)) 20 updateBatch.PubUpdates.Put("ns1", "key2", []byte("value2"), version.NewHeight(1, 2)) 21 updateBatch.PubUpdates.Put("ns2", "key3", []byte("value3"), version.NewHeight(1, 3)) 22 updateBatch.PubUpdates.Put("ns3", "key4", []byte("value4"), version.NewHeight(1, 4)) 23 updateBatch.PubUpdates.Put("ns3", "key5", []byte("value5"), version.NewHeight(1, 5)) 24 updateBatch.PubUpdates.Delete("ns3", "key6", version.NewHeight(1, 6)) 25 26 bb := &UpdatesBytesBuilder{} 27 bytes, err := bb.DeterministicBytesForPubAndHashUpdates(updateBatch) 28 assert.NoError(t, err) 29 assert.True(t, len(bytes) > 0) 30 for i := 0; i < 100; i++ { 31 b, _ := bb.DeterministicBytesForPubAndHashUpdates(updateBatch) 32 assert.Equal(t, bytes, b) 33 } 34 35 expectedProto := &KVWritesBatchProto{ 36 Kvwrites: []*KVWriteProto{ 37 { 38 Namespace: "ns1", 39 Key: []byte("key1"), 40 Value: []byte("value1"), 41 VersionBytes: version.NewHeight(1, 1).ToBytes(), 42 }, 43 { 44 Key: []byte("key2"), // Namespace should not be present, if same as for the previous entry 45 Value: []byte("value2"), 46 VersionBytes: version.NewHeight(1, 2).ToBytes(), 47 }, 48 { 49 Namespace: "ns2", 50 Key: []byte("key3"), 51 Value: []byte("value3"), 52 VersionBytes: version.NewHeight(1, 3).ToBytes(), 53 }, 54 { 55 Namespace: "ns3", 56 Key: []byte("key4"), 57 Value: []byte("value4"), 58 VersionBytes: version.NewHeight(1, 4).ToBytes(), 59 }, 60 { 61 Key: []byte("key5"), 62 Value: []byte("value5"), 63 VersionBytes: version.NewHeight(1, 5).ToBytes(), 64 }, 65 { 66 Key: []byte("key6"), 67 IsDelete: true, 68 VersionBytes: version.NewHeight(1, 6).ToBytes(), 69 }, 70 }, 71 } 72 expectedBytes, err := proto.Marshal(expectedProto) 73 assert.NoError(t, err) 74 assert.Equal(t, expectedBytes, bytes) 75 } 76 77 func TestUpdateBatchBytesBuilderPublicWritesAndColls(t *testing.T) { 78 updateBatch := NewUpdateBatch() 79 updateBatch.PubUpdates.Put("ns1", "key1", []byte("value1"), version.NewHeight(1, 1)) 80 updateBatch.PubUpdates.Put("ns1", "key2", []byte("value2"), version.NewHeight(1, 2)) 81 updateBatch.HashUpdates.Put("ns1", "coll1", []byte("key3"), []byte("value3"), version.NewHeight(1, 3)) 82 updateBatch.HashUpdates.Put("ns1", "coll1", []byte("key4"), []byte("value4"), version.NewHeight(1, 4)) 83 updateBatch.HashUpdates.Put("ns1", "coll2", []byte("key5"), []byte("value5"), version.NewHeight(1, 5)) 84 updateBatch.HashUpdates.Delete("ns1", "coll2", []byte("key6"), version.NewHeight(1, 6)) 85 updateBatch.PubUpdates.Put("ns2", "key7", []byte("value7"), version.NewHeight(1, 7)) 86 87 bb := &UpdatesBytesBuilder{} 88 bytes, err := bb.DeterministicBytesForPubAndHashUpdates(updateBatch) 89 assert.NoError(t, err) 90 assert.True(t, len(bytes) > 0) 91 for i := 0; i < 100; i++ { 92 b, _ := bb.DeterministicBytesForPubAndHashUpdates(updateBatch) 93 assert.Equal(t, bytes, b) 94 } 95 96 expectedProto := &KVWritesBatchProto{ 97 Kvwrites: []*KVWriteProto{ 98 { 99 Namespace: "ns1", 100 Key: []byte("key1"), 101 Value: []byte("value1"), 102 VersionBytes: version.NewHeight(1, 1).ToBytes(), 103 }, 104 { 105 Key: []byte("key2"), // Namespace should not be present, if same as for the previous entry 106 Value: []byte("value2"), 107 VersionBytes: version.NewHeight(1, 2).ToBytes(), 108 }, 109 { 110 Collection: "coll1", 111 Key: []byte("key3"), 112 Value: []byte("value3"), 113 VersionBytes: version.NewHeight(1, 3).ToBytes(), 114 }, 115 { 116 Key: []byte("key4"), // Collection should not be present, if same as for the previous entry 117 Value: []byte("value4"), 118 VersionBytes: version.NewHeight(1, 4).ToBytes(), 119 }, 120 { 121 Collection: "coll2", 122 Key: []byte("key5"), 123 Value: []byte("value5"), 124 VersionBytes: version.NewHeight(1, 5).ToBytes(), 125 }, 126 { 127 Key: []byte("key6"), 128 IsDelete: true, 129 VersionBytes: version.NewHeight(1, 6).ToBytes(), 130 }, 131 { 132 Namespace: "ns2", 133 Key: []byte("key7"), 134 Value: []byte("value7"), 135 VersionBytes: version.NewHeight(1, 7).ToBytes(), 136 }, 137 }, 138 } 139 expectedBytes, err := proto.Marshal(expectedProto) 140 assert.NoError(t, err) 141 assert.Equal(t, expectedBytes, bytes) 142 } 143 144 func TestUpdateBatchBytesBuilderOnlyChannelConfig(t *testing.T) { 145 updateBatch := NewUpdateBatch() 146 updateBatch.PubUpdates.Put("", "resourcesconfigtx.CHANNEL_CONFIG_KEY", []byte("value1"), version.NewHeight(1, 1)) 147 148 bb := &UpdatesBytesBuilder{} 149 bytes, err := bb.DeterministicBytesForPubAndHashUpdates(updateBatch) 150 assert.NoError(t, err) 151 expectedProto := &KVWritesBatchProto{} 152 expectedBytes, err := proto.Marshal(expectedProto) 153 assert.NoError(t, err) 154 assert.Equal(t, expectedBytes, bytes) 155 }