-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
kv_encoding.go
56 lines (43 loc) · 1.36 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pvtdatastorage
import (
"math"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/protos/ledger/rwset"
)
var (
pendingCommitKey = []byte{0}
lastCommittedBlkkey = []byte{1}
pvtDataKeyPrefix = []byte{2}
emptyValue = []byte{}
)
func encodePK(blockNum uint64, tranNum uint64) blkTranNumKey {
return append(pvtDataKeyPrefix, version.NewHeight(blockNum, tranNum).ToBytes()...)
}
func decodePK(key blkTranNumKey) (blockNum uint64, tranNum uint64) {
height, _ := version.NewHeightFromBytes(key[1:])
return height.BlockNum, height.TxNum
}
func getKeysForRangeScanByBlockNum(blockNum uint64) (startKey []byte, endKey []byte) {
startKey = encodePK(blockNum, 0)
endKey = encodePK(blockNum, math.MaxUint64)
return
}
func encodePvtRwSet(txPvtRwSet *rwset.TxPvtReadWriteSet) ([]byte, error) {
return proto.Marshal(txPvtRwSet)
}
func decodePvtRwSet(encodedBytes []byte) (*rwset.TxPvtReadWriteSet, error) {
writeset := &rwset.TxPvtReadWriteSet{}
return writeset, proto.Unmarshal(encodedBytes, writeset)
}
func encodeBlockNum(blockNum uint64) []byte {
return proto.EncodeVarint(blockNum)
}
func decodeBlockNum(blockNumBytes []byte) uint64 {
s, _ := proto.DecodeVarint(blockNumBytes)
return s
}