Skip to content

Commit 1364259

Browse files
authored
refactor(core): new pending pkg (#3564)
* refactor(core): create pending pkg * refactor(pending): rename PendingState and PendingStateWriter to State and StateWriter * ci: fix linter
1 parent 925371e commit 1364259

77 files changed

Lines changed: 559 additions & 465 deletions

Some content is hidden

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

adapters/sn2core/sn2core.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/NethermindEth/juno/core"
99
"github.com/NethermindEth/juno/core/crypto"
1010
"github.com/NethermindEth/juno/core/felt"
11+
"github.com/NethermindEth/juno/core/pending"
1112
"github.com/NethermindEth/juno/starknet"
1213
"github.com/NethermindEth/juno/utils"
1314
"github.com/ethereum/go-ethereum/common"
@@ -509,19 +510,21 @@ func IsCandidateTx(response *starknet.PreConfirmedBlock, id int) bool {
509510
func AdaptPreConfirmedBlock(
510511
response *starknet.PreConfirmedBlock,
511512
number uint64,
512-
) (core.PreConfirmed, error) {
513+
) (pending.PreConfirmed, error) {
513514
if response == nil {
514-
return core.PreConfirmed{}, errors.New("nil preconfirmed block")
515+
return pending.PreConfirmed{}, errors.New("nil preconfirmed block")
515516
}
516517

517518
if response.Status != "PRE_CONFIRMED" {
518-
return core.PreConfirmed{}, errors.New("invalid status for pre_confirmed block")
519+
return pending.PreConfirmed{}, errors.New("invalid status for pre_confirmed block")
519520
}
520521

521522
isInvalidPayloadSizes := len(response.Transactions) != len(response.TransactionStateDiffs) ||
522523
len(response.Transactions) != len(response.Receipts)
523524
if isInvalidPayloadSizes {
524-
return core.PreConfirmed{}, errors.New("invalid sizes of transactions, state diffs and receipts")
525+
return pending.PreConfirmed{}, errors.New(
526+
"invalid sizes of transactions, state diffs and receipts",
527+
)
525528
}
526529

527530
preConfirmedTxCount := 0
@@ -545,12 +548,12 @@ func AdaptPreConfirmedBlock(
545548
if !IsCandidateTx(response, i) {
546549
txns[preIdx], err = AdaptTransaction(&response.Transactions[i])
547550
if err != nil {
548-
return core.PreConfirmed{}, err
551+
return pending.PreConfirmed{}, err
549552
}
550553
var stateDiff core.StateDiff
551554
stateDiff, err = AdaptStateDiff(response.TransactionStateDiffs[i])
552555
if err != nil {
553-
return core.PreConfirmed{}, err
556+
return pending.PreConfirmed{}, err
554557
}
555558
txStateDiffs[preIdx] = &stateDiff
556559
receipts[preIdx] = AdaptTransactionReceipt(response.Receipts[i])
@@ -559,7 +562,7 @@ func AdaptPreConfirmedBlock(
559562
} else {
560563
candidateTxs[candIdx], err = AdaptTransaction(&response.Transactions[i])
561564
if err != nil {
562-
return core.PreConfirmed{}, err
565+
return pending.PreConfirmed{}, err
563566
}
564567
candIdx++
565568
}
@@ -606,7 +609,7 @@ func AdaptPreConfirmedBlock(
606609
Transactions: txns,
607610
Receipts: receipts,
608611
}
609-
return core.NewPreConfirmed(adaptedBlock, &stateUpdate, txStateDiffs, candidateTxs), nil
612+
return pending.NewPreConfirmed(adaptedBlock, &stateUpdate, txStateDiffs, candidateTxs), nil
610613
}
611614

612615
func safeFeltToUint64(f *felt.Felt) uint64 {

adapters/sn2core/sn2core_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/NethermindEth/juno/clients/feeder"
99
"github.com/NethermindEth/juno/core"
1010
"github.com/NethermindEth/juno/core/felt"
11+
"github.com/NethermindEth/juno/core/pending"
1112
"github.com/NethermindEth/juno/starknet"
1213
"github.com/NethermindEth/juno/utils"
1314
"github.com/stretchr/testify/assert"
@@ -717,7 +718,7 @@ func getPreconfirmedReceipts(
717718

718719
func assertPreConfirmedBlockBasics(
719720
t *testing.T,
720-
preConfirmed *core.PreConfirmed,
721+
preConfirmed *pending.PreConfirmed,
721722
blockNum uint64,
722723
response *starknet.PreConfirmedBlock,
723724
expectedTxCount int,

blockchain/blockchain.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/NethermindEth/juno/blockchain/statebackend"
77
"github.com/NethermindEth/juno/core"
88
"github.com/NethermindEth/juno/core/felt"
9+
"github.com/NethermindEth/juno/core/pending"
910
"github.com/NethermindEth/juno/db"
1011
"github.com/NethermindEth/juno/feed"
1112
"github.com/NethermindEth/juno/utils"
@@ -59,7 +60,7 @@ type Reader interface {
5960
EventFilter(
6061
addresses []felt.Address,
6162
keys [][]felt.Felt,
62-
preConfirmedFn func() (*core.PreConfirmed, error),
63+
preConfirmedFn func() (*pending.PreConfirmed, error),
6364
) (EventFilterer, error)
6465

6566
Network() *utils.Network
@@ -370,7 +371,7 @@ func (b *Blockchain) StateAtBlockHash(
370371
func (b *Blockchain) EventFilter(
371372
addresses []felt.Address,
372373
keys [][]felt.Felt,
373-
preConfirmedFn func() (*core.PreConfirmed, error),
374+
preConfirmedFn func() (*pending.PreConfirmed, error),
374375
) (EventFilterer, error) {
375376
b.listener.OnRead("EventFilter")
376377
latest, err := core.GetChainHeight(b.database)

blockchain/blockchain_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/NethermindEth/juno/clients/feeder"
99
"github.com/NethermindEth/juno/core"
1010
"github.com/NethermindEth/juno/core/felt"
11+
"github.com/NethermindEth/juno/core/pending"
1112
"github.com/NethermindEth/juno/db"
1213
"github.com/NethermindEth/juno/db/memory"
1314
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
@@ -450,8 +451,8 @@ func TestState(t *testing.T) {
450451

451452
func TestEvents(t *testing.T) {
452453
var pendingB *core.Block
453-
preConfirmedFunc := func() (*core.PreConfirmed, error) { //nolint:unparam // used in tests
454-
preConfirmed := core.NewPreConfirmed(pendingB, nil, nil, nil)
454+
preConfirmedFunc := func() (*pending.PreConfirmed, error) { //nolint:unparam // used in tests
455+
preConfirmed := pending.NewPreConfirmed(pendingB, nil, nil, nil)
455456
return &preConfirmed, nil
456457
}
457458

blockchain/event_filter.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/NethermindEth/juno/core"
1010
"github.com/NethermindEth/juno/core/felt"
11+
"github.com/NethermindEth/juno/core/pending"
1112
"github.com/NethermindEth/juno/db"
1213
)
1314

@@ -30,7 +31,7 @@ type EventFilter struct {
3031
toBlock uint64
3132
matcher EventMatcher
3233
maxScanned uint // maximum number of scanned blocks in single call.
33-
preConfirmedFn func() (*core.PreConfirmed, error)
34+
preConfirmedFn func() (*pending.PreConfirmed, error)
3435
cachedFilters *AggregatedBloomFilterCache
3536
runningFilter *core.RunningEventFilter
3637
}
@@ -47,7 +48,7 @@ func newEventFilter(
4748
contractAddresses []felt.Address,
4849
keys [][]felt.Felt,
4950
fromBlock, toBlock uint64,
50-
preConfirmedFn func() (*core.PreConfirmed, error),
51+
preConfirmedFn func() (*pending.PreConfirmed, error),
5152
cachedFilters *AggregatedBloomFilterCache,
5253
runningFilter *core.RunningEventFilter,
5354
) *EventFilter {
@@ -289,7 +290,7 @@ func (e *EventFilter) pendingEvents(
289290
) ([]FilteredEvent, ContinuationToken, error) {
290291
preConfirmed, err := e.preConfirmedFn()
291292
if err != nil {
292-
if errors.Is(err, core.ErrPreConfirmedNotFound) {
293+
if errors.Is(err, pending.ErrPreConfirmedNotFound) {
293294
return matchedEvents, ContinuationToken{}, nil
294295
}
295296
return nil, ContinuationToken{}, err
@@ -356,7 +357,7 @@ func (e *EventFilter) processPreLatestBlock(
356357
// processPreConfirmedBlock processes pre-confirmed block events
357358
func (e *EventFilter) processPreConfirmedBlock(
358359
matchedEvents []FilteredEvent,
359-
preConfirmed *core.PreConfirmed,
360+
preConfirmed *pending.PreConfirmed,
360361
skippedEvents,
361362
chunkSize uint64,
362363
) ([]FilteredEvent, ContinuationToken, error) {

builder/builder.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/NethermindEth/juno/blockchain"
88
"github.com/NethermindEth/juno/core"
99
"github.com/NethermindEth/juno/core/felt"
10+
"github.com/NethermindEth/juno/core/pending"
1011
"github.com/NethermindEth/juno/mempool"
1112
"github.com/NethermindEth/juno/utils"
1213
"github.com/consensys/gnark-crypto/ecc/stark-curve/ecdsa"
@@ -52,8 +53,17 @@ func (b *Builder) Network() *utils.Network {
5253
return b.blockchain.Network()
5354
}
5455

55-
func (b *Builder) Finalise(preconfirmed *core.PreConfirmed, signer utils.BlockSignFunc, privateKey *ecdsa.PrivateKey) error {
56-
return b.blockchain.Finalise(preconfirmed.Block, preconfirmed.StateUpdate, preconfirmed.NewClasses, signer)
56+
func (b *Builder) Finalise(
57+
preconfirmed *pending.PreConfirmed,
58+
signer utils.BlockSignFunc,
59+
privateKey *ecdsa.PrivateKey,
60+
) error {
61+
return b.blockchain.Finalise(
62+
preconfirmed.Block,
63+
preconfirmed.StateUpdate,
64+
preconfirmed.NewClasses,
65+
signer,
66+
)
5767
}
5868

5969
func (b *Builder) InitPreconfirmedBlock(params *BuildParams) (*BuildState, error) {
@@ -101,7 +111,7 @@ func (b *Builder) InitPreconfirmedBlock(params *BuildParams) (*BuildState, error
101111
OldRoot: header.GlobalStateRoot,
102112
StateDiff: &emptyStateDiff,
103113
}
104-
preconfirmed := core.PreConfirmed{
114+
preconfirmed := pending.PreConfirmed{
105115
Block: &preconfirmedBlock,
106116
StateUpdate: &su,
107117
NewClasses: newClasses,
@@ -132,7 +142,7 @@ func (b *Builder) PendingState(
132142
buildState *BuildState,
133143
) (core.StateReader, func() error, error) {
134144
if buildState.PreConfirmed == nil {
135-
return nil, nil, core.ErrPreConfirmedNotFound
145+
return nil, nil, pending.ErrPreConfirmedNotFound
136146
}
137147

138148
headState, headCloser, err := b.blockchain.HeadState()
@@ -141,7 +151,7 @@ func (b *Builder) PendingState(
141151
}
142152

143153
// TODO: remove the state closer once we refactor the state
144-
return core.NewPendingState(
154+
return pending.NewState(
145155
buildState.PreConfirmed.StateUpdate.StateDiff,
146156
buildState.PreConfirmed.NewClasses,
147157
headState,

builder/executor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/NethermindEth/juno/blockchain"
66
"github.com/NethermindEth/juno/core"
77
"github.com/NethermindEth/juno/core/felt"
8+
"github.com/NethermindEth/juno/core/pending"
89
"github.com/NethermindEth/juno/mempool"
910
"github.com/NethermindEth/juno/utils"
1011
"github.com/NethermindEth/juno/vm"
@@ -58,7 +59,7 @@ func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransact
5859
}()
5960

6061
// Create a state writer for the transaction execution
61-
stateWriter := core.NewPendingStateWriter(
62+
stateWriter := pending.NewStateWriter(
6263
state.PreConfirmed.StateUpdate.StateDiff,
6364
state.PreConfirmed.NewClasses,
6465
headState,
@@ -133,7 +134,7 @@ func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransact
133134
// processClassDeclaration handles class declaration storage for declare transactions
134135
func (e *executor) processClassDeclaration(
135136
txn *mempool.BroadcastedTransaction,
136-
state *core.PendingStateWriter,
137+
state *pending.StateWriter,
137138
) error {
138139
if t, ok := (txn.Transaction).(*core.DeclareTransaction); ok {
139140
if err := state.SetContractClass(t.ClassHash, txn.DeclaredClass); err != nil {
@@ -153,7 +154,7 @@ func (e *executor) processClassDeclaration(
153154

154155
// updatePreconfirmedBlock updates the preconfirmed block with transaction results
155156
func updatePreconfirmedBlock(
156-
preconfirmed *core.PreConfirmed,
157+
preconfirmed *pending.PreConfirmed,
157158
receipts []*core.TransactionReceipt,
158159
transactions []core.Transaction,
159160
stateDiffs []*core.StateDiff,

builder/result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"github.com/Masterminds/semver/v3"
55
"github.com/NethermindEth/juno/blockchain"
66
"github.com/NethermindEth/juno/consensus/types"
7-
"github.com/NethermindEth/juno/core"
87
"github.com/NethermindEth/juno/core/felt"
8+
"github.com/NethermindEth/juno/core/pending"
99
)
1010

1111
type BuildResult struct {
12-
PreConfirmed *core.PreConfirmed
12+
PreConfirmed *pending.PreConfirmed
1313
SimulateResult *blockchain.SimulateResult
1414
L2GasConsumed uint64
1515
}

builder/state.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66

77
"github.com/NethermindEth/juno/core"
88
"github.com/NethermindEth/juno/core/felt"
9+
"github.com/NethermindEth/juno/core/pending"
910
"github.com/NethermindEth/juno/utils"
1011
)
1112

1213
type BuildState struct {
13-
PreConfirmed *core.PreConfirmed
14+
PreConfirmed *pending.PreConfirmed
1415
L2GasConsumed uint64
1516
RevealedBlockHash *felt.Felt
1617
}
@@ -24,7 +25,7 @@ func (b *BuildState) PreConfirmedBlock() *core.Block {
2425

2526
func (b *BuildState) ClearPending() error {
2627
b.L2GasConsumed = 0
27-
b.PreConfirmed = &core.PreConfirmed{}
28+
b.PreConfirmed = &pending.PreConfirmed{}
2829
b.RevealedBlockHash = nil
2930

3031
return nil
@@ -43,8 +44,8 @@ func (b *BuildState) Clone() BuildState {
4344
}
4445
}
4546

46-
func clonePreconfirmed(preconfirmed *core.PreConfirmed) *core.PreConfirmed {
47-
return &core.PreConfirmed{
47+
func clonePreconfirmed(preconfirmed *pending.PreConfirmed) *pending.PreConfirmed {
48+
return &pending.PreConfirmed{
4849
Block: cloneBlock(preconfirmed.Block),
4950
StateUpdate: cloneStateUpdate(preconfirmed.StateUpdate),
5051
NewClasses: maps.Clone(preconfirmed.NewClasses),

consensus/datasource/consensus_data_source.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/NethermindEth/juno/consensus/proposer"
1212
"github.com/NethermindEth/juno/consensus/types"
1313
"github.com/NethermindEth/juno/core"
14+
"github.com/NethermindEth/juno/core/pending"
1415
"github.com/NethermindEth/juno/sync"
1516
)
1617

@@ -71,14 +72,17 @@ func (c *consensusDataSource[V, H]) BlockHeaderLatest(ctx context.Context) (*cor
7172
return committedBlock.Block.Header, nil
7273
}
7374

74-
func (c *consensusDataSource[V, H]) BlockPreLatest(ctx context.Context) (core.PreLatest, error) {
75-
return core.PreLatest{}, errors.New("not implemented") // TODO: Revise this
75+
func (c *consensusDataSource[V, H]) BlockPreLatest(ctx context.Context) (pending.PreLatest, error) {
76+
return pending.PreLatest{}, errors.New("not implemented") // TODO: Revise this
7677
}
7778

78-
func (c *consensusDataSource[V, H]) PreConfirmedBlockByNumber(ctx context.Context, blockNumber uint64) (core.PreConfirmed, error) {
79+
func (c *consensusDataSource[V, H]) PreConfirmedBlockByNumber(
80+
ctx context.Context,
81+
blockNumber uint64,
82+
) (pending.PreConfirmed, error) {
7983
preconfirmed := c.proposer.Preconfirmed()
8084
if preconfirmed.Block.Number != blockNumber {
81-
return core.PreConfirmed{}, fmt.Errorf("block %d is not preconfirmed", blockNumber)
85+
return pending.PreConfirmed{}, fmt.Errorf("block %d is not preconfirmed", blockNumber)
8286
}
8387
return *preconfirmed, nil
8488
}

0 commit comments

Comments
 (0)