Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement starknet_getStorageProof RPC #2383

Merged
merged 11 commits into from
Jan 23, 2025
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 @@
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 @@
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)

Check warning on line 143 in core/state.go

View check run for this annotation

Codecov / codecov/patch

core/state.go#L142-L143

Added lines #L142 - L143 were not covered by tests
}

// 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 @@
"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 @@
}
return declaredClass, nil
}

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

Check warning on line 95 in core/state_snapshot.go

View check run for this annotation

Codecov / codecov/patch

core/state_snapshot.go#L94-L95

Added lines #L94 - L95 were not covered by tests
}

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

Check warning on line 99 in core/state_snapshot.go

View check run for this annotation

Codecov / codecov/patch

core/state_snapshot.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}

func (s *stateSnapshot) ContractStorageTrie(addr *felt.Felt) (*trie.Trie, error) {
return nil, ErrHistoricalTrieNotSupported

Check warning on line 103 in core/state_snapshot.go

View check run for this annotation

Codecov / codecov/patch

core/state_snapshot.go#L102-L103

Added lines #L102 - L103 were not covered by tests
}
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
Loading