3 Star 6 Fork 7

Gitee 极速下载 / Hyperledger fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
version_field_encoding.go 2.86 KB
一键复制 编辑 原始数据 按行查看 历史
Wenjian Qiao 提交于 2019-07-02 12:28 . [FAB-15689] Check decoding error
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package statecouchdb
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
proto "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb/msgs"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
)
func encodeVersionAndMetadata(version *version.Height, metadata []byte) (string, error) {
msg := &msgs.VersionFieldProto{
VersionBytes: version.ToBytes(),
Metadata: metadata,
}
msgBytes, err := proto.Marshal(msg)
if err != nil {
return "", err
}
msgBase64 := base64.StdEncoding.EncodeToString(msgBytes)
encodedVersionField := append([]byte{byte(0)}, []byte(msgBase64)...)
return string(encodedVersionField), nil
}
func decodeVersionAndMetadata(encodedstr string) (*version.Height, []byte, error) {
if oldFormatEncoding(encodedstr) {
return decodeVersionOldFormat(encodedstr), nil, nil
}
versionFieldBytes, err := base64.StdEncoding.DecodeString(encodedstr[1:])
if err != nil {
return nil, nil, err
}
versionFieldMsg := &msgs.VersionFieldProto{}
if err = proto.Unmarshal(versionFieldBytes, versionFieldMsg); err != nil {
return nil, nil, err
}
ver, _, err := version.NewHeightFromBytes(versionFieldMsg.VersionBytes)
if err != nil {
return nil, nil, err
}
return ver, versionFieldMsg.Metadata, nil
}
// encodeVersionOldFormat return string representation of version
// With the introduction of metadata feature, we change the encoding (see function below). However, we retain
// this function for test so as to make sure that we can decode old format and support mixed formats present
// in a statedb. This function should be used only in tests to generate the encoding in old format
func encodeVersionOldFormat(version *version.Height) string {
return fmt.Sprintf("%v:%v", version.BlockNum, version.TxNum)
}
// decodeVersionOldFormat separates the version and value from encoded string
// See comments in the function `encodeVersionOldFormat`. We retain this function as is
// to use this for decoding the old format data present in the statedb. This function
// should not be used directly or in a tests. The function 'decodeVersionAndMetadata' should be used
// for all decodings - which is expected to detect the encoded format and direct the call
// to this function for decoding the versions encoded in the old format
func decodeVersionOldFormat(encodedVersion string) *version.Height {
versionArray := strings.Split(fmt.Sprintf("%s", encodedVersion), ":")
// convert the blockNum from String to unsigned int
blockNum, _ := strconv.ParseUint(versionArray[0], 10, 64)
// convert the txNum from String to unsigned int
txNum, _ := strconv.ParseUint(versionArray[1], 10, 64)
return version.NewHeight(blockNum, txNum)
}
func oldFormatEncoding(encodedstr string) bool {
return []byte(encodedstr)[0] != byte(0)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/fabric.git
git@gitee.com:mirrors/fabric.git
mirrors
fabric
Hyperledger fabric
v1.4.3

搜索帮助

344bd9b3 5694891 D2dac590 5694891