11package builder
22
33import (
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
913type 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+ }
0 commit comments