Skip to content

Commit

Permalink
Fixes getStorageAt undeployed contract response
Browse files Browse the repository at this point in the history
fix

Revert "Fixes getStorageAt undeployed contract response"

This reverts commit bd44ef2d79ace6abe9358c053ea3b640dc389bb0.

fix

fix test

fix

fix
  • Loading branch information
weiihann committed Nov 4, 2024
1 parent e4240ac commit 3e3564b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func (h *Handler) StorageAt(address, key felt.Felt, id BlockID) (*felt.Felt, *js
}
defer h.callAndLogErr(stateCloser, "Error closing state reader in getStorageAt")

// This checks if the contract exists because if a key doesn't exist in contract storage,
// the returned value is always zero and error is nil.
_, err := stateReader.ContractNonce(&address)
if err != nil {
return nil, ErrContractNotFound
}

value, err := stateReader.ContractStorage(&address, &key)
if err != nil {
return nil, ErrContractNotFound
Expand Down
9 changes: 7 additions & 2 deletions rpc/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func TestStorageAt(t *testing.T) {

t.Run("non-existent contract", func(t *testing.T) {
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
mockState.EXPECT().ContractStorage(gomock.Any(), gomock.Any()).Return(nil, errors.New("non-existent contract"))
// we check if the contract exists by getting its nonce
mockState.EXPECT().ContractNonce(gomock.Any()).Return(nil, errors.New("non-existent contract"))

storage, rpcErr := handler.StorageAt(felt.Zero, felt.Zero, rpc.BlockID{Latest: true})
require.Nil(t, storage)
Expand All @@ -132,7 +133,8 @@ func TestStorageAt(t *testing.T) {

t.Run("non-existent key", func(t *testing.T) {
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
mockState.EXPECT().ContractStorage(gomock.Any(), gomock.Any()).Return(&felt.Zero, errors.New("non-existent key"))
// we check if the contract exists by getting its nonce
mockState.EXPECT().ContractNonce(gomock.Any()).Return(nil, errors.New("non-existent contract"))

storage, rpcErr := handler.StorageAt(felt.Zero, felt.Zero, rpc.BlockID{Latest: true})
require.Nil(t, storage)
Expand All @@ -143,6 +145,7 @@ func TestStorageAt(t *testing.T) {

t.Run("blockID - latest", func(t *testing.T) {
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
mockState.EXPECT().ContractNonce(&felt.Zero).Return(nil, nil)
mockState.EXPECT().ContractStorage(gomock.Any(), gomock.Any()).Return(expectedStorage, nil)

storage, rpcErr := handler.StorageAt(felt.Zero, felt.Zero, rpc.BlockID{Latest: true})
Expand All @@ -152,6 +155,7 @@ func TestStorageAt(t *testing.T) {

t.Run("blockID - hash", func(t *testing.T) {
mockReader.EXPECT().StateAtBlockHash(&felt.Zero).Return(mockState, nopCloser, nil)
mockState.EXPECT().ContractNonce(&felt.Zero).Return(nil, nil)
mockState.EXPECT().ContractStorage(gomock.Any(), gomock.Any()).Return(expectedStorage, nil)

storage, rpcErr := handler.StorageAt(felt.Zero, felt.Zero, rpc.BlockID{Hash: &felt.Zero})
Expand All @@ -161,6 +165,7 @@ func TestStorageAt(t *testing.T) {

t.Run("blockID - number", func(t *testing.T) {
mockReader.EXPECT().StateAtBlockNumber(uint64(0)).Return(mockState, nopCloser, nil)
mockState.EXPECT().ContractNonce(&felt.Zero).Return(nil, nil)
mockState.EXPECT().ContractStorage(gomock.Any(), gomock.Any()).Return(expectedStorage, nil)

storage, rpcErr := handler.StorageAt(felt.Zero, felt.Zero, rpc.BlockID{Number: 0})
Expand Down

0 comments on commit 3e3564b

Please sign in to comment.