Skip to content

Commit a122dbe

Browse files
authored
internal/ethapi: return error code -32602 for invalid storage key (#33282)
This was found because other clients are failing RPC tests generated by Geth. Nethermind and Besu return the correct error code, -32602, in this situation.
1 parent fed8e09 commit a122dbe

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

internal/ethapi/api.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,9 @@ func (api *BlockChainAPI) GetProof(ctx context.Context, address common.Address,
374374
// Deserialize all keys. This prevents state access on invalid input.
375375
for i, hexKey := range storageKeys {
376376
var err error
377-
keys[i], keyLengths[i], err = decodeHash(hexKey)
377+
keys[i], keyLengths[i], err = decodeStorageKey(hexKey)
378378
if err != nil {
379-
return nil, err
379+
return nil, &invalidParamsError{fmt.Sprintf("%v: %q", err, hexKey)}
380380
}
381381
}
382382
statedb, header, err := api.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
@@ -441,21 +441,22 @@ func (api *BlockChainAPI) GetProof(ctx context.Context, address common.Address,
441441
}, statedb.Error()
442442
}
443443

444-
// decodeHash parses a hex-encoded 32-byte hash. The input may optionally
445-
// be prefixed by 0x and can have a byte length up to 32.
446-
func decodeHash(s string) (h common.Hash, inputLength int, err error) {
444+
// decodeStorageKey parses a hex-encoded 32-byte hash.
445+
// For legacy compatibility reasons, we parse these keys leniently,
446+
// with the 0x prefix being optional.
447+
func decodeStorageKey(s string) (h common.Hash, inputLength int, err error) {
447448
if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") {
448449
s = s[2:]
449450
}
450451
if (len(s) & 1) > 0 {
451452
s = "0" + s
452453
}
453454
if len(s) > 64 {
454-
return common.Hash{}, len(s) / 2, errors.New("hex string too long, want at most 32 bytes")
455+
return common.Hash{}, len(s) / 2, errors.New("storage key too long (want at most 32 bytes)")
455456
}
456457
b, err := hex.DecodeString(s)
457458
if err != nil {
458-
return common.Hash{}, 0, errors.New("hex string invalid")
459+
return common.Hash{}, 0, errors.New("invalid hex in storage key")
459460
}
460461
return common.BytesToHash(b), len(b), nil
461462
}
@@ -589,9 +590,9 @@ func (api *BlockChainAPI) GetStorageAt(ctx context.Context, address common.Addre
589590
if state == nil || err != nil {
590591
return nil, err
591592
}
592-
key, _, err := decodeHash(hexKey)
593+
key, _, err := decodeStorageKey(hexKey)
593594
if err != nil {
594-
return nil, fmt.Errorf("unable to decode storage key: %s", err)
595+
return nil, &invalidParamsError{fmt.Sprintf("%v: %q", err, hexKey)}
595596
}
596597
res := state.GetState(address, key)
597598
return res[:], state.Error()

0 commit comments

Comments
 (0)