Skip to content

Commit

Permalink
Implement starknet_getStorageProof RPC (NethermindEth#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann authored Jan 23, 2025
1 parent 21f442f commit 6bf445a
Show file tree
Hide file tree
Showing 15 changed files with 1,409 additions and 237 deletions.
19 changes: 19 additions & 0 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type StateReader interface {
ContractNonce(addr *felt.Felt) (*felt.Felt, error)
ContractStorage(addr, key *felt.Felt) (*felt.Felt, error)
Class(classHash *felt.Felt) (*DeclaredClass, error)

ClassTrie() (*trie.Trie, error)
ContractTrie() (*trie.Trie, error)
ContractStorageTrie(addr *felt.Felt) (*trie.Trie, error)
}

type State struct {
Expand Down Expand Up @@ -124,6 +128,21 @@ func (s *State) Root() (*felt.Felt, error) {
return crypto.PoseidonArray(stateVersion, storageRoot, classesRoot), nil
}

func (s *State) ClassTrie() (*trie.Trie, error) {
// We don't need to call the closer function here because we are only reading the trie
tr, _, err := s.classesTrie()
return tr, err
}

func (s *State) ContractTrie() (*trie.Trie, error) {
tr, _, err := s.storage()
return tr, err
}

func (s *State) ContractStorageTrie(addr *felt.Felt) (*trie.Trie, error) {
return storage(addr, s.txn)
}

// storage returns a [core.Trie] that represents the Starknet global state in the given Txn context.
func (s *State) storage() (*trie.Trie, func() error, error) {
return s.globalTrie(db.StateTrie, trie.NewTriePedersen)
Expand Down
15 changes: 15 additions & 0 deletions core/state_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"errors"

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

var ErrHistoricalTrieNotSupported = errors.New("cannot support historical trie")

type stateSnapshot struct {
blockNumber uint64
state StateHistoryReader
Expand Down Expand Up @@ -87,3 +90,15 @@ func (s *stateSnapshot) Class(classHash *felt.Felt) (*DeclaredClass, error) {
}
return declaredClass, nil
}

func (s *stateSnapshot) ClassTrie() (*trie.Trie, error) {
return nil, ErrHistoricalTrieNotSupported
}

func (s *stateSnapshot) ContractTrie() (*trie.Trie, error) {
return nil, ErrHistoricalTrieNotSupported
}

func (s *stateSnapshot) ContractStorageTrie(addr *felt.Felt) (*trie.Trie, error) {
return nil, ErrHistoricalTrieNotSupported
}
4 changes: 4 additions & 0 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@ func (t *Trie) Dump() {
t.dump(0, nil)
}

func (t *Trie) HashFn() crypto.HashFn {
return t.hash
}

// Try to print a [Trie] in a somewhat human-readable form
/*
Todo: create more meaningful representation of trie. In the current format string, storage is being
Expand Down
46 changes: 46 additions & 0 deletions mocks/mock_state.go

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

62 changes: 0 additions & 62 deletions rpc/contract.go

This file was deleted.

174 changes: 0 additions & 174 deletions rpc/contract_test.go

This file was deleted.

11 changes: 11 additions & 0 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
ErrTooManyKeysInFilter = &jsonrpc.Error{Code: 34, Message: "Too many keys provided in a filter"}
ErrContractError = &jsonrpc.Error{Code: 40, Message: "Contract error"}
ErrTransactionExecutionError = &jsonrpc.Error{Code: 41, Message: "Transaction execution error"}
ErrStorageProofNotSupported = &jsonrpc.Error{Code: 42, Message: "The node doesn't support storage proofs for blocks that are too far in the past"} //nolint:lll
ErrInvalidContractClass = &jsonrpc.Error{Code: 50, Message: "Invalid contract class"}
ErrClassAlreadyDeclared = &jsonrpc.Error{Code: 51, Message: "Class already declared"}
ErrInternal = &jsonrpc.Error{Code: jsonrpc.InternalError, Message: "Internal error"}
Expand Down Expand Up @@ -459,6 +460,16 @@ func (h *Handler) MethodsV0_7() ([]jsonrpc.Method, string) { //nolint: funlen
Params: []jsonrpc.Parameter{{Name: "contract_address"}, {Name: "key"}, {Name: "block_id"}},
Handler: h.StorageAt,
},
{
Name: "starknet_getStorageProof",
Params: []jsonrpc.Parameter{
{Name: "block_id"},
{Name: "class_hashes", Optional: true},
{Name: "contract_addresses", Optional: true},
{Name: "contracts_storage_keys", Optional: true},
},
Handler: h.StorageProof,
},
{
Name: "starknet_getClassHashAt",
Params: []jsonrpc.Parameter{{Name: "block_id"}, {Name: "contract_address"}},
Expand Down
Loading

0 comments on commit 6bf445a

Please sign in to comment.