Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/ethapi: return error when requesting invalid trie key #25762

Merged
merged 5 commits into from
Sep 16, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ethapi

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -674,15 +675,19 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
}

// create the proof for the storageKeys
for i, key := range storageKeys {
for i, hexKey := range storageKeys {
key, err := decodeHash(hexKey)
lightclient marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
if storageTrie != nil {
proof, storageError := state.GetStorageProof(address, common.HexToHash(key))
proof, storageError := state.GetStorageProof(address, key)
if storageError != nil {
return nil, storageError
}
storageProof[i] = StorageResult{key, (*hexutil.Big)(state.GetState(address, common.HexToHash(key)).Big()), toHexSlice(proof)}
storageProof[i] = StorageResult{hexKey, (*hexutil.Big)(state.GetState(address, key).Big()), toHexSlice(proof)}
} else {
storageProof[i] = StorageResult{key, &hexutil.Big{}, []string{}}
storageProof[i] = StorageResult{hexKey, &hexutil.Big{}, []string{}}
}
}

Expand All @@ -703,6 +708,24 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
}, state.Error()
}

func decodeHash(s string) (common.Hash, error) {
if has0xPrefix(s) {
s = s[2:]
}
b, err := hex.DecodeString(s)
if err != nil {
return common.Hash{}, fmt.Errorf("hex string invalid")
}
if len(b) > 32 {
return common.Hash{}, fmt.Errorf("hex string too long, want at most 32 bytes")
}
return common.BytesToHash(b), nil
}

func has0xPrefix(input string) bool {
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
}

// GetHeaderByNumber returns the requested canonical block header.
// * When blockNr is -1 the chain head is returned.
// * When blockNr is -2 the pending chain head is returned.
Expand Down Expand Up @@ -821,12 +844,16 @@ func (s *BlockChainAPI) GetCode(ctx context.Context, address common.Address, blo
// GetStorageAt returns the storage from the state at the given address, key and
// block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta block
// numbers are also allowed.
func (s *BlockChainAPI) GetStorageAt(ctx context.Context, address common.Address, key string, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
func (s *BlockChainAPI) GetStorageAt(ctx context.Context, address common.Address, hexKey string, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
state, _, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
if state == nil || err != nil {
return nil, err
}
res := state.GetState(address, common.HexToHash(key))
key, err := decodeHash(hexKey)
if err != nil {
return nil, fmt.Errorf("unable to decode storage key: %s", err)
}
res := state.GetState(address, key)
return res[:], state.Error()
}

Expand Down