Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii committed Nov 11, 2024
1 parent b3086ca commit a539a29
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,26 @@ public final class Runtime {
}

// update accumulation history
_ = try state.accumulationHistory.remove(at: 0)
let accumulated = accumulatableReports[0 ..< numAccumulated]
let newHistory = Set(accumulated.map(\.packageSpecification.workPackageHash))
state.accumulationHistory[state.accumulationHistory.array.count - 1] = newHistory
let newHistoryItem = Set(accumulated.map(\.packageSpecification.workPackageHash))
for i in 0 ..< config.value.epochLength {
if i == config.value.epochLength - 1 {
state.accumulationHistory[i] = newHistoryItem
} else {
state.accumulationHistory[i] = state.accumulationHistory[i + 1]
}
}

// update accumulation queue
for i in 0 ..< config.value.epochLength {
let queueIdx = (curIndex - i) % config.value.epochLength
let queueIdx = (curIndex - i) %% config.value.epochLength
if i == 0 {
state.editAccumulatedItems(items: &newQueueItems, accumulatedPackages: newHistory)
state.editAccumulatedItems(items: &newQueueItems, accumulatedPackages: newHistoryItem)
state.accumulationQueue[queueIdx] = newQueueItems
} else if i >= 1, i < state.timeslot - prevTimeslot {
state.accumulationQueue[queueIdx] = []
} else {
state.editAccumulatedItems(items: &state.accumulationQueue[queueIdx], accumulatedPackages: newHistory)
state.editAccumulatedItems(items: &state.accumulationQueue[queueIdx], accumulatedPackages: newHistoryItem)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Utils/Sources/Utils/Extensions/Int+Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
infix operator %%

extension Int {
public static func %% (_ left: Int, _ right: Int) -> Int {
if left >= 0 { return left % right }
if left >= -right { return left + right }
return ((left % right) + right) % right
}
}
16 changes: 16 additions & 0 deletions Utils/Tests/UtilsTests/Extensions/IntTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Testing

@testable import Utils

struct IntUtilsTests {
@Test func mod() throws {
#expect((1 %% 5) == 1)
#expect((0 %% 5) == 0)
#expect((-1 %% 5) == 4)
#expect((5 %% 3) == 2)
#expect((-5 %% 3) == 1)
#expect((-1 %% 3) == 2)
#expect((-10 %% 3) == 2)
#expect((-10 %% -3) == -1)
}
}

0 comments on commit a539a29

Please sign in to comment.