From f9089bc243f09aeb2ae7be4367f9e92feceebbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowosielski?= Date: Wed, 16 Oct 2024 11:18:05 +0200 Subject: [PATCH] update after the specs PR merged --- blockchain/blockchain.go | 12 +++++ blockchain/pending.go | 25 ---------- core/state.go | 7 ++- core/state_snapshot.go | 23 --------- mocks/mock_blockchain.go | 16 ++++++ mocks/mock_state.go | 64 ------------------------ mocks/mock_trie.go | 104 +++++++++++++++++++++++++++++++++++++++ rpc/handlers.go | 6 +-- rpc/storage.go | 53 ++++++++++---------- rpc/storage_test.go | 18 ++++--- 10 files changed, 178 insertions(+), 150 deletions(-) create mode 100644 mocks/mock_trie.go diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index ee2411c471..bea2c99b69 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -36,6 +36,7 @@ type Reader interface { StateUpdateByHash(hash *felt.Felt) (update *core.StateUpdate, err error) HeadState() (core.StateReader, StateCloser, error) + HeadTrie() (core.TrieReader, StateCloser, error) StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error) StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error) PendingState() (core.StateReader, StateCloser, error) @@ -769,6 +770,17 @@ func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) { return core.NewState(txn), txn.Discard, nil } +func (b *Blockchain) HeadTrie() (core.TrieReader, StateCloser, error) { + // Note: I'm not sure I should open a new db txn since the TrieReader is a State + // so the same instance of the state we create in HeadState will do job. + txn, err := b.database.NewTransaction(false) + if err != nil { + return nil, nil, err + } + + return core.NewState(txn), txn.Discard, nil +} + // StateAtBlockNumber returns a StateReader that provides a stable view to the state at the given block number func (b *Blockchain) StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error) { b.listener.OnRead("StateAtBlockNumber") diff --git a/blockchain/pending.go b/blockchain/pending.go index 5b92247a16..b69cd0ee06 100644 --- a/blockchain/pending.go +++ b/blockchain/pending.go @@ -1,11 +1,8 @@ package blockchain import ( - "errors" - "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/core/trie" ) type Pending struct { @@ -68,25 +65,3 @@ func (p *PendingState) Class(classHash *felt.Felt) (*core.DeclaredClass, error) return p.head.Class(classHash) } - -// Note[pnowosie]: Maybe extending StateReader with the following methods was not a good idea? -func (p *PendingState) ClassTrie() (*trie.Trie, func() error, error) { - return nil, nopCloser, errFeatureNotImplemented -} - -func (p *PendingState) StorageTrie() (*trie.Trie, func() error, error) { - return nil, nopCloser, errFeatureNotImplemented -} - -func (p *PendingState) StorageTrieForAddr(*felt.Felt) (*trie.Trie, error) { - return nil, errFeatureNotImplemented -} - -func (p *PendingState) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) { - return nil, nil, errFeatureNotImplemented -} - -var ( - errFeatureNotImplemented = errors.New("feature not implemented for a historical state") - nopCloser = func() error { return nil } -) diff --git a/core/state.go b/core/state.go index c11b15bfd5..3e2b55f8dc 100644 --- a/core/state.go +++ b/core/state.go @@ -42,8 +42,13 @@ type StateReader interface { ContractNonce(addr *felt.Felt) (*felt.Felt, error) ContractStorage(addr, key *felt.Felt) (*felt.Felt, error) Class(classHash *felt.Felt) (*DeclaredClass, error) +} + +// TrieReader used for storage proofs, can only be supported by current state implementation (for now, we plan to add db snapshots) +var _ TrieReader = (*State)(nil) - // NOTE: Not a best way to add them here - it assumes current state and atm cannot be implemented for hitsrical states +//go:generate mockgen -destination=../mocks/mock_trie.go -package=mocks github.com/NethermindEth/juno/core TrieReader +type TrieReader interface { ClassTrie() (*trie.Trie, func() error, error) StorageTrie() (*trie.Trie, func() error, error) StorageTrieForAddr(addr *felt.Felt) (*trie.Trie, error) diff --git a/core/state_snapshot.go b/core/state_snapshot.go index 7ca9990505..a7062d4dae 100644 --- a/core/state_snapshot.go +++ b/core/state_snapshot.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/core/trie" "github.com/NethermindEth/juno/db" ) @@ -88,25 +87,3 @@ func (s *stateSnapshot) Class(classHash *felt.Felt) (*DeclaredClass, error) { } return declaredClass, nil } - -// Note[pnowosie]: Maybe extending StateReader with the following methods was not a good idea? -func (s *stateSnapshot) ClassTrie() (*trie.Trie, func() error, error) { - return nil, nopCloser, errFeatureNotImplemented -} - -func (s *stateSnapshot) StorageTrie() (*trie.Trie, func() error, error) { - return nil, nopCloser, errFeatureNotImplemented -} - -func (s *stateSnapshot) StorageTrieForAddr(*felt.Felt) (*trie.Trie, error) { - return nil, errFeatureNotImplemented -} - -func (s *stateSnapshot) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) { - return nil, nil, errFeatureNotImplemented -} - -var ( - errFeatureNotImplemented = errors.New("feature not implemented for a historical state") - nopCloser = func() error { return nil } -) diff --git a/mocks/mock_blockchain.go b/mocks/mock_blockchain.go index 8d6bf6045d..b90e9eff84 100644 --- a/mocks/mock_blockchain.go +++ b/mocks/mock_blockchain.go @@ -163,6 +163,22 @@ func (mr *MockReaderMockRecorder) HeadState() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HeadState", reflect.TypeOf((*MockReader)(nil).HeadState)) } +// HeadTrie mocks base method. +func (m *MockReader) HeadTrie() (core.TrieReader, func() error, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HeadTrie") + ret0, _ := ret[0].(core.TrieReader) + ret1, _ := ret[1].(func() error) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// HeadTrie indicates an expected call of HeadTrie. +func (mr *MockReaderMockRecorder) HeadTrie() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HeadTrie", reflect.TypeOf((*MockReader)(nil).HeadTrie)) +} + // HeadsHeader mocks base method. func (m *MockReader) HeadsHeader() (*core.Header, error) { m.ctrl.T.Helper() diff --git a/mocks/mock_state.go b/mocks/mock_state.go index 4842ee4516..8994085984 100644 --- a/mocks/mock_state.go +++ b/mocks/mock_state.go @@ -14,7 +14,6 @@ import ( core "github.com/NethermindEth/juno/core" felt "github.com/NethermindEth/juno/core/felt" - trie "github.com/NethermindEth/juno/core/trie" gomock "go.uber.org/mock/gomock" ) @@ -56,22 +55,6 @@ func (mr *MockStateHistoryReaderMockRecorder) Class(arg0 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Class", reflect.TypeOf((*MockStateHistoryReader)(nil).Class), arg0) } -// ClassTrie mocks base method. -func (m *MockStateHistoryReader) ClassTrie() (*trie.Trie, func() error, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ClassTrie") - ret0, _ := ret[0].(*trie.Trie) - ret1, _ := ret[1].(func() error) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 -} - -// ClassTrie indicates an expected call of ClassTrie. -func (mr *MockStateHistoryReaderMockRecorder) ClassTrie() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ClassTrie", reflect.TypeOf((*MockStateHistoryReader)(nil).ClassTrie)) -} - // ContractClassHash mocks base method. func (m *MockStateHistoryReader) ContractClassHash(arg0 *felt.Felt) (*felt.Felt, error) { m.ctrl.T.Helper() @@ -176,50 +159,3 @@ func (mr *MockStateHistoryReaderMockRecorder) ContractStorageAt(arg0, arg1, arg2 mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ContractStorageAt", reflect.TypeOf((*MockStateHistoryReader)(nil).ContractStorageAt), arg0, arg1, arg2) } - -// StateAndClassRoot mocks base method. -func (m *MockStateHistoryReader) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "StateAndClassRoot") - ret0, _ := ret[0].(*felt.Felt) - ret1, _ := ret[1].(*felt.Felt) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 -} - -// StateAndClassRoot indicates an expected call of StateAndClassRoot. -func (mr *MockStateHistoryReaderMockRecorder) StateAndClassRoot() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StateAndClassRoot", reflect.TypeOf((*MockStateHistoryReader)(nil).StateAndClassRoot)) -} - -// StorageTrie mocks base method. -func (m *MockStateHistoryReader) StorageTrie() (*trie.Trie, func() error, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "StorageTrie") - ret0, _ := ret[0].(*trie.Trie) - ret1, _ := ret[1].(func() error) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 -} - -// StorageTrie indicates an expected call of StorageTrie. -func (mr *MockStateHistoryReaderMockRecorder) StorageTrie() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StorageTrie", reflect.TypeOf((*MockStateHistoryReader)(nil).StorageTrie)) -} - -// StorageTrieForAddr mocks base method. -func (m *MockStateHistoryReader) StorageTrieForAddr(arg0 *felt.Felt) (*trie.Trie, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "StorageTrieForAddr", arg0) - ret0, _ := ret[0].(*trie.Trie) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// StorageTrieForAddr indicates an expected call of StorageTrieForAddr. -func (mr *MockStateHistoryReaderMockRecorder) StorageTrieForAddr(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StorageTrieForAddr", reflect.TypeOf((*MockStateHistoryReader)(nil).StorageTrieForAddr), arg0) -} diff --git a/mocks/mock_trie.go b/mocks/mock_trie.go new file mode 100644 index 0000000000..570a055c4e --- /dev/null +++ b/mocks/mock_trie.go @@ -0,0 +1,104 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/NethermindEth/juno/core (interfaces: TrieReader) +// +// Generated by this command: +// +// mockgen -destination=../mocks/mock_trie.go -package=mocks github.com/NethermindEth/juno/core TrieReader +// + +// Package mocks is a generated GoMock package. +package mocks + +import ( + reflect "reflect" + + felt "github.com/NethermindEth/juno/core/felt" + trie "github.com/NethermindEth/juno/core/trie" + gomock "go.uber.org/mock/gomock" +) + +// MockTrieReader is a mock of TrieReader interface. +type MockTrieReader struct { + ctrl *gomock.Controller + recorder *MockTrieReaderMockRecorder +} + +// MockTrieReaderMockRecorder is the mock recorder for MockTrieReader. +type MockTrieReaderMockRecorder struct { + mock *MockTrieReader +} + +// NewMockTrieReader creates a new mock instance. +func NewMockTrieReader(ctrl *gomock.Controller) *MockTrieReader { + mock := &MockTrieReader{ctrl: ctrl} + mock.recorder = &MockTrieReaderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockTrieReader) EXPECT() *MockTrieReaderMockRecorder { + return m.recorder +} + +// ClassTrie mocks base method. +func (m *MockTrieReader) ClassTrie() (*trie.Trie, func() error, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ClassTrie") + ret0, _ := ret[0].(*trie.Trie) + ret1, _ := ret[1].(func() error) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ClassTrie indicates an expected call of ClassTrie. +func (mr *MockTrieReaderMockRecorder) ClassTrie() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ClassTrie", reflect.TypeOf((*MockTrieReader)(nil).ClassTrie)) +} + +// StateAndClassRoot mocks base method. +func (m *MockTrieReader) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StateAndClassRoot") + ret0, _ := ret[0].(*felt.Felt) + ret1, _ := ret[1].(*felt.Felt) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// StateAndClassRoot indicates an expected call of StateAndClassRoot. +func (mr *MockTrieReaderMockRecorder) StateAndClassRoot() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StateAndClassRoot", reflect.TypeOf((*MockTrieReader)(nil).StateAndClassRoot)) +} + +// StorageTrie mocks base method. +func (m *MockTrieReader) StorageTrie() (*trie.Trie, func() error, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StorageTrie") + ret0, _ := ret[0].(*trie.Trie) + ret1, _ := ret[1].(func() error) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// StorageTrie indicates an expected call of StorageTrie. +func (mr *MockTrieReaderMockRecorder) StorageTrie() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StorageTrie", reflect.TypeOf((*MockTrieReader)(nil).StorageTrie)) +} + +// StorageTrieForAddr mocks base method. +func (m *MockTrieReader) StorageTrieForAddr(arg0 *felt.Felt) (*trie.Trie, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StorageTrieForAddr", arg0) + ret0, _ := ret[0].(*trie.Trie) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// StorageTrieForAddr indicates an expected call of StorageTrieForAddr. +func (mr *MockTrieReaderMockRecorder) StorageTrieForAddr(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StorageTrieForAddr", reflect.TypeOf((*MockTrieReader)(nil).StorageTrieForAddr), arg0) +} diff --git a/rpc/handlers.go b/rpc/handlers.go index f8002f6236..672fbc3d9b 100644 --- a/rpc/handlers.go +++ b/rpc/handlers.go @@ -60,9 +60,9 @@ var ( ErrSubscriptionNotFound = &jsonrpc.Error{Code: 100, Message: "Subscription not found"} // TODO[pnowosie]: Update the error while specification describe it - ErrBlockNotRecentForProof = &jsonrpc.Error{ - Code: 1001, - Message: "Block is not sufficiently recent for storage proofs. Use 'latest' as block id", + ErrStorageProofNotSupported = &jsonrpc.Error{ + Code: 42, + Message: "the node doesn't support storage proofs for blocks that are too far in the past. Use 'latest' as block id", } ) diff --git a/rpc/storage.go b/rpc/storage.go index 29e77298ae..6512aa9e7a 100644 --- a/rpc/storage.go +++ b/rpc/storage.go @@ -42,6 +42,10 @@ func (h *Handler) StorageProof( classes, contracts []felt.Felt, storageKeys []StorageKeys, ) (*StorageProofResult, *jsonrpc.Error) { + if !id.Latest { + return nil, ErrStorageProofNotSupported + } + stateReader, stateCloser, err := h.bcReader.HeadState() if err != nil { return nil, ErrInternal.CloneWithData(err) @@ -53,11 +57,13 @@ func (h *Handler) StorageProof( return nil, ErrInternal.CloneWithData(err) } - if !id.Latest { - return nil, ErrBlockNotRecentForProof + trieReader, stateCloser2, err := h.bcReader.HeadTrie() + if err != nil { + return nil, ErrInternal.CloneWithData(err) } + defer h.callAndLogErr(stateCloser2, "Error closing trie reader in getStorageProof") - storageRoot, classRoot, err := stateReader.StateAndClassRoot() + storageRoot, classRoot, err := trieReader.StateAndClassRoot() if err != nil { return nil, ErrInternal.CloneWithData(err) } @@ -70,15 +76,15 @@ func (h *Handler) StorageProof( }, } - result.ClassesProof, err = getClassesProof(stateReader, classes) + result.ClassesProof, err = getClassesProof(trieReader, classes) if err != nil { return nil, ErrInternal.CloneWithData(err) } - result.ContractsProof, err = getContractsProof(stateReader, contracts) + result.ContractsProof, err = getContractsProof(stateReader, trieReader, contracts) if err != nil { return nil, ErrInternal.CloneWithData(err) } - result.ContractsStorageProofs, err = getContractsStorageProofs(stateReader, storageKeys) + result.ContractsStorageProofs, err = getContractsStorageProofs(trieReader, storageKeys) if err != nil { return nil, ErrInternal.CloneWithData(err) } @@ -113,27 +119,24 @@ func (mbn *MerkleBinaryNode) AsProofNode() trie.ProofNode { } } -// TODO[pnowosie]: link to specs +// https://github.com/starkware-libs/starknet-specs/blob/8cf463b79ba1dd876f67c7f637e5ea48beb07b5b/api/starknet_api_openrpc.json#L3720 type MerkleEdgeNode struct { - Path *felt.Felt `json:"path"` + Path string `json:"path"` Length int `json:"length"` Child *felt.Felt `json:"child"` } func (men *MerkleEdgeNode) AsProofNode() trie.ProofNode { - pbs := men.Path.Bytes() + f, _ := new(felt.Felt).SetString(men.Path) + pbs := f.Bytes() path := trie.NewKey(uint8(men.Length), pbs[:]) + return &trie.Edge{ Path: &path, Child: men.Child, } } -// TODO[pnowosie]: link to specs, but hoping for removal -type MerkleLeafNode struct { - Value *felt.Felt `json:"value"` -} - // HashToNode represents an item in `NODE_HASH_TO_NODE_MAPPING` specified here // https://github.com/starkware-libs/starknet-specs/blob/647caa00c0223e1daab1b2f3acc4e613ba2138aa/api/starknet_api_openrpc.json#L3667 type HashToNode struct { @@ -141,26 +144,26 @@ type HashToNode struct { Node MerkleNode `json:"node"` } -// TODO[pnowosie]: link to specs +// https://github.com/starkware-libs/starknet-specs/blob/8cf463b79ba1dd876f67c7f637e5ea48beb07b5b/api/starknet_api_openrpc.json#L986 type LeafData struct { Nonce *felt.Felt `json:"nonce"` ClassHash *felt.Felt `json:"class_hash"` } -// TODO[pnowosie]: link to specs +// https://github.com/starkware-libs/starknet-specs/blob/8cf463b79ba1dd876f67c7f637e5ea48beb07b5b/api/starknet_api_openrpc.json#L979 type ContractProof struct { Nodes []*HashToNode `json:"nodes"` LeavesData []*LeafData `json:"contract_leaves_data"` } -// TODO[pnowosie]: link to specs +// https://github.com/starkware-libs/starknet-specs/blob/8cf463b79ba1dd876f67c7f637e5ea48beb07b5b/api/starknet_api_openrpc.json#L1011 type GlobalRoots struct { ContractsTreeRoot *felt.Felt `json:"contracts_tree_root"` ClassesTreeRoot *felt.Felt `json:"classes_tree_root"` BlockHash *felt.Felt `json:"block_hash"` } -// TODO[pnowosie]: link to specs +// https://github.com/starkware-libs/starknet-specs/blob/8cf463b79ba1dd876f67c7f637e5ea48beb07b5b/api/starknet_api_openrpc.json#L970 type StorageProofResult struct { ClassesProof []*HashToNode `json:"classes_proof"` ContractsProof *ContractProof `json:"contracts_proof"` @@ -168,7 +171,7 @@ type StorageProofResult struct { GlobalRoots *GlobalRoots `json:"global_roots"` } -func getClassesProof(reader core.StateReader, classes []felt.Felt) ([]*HashToNode, error) { +func getClassesProof(reader core.TrieReader, classes []felt.Felt) ([]*HashToNode, error) { cTrie, _, err := reader.ClassTrie() if err != nil { return nil, err @@ -184,8 +187,8 @@ func getClassesProof(reader core.StateReader, classes []felt.Felt) ([]*HashToNod return result, nil } -func getContractsProof(reader core.StateReader, contracts []felt.Felt) (*ContractProof, error) { - sTrie, _, err := reader.StorageTrie() +func getContractsProof(stReader core.StateReader, trReader core.TrieReader, contracts []felt.Felt) (*ContractProof, error) { + sTrie, _, err := trReader.StorageTrie() if err != nil { return nil, err } @@ -196,7 +199,7 @@ func getContractsProof(reader core.StateReader, contracts []felt.Felt) (*Contrac } for _, contract := range contracts { - leafData, err := getLeafData(reader, &contract) + leafData, err := getLeafData(stReader, &contract) if err != nil { return nil, err } @@ -231,7 +234,7 @@ func getLeafData(reader core.StateReader, contract *felt.Felt) (*LeafData, error }, nil } -func getContractsStorageProofs(reader core.StateReader, keys []StorageKeys) ([][]*HashToNode, error) { +func getContractsStorageProofs(reader core.TrieReader, keys []StorageKeys) ([][]*HashToNode, error) { result := make([][]*HashToNode, 0, len(keys)) for _, key := range keys { @@ -278,7 +281,7 @@ func getProof(t *trie.Trie, elt *felt.Felt) ([]*HashToNode, error) { path := edge.Path f := path.Felt() merkle = &MerkleEdgeNode{ - Path: &f, // TODO[pnowosie]: specs says its int + Path: f.String(), Length: int(edge.Len()), Child: edge.Child, } @@ -290,7 +293,5 @@ func getProof(t *trie.Trie, elt *felt.Felt) ([]*HashToNode, error) { } } - // TODO[pnowosie]: add a LeafNode here ... are they really needed? - return hashnodes, nil } diff --git a/rpc/storage_test.go b/rpc/storage_test.go index a3aede1719..9f7318c46d 100644 --- a/rpc/storage_test.go +++ b/rpc/storage_test.go @@ -128,14 +128,16 @@ func TestStorageProof(t *testing.T) { mockReader := mocks.NewMockReader(mockCtrl) mockState := mocks.NewMockStateHistoryReader(mockCtrl) + mockTrie := mocks.NewMockTrieReader(mockCtrl) mockReader.EXPECT().HeadState().Return(mockState, func() error { return nil }, nil).AnyTimes() + mockReader.EXPECT().HeadTrie().Return(mockTrie, func() error { return nil }, nil).AnyTimes() mockReader.EXPECT().Head().Return(&core.Block{Header: &core.Header{Hash: blkHash, Number: blockNumber}}, nil).AnyTimes() - mockState.EXPECT().StateAndClassRoot().Return(stgRoor, clsRoot, nil).AnyTimes() - mockState.EXPECT().ClassTrie().Return(tempTrie, nopCloser, nil).AnyTimes() - mockState.EXPECT().StorageTrie().Return(tempTrie, nopCloser, nil).AnyTimes() + mockTrie.EXPECT().StateAndClassRoot().Return(stgRoor, clsRoot, nil).AnyTimes() + mockTrie.EXPECT().ClassTrie().Return(tempTrie, nopCloser, nil).AnyTimes() + mockTrie.EXPECT().StorageTrie().Return(tempTrie, nopCloser, nil).AnyTimes() log := utils.NewNopZapLogger() handler := rpc.New(mockReader, nil, nil, "", log) @@ -182,12 +184,12 @@ func TestStorageProof(t *testing.T) { }) t.Run("error is returned whenever not latest block is requested", func(t *testing.T) { proof, rpcErr := handler.StorageProof(rpc.BlockID{Number: 1}, nil, nil, nil) - assert.Equal(t, rpc.ErrBlockNotRecentForProof, rpcErr) + assert.Equal(t, rpc.ErrStorageProofNotSupported, rpcErr) require.Nil(t, proof) }) t.Run("error is returned even when blknum matches head", func(t *testing.T) { proof, rpcErr := handler.StorageProof(rpc.BlockID{Number: blockNumber}, nil, nil, nil) - assert.Equal(t, rpc.ErrBlockNotRecentForProof, rpcErr) + assert.Equal(t, rpc.ErrStorageProofNotSupported, rpcErr) require.Nil(t, proof) }) t.Run("empty request", func(t *testing.T) { @@ -242,7 +244,7 @@ func TestStorageProof(t *testing.T) { }) t.Run("contract storage trie address does not exist in a trie", func(t *testing.T) { contract := utils.HexToFelt(t, "0xdead") - mockState.EXPECT().StorageTrieForAddr(contract).Return(emptyTrie(t), nil).Times(1) + mockTrie.EXPECT().StorageTrieForAddr(contract).Return(emptyTrie(t), nil).Times(1) storageKeys := []rpc.StorageKeys{{Contract: *contract, Keys: []felt.Felt{*key}}} proof, rpcErr := handler.StorageProof(blockLatest, nil, nil, storageKeys) @@ -254,7 +256,7 @@ func TestStorageProof(t *testing.T) { //nolint:dupl t.Run("contract storage trie key slot does not exist in a trie", func(t *testing.T) { contract := utils.HexToFelt(t, "0xabcd") - mockState.EXPECT().StorageTrieForAddr(gomock.Any()).Return(tempTrie, nil).Times(1) + mockTrie.EXPECT().StorageTrieForAddr(gomock.Any()).Return(tempTrie, nil).Times(1) storageKeys := []rpc.StorageKeys{{Contract: *contract, Keys: []felt.Felt{*noSuchKey}}} proof, rpcErr := handler.StorageProof(blockLatest, nil, nil, storageKeys) @@ -268,7 +270,7 @@ func TestStorageProof(t *testing.T) { //nolint:dupl t.Run("contract storage trie address/key exists in a trie", func(t *testing.T) { contract := utils.HexToFelt(t, "0xabcd") - mockState.EXPECT().StorageTrieForAddr(gomock.Any()).Return(tempTrie, nil).Times(1) + mockTrie.EXPECT().StorageTrieForAddr(gomock.Any()).Return(tempTrie, nil).Times(1) storageKeys := []rpc.StorageKeys{{Contract: *contract, Keys: []felt.Felt{*key}}} proof, rpcErr := handler.StorageProof(blockLatest, nil, nil, storageKeys)