Skip to content

Commit

Permalink
linter & formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowosie committed Oct 11, 2024
1 parent fcf4395 commit b292454
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 61 deletions.
24 changes: 14 additions & 10 deletions blockchain/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }
)
17 changes: 10 additions & 7 deletions core/state_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 }
)
5 changes: 4 additions & 1 deletion rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
6 changes: 5 additions & 1 deletion rpc/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 2 additions & 42 deletions rpc/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit b292454

Please sign in to comment.