From a539a2917741fb0079bb0f808cd81919992236a6 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Mon, 11 Nov 2024 10:51:26 +0800 Subject: [PATCH] fix --- .../Blockchain/RuntimeProtocols/Runtime.swift | 17 +++++++++++------ Utils/Sources/Utils/Extensions/Int+Utils.swift | 9 +++++++++ .../Tests/UtilsTests/Extensions/IntTests.swift | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 Utils/Sources/Utils/Extensions/Int+Utils.swift create mode 100644 Utils/Tests/UtilsTests/Extensions/IntTests.swift diff --git a/Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift b/Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift index fc90465a..eb50be6f 100644 --- a/Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift +++ b/Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift @@ -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) } } } diff --git a/Utils/Sources/Utils/Extensions/Int+Utils.swift b/Utils/Sources/Utils/Extensions/Int+Utils.swift new file mode 100644 index 00000000..366f75cc --- /dev/null +++ b/Utils/Sources/Utils/Extensions/Int+Utils.swift @@ -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 + } +} diff --git a/Utils/Tests/UtilsTests/Extensions/IntTests.swift b/Utils/Tests/UtilsTests/Extensions/IntTests.swift new file mode 100644 index 00000000..dbe6db04 --- /dev/null +++ b/Utils/Tests/UtilsTests/Extensions/IntTests.swift @@ -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) + } +}