@@ -13,7 +13,8 @@ import (
13
13
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
14
14
)
15
15
16
- // encode value encodes the versioned value
16
+ // encode value encodes the versioned value. starting in v1.3 the encoding begins with a nil
17
+ // byte and includes metadata.
17
18
func encodeValue (v * statedb.VersionedValue ) ([]byte , error ) {
18
19
vvMsg := & msgs.VersionedValueProto {
19
20
VersionBytes : v .Version .ToBytes (),
@@ -28,6 +29,8 @@ func encodeValue(v *statedb.VersionedValue) ([]byte, error) {
28
29
return encodedValue , nil
29
30
}
30
31
32
+ // decodeValue decodes the statedb value bytes using either the old (pre-v1.3) encoding
33
+ // or the new (v1.3 and later) encoding that supports metadata.
31
34
func decodeValue (encodedValue []byte ) (* statedb.VersionedValue , error ) {
32
35
if oldFormatEncoding (encodedValue ) {
33
36
val , ver := decodeValueOldFormat (encodedValue )
@@ -49,7 +52,7 @@ func decodeValue(encodedValue []byte) (*statedb.VersionedValue, error) {
49
52
}
50
53
51
54
// encodeValueOldFormat appends the value to the version, allows storage of version and value in binary form.
52
- // With the intorduction of metadata feature, we change the encoding (see function below). However, we retain
55
+ // With the introduction of metadata feature in v1.3 , we change the encoding (see function below). However, we retain
53
56
// this funtion for test so as to make sure that we can decode old format and support mixed formats present
54
57
// in a statedb. This function should be used only in tests to generate the encoding in old format
55
58
func encodeValueOldFormat (value []byte , version * version.Height ) []byte {
@@ -62,7 +65,7 @@ func encodeValueOldFormat(value []byte, version *version.Height) []byte {
62
65
63
66
// decodeValueOldFormat separates the version and value from a binary value
64
67
// See comments in the function `encodeValueOldFormat`. We retain this function as is
65
- // to use this for decoding the old format data present in the statedb. This function
68
+ // to use this for decoding the old format (pre-v1.3) data present in the statedb. This function
66
69
// should not be used directly or in a tests. The function 'decodeValue' should be used
67
70
// for all decodings - which is expected to detect the encoded format and direct the call
68
71
// to this function for decoding the values encoded in the old format
@@ -72,6 +75,12 @@ func decodeValueOldFormat(encodedValue []byte) ([]byte, *version.Height) {
72
75
return value , height
73
76
}
74
77
78
+ // oldFormatEncoding checks whether the value is encoded using the old (pre-v1.3) format
79
+ // or new format (v1.3 and later for encoding metadata).
75
80
func oldFormatEncoding (encodedValue []byte ) bool {
76
- return encodedValue [0 ] != byte (0 )
81
+ return encodedValue [0 ] != byte (0 ) ||
82
+ (encodedValue [0 ]| encodedValue [1 ]) == byte (0 ) // this check covers a corner case
83
+ // where the old formatted value happens to start with a nil byte. In this corner case,
84
+ // the channel config happen to be persisted for the tuple <block 0, tran 0>. So, this
85
+ // is assumed that block 0 contains a single transaction (i.e., tran 0)
77
86
}
0 commit comments