Skip to content

Commit 67038c1

Browse files
committed
feat: Implement proposer component
1 parent f950632 commit 67038c1

File tree

6 files changed

+538
-281
lines changed

6 files changed

+538
-281
lines changed

builder/builder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func (b *Builder) PendingState(buildState *BuildState) (core.StateReader, func()
137137
}
138138

139139
func (b *Builder) RunTxns(state *BuildState, txns []mempool.BroadcastedTransaction) error {
140+
if len(txns) == 0 {
141+
return nil
142+
}
140143
return b.executor.RunTxns(state, txns)
141144
}
142145

builder/executor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func NewExecutor(
4343
// RunTxns executes the provided transaction and applies the state changes
4444
// to the pending state
4545
func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransaction) (err error) {
46+
if len(txns) == 0 {
47+
return nil
48+
}
49+
4650
headState, headCloser, err := e.blockchain.HeadState()
4751
if err != nil {
4852
return err

builder/state.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package builder
22

33
import (
4+
"maps"
5+
"slices"
6+
47
"github.com/NethermindEth/juno/core"
58
"github.com/NethermindEth/juno/core/felt"
69
"github.com/NethermindEth/juno/sync"
10+
"github.com/NethermindEth/juno/utils"
711
)
812

913
type BuildState struct {
@@ -26,3 +30,60 @@ func (b *BuildState) ClearPending() error {
2630

2731
return nil
2832
}
33+
34+
// Assumptions we make to avoid deep copying some fields and types:
35+
// - This function is only called before running `Finish`
36+
// - *felt.Felt is immutable
37+
// - *GasPrice is immutable
38+
// - Signatures and EventsBloom are not set before `Finish` is called
39+
func (b *BuildState) Clone() BuildState {
40+
return BuildState{
41+
Pending: clonePending(b.Pending),
42+
RevealedBlockHash: b.RevealedBlockHash, // Safe to reuse an immutable value
43+
L2GasConsumed: b.L2GasConsumed, // Value, safe to shallow copy
44+
}
45+
}
46+
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),
52+
}
53+
}
54+
55+
func cloneBlock(block *core.Block) *core.Block {
56+
return &core.Block{
57+
Header: utils.HeapPtr(*block.Header),
58+
Transactions: slices.Clone(block.Transactions),
59+
Receipts: slices.Clone(block.Receipts),
60+
}
61+
}
62+
63+
func cloneStateUpdate(stateUpdate *core.StateUpdate) *core.StateUpdate {
64+
return &core.StateUpdate{
65+
BlockHash: stateUpdate.BlockHash,
66+
NewRoot: stateUpdate.NewRoot,
67+
OldRoot: stateUpdate.OldRoot,
68+
StateDiff: cloneStateDiff(stateUpdate.StateDiff),
69+
}
70+
}
71+
72+
func cloneStateDiff(stateDiff *core.StateDiff) *core.StateDiff {
73+
return &core.StateDiff{
74+
StorageDiffs: cloneStorageDiffs(stateDiff.StorageDiffs),
75+
Nonces: maps.Clone(stateDiff.Nonces),
76+
DeployedContracts: maps.Clone(stateDiff.DeployedContracts),
77+
DeclaredV0Classes: slices.Clone(stateDiff.DeclaredV0Classes),
78+
DeclaredV1Classes: maps.Clone(stateDiff.DeclaredV1Classes),
79+
ReplacedClasses: maps.Clone(stateDiff.ReplacedClasses),
80+
}
81+
}
82+
83+
func cloneStorageDiffs(storageDiffs map[felt.Felt]map[felt.Felt]*felt.Felt) map[felt.Felt]map[felt.Felt]*felt.Felt {
84+
result := make(map[felt.Felt]map[felt.Felt]*felt.Felt)
85+
for key := range storageDiffs {
86+
result[key] = maps.Clone(storageDiffs[key])
87+
}
88+
return result
89+
}

consensus/proposer/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package proposer
1+
package proposer_test
22

33
import (
44
_ "github.com/NethermindEth/juno/encoder/registry"

0 commit comments

Comments
 (0)