Skip to content

Commit

Permalink
update after the specs PR merged
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowosie committed Oct 16, 2024
1 parent a6d19b2 commit f3ded4a
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 150 deletions.
12 changes: 12 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Reader interface {
StateUpdateByHash(hash *felt.Felt) (update *core.StateUpdate, err error)

HeadState() (core.StateReader, StateCloser, error)
HeadTrie() (core.TrieReader, StateCloser, error)
StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error)
StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error)
PendingState() (core.StateReader, StateCloser, error)
Expand Down Expand Up @@ -769,6 +770,17 @@ func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) {
return core.NewState(txn), txn.Discard, nil
}

func (b *Blockchain) HeadTrie() (core.TrieReader, StateCloser, error) {

Check warning on line 773 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L773

Added line #L773 was not covered by tests
// Note: I'm not sure I should open a new db txn since the TrieReader is a State
// so the same instance of the state we create in HeadState will do job.
txn, err := b.database.NewTransaction(false)
if err != nil {
return nil, nil, err

Check warning on line 778 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L776-L778

Added lines #L776 - L778 were not covered by tests
}

return core.NewState(txn), txn.Discard, nil

Check warning on line 781 in blockchain/blockchain.go

View check run for this annotation

Codecov / codecov/patch

blockchain/blockchain.go#L781

Added line #L781 was not covered by tests
}

// StateAtBlockNumber returns a StateReader that provides a stable view to the state at the given block number
func (b *Blockchain) StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error) {
b.listener.OnRead("StateAtBlockNumber")
Expand Down
25 changes: 0 additions & 25 deletions blockchain/pending.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package blockchain

import (
"errors"

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

type Pending struct {
Expand Down Expand Up @@ -68,25 +65,3 @@ func (p *PendingState) Class(classHash *felt.Felt) (*core.DeclaredClass, error)

return p.head.Class(classHash)
}

// Note[pnowosie]: Maybe extending StateReader with the following methods was not a good idea?
func (p *PendingState) ClassTrie() (*trie.Trie, func() error, error) {
return nil, nopCloser, errFeatureNotImplemented
}

func (p *PendingState) StorageTrie() (*trie.Trie, func() error, error) {
return nil, nopCloser, errFeatureNotImplemented
}

func (p *PendingState) StorageTrieForAddr(*felt.Felt) (*trie.Trie, error) {
return nil, errFeatureNotImplemented
}

func (p *PendingState) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) {
return nil, nil, errFeatureNotImplemented
}

var (
errFeatureNotImplemented = errors.New("feature not implemented for a historical state")
nopCloser = func() error { return nil }
)
7 changes: 6 additions & 1 deletion core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ type StateReader interface {
ContractNonce(addr *felt.Felt) (*felt.Felt, error)
ContractStorage(addr, key *felt.Felt) (*felt.Felt, error)
Class(classHash *felt.Felt) (*DeclaredClass, error)
}

// TrieReader used for storage proofs, can only be supported by current state implementation (for now, we plan to add db snapshots)
var _ TrieReader = (*State)(nil)

// NOTE: Not a best way to add them here - it assumes current state and atm cannot be implemented for hitsrical states
//go:generate mockgen -destination=../mocks/mock_trie.go -package=mocks github.com/NethermindEth/juno/core TrieReader
type TrieReader interface {
ClassTrie() (*trie.Trie, func() error, error)
StorageTrie() (*trie.Trie, func() error, error)
StorageTrieForAddr(addr *felt.Felt) (*trie.Trie, error)
Expand Down
23 changes: 0 additions & 23 deletions core/state_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"

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

Expand Down Expand Up @@ -88,25 +87,3 @@ func (s *stateSnapshot) Class(classHash *felt.Felt) (*DeclaredClass, error) {
}
return declaredClass, nil
}

// 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, errFeatureNotImplemented
}

func (s *stateSnapshot) StorageTrie() (*trie.Trie, func() error, error) {
return nil, nopCloser, errFeatureNotImplemented
}

func (s *stateSnapshot) StorageTrieForAddr(*felt.Felt) (*trie.Trie, error) {
return nil, errFeatureNotImplemented
}

func (s *stateSnapshot) StateAndClassRoot() (*felt.Felt, *felt.Felt, error) {
return nil, nil, errFeatureNotImplemented
}

var (
errFeatureNotImplemented = errors.New("feature not implemented for a historical state")
nopCloser = func() error { return nil }
)
16 changes: 16 additions & 0 deletions mocks/mock_blockchain.go

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

64 changes: 0 additions & 64 deletions mocks/mock_state.go

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

104 changes: 104 additions & 0 deletions mocks/mock_trie.go

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

6 changes: 3 additions & 3 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ 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",
ErrStorageProofNotSupported = &jsonrpc.Error{
Code: 42,
Message: "the node doesn't support storage proofs for blocks that are too far in the past. Use 'latest' as block id",
}
)

Expand Down
Loading

0 comments on commit f3ded4a

Please sign in to comment.