Skip to content

Commit a58e4b1

Browse files
committed
feat: Basic integration for preconfirmed block
1 parent 5abdad0 commit a58e4b1

15 files changed

Lines changed: 126 additions & 127 deletions

File tree

builder/builder.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func New(
4949
}
5050
}
5151

52-
func (b *Builder) Finalise(pending *sync.Pending, signer utils.BlockSignFunc, privateKey *ecdsa.PrivateKey) error {
53-
return b.blockchain.Finalise(pending.Block, pending.StateUpdate, pending.NewClasses, signer)
52+
func (b *Builder) Finalise(preconfirmed *core.PreConfirmed, signer utils.BlockSignFunc, privateKey *ecdsa.PrivateKey) error {
53+
return b.blockchain.Finalise(preconfirmed.Block, preconfirmed.StateUpdate, preconfirmed.NewClasses, signer)
5454
}
5555

56-
func (b *Builder) InitPendingBlock(params *BuildParams) (*BuildState, error) {
56+
func (b *Builder) InitPreconfirmedBlock(params *BuildParams) (*BuildState, error) {
5757
header, err := b.blockchain.HeadsHeader()
5858
if err != nil {
5959
return nil, err
@@ -64,7 +64,7 @@ func (b *Builder) InitPendingBlock(params *BuildParams) (*BuildState, error) {
6464
return nil, err
6565
}
6666

67-
pendingBlock := core.Block{
67+
preconfirmedBlock := core.Block{
6868
Header: &core.Header{
6969
Hash: nil, // To be set after finishing execution
7070
ParentHash: header.Hash,
@@ -97,14 +97,16 @@ func (b *Builder) InitPendingBlock(params *BuildParams) (*BuildState, error) {
9797
su := core.StateUpdate{
9898
StateDiff: &emptyStateDiff,
9999
}
100-
pending := sync.Pending{
101-
Block: &pendingBlock,
102-
StateUpdate: &su,
103-
NewClasses: newClasses,
100+
preconfirmed := core.PreConfirmed{
101+
Block: &preconfirmedBlock,
102+
StateUpdate: &su,
103+
NewClasses: newClasses,
104+
TransactionStateDiffs: []*core.StateDiff{},
105+
CandidateTxs: []core.Transaction{},
104106
}
105107

106108
return &BuildState{
107-
Pending: &pending,
109+
Preconfirmed: &preconfirmed,
108110
RevealedBlockHash: revealedBlockHash,
109111
L2GasConsumed: 0,
110112
}, nil
@@ -123,7 +125,7 @@ func (b *Builder) getRevealedBlockHash(blockHeight uint64) (*felt.Felt, error) {
123125
}
124126

125127
func (b *Builder) PendingState(buildState *BuildState) (core.StateReader, func() error, error) {
126-
if buildState.Pending == nil {
128+
if buildState.Preconfirmed == nil {
127129
return nil, nil, sync.ErrPendingBlockNotFound
128130
}
129131

@@ -133,7 +135,7 @@ func (b *Builder) PendingState(buildState *BuildState) (core.StateReader, func()
133135
}
134136

135137
// TODO: remove the state closer once we refactor the state
136-
return sync.NewPendingState(buildState.Pending.StateUpdate.StateDiff, buildState.Pending.NewClasses, headState), headCloser, nil
138+
return sync.NewPendingState(buildState.Preconfirmed.StateUpdate.StateDiff, buildState.Preconfirmed.NewClasses, headState), headCloser, nil
137139
}
138140

139141
func (b *Builder) RunTxns(state *BuildState, txns []mempool.BroadcastedTransaction) error {
@@ -160,7 +162,7 @@ func (b *Builder) Finish(state *BuildState) (BuildResult, error) {
160162

161163
// Todo: we ignore some values until the spec is Finalised: VersionConstantCommitment, NextL2GasPriceFRI
162164
buildResult := BuildResult{
163-
Pending: state.Pending,
165+
Preconfirmed: state.Preconfirmed,
164166
SimulateResult: &simulatedResult,
165167
L2GasConsumed: state.L2GasConsumed,
166168
}

builder/executor.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewExecutor(
4141
}
4242

4343
// RunTxns executes the provided transaction and applies the state changes
44-
// to the pending state
44+
// to the preconfirmed state
4545
func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransaction) (err error) {
4646
if len(txns) == 0 {
4747
return nil
@@ -58,7 +58,7 @@ func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransact
5858
}()
5959

6060
// Create a state writer for the transaction execution
61-
stateWriter := sync.NewPendingStateWriter(state.Pending.StateUpdate.StateDiff, state.Pending.NewClasses, headState)
61+
stateWriter := sync.NewPendingStateWriter(state.Preconfirmed.StateUpdate.StateDiff, state.Preconfirmed.NewClasses, headState)
6262

6363
// Prepare declared classes, if any
6464
var declaredClasses []core.Class
@@ -80,7 +80,7 @@ func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransact
8080
declaredClasses,
8181
paidFeesOnL1,
8282
&vm.BlockInfo{
83-
Header: state.Pending.Block.Header,
83+
Header: state.Preconfirmed.Block.Header,
8484
BlockHashToBeRevealed: state.RevealedBlockHash,
8585
},
8686
stateWriter,
@@ -102,16 +102,16 @@ func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransact
102102

103103
// Adapt results to core type (which use reference types)
104104
receipts := make([]*core.TransactionReceipt, len(txns))
105-
mergedStateDiff := vm2core.AdaptStateDiff(vmResults.Traces[0].StateDiff)
105+
stateDiffs := make([]*core.StateDiff, len(vmResults.Traces))
106106
for i, trace := range vmResults.Traces {
107107
adaptedStateDiff := vm2core.AdaptStateDiff(trace.StateDiff)
108-
mergedStateDiff.Merge(&adaptedStateDiff)
109108
adaptedReceipt := vm2core.Receipt(vmResults.OverallFees[i], txns[i].Transaction, &vmResults.Traces[i], &vmResults.Receipts[i])
110109
receipts[i] = &adaptedReceipt
110+
stateDiffs[i] = &adaptedStateDiff
111111
}
112112

113-
// Update pending block with transaction results
114-
updatePendingBlock(state.Pending, receipts, coreTxns, mergedStateDiff)
113+
// Update preconfirmed block with transaction results
114+
updatePreconfirmedBlock(state.Preconfirmed, receipts, coreTxns, stateDiffs)
115115

116116
for i := range vmResults.GasConsumed {
117117
state.L2GasConsumed += vmResults.GasConsumed[i].L2Gas
@@ -138,22 +138,25 @@ func (e *executor) processClassDeclaration(txn *mempool.BroadcastedTransaction,
138138
return nil
139139
}
140140

141-
// updatePendingBlock updates the pending block with transaction results
142-
func updatePendingBlock(
143-
pending *sync.Pending,
141+
// updatePreconfirmedBlock updates the preconfirmed block with transaction results
142+
func updatePreconfirmedBlock(
143+
preconfirmed *core.PreConfirmed,
144144
receipts []*core.TransactionReceipt,
145145
transactions []core.Transaction,
146-
stateDiff core.StateDiff,
146+
stateDiffs []*core.StateDiff,
147147
) {
148-
pending.Block.Receipts = append(pending.Block.Receipts, receipts...)
149-
pending.Block.Transactions = append(pending.Block.Transactions, transactions...)
150-
pending.Block.TransactionCount += uint64(len(transactions))
148+
preconfirmed.Block.Receipts = append(preconfirmed.Block.Receipts, receipts...)
149+
preconfirmed.TransactionStateDiffs = append(preconfirmed.TransactionStateDiffs, stateDiffs...)
150+
preconfirmed.Block.Transactions = append(preconfirmed.Block.Transactions, transactions...)
151+
preconfirmed.Block.TransactionCount += uint64(len(transactions))
151152
for _, receipt := range receipts {
152-
pending.Block.EventCount += uint64(len(receipt.Events))
153+
preconfirmed.Block.EventCount += uint64(len(receipt.Events))
154+
}
155+
for _, stateDiff := range stateDiffs {
156+
preconfirmed.StateUpdate.StateDiff.Merge(stateDiff)
153157
}
154-
pending.StateUpdate.StateDiff.Merge(&stateDiff)
155158
}
156159

157160
func (e *executor) Finish(state *BuildState) (blockchain.SimulateResult, error) {
158-
return e.blockchain.Simulate(state.Pending.Block, state.Pending.StateUpdate, state.Pending.NewClasses, nil)
161+
return e.blockchain.Simulate(state.Preconfirmed.Block, state.Preconfirmed.StateUpdate, state.Preconfirmed.NewClasses, nil)
159162
}

builder/result.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@ 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"
78
"github.com/NethermindEth/juno/core/felt"
8-
"github.com/NethermindEth/juno/sync"
99
)
1010

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

1717
func (b *BuildResult) ProposalCommitment() (types.ProposalCommitment, error) {
18-
version, err := semver.NewVersion(b.Pending.Block.ProtocolVersion)
18+
version, err := semver.NewVersion(b.Preconfirmed.Block.ProtocolVersion)
1919
if err != nil {
2020
return types.ProposalCommitment{}, err
2121
}
2222

2323
return types.ProposalCommitment{
24-
BlockNumber: b.Pending.Block.Number,
25-
Builder: *b.Pending.Block.SequencerAddress,
26-
ParentCommitment: *b.Pending.Block.ParentHash,
27-
Timestamp: b.Pending.Block.Timestamp,
24+
BlockNumber: b.Preconfirmed.Block.Number,
25+
Builder: *b.Preconfirmed.Block.SequencerAddress,
26+
ParentCommitment: *b.Preconfirmed.Block.ParentHash,
27+
Timestamp: b.Preconfirmed.Block.Timestamp,
2828
ProtocolVersion: *version,
29-
OldStateRoot: *b.Pending.StateUpdate.OldRoot,
29+
OldStateRoot: *b.Preconfirmed.StateUpdate.OldRoot,
3030
StateDiffCommitment: *b.SimulateResult.BlockCommitments.StateDiffCommitment,
3131
TransactionCommitment: *b.SimulateResult.BlockCommitments.TransactionCommitment,
3232
EventCommitment: *b.SimulateResult.BlockCommitments.EventCommitment,
3333
ReceiptCommitment: *b.SimulateResult.BlockCommitments.ReceiptCommitment,
3434
ConcatenatedCounts: b.SimulateResult.ConcatCount,
35-
L1GasPriceFRI: *b.Pending.Block.L1GasPriceSTRK,
36-
L1DataGasPriceFRI: *b.Pending.Block.L1DataGasPrice.PriceInFri,
37-
L2GasPriceFRI: *b.Pending.Block.L2GasPrice.PriceInFri,
35+
L1GasPriceFRI: *b.Preconfirmed.Block.L1GasPriceSTRK,
36+
L1DataGasPriceFRI: *b.Preconfirmed.Block.L1DataGasPrice.PriceInFri,
37+
L2GasPriceFRI: *b.Preconfirmed.Block.L2GasPrice.PriceInFri,
3838
L2GasUsed: *new(felt.Felt).SetUint64(b.L2GasConsumed),
39-
L1DAMode: b.Pending.Block.L1DAMode,
39+
L1DAMode: b.Preconfirmed.Block.L1DAMode,
4040
}, nil
4141
}

builder/state.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@ import (
66

77
"github.com/NethermindEth/juno/core"
88
"github.com/NethermindEth/juno/core/felt"
9-
"github.com/NethermindEth/juno/sync"
109
"github.com/NethermindEth/juno/utils"
1110
)
1211

1312
type BuildState struct {
14-
Pending *sync.Pending
13+
Preconfirmed *core.PreConfirmed
1514
L2GasConsumed uint64
1615
RevealedBlockHash *felt.Felt
1716
}
1817

1918
func (b *BuildState) PendingBlock() *core.Block {
20-
if b.Pending == nil {
19+
if b.Preconfirmed == nil {
2120
return nil
2221
}
23-
return b.Pending.Block
22+
return b.Preconfirmed.Block
2423
}
2524

2625
func (b *BuildState) ClearPending() error {
2726
b.L2GasConsumed = 0
28-
b.Pending = &sync.Pending{}
27+
b.Preconfirmed = &core.PreConfirmed{}
2928
b.RevealedBlockHash = nil
3029

3130
return nil
@@ -38,25 +37,27 @@ func (b *BuildState) ClearPending() error {
3837
// - Signatures and EventsBloom are not set before `Finish` is called
3938
func (b *BuildState) Clone() BuildState {
4039
return BuildState{
41-
Pending: clonePending(b.Pending),
40+
Preconfirmed: clonePreconfirmed(b.Preconfirmed),
4241
RevealedBlockHash: b.RevealedBlockHash, // Safe to reuse an immutable value
4342
L2GasConsumed: b.L2GasConsumed, // Value, safe to shallow copy
4443
}
4544
}
4645

47-
func clonePending(pending *sync.Pending) *sync.Pending {
48-
return &sync.Pending{
49-
Block: cloneBlock(pending.Block),
50-
StateUpdate: cloneStateUpdate(pending.StateUpdate),
51-
NewClasses: maps.Clone(pending.NewClasses),
46+
func clonePreconfirmed(preconfirmed *core.PreConfirmed) *core.PreConfirmed {
47+
return &core.PreConfirmed{
48+
Block: cloneBlock(preconfirmed.Block),
49+
StateUpdate: cloneStateUpdate(preconfirmed.StateUpdate),
50+
NewClasses: maps.Clone(preconfirmed.NewClasses),
51+
TransactionStateDiffs: preconfirmed.TransactionStateDiffs,
52+
CandidateTxs: preconfirmed.CandidateTxs,
5253
}
5354
}
5455

5556
func cloneBlock(block *core.Block) *core.Block {
5657
return &core.Block{
5758
Header: utils.HeapPtr(*block.Header),
58-
Transactions: slices.Clone(block.Transactions),
59-
Receipts: slices.Clone(block.Receipts),
59+
Transactions: block.Transactions,
60+
Receipts: block.Receipts,
6061
}
6162
}
6263

consensus/datasource/consensus_data_source.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ func (c *consensusDataSource[V, H, A]) BlockLatest(ctx context.Context) (*core.B
7070
}
7171

7272
func (c *consensusDataSource[V, H, A]) BlockPending(ctx context.Context) (sync.Pending, error) {
73-
return *c.proposer.Pending(), nil
73+
return sync.Pending{}, errors.New("not implemented") // TODO: Revise this
7474
}
7575

7676
func (c *consensusDataSource[V, H, A]) PreConfirmedBlockByNumber(ctx context.Context, blockNumber uint64) (core.PreConfirmed, error) {
77-
return core.PreConfirmed{}, errors.New("not implemented") // TODO: Implement this
77+
return *c.proposer.Preconfirmed(), nil
7878
}

consensus/driver/commit_listener.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func (b *commitListener[V, H, A]) Commit(ctx context.Context, height types.Heigh
5252
}
5353

5454
committedBlock := sync.CommittedBlock{
55-
Block: buildResult.Pending.Block,
56-
StateUpdate: buildResult.Pending.StateUpdate,
57-
NewClasses: buildResult.Pending.NewClasses,
55+
Block: buildResult.Preconfirmed.Block,
56+
StateUpdate: buildResult.Preconfirmed.StateUpdate,
57+
NewClasses: buildResult.Preconfirmed.NewClasses,
5858
Persisted: make(chan struct{}),
5959
}
6060

consensus/p2p/proposer/proposer_adapter.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,36 @@ func (a *starknetProposerAdapter) ProposalInit(proposal *starknet.Proposal) (typ
3636
// TODO: Implement this function properly
3737
func (a *starknetProposerAdapter) ProposalBlockInfo(buildResult *builder.BuildResult) (types.BlockInfo, error) {
3838
return types.BlockInfo{
39-
BlockNumber: buildResult.Pending.Block.Number,
40-
Builder: *buildResult.Pending.Block.SequencerAddress,
41-
Timestamp: buildResult.Pending.Block.Timestamp,
42-
L2GasPriceFRI: *buildResult.Pending.Block.L2GasPrice.PriceInFri,
43-
L1GasPriceWEI: *buildResult.Pending.Block.L1GasPriceSTRK,
44-
L1DataGasPriceWEI: *buildResult.Pending.Block.L1DataGasPrice.PriceInFri,
39+
BlockNumber: buildResult.Preconfirmed.Block.Number,
40+
Builder: *buildResult.Preconfirmed.Block.SequencerAddress,
41+
Timestamp: buildResult.Preconfirmed.Block.Timestamp,
42+
L2GasPriceFRI: *buildResult.Preconfirmed.Block.L2GasPrice.PriceInFri,
43+
L1GasPriceWEI: *buildResult.Preconfirmed.Block.L1GasPriceSTRK,
44+
L1DataGasPriceWEI: *buildResult.Preconfirmed.Block.L1DataGasPrice.PriceInFri,
4545
EthToStrkRate: felt.One, // TODO: Double check if this is used
46-
L1DAMode: buildResult.Pending.Block.L1DAMode,
46+
L1DAMode: buildResult.Preconfirmed.Block.L1DAMode,
4747
}, nil
4848
}
4949

5050
// TODO: Implement this function properly
5151
func (a *starknetProposerAdapter) ProposalTransactions(buildResult *builder.BuildResult) ([]types.Transaction, error) {
52-
transactions := make([]types.Transaction, len(buildResult.Pending.Block.Transactions))
53-
for i := range buildResult.Pending.Block.Transactions {
52+
transactions := make([]types.Transaction, len(buildResult.Preconfirmed.Block.Transactions))
53+
for i := range buildResult.Preconfirmed.Block.Transactions {
5454
var class core.Class
5555
var paidFeeOnL1 *felt.Felt
5656

57-
switch tx := buildResult.Pending.Block.Transactions[i].(type) {
57+
switch tx := buildResult.Preconfirmed.Block.Transactions[i].(type) {
5858
case *core.DeclareTransaction:
5959
var ok bool
60-
if class, ok = buildResult.Pending.NewClasses[*tx.ClassHash]; !ok {
60+
if class, ok = buildResult.Preconfirmed.NewClasses[*tx.ClassHash]; !ok {
6161
return nil, errors.New("class not found")
6262
}
6363
case *core.L1HandlerTransaction:
6464
paidFeeOnL1 = felt.One.Clone()
6565
}
6666

6767
transactions[i] = types.Transaction{
68-
Transaction: buildResult.Pending.Block.Transactions[i],
68+
Transaction: buildResult.Preconfirmed.Block.Transactions[i],
6969
Class: class,
7070
PaidFeeOnL1: paidFeeOnL1,
7171
}

consensus/p2p/validator/empty_fixtures_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/NethermindEth/juno/core"
1111
"github.com/NethermindEth/juno/core/felt"
1212
"github.com/NethermindEth/juno/db"
13-
"github.com/NethermindEth/juno/sync"
1413
"github.com/NethermindEth/juno/utils"
1514
"github.com/starknet-io/starknet-p2pspecs/p2p/proto/common"
1615
"github.com/starknet-io/starknet-p2pspecs/p2p/proto/consensus/consensus"
@@ -93,7 +92,7 @@ func NewEmptyTestFixture(
9392

9493
func EmptyBuildResult(headBlock *core.Block, proposer, expectedHash *felt.Felt, timestamp uint64) builder.BuildResult {
9594
return builder.BuildResult{
96-
Pending: &sync.Pending{
95+
Preconfirmed: &core.PreConfirmed{
9796
Block: &core.Block{
9897
Header: &core.Header{
9998
Hash: expectedHash,
@@ -126,7 +125,9 @@ func EmptyBuildResult(headBlock *core.Block, proposer, expectedHash *felt.Felt,
126125
OldRoot: headBlock.GlobalStateRoot,
127126
StateDiff: utils.HeapPtr(core.EmptyStateDiff()),
128127
},
129-
NewClasses: make(map[felt.Felt]core.Class),
128+
NewClasses: make(map[felt.Felt]core.Class),
129+
TransactionStateDiffs: []*core.StateDiff{},
130+
CandidateTxs: []core.Transaction{},
130131
},
131132
SimulateResult: &blockchain.SimulateResult{
132133
BlockCommitments: &core.BlockCommitments{

0 commit comments

Comments
 (0)