Skip to content

Commit 3fb0091

Browse files
committed
Squashed commits from maksym/integrate-common-interfaces-rawdb
1 parent fe0b497 commit 3fb0091

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+657
-421
lines changed

adapters/p2p2core/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func AdaptStateDiff(
17-
reader core.StateReader,
17+
reader core.CommonStateReader,
1818
contractDiffs []*state.ContractDiff,
1919
classes []*class.Class,
2020
) (*core.StateDiff, error) {

blockchain/blockchain.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ type Reader interface {
3838
StateUpdateByHash(hash *felt.Felt) (update *core.StateUpdate, err error)
3939
L1HandlerTxnHash(msgHash *common.Hash) (l1HandlerTxnHash felt.Felt, err error)
4040

41-
HeadState() (core.StateReader, StateCloser, error)
42-
StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error)
43-
StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error)
41+
HeadState() (core.CommonStateReader, StateCloser, error)
42+
StateAtBlockHash(blockHash *felt.Felt) (core.CommonStateReader, StateCloser, error)
43+
StateAtBlockNumber(blockNumber uint64) (core.CommonStateReader, StateCloser, error)
4444

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

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

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

@@ -414,7 +414,9 @@ func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) {
414414

415415
// StateAtBlockNumber returns a StateReader that provides
416416
// a stable view to the state at the given block number
417-
func (b *Blockchain) StateAtBlockNumber(blockNumber uint64) (core.StateReader, StateCloser, error) {
417+
func (b *Blockchain) StateAtBlockNumber(
418+
blockNumber uint64,
419+
) (core.CommonStateReader, StateCloser, error) {
418420
b.listener.OnRead("StateAtBlockNumber")
419421
txn := b.database.NewIndexedBatch()
420422

@@ -428,7 +430,9 @@ func (b *Blockchain) StateAtBlockNumber(blockNumber uint64) (core.StateReader, S
428430

429431
// StateAtBlockHash returns a StateReader that provides
430432
// a stable view to the state at the given block hash
431-
func (b *Blockchain) StateAtBlockHash(blockHash *felt.Felt) (core.StateReader, StateCloser, error) {
433+
func (b *Blockchain) StateAtBlockHash(
434+
blockHash *felt.Felt,
435+
) (core.CommonStateReader, StateCloser, error) {
432436
b.listener.OnRead("StateAtBlockHash")
433437
if blockHash.IsZero() {
434438
memDB := memory.New()

builder/builder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ func (b *Builder) getRevealedBlockHash(blockHeight uint64) (*felt.Felt, error) {
128128
return header.Hash, nil
129129
}
130130

131-
func (b *Builder) PendingState(buildState *BuildState) (core.StateReader, func() error, error) {
131+
func (b *Builder) PendingState(
132+
buildState *BuildState,
133+
) (core.CommonStateReader, func() error, error) {
132134
if buildState.Preconfirmed == nil {
133135
return nil, nil, core.ErrPendingDataNotFound
134136
}

core/common_state.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package core
2+
3+
import (
4+
"github.com/NethermindEth/juno/core/felt"
5+
)
6+
7+
//go:generate mockgen -destination=../mocks/mock_common_state.go -package=mocks github.com/NethermindEth/juno/core CommonState
8+
type CommonState interface {
9+
CommonStateReader
10+
11+
ContractStorageAt(addr, key *felt.Felt, blockNumber uint64) (felt.Felt, error)
12+
ContractNonceAt(addr *felt.Felt, blockNumber uint64) (felt.Felt, error)
13+
ContractClassHashAt(addr *felt.Felt, blockNumber uint64) (felt.Felt, error)
14+
ContractDeployedAt(addr *felt.Felt, blockNumber uint64) (bool, error)
15+
16+
Update(
17+
blockNum uint64,
18+
update *StateUpdate,
19+
declaredClasses map[felt.Felt]ClassDefinition,
20+
skipVerifyNewRoot bool,
21+
) error
22+
Revert(blockNum uint64, update *StateUpdate) error
23+
Commitment() (felt.Felt, error)
24+
}
25+
26+
type CommonStateReader interface {
27+
ContractClassHash(addr *felt.Felt) (felt.Felt, error)
28+
ContractNonce(addr *felt.Felt) (felt.Felt, error)
29+
ContractStorage(addr, key *felt.Felt) (felt.Felt, error)
30+
Class(classHash *felt.Felt) (*DeclaredClassDefinition, error)
31+
32+
ClassTrie() (CommonTrie, error)
33+
ContractTrie() (CommonTrie, error)
34+
ContractStorageTrie(addr *felt.Felt) (CommonTrie, error)
35+
CompiledClassHash(classHash *felt.SierraClassHash) (felt.CasmClassHash, error)
36+
CompiledClassHashV2(classHash *felt.SierraClassHash) (felt.CasmClassHash, error)
37+
}

core/state/commontrie/trie.go renamed to core/common_trie.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package commontrie
1+
package core
22

33
import (
44
"github.com/NethermindEth/juno/core/crypto"
55
"github.com/NethermindEth/juno/core/felt"
66
)
77

8-
type Trie interface {
8+
type CommonTrie interface {
9+
Update(key, value *felt.Felt) error
910
Get(key *felt.Felt) (felt.Felt, error)
1011
Hash() (felt.Felt, error)
1112
HashFn() crypto.HashFn

core/history.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55

66
"github.com/NethermindEth/juno/core/felt"
7-
"github.com/NethermindEth/juno/core/state/commontrie"
87
"github.com/NethermindEth/juno/db"
98
)
109

@@ -110,14 +109,14 @@ func (s *deprecatedStateHistory) CompiledClassHashV2(
110109
return s.state.CompiledClassHashV2(classHash)
111110
}
112111

113-
func (s *deprecatedStateHistory) ClassTrie() (commontrie.Trie, error) {
112+
func (s *stateSnapshot) ClassTrie() (CommonTrie, error) {
114113
return nil, ErrHistoricalTrieNotSupported
115114
}
116115

117-
func (s *deprecatedStateHistory) ContractTrie() (commontrie.Trie, error) {
116+
func (s *stateSnapshot) ContractTrie() (CommonTrie, error) {
118117
return nil, ErrHistoricalTrieNotSupported
119118
}
120119

121-
func (s *deprecatedStateHistory) ContractStorageTrie(addr *felt.Felt) (commontrie.Trie, error) {
120+
func (s *stateSnapshot) ContractStorageTrie(addr *felt.Felt) (CommonTrie, error) {
122121
return nil, ErrHistoricalTrieNotSupported
123122
}

core/history_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestStateHistory(t *testing.T) {
6868
require.NoError(t, err)
6969

7070
for desc, test := range map[string]struct {
71-
snapshot core.StateReader
71+
snapshot core.CommonStateReader
7272
checker func(*testing.T, felt.Felt, error)
7373
}{
7474
"contract is not deployed": {

core/mocks/mock_commonstate_reader.go

Lines changed: 0 additions & 148 deletions
This file was deleted.

core/pending.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ type PendingData interface {
4747
ReceiptByHash(hash *felt.Felt) (*TransactionReceipt, *felt.Felt, uint64, error)
4848
// PendingStateBeforeIndex returns the state obtained by applying all transaction state diffs
4949
// up to given index in the pre-confirmed block.
50-
PendingStateBeforeIndex(baseState StateReader, index uint) (StateReader, error)
50+
PendingStateBeforeIndex(baseState CommonStateReader, index uint) (CommonStateReader, error)
5151
// PendingState returns the state resulting from execution of the pending data
52-
PendingState(baseState StateReader) StateReader
52+
PendingState(baseState CommonStateReader) CommonStateReader
5353
}
5454

5555
type Pending struct {
@@ -136,11 +136,14 @@ func (p *Pending) ReceiptByHash(
136136
return nil, nil, 0, ErrTransactionReceiptNotFound
137137
}
138138

139-
func (p *Pending) PendingStateBeforeIndex(baseState StateReader, index uint) (StateReader, error) {
139+
func (p *Pending) PendingStateBeforeIndex(
140+
baseState CommonStateReader,
141+
index uint,
142+
) (CommonStateReader, error) {
140143
return nil, ErrPendingStateBeforeIndexNotSupported
141144
}
142145

143-
func (p *Pending) PendingState(baseState StateReader) StateReader {
146+
func (p *Pending) PendingState(baseState CommonStateReader) CommonStateReader {
144147
return NewPendingState(
145148
p.StateUpdate.StateDiff,
146149
p.NewClasses,
@@ -290,9 +293,9 @@ func (p *PreConfirmed) ReceiptByHash(
290293
}
291294

292295
func (p *PreConfirmed) PendingStateBeforeIndex(
293-
baseState StateReader,
296+
baseState CommonStateReader,
294297
index uint,
295-
) (StateReader, error) {
298+
) (CommonStateReader, error) {
296299
if index > uint(len(p.Block.Transactions)) {
297300
return nil, ErrTransactionIndexOutOfBounds
298301
}
@@ -316,7 +319,7 @@ func (p *PreConfirmed) PendingStateBeforeIndex(
316319
return NewPendingState(&stateDiff, newClasses, baseState), nil
317320
}
318321

319-
func (p *PreConfirmed) PendingState(baseState StateReader) StateReader {
322+
func (p *PreConfirmed) PendingState(baseState CommonStateReader) CommonStateReader {
320323
stateDiff := EmptyStateDiff()
321324
newClasses := make(map[felt.Felt]ClassDefinition)
322325

0 commit comments

Comments
 (0)