Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1488 from maoueh/fix/rlp-encoding-uint64-extra-zeros
Browse files Browse the repository at this point in the history
Fixed RLP encoding extra leading zeros on uint64
  • Loading branch information
Silas Davis authored May 20, 2021
2 parents 97c7eda + 9b4113e commit 394d62a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions encoding/rlp/rlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ func encodeUint8(input uint8) ([]byte, error) {
}

func encodeUint64(i uint64) ([]byte, error) {
size := bits.Len64(i)/8 + 1
if size == 1 {
// Byte-wise ceiling
byteCount := (bits.Len64(i) + 7) / 8
if byteCount == 1 {
return encodeUint8(uint8(i))
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(i))
return encodeString(b[8-size:])
return encodeString(b[8-byteCount:])
}

func encodeBigInt(b *big.Int) ([]byte, error) {
Expand Down
12 changes: 6 additions & 6 deletions encoding/rlp/rlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ func TestEncoding(t *testing.T) {
t.Run("Integers", func(t *testing.T) {
var tests = []testCase{
{
[]uint64{23, 30400},
[]byte{0xc4, 0x17, 0x82, 0x76, 0xc0},
[][]byte{{23}, {0x76, 0xc0}},
[]uint64{23, 233, 30400},
[]byte{0xc6, 0x17, 0x81, 0xe9, 0x82, 0x76, 0xc0},
[][]byte{{23}, {233}, {0x76, 0xc0}},
},
{
[]*big.Int{big.NewInt(23), big.NewInt(30400)},
[]byte{0xc4, 0x17, 0x82, 0x76, 0xc0},
[][]byte{{23}, {0x76, 0xc0}},
[]*big.Int{big.NewInt(23), big.NewInt(233), big.NewInt(30400)},
[]byte{0xc6, 0x17, 0x81, 0xe9, 0x82, 0x76, 0xc0},
[][]byte{{23}, {233}, {0x76, 0xc0}},
},
}

Expand Down

0 comments on commit 394d62a

Please sign in to comment.