Skip to content

Commit 9ecbae8

Browse files
authored
add accumulate test (#295)
* add accumulate test * comment out tests
1 parent 7860ca6 commit 9ecbae8

File tree

9 files changed

+262
-39
lines changed

9 files changed

+262
-39
lines changed

Blockchain/Sources/Blockchain/RuntimeProtocols/Accumulation.swift

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Codec
12
import Utils
23

34
public enum AccumulationError: Error {
@@ -7,7 +8,7 @@ public enum AccumulationError: Error {
78

89
public struct AccumulationQueueItem: Sendable, Equatable, Codable {
910
public var workReport: WorkReport
10-
public var dependencies: Set<Data32>
11+
@CodingAs<SortedSet<Data32>> public var dependencies: Set<Data32>
1112

1213
public init(workReport: WorkReport, dependencies: Set<Data32>) {
1314
self.workReport = workReport
@@ -68,7 +69,6 @@ public protocol Accumulation: ServiceAccounts {
6869
>,
6970
ProtocolConfig.TotalNumberOfCores
7071
> { get }
71-
var entropyPool: EntropyPool { get }
7272
var accumlateFunction: AccumulateFunction { get }
7373
var onTransferFunction: OnTransferFunction { get }
7474
var accumulationQueue: StateKeys.AccumulationQueueKey.Value { get }
@@ -82,8 +82,9 @@ extension Accumulation {
8282
state: AccumulateState,
8383
workReports: [WorkReport],
8484
service: ServiceIndex,
85-
block: BlockRef,
86-
privilegedGas: [ServiceIndex: Gas]
85+
privilegedGas: [ServiceIndex: Gas],
86+
entropy: Data32,
87+
timeslot: TimeslotIndex
8788
) async throws -> SingleAccumulationOutput {
8889
var gas = Gas(0)
8990
var arguments: [AccumulateArguments] = []
@@ -111,9 +112,8 @@ extension Accumulation {
111112
serviceIndex: service,
112113
gas: gas,
113114
arguments: arguments,
114-
initialIndex: Blake2b256.hash(service.encode(), entropyPool.t0.data, block.header.timeslot.encode())
115-
.data.decode(UInt32.self),
116-
timeslot: block.header.timeslot
115+
initialIndex: Blake2b256.hash(service.encode(), entropy.data, timeslot.encode()).data.decode(UInt32.self),
116+
timeslot: timeslot
117117
)
118118

119119
return SingleAccumulationOutput(
@@ -127,10 +127,11 @@ extension Accumulation {
127127
/// parallelized accumulate function ∆*
128128
private mutating func parallelizedAccumulate(
129129
config: ProtocolConfigRef,
130-
block: BlockRef,
131130
state: AccumulateState,
132131
workReports: [WorkReport],
133-
privilegedGas: [ServiceIndex: Gas]
132+
privilegedGas: [ServiceIndex: Gas],
133+
entropy: Data32,
134+
timeslot: TimeslotIndex
134135
) async throws -> ParallelAccumulationOutput {
135136
var services = Set<ServiceIndex>()
136137
var gasUsed = Gas(0)
@@ -165,8 +166,9 @@ extension Accumulation {
165166
state: state,
166167
workReports: workReports,
167168
service: service,
168-
block: block,
169-
privilegedGas: privilegedGas
169+
privilegedGas: privilegedGas,
170+
entropy: entropy,
171+
timeslot: timeslot
170172
)
171173
gasUsed += singleOutput.gasUsed
172174

@@ -214,11 +216,12 @@ extension Accumulation {
214216
/// outer accumulate function ∆+
215217
private mutating func outerAccumulate(
216218
config: ProtocolConfigRef,
217-
block: BlockRef,
218219
state: AccumulateState,
219220
workReports: [WorkReport],
220221
privilegedGas: [ServiceIndex: Gas],
221-
gasLimit: Gas
222+
gasLimit: Gas,
223+
entropy: Data32,
224+
timeslot: TimeslotIndex
222225
) async throws -> AccumulationOutput {
223226
var i = 0
224227
var sumGasRequired = Gas(0)
@@ -242,18 +245,20 @@ extension Accumulation {
242245
} else {
243246
let parallelOutput = try await parallelizedAccumulate(
244247
config: config,
245-
block: block,
246248
state: state,
247249
workReports: Array(workReports[0 ..< i]),
248-
privilegedGas: privilegedGas
250+
privilegedGas: privilegedGas,
251+
entropy: entropy,
252+
timeslot: timeslot
249253
)
250254
let outerOutput = try await outerAccumulate(
251255
config: config,
252-
block: block,
253256
state: parallelOutput.state,
254257
workReports: Array(workReports[i ..< workReports.count]),
255258
privilegedGas: [:],
256-
gasLimit: gasLimit - parallelOutput.gasUsed
259+
gasLimit: gasLimit - parallelOutput.gasUsed,
260+
entropy: entropy,
261+
timeslot: timeslot
257262
)
258263
return AccumulationOutput(
259264
numAccumulated: i + outerOutput.numAccumulated,
@@ -305,7 +310,7 @@ extension Accumulation {
305310

306311
editAccumulatedItems(
307312
items: &newQueueItems,
308-
accumulatedPackages: Set(history.array.reduce(into: Set<Data32>()) { $0.formUnion($1) })
313+
accumulatedPackages: Set(history.array.reduce(into: Set<Data32>()) { $0.formUnion($1.array) })
309314
)
310315

311316
return (zeroPrereqReports, newQueueItems)
@@ -328,16 +333,16 @@ extension Accumulation {
328333

329334
public mutating func update(
330335
config: ProtocolConfigRef,
331-
block: BlockRef,
332-
workReports: [WorkReport]
336+
workReports: [WorkReport],
337+
entropy: Data32,
338+
timeslot: TimeslotIndex
333339
) async throws -> (numAccumulated: Int, state: AccumulateState, commitments: Set<Commitment>) {
334340
let sumPrevilegedGas = privilegedServices.basicGas.values.reduce(Gas(0)) { $0 + $1.value }
335341
let minTotalGas = config.value.workReportAccumulationGas * Gas(config.value.totalNumberOfCores) + sumPrevilegedGas
336342
let gasLimit = max(config.value.totalAccumulationGas, minTotalGas)
337343

338344
let res = try await outerAccumulate(
339345
config: config,
340-
block: block,
341346
state: AccumulateState(
342347
newServiceAccounts: [:],
343348
validatorQueue: validatorQueue,
@@ -346,7 +351,9 @@ extension Accumulation {
346351
),
347352
workReports: workReports,
348353
privilegedGas: privilegedServices.basicGas,
349-
gasLimit: gasLimit
354+
gasLimit: gasLimit,
355+
entropy: entropy,
356+
timeslot: timeslot
350357
)
351358

352359
var transferGroups = [ServiceIndex: [DeferredTransfers]]()

Blockchain/Sources/Blockchain/RuntimeProtocols/Guaranteeing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public protocol Guaranteeing {
4545
ProtocolConfig.EpochLength
4646
> { get }
4747
var accumulationHistory: ConfigFixedSizeArray<
48-
Set<Data32>,
48+
SortedUniqueArray<Data32>,
4949
ProtocolConfig.EpochLength
5050
> { get }
5151

@@ -185,7 +185,7 @@ extension Guaranteeing {
185185
}
186186

187187
let recentWorkPackageHashes: Set<Data32> = Set(recentHistory.items.flatMap(\.lookup.keys))
188-
let accumulateHistoryReports = Set(accumulationHistory.array.flatMap(\.self))
188+
let accumulateHistoryReports = Set(accumulationHistory.array.flatMap(\.array))
189189
let accumulateQueueReports = Set(accumulationQueue.array.flatMap(\.self)
190190
.flatMap(\.workReport.refinementContext.prerequisiteWorkPackages))
191191
let pendingWorkReportHashes = Set(reports.array.flatMap { $0?.workReport.refinementContext.prerequisiteWorkPackages ?? [] })

Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ public final class Runtime {
232232
)
233233

234234
// accumulate and transfers
235-
let (numAccumulated, accumulateState, _) = try await state.update(config: config, block: block, workReports: accumulatableReports)
235+
let (numAccumulated, accumulateState, _) = try await state.update(
236+
config: config,
237+
workReports: accumulatableReports,
238+
entropy: state.entropyPool.t0,
239+
timeslot: block.header.timeslot
240+
)
236241

237242
state.authorizationQueue = accumulateState.authorizationQueue
238243
state.validatorQueue = accumulateState.validatorQueue
@@ -255,7 +260,7 @@ public final class Runtime {
255260
let newHistoryItem = Set(accumulated.map(\.packageSpecification.workPackageHash))
256261
for i in 0 ..< config.value.epochLength {
257262
if i == config.value.epochLength - 1 {
258-
state.accumulationHistory[i] = newHistoryItem
263+
state.accumulationHistory[i] = .init(newHistoryItem)
259264
} else {
260265
state.accumulationHistory[i] = state.accumulationHistory[i + 1]
261266
}

Blockchain/Sources/Blockchain/State/State.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ extension State: Dummy {
296296
)
297297
let accumulationHistory: StateKeys.AccumulationHistoryKey.Value = try! ConfigFixedSizeArray(
298298
config: config,
299-
defaultValue: Set<Data32>()
299+
defaultValue: .init()
300300
)
301301

302302
let kv: [(any StateKey, Codable & Sendable)] = [
@@ -481,8 +481,10 @@ extension State: Preimages {
481481
}
482482
}
483483

484-
struct DummyFunction: AccumulateFunction, OnTransferFunction {
485-
func invoke(
484+
public struct DummyFunction: AccumulateFunction, OnTransferFunction {
485+
public init() {}
486+
487+
public func invoke(
486488
config _: ProtocolConfigRef,
487489
accounts _: inout some ServiceAccounts,
488490
state _: AccumulateState,
@@ -495,7 +497,7 @@ struct DummyFunction: AccumulateFunction, OnTransferFunction {
495497
fatalError("not implemented")
496498
}
497499

498-
func invoke(
500+
public func invoke(
499501
config _: ProtocolConfigRef,
500502
service _: ServiceIndex,
501503
serviceAccounts _: inout some ServiceAccounts,

Blockchain/Sources/Blockchain/State/StateKeys.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public enum StateKeys {
241241

242242
public struct AccumulationHistoryKey: StateKey {
243243
public typealias Value = ConfigFixedSizeArray<
244-
Set<Data32>,
244+
SortedUniqueArray<Data32>,
245245
ProtocolConfig.EpochLength
246246
>
247247

0 commit comments

Comments
 (0)