mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
common: fix BytesToNumber - remove redundant ReadVarInt
* original completely broken with meaningless test. * revu: this appears to be deadcode but fixed to prevent bitrot. * todo: consider removing this function entirely. Both functions appear to be dead code.
This commit is contained in:
parent
d297ea7dd8
commit
78afc228ee
2 changed files with 47 additions and 72 deletions
|
|
@ -61,50 +61,24 @@ func NumberToBytes(num interface{}, bits int) []byte {
|
||||||
return buf.Bytes()[buf.Len()-(bits/8):]
|
return buf.Bytes()[buf.Len()-(bits/8):]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes to number
|
// Converts byte slice to a unsigned integer. Bytes are
|
||||||
//
|
// interpreted as big-endian image of unsigned 64bit integer.
|
||||||
// Attempts to cast a byte slice to a unsigned integer
|
// Slices less than 8 bytes are left padded before conversion;
|
||||||
|
// For slices larger than 8 bytes the trailing bytes are ignored.
|
||||||
func BytesToNumber(b []byte) uint64 {
|
func BytesToNumber(b []byte) uint64 {
|
||||||
var number uint64
|
var data = LeftPadBytes(b, 8)
|
||||||
|
|
||||||
// Make sure the buffer is 64bits
|
var n uint64
|
||||||
data := make([]byte, 8)
|
n |= uint64(data[0]) << 56
|
||||||
data = append(data[:len(b)], b...)
|
n |= uint64(data[1]) << 48
|
||||||
|
n |= uint64(data[2]) << 40
|
||||||
|
n |= uint64(data[3]) << 32
|
||||||
|
n |= uint64(data[4]) << 24
|
||||||
|
n |= uint64(data[5]) << 16
|
||||||
|
n |= uint64(data[6]) << 8
|
||||||
|
n |= uint64(data[7])
|
||||||
|
|
||||||
buf := bytes.NewReader(data)
|
return n
|
||||||
err := binary.Read(buf, binary.BigEndian, &number)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("BytesToNumber failed:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return number
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read variable int
|
|
||||||
//
|
|
||||||
// Read a variable length number in big endian byte order
|
|
||||||
func ReadVarInt(buff []byte) (ret uint64) {
|
|
||||||
switch l := len(buff); {
|
|
||||||
case l > 4:
|
|
||||||
d := LeftPadBytes(buff, 8)
|
|
||||||
binary.Read(bytes.NewReader(d), binary.BigEndian, &ret)
|
|
||||||
case l > 2:
|
|
||||||
var num uint32
|
|
||||||
d := LeftPadBytes(buff, 4)
|
|
||||||
binary.Read(bytes.NewReader(d), binary.BigEndian, &num)
|
|
||||||
ret = uint64(num)
|
|
||||||
case l > 1:
|
|
||||||
var num uint16
|
|
||||||
d := LeftPadBytes(buff, 2)
|
|
||||||
binary.Read(bytes.NewReader(d), binary.BigEndian, &num)
|
|
||||||
ret = uint64(num)
|
|
||||||
default:
|
|
||||||
var num uint8
|
|
||||||
binary.Read(bytes.NewReader(buff), binary.BigEndian, &num)
|
|
||||||
ret = uint64(num)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an exact copy of the provided bytes
|
// Returns an exact copy of the provided bytes
|
||||||
|
|
|
||||||
|
|
@ -39,40 +39,41 @@ func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
|
func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
|
||||||
datasmall := []byte{0xe9, 0x38, 0xe9, 0x38}
|
data := [][]byte{
|
||||||
datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38}
|
// check for zero-len
|
||||||
|
[]byte{},
|
||||||
|
// check for len < 8
|
||||||
|
[]byte{0xfe},
|
||||||
|
[]byte{0xca, 0xfe},
|
||||||
|
[]byte{0xca, 0xfe, 0xba, 0xbe},
|
||||||
|
// check case len < 8
|
||||||
|
[]byte{0x7f, 0x6f, 0x5f, 0x4f, 0x3f, 0x2f, 0x1f, 0x0f},
|
||||||
|
// check case len = 8
|
||||||
|
[]byte{0x7f, 0x6f, 0x5f, 0x4f, 0x3f, 0x2f, 0x1f, 0x0f, 0xba, 0xdc, 0x0d},
|
||||||
|
}
|
||||||
|
expected := []uint64{
|
||||||
|
0x0,
|
||||||
|
0xfe,
|
||||||
|
0xcafe,
|
||||||
|
0xcafebabe,
|
||||||
|
0x7f6f5f4f3f2f1f0f,
|
||||||
|
0x7f6f5f4f3f2f1f0f,
|
||||||
|
}
|
||||||
|
|
||||||
var expsmall uint64 = 0xe938e938
|
// assert-test-invariants:
|
||||||
var explarge uint64 = 0x0
|
c.Assert(len(data[0]), checker.Equals, 0)
|
||||||
|
c.Assert(len(data[1]), checker.Equals, 1)
|
||||||
|
c.Assert(len(data[2]), checker.Equals, 2)
|
||||||
|
c.Assert(len(data[3]), checker.Equals, 4)
|
||||||
|
c.Assert(len(data[4]), checker.Equals, 8)
|
||||||
|
c.Assert(len(data[5]), checker.Equals, 11)
|
||||||
|
|
||||||
ressmall := BytesToNumber(datasmall)
|
c.Assert(len(expected), checker.Equals, len(data))
|
||||||
reslarge := BytesToNumber(datalarge)
|
|
||||||
|
|
||||||
c.Assert(ressmall, checker.Equals, expsmall)
|
for i := 0; i < len(data); i++ {
|
||||||
c.Assert(reslarge, checker.Equals, explarge)
|
have := BytesToNumber(data[i])
|
||||||
|
c.Assert(have, checker.Equals, expected[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BytesSuite) TestReadVarInt(c *checker.C) {
|
|
||||||
data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
|
|
||||||
data4 := []byte{1, 2, 3, 4}
|
|
||||||
data2 := []byte{1, 2}
|
|
||||||
data1 := []byte{1}
|
|
||||||
|
|
||||||
exp8 := uint64(72623859790382856)
|
|
||||||
exp4 := uint64(16909060)
|
|
||||||
exp2 := uint64(258)
|
|
||||||
exp1 := uint64(1)
|
|
||||||
|
|
||||||
res8 := ReadVarInt(data8)
|
|
||||||
res4 := ReadVarInt(data4)
|
|
||||||
res2 := ReadVarInt(data2)
|
|
||||||
res1 := ReadVarInt(data1)
|
|
||||||
|
|
||||||
c.Assert(res8, checker.Equals, exp8)
|
|
||||||
c.Assert(res4, checker.Equals, exp4)
|
|
||||||
c.Assert(res2, checker.Equals, exp2)
|
|
||||||
c.Assert(res1, checker.Equals, exp1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BytesSuite) TestCopyBytes(c *checker.C) {
|
func (s *BytesSuite) TestCopyBytes(c *checker.C) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue