Skip to content

Commit

Permalink
fix: support storage key RPC response with <32 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
meyer9 committed Oct 22, 2024
1 parent d8012e0 commit 1703d7c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions op-service/eth/account_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type StorageProofEntry struct {
Key common.Hash `json:"key"`
Key hexutil.Big `json:"key"`
Value hexutil.Big `json:"value"`
Proof []hexutil.Bytes `json:"proof"`
}
Expand Down Expand Up @@ -46,20 +46,20 @@ func (res *AccountResult) Verify(stateRoot common.Hash) error {
return fmt.Errorf("failed to load storage proof node %d of storage value %d into mem db: %w", j, i, err)
}
}
path := crypto.Keccak256(entry.Key[:])
path := crypto.Keccak256(entry.Key.ToInt().FillBytes(make([]byte, 32)))
val, err := trie.VerifyProof(res.StorageHash, path, db)
if err != nil {
return fmt.Errorf("failed to verify storage value %d with key %s (path %x) in storage trie %s: %w", i, entry.Key, path, res.StorageHash, err)
return fmt.Errorf("failed to verify storage value %d with key %s (path %x) in storage trie %s: %w", i, entry.Key.String(), path, res.StorageHash, err)
}
if val == nil && entry.Value.ToInt().Cmp(common.Big0) == 0 { // empty storage is zero by default
continue
}
comparison, err := rlp.EncodeToBytes(entry.Value.ToInt().Bytes())
if err != nil {
return fmt.Errorf("failed to encode storage value %d with key %s (path %x) in storage trie %s: %w", i, entry.Key, path, res.StorageHash, err)
return fmt.Errorf("failed to encode storage value %d with key %s (path %x) in storage trie %s: %w", i, entry.Key.String(), path, res.StorageHash, err)
}
if !bytes.Equal(val, comparison) {
return fmt.Errorf("value %d in storage proof does not match proven value at key %s (path %x)", i, entry.Key, path)
return fmt.Errorf("value %d in storage proof does not match proven value at key %s (path %x)", i, entry.Key.String(), path)
}
}

Expand Down
2 changes: 1 addition & 1 deletion op-service/eth/account_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestAccountResult_Verify(t *testing.T) {
func FuzzAccountResult_StorageProof(f *testing.F) {
f.Fuzz(func(t *testing.T, key []byte, value []byte) {
result := makeResult(t)
result.StorageProof[0].Key = common.BytesToHash(key)
result.StorageProof[0].Key = hexutil.Big(*common.BytesToHash(key).Big())
result.StorageProof[0].Value = hexutil.Big(*(new(big.Int).SetBytes(value)))
require.NotNil(t, result.Verify(goodRoot), "does not verify against bad proof")
})
Expand Down
4 changes: 2 additions & 2 deletions op-service/sources/eth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ func (s *EthClient) GetProof(ctx context.Context, address common.Address, storag
return nil, fmt.Errorf("missing storage proof data, got %d proof entries but requested %d storage keys", len(getProofResponse.StorageProof), len(storage))
}
for i, key := range storage {
if key != getProofResponse.StorageProof[i].Key {
return nil, fmt.Errorf("unexpected storage proof key difference for entry %d: got %s but requested %s", i, getProofResponse.StorageProof[i].Key, key)
if key != common.BigToHash(getProofResponse.StorageProof[i].Key.ToInt()) {
return nil, fmt.Errorf("unexpected storage proof key difference for entry %d: got %s but requested %s", i, getProofResponse.StorageProof[i].Key.String(), key)
}
}
return getProofResponse, nil
Expand Down

0 comments on commit 1703d7c

Please sign in to comment.