Skip to content

Commit

Permalink
tested - does proof&verify works correctly?
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowosie committed Oct 10, 2024
1 parent 03a531d commit a33464e
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 56 deletions.
2 changes: 2 additions & 0 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type StateReader interface {
ContractNonce(addr *felt.Felt) (*felt.Felt, error)
ContractStorage(addr, key *felt.Felt) (*felt.Felt, error)
Class(classHash *felt.Felt) (*DeclaredClass, error)

// NOTE: Not a best way to add them here - it assumes current state and atm cannot be implemented for hitsrical states
ClassTrie() (*trie.Trie, func() error, error)
StorageTrie() (*trie.Trie, func() error, error)
StorageTrieForAddr(addr *felt.Felt) (*trie.Trie, error)
Expand Down
64 changes: 64 additions & 0 deletions mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ var (

// These errors can be only be returned by Juno-specific methods.
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"}
)

const (
Expand Down
44 changes: 36 additions & 8 deletions rpc/storage.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package rpc

import (
"errors"
"fmt"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/core/trie"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/jsonrpc"
)

Expand Down Expand Up @@ -46,6 +50,10 @@ func (h *Handler) StorageProof(id BlockID, classes, contracts []felt.Felt, stora
return nil, ErrInternal.CloneWithData(err)
}

if !(id.Latest || head.Number == id.Number) {
return nil, ErrBlockNotRecentForProof
}

storageRoot, classRoot, err := stateReader.StateAndClassRoot()
if err != nil {
return nil, ErrInternal.CloneWithData(err)
Expand Down Expand Up @@ -166,12 +174,7 @@ func getContractsProof(reader core.StateReader, contracts []felt.Felt) (*Contrac
}

for _, contract := range contracts {
leafData := &LeafData{}
leafData.Nonce, err = reader.ContractNonce(&contract)
if err != nil {
return nil, err
}
leafData.ClassHash, err = reader.ContractClassHash(&contract)
leafData, err := addLeafDataIfExists(reader, &contract)
if err != nil {
return nil, err
}
Expand All @@ -187,6 +190,25 @@ func getContractsProof(reader core.StateReader, contracts []felt.Felt) (*Contrac
return result, nil
}

func addLeafDataIfExists(reader core.StateReader, contract *felt.Felt) (*LeafData, error) {
nonce, err := reader.ContractNonce(contract)
if errors.Is(err, db.ErrKeyNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
classHash, err := reader.ContractClassHash(contract)
if err != nil {
return nil, err
}

return &LeafData{
Nonce: nonce,
ClassHash: classHash,
}, nil
}

func getContractsStorageProofs(reader core.StateReader, keys []StorageKeys) ([][]*HashToNode, error) {
result := make([][]*HashToNode, 0, len(keys))

Expand All @@ -196,15 +218,16 @@ func getContractsStorageProofs(reader core.StateReader, keys []StorageKeys) ([][
// Note: if contract does not exist, `StorageTrieForAddr()` returns an empty trie, not an error
return nil, err
}

nodes := []*HashToNode{}
result = append(result, nodes)
for _, slot := range key.Keys {
proof, err := getProof(cstrie, &slot)
if err != nil {
return nil, err
}
nodes = append(nodes, proof...)
}
result = append(result, nodes)
}

return result, nil
Expand All @@ -214,6 +237,10 @@ func getProof(t *trie.Trie, elt *felt.Felt) ([]*HashToNode, error) {
feltBytes := elt.Bytes()
key := trie.NewKey(core.ContractStorageTrieHeight, feltBytes[:])
nodes, err := trie.GetProof(&key, t)
for i, n := range nodes {
fmt.Printf("[%d]", i)
n.PrettyPrint()
}
if err != nil {
return nil, err
}
Expand All @@ -230,7 +257,8 @@ func getProof(t *trie.Trie, elt *felt.Felt) ([]*HashToNode, error) {
}
}
if edge, ok := node.(*trie.Edge); ok {
f := edge.Path.Felt()
path := edge.Path
f := path.Felt()
merkle = &MerkleEdgeNode{
Path: &f, // TODO[pnowosie]: specs says its int
Length: int(edge.Len()),
Expand Down
Loading

0 comments on commit a33464e

Please sign in to comment.