From 1e498f84364175e0d15cab765c22b263c01eeca9 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 24 Sep 2020 13:00:55 +0200 Subject: [PATCH 1/3] common: remove usage of deprecated function 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. --- common/bytes.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index 634041804d0b..2aaa1bc31f7c 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -17,7 +17,11 @@ // Package common contains various helper functions. package common -import "encoding/hex" +import ( + "encoding/hex" + + "github.com/ethereum/go-ethereum/common/hexutil" +) // ToHex returns the hex representation of b, prefixed with '0x'. // For empty slices, the return value is "0x0". @@ -35,7 +39,7 @@ func ToHex(b []byte) string { func ToHexArray(b [][]byte) []string { r := make([]string, len(b)) for i := range b { - r[i] = ToHex(b[i]) + r[i] = hexutil.Encode(b[i]) } return r } From 685a2d22b592f97d766f5c308e117a5673c6c265 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 8 Oct 2020 20:08:54 +0200 Subject: [PATCH 2/3] common: remove hexToArray function --- common/bytes.go | 23 ----------------------- internal/ethapi/api.go | 13 +++++++++++-- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index 2aaa1bc31f7c..7827bb572e13 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -19,31 +19,8 @@ package common import ( "encoding/hex" - - "github.com/ethereum/go-ethereum/common/hexutil" ) -// ToHex returns the hex representation of b, prefixed with '0x'. -// For empty slices, the return value is "0x0". -// -// Deprecated: use hexutil.Encode instead. -func ToHex(b []byte) string { - hex := Bytes2Hex(b) - if len(hex) == 0 { - hex = "0" - } - return "0x" + hex -} - -// ToHexArray creates a array of hex-string based on []byte -func ToHexArray(b [][]byte) []string { - r := make([]string, len(b)) - for i := range b { - r[i] = hexutil.Encode(b[i]) - } - return r -} - // FromHex returns the bytes represented by the hexadecimal string s. // s may be prefixed with "0x". func FromHex(s string) []byte { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index c035b7a9edf1..65f256fe2718 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -592,6 +592,15 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre codeHash = crypto.Keccak256Hash(nil) } + // toHexArray creates a array of hex-string based on []byte + toHexArray := func(b [][]byte) []string { + r := make([]string, len(b)) + for i := range b { + r[i] = hexutil.Encode(b[i]) + } + return r + } + // create the proof for the storageKeys for i, key := range storageKeys { if storageTrie != nil { @@ -599,7 +608,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre if storageError != nil { return nil, storageError } - storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), common.ToHexArray(proof)} + storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), toHexArray(proof)} } else { storageProof[i] = StorageResult{key, &hexutil.Big{}, []string{}} } @@ -613,7 +622,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre return &AccountResult{ Address: address, - AccountProof: common.ToHexArray(accountProof), + AccountProof: toHexArray(accountProof), Balance: (*hexutil.Big)(state.GetBalance(address)), CodeHash: codeHash, Nonce: hexutil.Uint64(state.GetNonce(address)), From 67eac34d671f71ad55889bc8198825c60f65a1a5 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Wed, 14 Oct 2020 19:44:12 +0200 Subject: [PATCH 3/3] internal/ethapi: moved toHexSlice out --- internal/ethapi/api.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 65f256fe2718..262d25f4ef56 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -592,15 +592,6 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre codeHash = crypto.Keccak256Hash(nil) } - // toHexArray creates a array of hex-string based on []byte - toHexArray := func(b [][]byte) []string { - r := make([]string, len(b)) - for i := range b { - r[i] = hexutil.Encode(b[i]) - } - return r - } - // create the proof for the storageKeys for i, key := range storageKeys { if storageTrie != nil { @@ -608,7 +599,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre if storageError != nil { return nil, storageError } - storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), toHexArray(proof)} + storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), toHexSlice(proof)} } else { storageProof[i] = StorageResult{key, &hexutil.Big{}, []string{}} } @@ -622,7 +613,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre return &AccountResult{ Address: address, - AccountProof: toHexArray(accountProof), + AccountProof: toHexSlice(accountProof), Balance: (*hexutil.Big)(state.GetBalance(address)), CodeHash: codeHash, Nonce: hexutil.Uint64(state.GetNonce(address)), @@ -1946,3 +1937,12 @@ func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error { } return nil } + +// toHexSlice creates a slice of hex-strings based on []byte. +func toHexSlice(b [][]byte) []string { + r := make([]string, len(b)) + for i := range b { + r[i] = hexutil.Encode(b[i]) + } + return r +}