Skip to content

Commit 2a1a531

Browse files
authored
Merge pull request ethereum#3570 from fjl/hexutil-zero-fix
common/hexutil: fix EncodeBig, Big.MarshalJSON
2 parents b5a100b + 51f6b6d commit 2a1a531

File tree

3 files changed

+4
-13
lines changed

3 files changed

+4
-13
lines changed

common/hexutil/hexutil.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,7 @@ func EncodeBig(bigint *big.Int) string {
169169
if nbits == 0 {
170170
return "0x0"
171171
}
172-
enc := make([]byte, 2, (nbits/8)*2+2)
173-
copy(enc, "0x")
174-
for i := len(bigint.Bits()) - 1; i >= 0; i-- {
175-
enc = strconv.AppendUint(enc, uint64(bigint.Bits()[i]), 16)
176-
}
177-
return string(enc)
172+
return fmt.Sprintf("0x%x", bigint)
178173
}
179174

180175
func has0xPrefix(input string) bool {

common/hexutil/hexutil_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
{referenceBig("1"), "0x1"},
4747
{referenceBig("ff"), "0xff"},
4848
{referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
49+
{referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"},
4950
}
5051

5152
encodeUint64Tests = []marshalTest{

common/hexutil/json.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,8 @@ func (b *Big) MarshalJSON() ([]byte, error) {
109109
if nbits == 0 {
110110
return jsonZero, nil
111111
}
112-
enc := make([]byte, 3, (nbits/8)*2+4)
113-
copy(enc, `"0x`)
114-
for i := len(bigint.Bits()) - 1; i >= 0; i-- {
115-
enc = strconv.AppendUint(enc, uint64(bigint.Bits()[i]), 16)
116-
}
117-
enc = append(enc, '"')
118-
return enc, nil
112+
enc := fmt.Sprintf(`"0x%x"`, bigint)
113+
return []byte(enc), nil
119114
}
120115

121116
// UnmarshalJSON implements json.Unmarshaler.

0 commit comments

Comments
 (0)