Skip to content

Commit

Permalink
Implement starknet_getStorageProof
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jan 16, 2025
1 parent 0c6508c commit e697545
Show file tree
Hide file tree
Showing 15 changed files with 1,468 additions and 250 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 @@ -718,6 +718,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.

Loading

0 comments on commit e697545

Please sign in to comment.