forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kv_encoding.go
262 lines (225 loc) · 8.64 KB
/
kv_encoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pvtdatastorage
import (
"bytes"
"encoding/binary"
"math"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/ledger/rwset"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/pkg/errors"
"github.com/willf/bitset"
)
var (
pendingCommitKey = []byte{0}
lastCommittedBlkkey = []byte{1}
pvtDataKeyPrefix = []byte{2}
expiryKeyPrefix = []byte{3}
eligibleMissingDataKeyPrefix = []byte{4}
ineligibleMissingDataKeyPrefix = []byte{5}
collElgKeyPrefix = []byte{6}
lastUpdatedOldBlocksKey = []byte{7}
nilByte = byte(0)
emptyValue = []byte{}
)
func getDataKeysForRangeScanByBlockNum(blockNum uint64) (startKey, endKey []byte) {
startKey = append(pvtDataKeyPrefix, version.NewHeight(blockNum, 0).ToBytes()...)
endKey = append(pvtDataKeyPrefix, version.NewHeight(blockNum+1, 0).ToBytes()...)
return
}
func getExpiryKeysForRangeScan(minBlkNum, maxBlkNum uint64) (startKey, endKey []byte) {
startKey = append(expiryKeyPrefix, version.NewHeight(minBlkNum, 0).ToBytes()...)
endKey = append(expiryKeyPrefix, version.NewHeight(maxBlkNum+1, 0).ToBytes()...)
return
}
func encodeLastCommittedBlockVal(blockNum uint64) []byte {
return proto.EncodeVarint(blockNum)
}
func decodeLastCommittedBlockVal(blockNumBytes []byte) uint64 {
s, _ := proto.DecodeVarint(blockNumBytes)
return s
}
func encodeDataKey(key *dataKey) []byte {
dataKeyBytes := append(pvtDataKeyPrefix, version.NewHeight(key.blkNum, key.txNum).ToBytes()...)
dataKeyBytes = append(dataKeyBytes, []byte(key.ns)...)
dataKeyBytes = append(dataKeyBytes, nilByte)
return append(dataKeyBytes, []byte(key.coll)...)
}
func encodeDataValue(collData *rwset.CollectionPvtReadWriteSet) ([]byte, error) {
return proto.Marshal(collData)
}
func encodeExpiryKey(expiryKey *expiryKey) []byte {
// reusing version encoding scheme here
return append(expiryKeyPrefix, version.NewHeight(expiryKey.expiringBlk, expiryKey.committingBlk).ToBytes()...)
}
func encodeExpiryValue(expiryData *ExpiryData) ([]byte, error) {
return proto.Marshal(expiryData)
}
func decodeExpiryKey(expiryKeyBytes []byte) (*expiryKey, error) {
height, _, err := version.NewHeightFromBytes(expiryKeyBytes[1:])
if err != nil {
return nil, err
}
return &expiryKey{expiringBlk: height.BlockNum, committingBlk: height.TxNum}, nil
}
func decodeExpiryValue(expiryValueBytes []byte) (*ExpiryData, error) {
expiryData := &ExpiryData{}
err := proto.Unmarshal(expiryValueBytes, expiryData)
return expiryData, err
}
func decodeDatakey(datakeyBytes []byte) (*dataKey, error) {
v, n, err := version.NewHeightFromBytes(datakeyBytes[1:])
if err != nil {
return nil, err
}
blkNum := v.BlockNum
tranNum := v.TxNum
remainingBytes := datakeyBytes[n+1:]
nilByteIndex := bytes.IndexByte(remainingBytes, nilByte)
ns := string(remainingBytes[:nilByteIndex])
coll := string(remainingBytes[nilByteIndex+1:])
return &dataKey{nsCollBlk{ns, coll, blkNum}, tranNum}, nil
}
func decodeDataValue(datavalueBytes []byte) (*rwset.CollectionPvtReadWriteSet, error) {
collPvtdata := &rwset.CollectionPvtReadWriteSet{}
err := proto.Unmarshal(datavalueBytes, collPvtdata)
return collPvtdata, err
}
func encodeMissingDataKey(key *missingDataKey) []byte {
if key.isEligible {
// When missing pvtData reconciler asks for missing data info,
// it is necessary to pass the missing pvtdata info associated with
// the most recent block so that missing pvtdata in the state db can
// be fixed sooner to reduce the "private data matching public hash version
// is not available" error during endorserments. In order to give priority
// to missing pvtData in the most recent block, we use reverse order
// preserving encoding for the missing data key. This simplifies the
// implementation of GetMissingPvtDataInfoForMostRecentBlocks().
keyBytes := append(eligibleMissingDataKeyPrefix, encodeReverseOrderVarUint64(key.blkNum)...)
keyBytes = append(keyBytes, []byte(key.ns)...)
keyBytes = append(keyBytes, nilByte)
return append(keyBytes, []byte(key.coll)...)
}
keyBytes := append(ineligibleMissingDataKeyPrefix, []byte(key.ns)...)
keyBytes = append(keyBytes, nilByte)
keyBytes = append(keyBytes, []byte(key.coll)...)
keyBytes = append(keyBytes, nilByte)
return append(keyBytes, []byte(encodeReverseOrderVarUint64(key.blkNum))...)
}
func decodeMissingDataKey(keyBytes []byte) *missingDataKey {
key := &missingDataKey{nsCollBlk: nsCollBlk{}}
if keyBytes[0] == eligibleMissingDataKeyPrefix[0] {
blkNum, numBytesConsumed := decodeReverseOrderVarUint64(keyBytes[1:])
splittedKey := bytes.Split(keyBytes[numBytesConsumed+1:], []byte{nilByte})
key.ns = string(splittedKey[0])
key.coll = string(splittedKey[1])
key.blkNum = blkNum
key.isEligible = true
return key
}
splittedKey := bytes.SplitN(keyBytes[1:], []byte{nilByte}, 3) //encoded bytes for blknum may contain empty bytes
key.ns = string(splittedKey[0])
key.coll = string(splittedKey[1])
key.blkNum, _ = decodeReverseOrderVarUint64(splittedKey[2])
key.isEligible = false
return key
}
func encodeMissingDataValue(bitmap *bitset.BitSet) ([]byte, error) {
return bitmap.MarshalBinary()
}
func decodeMissingDataValue(bitmapBytes []byte) (*bitset.BitSet, error) {
bitmap := &bitset.BitSet{}
if err := bitmap.UnmarshalBinary(bitmapBytes); err != nil {
return nil, err
}
return bitmap, nil
}
func encodeCollElgKey(blkNum uint64) []byte {
return append(collElgKeyPrefix, encodeReverseOrderVarUint64(blkNum)...)
}
func decodeCollElgKey(b []byte) uint64 {
blkNum, _ := decodeReverseOrderVarUint64(b[1:])
return blkNum
}
func encodeCollElgVal(m *CollElgInfo) ([]byte, error) {
return proto.Marshal(m)
}
func decodeCollElgVal(b []byte) (*CollElgInfo, error) {
m := &CollElgInfo{}
if err := proto.Unmarshal(b, m); err != nil {
return nil, errors.WithStack(err)
}
return m, nil
}
func createRangeScanKeysForEligibleMissingDataEntries(blkNum uint64) (startKey, endKey []byte) {
startKey = append(eligibleMissingDataKeyPrefix, encodeReverseOrderVarUint64(blkNum)...)
endKey = append(eligibleMissingDataKeyPrefix, encodeReverseOrderVarUint64(0)...)
return startKey, endKey
}
func createRangeScanKeysForIneligibleMissingData(maxBlkNum uint64, ns, coll string) (startKey, endKey []byte) {
startKey = encodeMissingDataKey(
&missingDataKey{
nsCollBlk: nsCollBlk{ns: ns, coll: coll, blkNum: maxBlkNum},
isEligible: false,
},
)
endKey = encodeMissingDataKey(
&missingDataKey{
nsCollBlk: nsCollBlk{ns: ns, coll: coll, blkNum: 0},
isEligible: false,
},
)
return
}
func createRangeScanKeysForCollElg() (startKey, endKey []byte) {
return encodeCollElgKey(math.MaxUint64),
encodeCollElgKey(0)
}
func datakeyRange(blockNum uint64) (startKey, endKey []byte) {
startKey = append(pvtDataKeyPrefix, version.NewHeight(blockNum, 0).ToBytes()...)
endKey = append(pvtDataKeyPrefix, version.NewHeight(blockNum, math.MaxUint64).ToBytes()...)
return
}
func eligibleMissingdatakeyRange(blkNum uint64) (startKey, endKey []byte) {
startKey = append(eligibleMissingDataKeyPrefix, encodeReverseOrderVarUint64(blkNum)...)
endKey = append(eligibleMissingDataKeyPrefix, encodeReverseOrderVarUint64(blkNum-1)...)
return
}
// encodeReverseOrderVarUint64 returns a byte-representation for a uint64 number such that
// the number is first subtracted from MaxUint64 and then all the leading 0xff bytes
// are trimmed and replaced by the number of such trimmed bytes. This helps in reducing the size.
// In the byte order comparison this encoding ensures that EncodeReverseOrderVarUint64(A) > EncodeReverseOrderVarUint64(B),
// If B > A
func encodeReverseOrderVarUint64(number uint64) []byte {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, math.MaxUint64-number)
numFFBytes := 0
for _, b := range bytes {
if b != 0xff {
break
}
numFFBytes++
}
size := 8 - numFFBytes
encodedBytes := make([]byte, size+1)
encodedBytes[0] = proto.EncodeVarint(uint64(numFFBytes))[0]
copy(encodedBytes[1:], bytes[numFFBytes:])
return encodedBytes
}
// decodeReverseOrderVarUint64 decodes the number from the bytes obtained from function 'EncodeReverseOrderVarUint64'.
// Also, returns the number of bytes that are consumed in the process
func decodeReverseOrderVarUint64(bytes []byte) (uint64, int) {
s, _ := proto.DecodeVarint(bytes)
numFFBytes := int(s)
decodedBytes := make([]byte, 8)
realBytesNum := 8 - numFFBytes
copy(decodedBytes[numFFBytes:], bytes[1:realBytesNum+1])
numBytesConsumed := realBytesNum + 1
for i := 0; i < numFFBytes; i++ {
decodedBytes[i] = 0xff
}
return (math.MaxUint64 - binary.BigEndian.Uint64(decodedBytes)), numBytesConsumed
}