3 Star 6 Fork 7

Gitee 极速下载 / Hyperledger fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
uint64_encoding.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
manish 提交于 2018-08-21 18:05 . Encode block number in reverse order
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package util
import (
"encoding/binary"
"math"
"github.com/golang/protobuf/proto"
)
// 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
}
1
https://gitee.com/mirrors/fabric.git
git@gitee.com:mirrors/fabric.git
mirrors
fabric
Hyperledger fabric
v1.4.12

搜索帮助

53164aa7 5694891 3bd8fe86 5694891