Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adapters/p2p2core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func AdaptStateDiff(
reader core.StateReader,
reader core.CommonStateReader,
contractDiffs []*state.ContractDiff,
classes []*class.Class,
) (*core.StateDiff, error) {
Expand Down
16 changes: 10 additions & 6 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type Reader interface {
StateUpdateByHash(hash *felt.Felt) (update *core.StateUpdate, err error)
L1HandlerTxnHash(msgHash *common.Hash) (l1HandlerTxnHash felt.Felt, err error)

HeadState() (core.StateReader, StateCloser, error)
StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error)
StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error)
HeadState() (core.CommonStateReader, StateCloser, error)
StateAtBlockHash(blockHash *felt.Felt) (core.CommonStateReader, StateCloser, error)
StateAtBlockNumber(blockNumber uint64) (core.CommonStateReader, StateCloser, error)

BlockCommitmentsByNumber(blockNumber uint64) (*core.BlockCommitments, error)

Expand Down Expand Up @@ -408,7 +408,7 @@ type StateCloser = func() error
var noopStateCloser = func() error { return nil } // TODO: remove this once we refactor the state

// HeadState returns a StateReader that provides a stable view to the latest state
func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) {
func (b *Blockchain) HeadState() (core.CommonStateReader, StateCloser, error) {
b.listener.OnRead("HeadState")
txn := b.database.NewIndexedBatch()

Expand All @@ -422,7 +422,9 @@ func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) {

// 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) {
func (b *Blockchain) StateAtBlockNumber(
blockNumber uint64,
) (core.CommonStateReader, StateCloser, error) {
b.listener.OnRead("StateAtBlockNumber")
txn := b.database.NewIndexedBatch()

Expand All @@ -436,7 +438,9 @@ func (b *Blockchain) StateAtBlockNumber(blockNumber uint64) (core.StateReader, S

// StateAtBlockHash returns a StateReader that provides
// a stable view to the state at the given block hash
func (b *Blockchain) StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error) {
func (b *Blockchain) StateAtBlockHash(
blockHash *felt.Felt,
) (core.CommonStateReader, StateCloser, error) {
b.listener.OnRead("StateAtBlockHash")
if blockHash.IsZero() {
memDB := memory.New()
Expand Down
4 changes: 3 additions & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func (b *Builder) getRevealedBlockHash(blockHeight uint64) (*felt.Felt, error) {
return header.Hash, nil
}

func (b *Builder) PendingState(buildState *BuildState) (core.StateReader, func() error, error) {
func (b *Builder) PendingState(
buildState *BuildState,
) (core.CommonStateReader, func() error, error) {
if buildState.Preconfirmed == nil {
return nil, nil, core.ErrPendingDataNotFound
}
Expand Down
37 changes: 37 additions & 0 deletions core/common_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package core

import (
"github.com/NethermindEth/juno/core/felt"
)

type CommonState interface {
CommonStateReader

ContractStorageAt(addr, key *felt.Felt, blockNumber uint64) (felt.Felt, error)
ContractNonceAt(addr *felt.Felt, blockNumber uint64) (felt.Felt, error)
ContractClassHashAt(addr *felt.Felt, blockNumber uint64) (felt.Felt, error)
ContractDeployedAt(addr *felt.Felt, blockNumber uint64) (bool, error)

Update(
blockNum uint64,
update *StateUpdate,
declaredClasses map[felt.Felt]ClassDefinition,
skipVerifyNewRoot bool,
) error
Revert(blockNum uint64, update *StateUpdate) error
Commitment() (felt.Felt, error)
}

//go:generate mockgen -destination=../mocks/mock_common_state_reader.go -package=mocks github.com/NethermindEth/juno/core CommonStateReader
type CommonStateReader interface {
ContractClassHash(addr *felt.Felt) (felt.Felt, error)
ContractNonce(addr *felt.Felt) (felt.Felt, error)
ContractStorage(addr, key *felt.Felt) (felt.Felt, error)
Class(classHash *felt.Felt) (*DeclaredClassDefinition, error)

ClassTrie() (CommonTrie, error)
ContractTrie() (CommonTrie, error)
ContractStorageTrie(addr *felt.Felt) (CommonTrie, error)
CompiledClassHash(classHash *felt.SierraClassHash) (felt.CasmClassHash, error)
CompiledClassHashV2(classHash *felt.SierraClassHash) (felt.CasmClassHash, error)
}
4 changes: 2 additions & 2 deletions core/state/commontrie/trie.go → core/common_trie.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package commontrie
package core

import (
"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
)

type Trie interface {
type CommonTrie interface {
Get(key *felt.Felt) (felt.Felt, error)
Hash() (felt.Felt, error)
HashFn() crypto.HashFn
Expand Down
7 changes: 3 additions & 4 deletions core/history.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/state/commontrie"
"github.com/NethermindEth/juno/db"
)

Expand Down Expand Up @@ -110,14 +109,14 @@ func (s *deprecatedStateHistory) CompiledClassHashV2(
return s.state.CompiledClassHashV2(classHash)
}

func (s *deprecatedStateHistory) ClassTrie() (commontrie.Trie, error) {
func (s *deprecatedStateHistory) ClassTrie() (CommonTrie, error) {
return nil, ErrHistoricalTrieNotSupported
}

func (s *deprecatedStateHistory) ContractTrie() (commontrie.Trie, error) {
func (s *deprecatedStateHistory) ContractTrie() (CommonTrie, error) {
return nil, ErrHistoricalTrieNotSupported
}

func (s *deprecatedStateHistory) ContractStorageTrie(addr *felt.Felt) (commontrie.Trie, error) {
func (s *deprecatedStateHistory) ContractStorageTrie(addr *felt.Felt) (CommonTrie, error) {
return nil, ErrHistoricalTrieNotSupported
}
2 changes: 1 addition & 1 deletion core/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestStateHistory(t *testing.T) {
require.NoError(t, err)

for desc, test := range map[string]struct {
snapshot core.StateReader
snapshot core.CommonStateReader
checker func(*testing.T, felt.Felt, error)
}{
"contract is not deployed": {
Expand Down
148 changes: 0 additions & 148 deletions core/mocks/mock_commonstate_reader.go

This file was deleted.

17 changes: 10 additions & 7 deletions core/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ type PendingData interface {
ReceiptByHash(hash *felt.Felt) (*TransactionReceipt, *felt.Felt, uint64, error)
// PendingStateBeforeIndex returns the state obtained by applying all transaction state diffs
// up to given index in the pre-confirmed block.
PendingStateBeforeIndex(baseState StateReader, index uint) (StateReader, error)
PendingStateBeforeIndex(baseState CommonStateReader, index uint) (CommonStateReader, error)
// PendingState returns the state resulting from execution of the pending data
PendingState(baseState StateReader) StateReader
PendingState(baseState CommonStateReader) CommonStateReader
}

type Pending struct {
Expand Down Expand Up @@ -136,11 +136,14 @@ func (p *Pending) ReceiptByHash(
return nil, nil, 0, ErrTransactionReceiptNotFound
}

func (p *Pending) PendingStateBeforeIndex(baseState StateReader, index uint) (StateReader, error) {
func (p *Pending) PendingStateBeforeIndex(
baseState CommonStateReader,
index uint,
) (CommonStateReader, error) {
return nil, ErrPendingStateBeforeIndexNotSupported
}

func (p *Pending) PendingState(baseState StateReader) StateReader {
func (p *Pending) PendingState(baseState CommonStateReader) CommonStateReader {
return NewPendingState(
p.StateUpdate.StateDiff,
p.NewClasses,
Expand Down Expand Up @@ -290,9 +293,9 @@ func (p *PreConfirmed) ReceiptByHash(
}

func (p *PreConfirmed) PendingStateBeforeIndex(
baseState StateReader,
baseState CommonStateReader,
index uint,
) (StateReader, error) {
) (CommonStateReader, error) {
if index > uint(len(p.Block.Transactions)) {
return nil, ErrTransactionIndexOutOfBounds
}
Expand All @@ -316,7 +319,7 @@ func (p *PreConfirmed) PendingStateBeforeIndex(
return NewPendingState(&stateDiff, newClasses, baseState), nil
}

func (p *PreConfirmed) PendingState(baseState StateReader) StateReader {
func (p *PreConfirmed) PendingState(baseState CommonStateReader) CommonStateReader {
stateDiff := EmptyStateDiff()
newClasses := make(map[felt.Felt]ClassDefinition)

Expand Down
Loading
Loading