-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
40 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Blockchain/Sources/Blockchain/RuntimeProtocols/ActivityStatistics.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Foundation | ||
import Utils | ||
|
||
enum ActivityStatisticsError: Error { | ||
case invalidAuthorKey | ||
} | ||
|
||
public protocol ActivityStatistics { | ||
var activityStatistics: ValidatorActivityStatistics { get } | ||
var timeslot: TimeslotIndex { get } | ||
var currentValidators: ConfigFixedSizeArray<ValidatorKey, ProtocolConfig.TotalNumberOfValidators> { get } | ||
} | ||
|
||
extension ActivityStatistics { | ||
public func update( | ||
config: ProtocolConfigRef, | ||
newTimeslot: TimeslotIndex, | ||
extrinsic: Extrinsic, | ||
authorIndex: ValidatorIndex | ||
) throws -> ValidatorActivityStatistics { | ||
let epochLength = UInt32(config.value.epochLength) | ||
let currentEpoch = timeslot / epochLength | ||
let newEpoch = newTimeslot / epochLength | ||
let isEpochChange = currentEpoch != newEpoch | ||
|
||
var acc = try isEpochChange | ||
? ConfigFixedSizeArray<_, ProtocolConfig.TotalNumberOfValidators>( | ||
config: config, | ||
defaultValue: ValidatorActivityStatistics.StatisticsItem.dummy(config: config) | ||
) : activityStatistics.accumulator | ||
|
||
let prev = isEpochChange ? activityStatistics.accumulator : activityStatistics.previous | ||
|
||
var item = acc[authorIndex] | ||
item.blocks += 1 | ||
item.tickets += UInt32(extrinsic.tickets.tickets.count) | ||
item.preimages += UInt32(extrinsic.preimages.preimages.count) | ||
item.preimagesBytes += UInt32(extrinsic.preimages.preimages.reduce(into: 0) { $0 += $1.data.count }) | ||
acc[authorIndex] = item | ||
|
||
for report in extrinsic.reports.guarantees { | ||
for cred in report.credential { | ||
acc[cred.index].guarantees += 1 | ||
} | ||
} | ||
|
||
for assurance in extrinsic.availability.assurances { | ||
acc[assurance.validatorIndex].assurances += 1 | ||
} | ||
|
||
return ValidatorActivityStatistics( | ||
accumulator: acc, | ||
previous: prev | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Blockchain | ||
import Codec | ||
import Foundation | ||
import Testing | ||
import Utils | ||
|
||
@testable import JAMTests | ||
|
||
struct StatisticsState: Equatable, Codable, ActivityStatistics { | ||
var activityStatistics: ValidatorActivityStatistics | ||
var timeslot: TimeslotIndex | ||
var currentValidators: ConfigFixedSizeArray<ValidatorKey, ProtocolConfig.TotalNumberOfValidators> | ||
} | ||
|
||
struct StatisticsInput: Codable { | ||
var timeslot: TimeslotIndex | ||
var author: ValidatorIndex | ||
var extrinsic: Extrinsic | ||
} | ||
|
||
struct StatisticsTestcase: Codable { | ||
var input: StatisticsInput | ||
var preState: StatisticsState | ||
var postState: StatisticsState | ||
} | ||
|
||
struct StatisticsTests { | ||
static func loadTests(variant: TestVariants) throws -> [Testcase] { | ||
try TestLoader.getTestcases(path: "statistics/\(variant)", extension: "bin") | ||
} | ||
|
||
func statisticsTests(_ testcase: Testcase, variant: TestVariants) throws { | ||
let config = variant.config | ||
let decoder = JamDecoder(data: testcase.data, config: config) | ||
let testcase = try decoder.decode(StatisticsTestcase.self) | ||
|
||
var state = testcase.preState | ||
let result = try state.update( | ||
config: config, | ||
newTimeslot: testcase.input.timeslot, | ||
extrinsic: testcase.input.extrinsic, | ||
authorIndex: testcase.input.author | ||
) | ||
state.activityStatistics = result | ||
|
||
#expect(state == testcase.postState) | ||
} | ||
|
||
@Test(arguments: try StatisticsTests.loadTests(variant: .tiny)) | ||
func tinyTests(_ testcase: Testcase) throws { | ||
try statisticsTests(testcase, variant: .tiny) | ||
} | ||
|
||
@Test(arguments: try StatisticsTests.loadTests(variant: .full)) | ||
func fullTests(_ testcase: Testcase) throws { | ||
try statisticsTests(testcase, variant: .full) | ||
} | ||
} |