diff --git a/blockchain/pending.go b/blockchain/pending.go index 5638ac9c35..5b92247a16 100644 --- a/blockchain/pending.go +++ b/blockchain/pending.go @@ -2,6 +2,7 @@ package blockchain import ( "errors" + "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/core/trie" @@ -69,20 +70,23 @@ func (p *PendingState) Class(classHash *felt.Felt) (*core.DeclaredClass, error) } // Note[pnowosie]: Maybe extending StateReader with the following methods was not a good idea? -func (s *PendingState) ClassTrie() (*trie.Trie, func() error, error) { - return nil, nopCloser, featureNotImplemented +func (p *PendingState) ClassTrie() (*trie.Trie, func() error, error) { + return nil, nopCloser, errFeatureNotImplemented } -func (s *PendingState) StorageTrie() (*trie.Trie, func() error, error) { - return nil, nopCloser, featureNotImplemented + +func (p *PendingState) StorageTrie() (*trie.Trie, func() error, error) { + return nil, nopCloser, errFeatureNotImplemented } -func (s *PendingState) StorageTrieForAddr(*felt.Felt) (*trie.Trie, error) { - return nil, featureNotImplemented + +func (p *PendingState) StorageTrieForAddr(*felt.Felt) (*trie.Trie, error) { + return nil, errFeatureNotImplemented } -func (s *PendingState) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) { - return nil, nil, featureNotImplemented + +func (p *PendingState) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) { + return nil, nil, errFeatureNotImplemented } var ( - featureNotImplemented = errors.New("feature not implemented for a historical state") - nopCloser = func() error { return nil } + errFeatureNotImplemented = errors.New("feature not implemented for a historical state") + nopCloser = func() error { return nil } ) diff --git a/core/state_snapshot.go b/core/state_snapshot.go index 723aa1e5fd..7ca9990505 100644 --- a/core/state_snapshot.go +++ b/core/state_snapshot.go @@ -2,9 +2,9 @@ package core import ( "errors" - "github.com/NethermindEth/juno/core/trie" "github.com/NethermindEth/juno/core/felt" + "github.com/NethermindEth/juno/core/trie" "github.com/NethermindEth/juno/db" ) @@ -91,19 +91,22 @@ func (s *stateSnapshot) Class(classHash *felt.Felt) (*DeclaredClass, error) { // 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, featureNotImplemented + return nil, nopCloser, errFeatureNotImplemented } + func (s *stateSnapshot) StorageTrie() (*trie.Trie, func() error, error) { - return nil, nopCloser, featureNotImplemented + return nil, nopCloser, errFeatureNotImplemented } + func (s *stateSnapshot) StorageTrieForAddr(*felt.Felt) (*trie.Trie, error) { - return nil, featureNotImplemented + return nil, errFeatureNotImplemented } + func (s *stateSnapshot) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) { - return nil, nil, featureNotImplemented + return nil, nil, errFeatureNotImplemented } var ( - featureNotImplemented = errors.New("feature not implemented for a historical state") - nopCloser = func() error { return nil } + errFeatureNotImplemented = errors.New("feature not implemented for a historical state") + nopCloser = func() error { return nil } ) diff --git a/rpc/handlers.go b/rpc/handlers.go index f684c39789..e9fcb4f935 100644 --- a/rpc/handlers.go +++ b/rpc/handlers.go @@ -60,7 +60,10 @@ 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"} + ErrBlockNotRecentForProof = &jsonrpc.Error{ + Code: 1001, + Message: "Block is not sufficiently recent for storage proofs. Use 'latest' as block id", + } ) const ( diff --git a/rpc/storage.go b/rpc/storage.go index 84b0ad3c10..29e77298ae 100644 --- a/rpc/storage.go +++ b/rpc/storage.go @@ -37,7 +37,11 @@ func (h *Handler) StorageAt(address, key felt.Felt, id BlockID) (*felt.Felt, *js // // It follows the specification defined here: // https://github.com/starkware-libs/starknet-specs/blob/647caa00c0223e1daab1b2f3acc4e613ba2138aa/api/starknet_api_openrpc.json#L910 -func (h *Handler) StorageProof(id BlockID, classes, contracts []felt.Felt, storageKeys []StorageKeys) (*StorageProofResult, *jsonrpc.Error) { +func (h *Handler) StorageProof( + id BlockID, + classes, contracts []felt.Felt, + storageKeys []StorageKeys, +) (*StorageProofResult, *jsonrpc.Error) { stateReader, stateCloser, err := h.bcReader.HeadState() if err != nil { return nil, ErrInternal.CloneWithData(err) diff --git a/rpc/storage_test.go b/rpc/storage_test.go index 03cdecbf84..a3aede1719 100644 --- a/rpc/storage_test.go +++ b/rpc/storage_test.go @@ -123,25 +123,6 @@ func TestStorageProof(t *testing.T) { require.NoError(t, err) require.NoError(t, tempTrie.Commit()) - // TODO[pnowosie]: There is smth wrong with proof verification, see `Trie proofs sanity check` test - //verifyIf := func(proof []*rpc.HashToNode, key *felt.Felt, value *felt.Felt) bool { - // root, err := tempTrie.Root() - // require.NoError(t, err) - // fmt.Println("root", root) - // - // pnodes := []trie.ProofNode{} - // for _, hn := range proof { - // pnodes = append(pnodes, NodeToProofNode(hn)) - // } - // - // kbs := key.Bytes() - // kkey := trie.NewKey(251, kbs[:]) - // result := trie.VerifyProof(root, &kkey, value, pnodes, tempTrie.HashFunc()) - // fmt.Println("result", result) - // - // return result - //} - mockCtrl := gomock.NewController(t) t.Cleanup(mockCtrl.Finish) @@ -270,6 +251,7 @@ func TestStorageProof(t *testing.T) { arityTest(t, proof, 0, 0, 0, 1) require.Len(t, proof.ContractsStorageProofs[0], 0) }) + //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) @@ -283,6 +265,7 @@ func TestStorageProof(t *testing.T) { verifyIf(proof.ContractsStorageProofs[0], noSuchKey, nil) }) + //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) @@ -332,26 +315,3 @@ func emptyTrie(t *testing.T) *trie.Trie { require.NoError(t, err) return tempTrie } - -func NodeToProofNode(hn *rpc.HashToNode) trie.ProofNode { - var proofNode trie.ProofNode - - switch pnode := hn.Node.(type) { - case *rpc.MerkleEdgeNode: - pbs := pnode.Path.Bytes() - path := trie.NewKey(uint8(pnode.Length), pbs[:]) - proofNode = &trie.Edge{ - Path: &path, - Child: pnode.Child, - } - case *rpc.MerkleBinaryNode: - proofNode = &trie.Binary{ - LeftHash: pnode.Left, - RightHash: pnode.Right, - } - default: - panic(fmt.Errorf("unsupported node type %T", pnode)) - } - - return proofNode -}