@@ -77,37 +77,53 @@ public struct AccumulationResult: Sendable {
7777}
7878
7979public struct AccountChanges : Sendable {
80+ public enum UpdateKind : Sendable {
81+ case newAccount( ServiceIndex , ServiceAccount )
82+ case removeAccount( ServiceIndex )
83+ case updateAccount( ServiceIndex , ServiceAccountDetails )
84+ case updateStorage( ServiceIndex , Data , Data ? )
85+ case updatePreimage( ServiceIndex , Data32 , Data ? )
86+ case updatePreimageInfo( ServiceIndex , Data32 , UInt32 , StateKeys . ServiceAccountPreimageInfoKey . Value ? )
87+ }
88+
89+ // records for checking conflicts
8090 public var newAccounts : [ ServiceIndex : ServiceAccount ]
8191 public var altered : Set < ServiceIndex >
82- public var accountUpdates : [ ServiceIndex : ServiceAccountDetails ]
83- public var storageUpdates : [ ServiceIndex : [ Data : Data ? ] ]
84- public var preimageUpdates : [ ServiceIndex : [ Data32 : Data ? ] ]
85- public var preimageInfoUpdates : [ ServiceIndex : [ Data32 : ( UInt32 , StateKeys . ServiceAccountPreimageInfoKey . Value ? ) ] ]
8692 public var removed : Set < ServiceIndex >
8793
94+ // array for apply sequential updates
95+ public var updates : [ UpdateKind ]
96+
8897 public init ( ) {
8998 newAccounts = [ : ]
9099 altered = [ ]
91- accountUpdates = [ : ]
92- storageUpdates = [ : ]
93- preimageUpdates = [ : ]
94- preimageInfoUpdates = [ : ]
95100 removed = [ ]
101+ updates = [ ]
102+ }
103+
104+ public mutating func addNewAccount( index: ServiceIndex , account: ServiceAccount ) {
105+ newAccounts [ index] = account
106+ updates. append ( . newAccount( index, account) )
107+ }
108+
109+ public mutating func addRemovedAccount( index: ServiceIndex ) {
110+ removed. insert ( index)
111+ updates. append ( . removeAccount( index) )
96112 }
97113
98114 public mutating func addAccountUpdate( index: ServiceIndex , account: ServiceAccountDetails ) {
99- accountUpdates [ index] = account
100115 altered. insert ( index)
116+ updates. append ( . updateAccount( index, account) )
101117 }
102118
103119 public mutating func addStorageUpdate( index: ServiceIndex , key: Data , value: Data ? ) {
104- storageUpdates [ index, default: [ : ] ] [ key] = value
105120 altered. insert ( index)
121+ updates. append ( . updateStorage( index, key, value) )
106122 }
107123
108124 public mutating func addPreimageUpdate( index: ServiceIndex , hash: Data32 , value: Data ? ) {
109- preimageUpdates [ index, default: [ : ] ] [ hash] = value
110125 altered. insert ( index)
126+ updates. append ( . updatePreimage( index, hash, value) )
111127 }
112128
113129 public mutating func addPreimageInfoUpdate(
@@ -116,32 +132,24 @@ public struct AccountChanges: Sendable {
116132 length: UInt32 ,
117133 value: StateKeys . ServiceAccountPreimageInfoKey . Value ?
118134 ) {
119- preimageInfoUpdates [ index, default: [ : ] ] [ hash] = ( length, value)
120135 altered. insert ( index)
136+ updates. append ( . updatePreimageInfo( index, hash, length, value) )
121137 }
122138
123139 public func apply( to accounts: ServiceAccountsMutRef ) async throws {
124- for (index, account) in newAccounts {
125- try await accounts. addNew ( serviceAccount: index, account: account)
126- }
127- for index in removed {
128- accounts. remove ( serviceAccount: index)
129- }
130- for (index, account) in accountUpdates {
131- accounts. set ( serviceAccount: index, account: account)
132- }
133- for (index, storage) in storageUpdates {
134- for (key, value) in storage {
140+ for update in updates {
141+ switch update {
142+ case let . newAccount( index, account) :
143+ try await accounts. addNew ( serviceAccount: index, account: account)
144+ case let . removeAccount( index) :
145+ accounts. remove ( serviceAccount: index)
146+ case let . updateAccount( index, account) :
147+ accounts. set ( serviceAccount: index, account: account)
148+ case let . updateStorage( index, key, value) :
135149 try await accounts. set ( serviceAccount: index, storageKey: key, value: value)
136- }
137- }
138- for (index, preimages) in preimageUpdates {
139- for (hash, value) in preimages {
150+ case let . updatePreimage( index, hash, value) :
140151 accounts. set ( serviceAccount: index, preimageHash: hash, value: value)
141- }
142- }
143- for (index, preimageInfos) in preimageInfoUpdates {
144- for (hash, ( length, value) ) in preimageInfos {
152+ case let . updatePreimageInfo( index, hash, length, value) :
145153 try await accounts. set ( serviceAccount: index, preimageHash: hash, length: length, value: value)
146154 }
147155 }
@@ -166,25 +174,7 @@ public struct AccountChanges: Sendable {
166174 }
167175 altered. formUnion ( other. altered)
168176 removed. formUnion ( other. removed)
169-
170- for (index, account) in other. accountUpdates {
171- accountUpdates [ index] = account
172- }
173- for (index, storage) in other. storageUpdates {
174- for (key, value) in storage {
175- storageUpdates [ index, default: [ : ] ] [ key] = value
176- }
177- }
178- for (index, preimages) in other. preimageUpdates {
179- for (hash, value) in preimages {
180- preimageUpdates [ index, default: [ : ] ] [ hash] = value
181- }
182- }
183- for (index, preimageInfos) in other. preimageInfoUpdates {
184- for (hash, info) in preimageInfos {
185- preimageInfoUpdates [ index, default: [ : ] ] [ hash] = info
186- }
187- }
177+ updates. append ( contentsOf: other. updates)
188178 }
189179}
190180
@@ -255,7 +245,7 @@ extension Accumulation {
255245 }
256246
257247 /// parallelized accumulate function ∆*
258- private mutating func parallelizedAccumulate(
248+ private func parallelizedAccumulate(
259249 config: ProtocolConfigRef ,
260250 state: AccumulateState ,
261251 workReports: [ WorkReport ] ,
@@ -297,11 +287,9 @@ extension Accumulation {
297287 ) { group in
298288 for service in serviceBatch {
299289 group. addTask { [ batchState] in
300- let parallelState = batchState. copy ( )
301-
302290 return try await Self . singleAccumulate (
303291 config: config,
304- state: parallelState ,
292+ state: batchState . copy ( ) ,
305293 workReports: workReports,
306294 service: service,
307295 alwaysAcc: alwaysAcc,
@@ -377,7 +365,7 @@ extension Accumulation {
377365 return batches
378366 }
379367
380- private mutating func mergeParallelBatchResults(
368+ private func mergeParallelBatchResults(
381369 batchResults: [ ( ServiceIndex , AccumulationResult ) ] ,
382370 currentState: inout AccumulateState ,
383371 overallAccountChanges: inout AccountChanges ,
@@ -437,7 +425,7 @@ extension Accumulation {
437425 }
438426
439427 /// outer accumulate function ∆+
440- private mutating func outerAccumulate(
428+ private func outerAccumulate(
441429 config: ProtocolConfigRef ,
442430 state: AccumulateState ,
443431 workReports: [ WorkReport ] ,
@@ -589,7 +577,7 @@ extension Accumulation {
589577 }
590578
591579 // accumulate execution
592- private mutating func execution(
580+ private func execution(
593581 config: ProtocolConfigRef ,
594582 workReports: [ WorkReport ] ,
595583 state: AccumulateState ,
0 commit comments