Skip to content

Commit 3eebf34

Browse files
common: remove ToHex and ToHexArray (#21610)
ToHex was deprecated a couple years ago. The last remaining use was in ToHexArray, which itself only had a single call site. This just moves ToHexArray near its only remaining call site and implements it using hexutil.Encode. This changes the default behaviour of ToHexArray and with it the behaviour of eth_getProof. Previously we encoded an empty slice as 0, now the empty slice is encoded as 0x.
1 parent b63bffe commit 3eebf34

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

Diff for: common/bytes.go

+3-22
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,9 @@
1717
// Package common contains various helper functions.
1818
package common
1919

20-
import "encoding/hex"
21-
22-
// ToHex returns the hex representation of b, prefixed with '0x'.
23-
// For empty slices, the return value is "0x0".
24-
//
25-
// Deprecated: use hexutil.Encode instead.
26-
func ToHex(b []byte) string {
27-
hex := Bytes2Hex(b)
28-
if len(hex) == 0 {
29-
hex = "0"
30-
}
31-
return "0x" + hex
32-
}
33-
34-
// ToHexArray creates a array of hex-string based on []byte
35-
func ToHexArray(b [][]byte) []string {
36-
r := make([]string, len(b))
37-
for i := range b {
38-
r[i] = ToHex(b[i])
39-
}
40-
return r
41-
}
20+
import (
21+
"encoding/hex"
22+
)
4223

4324
// FromHex returns the bytes represented by the hexadecimal string s.
4425
// s may be prefixed with "0x".

Diff for: internal/ethapi/api.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre
599599
if storageError != nil {
600600
return nil, storageError
601601
}
602-
storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), common.ToHexArray(proof)}
602+
storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), toHexSlice(proof)}
603603
} else {
604604
storageProof[i] = StorageResult{key, &hexutil.Big{}, []string{}}
605605
}
@@ -613,7 +613,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre
613613

614614
return &AccountResult{
615615
Address: address,
616-
AccountProof: common.ToHexArray(accountProof),
616+
AccountProof: toHexSlice(accountProof),
617617
Balance: (*hexutil.Big)(state.GetBalance(address)),
618618
CodeHash: codeHash,
619619
Nonce: hexutil.Uint64(state.GetNonce(address)),
@@ -1943,3 +1943,12 @@ func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error {
19431943
}
19441944
return nil
19451945
}
1946+
1947+
// toHexSlice creates a slice of hex-strings based on []byte.
1948+
func toHexSlice(b [][]byte) []string {
1949+
r := make([]string, len(b))
1950+
for i := range b {
1951+
r[i] = hexutil.Encode(b[i])
1952+
}
1953+
return r
1954+
}

0 commit comments

Comments
 (0)