Skip to content

Commit

Permalink
add accumulate test (#295)
Browse files Browse the repository at this point in the history
* add accumulate test

* comment out tests
  • Loading branch information
xlc authored Feb 19, 2025
1 parent 7860ca6 commit 9ecbae8
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 39 deletions.
51 changes: 29 additions & 22 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Accumulation.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Codec
import Utils

public enum AccumulationError: Error {
Expand All @@ -7,7 +8,7 @@ public enum AccumulationError: Error {

public struct AccumulationQueueItem: Sendable, Equatable, Codable {
public var workReport: WorkReport
public var dependencies: Set<Data32>
@CodingAs<SortedSet<Data32>> public var dependencies: Set<Data32>

public init(workReport: WorkReport, dependencies: Set<Data32>) {
self.workReport = workReport
Expand Down Expand Up @@ -68,7 +69,6 @@ public protocol Accumulation: ServiceAccounts {
>,
ProtocolConfig.TotalNumberOfCores
> { get }
var entropyPool: EntropyPool { get }
var accumlateFunction: AccumulateFunction { get }
var onTransferFunction: OnTransferFunction { get }
var accumulationQueue: StateKeys.AccumulationQueueKey.Value { get }
Expand All @@ -82,8 +82,9 @@ extension Accumulation {
state: AccumulateState,
workReports: [WorkReport],
service: ServiceIndex,
block: BlockRef,
privilegedGas: [ServiceIndex: Gas]
privilegedGas: [ServiceIndex: Gas],
entropy: Data32,
timeslot: TimeslotIndex
) async throws -> SingleAccumulationOutput {
var gas = Gas(0)
var arguments: [AccumulateArguments] = []
Expand Down Expand Up @@ -111,9 +112,8 @@ extension Accumulation {
serviceIndex: service,
gas: gas,
arguments: arguments,
initialIndex: Blake2b256.hash(service.encode(), entropyPool.t0.data, block.header.timeslot.encode())
.data.decode(UInt32.self),
timeslot: block.header.timeslot
initialIndex: Blake2b256.hash(service.encode(), entropy.data, timeslot.encode()).data.decode(UInt32.self),
timeslot: timeslot
)

return SingleAccumulationOutput(
Expand All @@ -127,10 +127,11 @@ extension Accumulation {
/// parallelized accumulate function ∆*
private mutating func parallelizedAccumulate(
config: ProtocolConfigRef,
block: BlockRef,
state: AccumulateState,
workReports: [WorkReport],
privilegedGas: [ServiceIndex: Gas]
privilegedGas: [ServiceIndex: Gas],
entropy: Data32,
timeslot: TimeslotIndex
) async throws -> ParallelAccumulationOutput {
var services = Set<ServiceIndex>()
var gasUsed = Gas(0)
Expand Down Expand Up @@ -165,8 +166,9 @@ extension Accumulation {
state: state,
workReports: workReports,
service: service,
block: block,
privilegedGas: privilegedGas
privilegedGas: privilegedGas,
entropy: entropy,
timeslot: timeslot
)
gasUsed += singleOutput.gasUsed

Expand Down Expand Up @@ -214,11 +216,12 @@ extension Accumulation {
/// outer accumulate function ∆+
private mutating func outerAccumulate(
config: ProtocolConfigRef,
block: BlockRef,
state: AccumulateState,
workReports: [WorkReport],
privilegedGas: [ServiceIndex: Gas],
gasLimit: Gas
gasLimit: Gas,
entropy: Data32,
timeslot: TimeslotIndex
) async throws -> AccumulationOutput {
var i = 0
var sumGasRequired = Gas(0)
Expand All @@ -242,18 +245,20 @@ extension Accumulation {
} else {
let parallelOutput = try await parallelizedAccumulate(
config: config,
block: block,
state: state,
workReports: Array(workReports[0 ..< i]),
privilegedGas: privilegedGas
privilegedGas: privilegedGas,
entropy: entropy,
timeslot: timeslot
)
let outerOutput = try await outerAccumulate(
config: config,
block: block,
state: parallelOutput.state,
workReports: Array(workReports[i ..< workReports.count]),
privilegedGas: [:],
gasLimit: gasLimit - parallelOutput.gasUsed
gasLimit: gasLimit - parallelOutput.gasUsed,
entropy: entropy,
timeslot: timeslot
)
return AccumulationOutput(
numAccumulated: i + outerOutput.numAccumulated,
Expand Down Expand Up @@ -305,7 +310,7 @@ extension Accumulation {

editAccumulatedItems(
items: &newQueueItems,
accumulatedPackages: Set(history.array.reduce(into: Set<Data32>()) { $0.formUnion($1) })
accumulatedPackages: Set(history.array.reduce(into: Set<Data32>()) { $0.formUnion($1.array) })
)

return (zeroPrereqReports, newQueueItems)
Expand All @@ -328,16 +333,16 @@ extension Accumulation {

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

let res = try await outerAccumulate(
config: config,
block: block,
state: AccumulateState(
newServiceAccounts: [:],
validatorQueue: validatorQueue,
Expand All @@ -346,7 +351,9 @@ extension Accumulation {
),
workReports: workReports,
privilegedGas: privilegedServices.basicGas,
gasLimit: gasLimit
gasLimit: gasLimit,
entropy: entropy,
timeslot: timeslot
)

var transferGroups = [ServiceIndex: [DeferredTransfers]]()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public protocol Guaranteeing {
ProtocolConfig.EpochLength
> { get }
var accumulationHistory: ConfigFixedSizeArray<
Set<Data32>,
SortedUniqueArray<Data32>,
ProtocolConfig.EpochLength
> { get }

Expand Down Expand Up @@ -185,7 +185,7 @@ extension Guaranteeing {
}

let recentWorkPackageHashes: Set<Data32> = Set(recentHistory.items.flatMap(\.lookup.keys))
let accumulateHistoryReports = Set(accumulationHistory.array.flatMap(\.self))
let accumulateHistoryReports = Set(accumulationHistory.array.flatMap(\.array))
let accumulateQueueReports = Set(accumulationQueue.array.flatMap(\.self)
.flatMap(\.workReport.refinementContext.prerequisiteWorkPackages))
let pendingWorkReportHashes = Set(reports.array.flatMap { $0?.workReport.refinementContext.prerequisiteWorkPackages ?? [] })
Expand Down
9 changes: 7 additions & 2 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ public final class Runtime {
)

// accumulate and transfers
let (numAccumulated, accumulateState, _) = try await state.update(config: config, block: block, workReports: accumulatableReports)
let (numAccumulated, accumulateState, _) = try await state.update(
config: config,
workReports: accumulatableReports,
entropy: state.entropyPool.t0,
timeslot: block.header.timeslot
)

state.authorizationQueue = accumulateState.authorizationQueue
state.validatorQueue = accumulateState.validatorQueue
Expand All @@ -255,7 +260,7 @@ public final class Runtime {
let newHistoryItem = Set(accumulated.map(\.packageSpecification.workPackageHash))
for i in 0 ..< config.value.epochLength {
if i == config.value.epochLength - 1 {
state.accumulationHistory[i] = newHistoryItem
state.accumulationHistory[i] = .init(newHistoryItem)
} else {
state.accumulationHistory[i] = state.accumulationHistory[i + 1]
}
Expand Down
10 changes: 6 additions & 4 deletions Blockchain/Sources/Blockchain/State/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ extension State: Dummy {
)
let accumulationHistory: StateKeys.AccumulationHistoryKey.Value = try! ConfigFixedSizeArray(
config: config,
defaultValue: Set<Data32>()
defaultValue: .init()
)

let kv: [(any StateKey, Codable & Sendable)] = [
Expand Down Expand Up @@ -481,8 +481,10 @@ extension State: Preimages {
}
}

struct DummyFunction: AccumulateFunction, OnTransferFunction {
func invoke(
public struct DummyFunction: AccumulateFunction, OnTransferFunction {
public init() {}

public func invoke(
config _: ProtocolConfigRef,
accounts _: inout some ServiceAccounts,
state _: AccumulateState,
Expand All @@ -495,7 +497,7 @@ struct DummyFunction: AccumulateFunction, OnTransferFunction {
fatalError("not implemented")
}

func invoke(
public func invoke(
config _: ProtocolConfigRef,
service _: ServiceIndex,
serviceAccounts _: inout some ServiceAccounts,
Expand Down
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/State/StateKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public enum StateKeys {

public struct AccumulationHistoryKey: StateKey {
public typealias Value = ConfigFixedSizeArray<
Set<Data32>,
SortedUniqueArray<Data32>,
ProtocolConfig.EpochLength
>

Expand Down
Loading

0 comments on commit 9ecbae8

Please sign in to comment.