-
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
188 additions
and
41 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
Blockchain/Sources/Blockchain/RuntimeProtocols/Authorization.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,92 @@ | ||
import Utils | ||
|
||
public enum AuthorizationError: Error { | ||
case invalidReportAuthorizer | ||
case emptyAuthorizationQueue | ||
} | ||
|
||
public struct AuthorizationPostState: Sendable, Equatable { | ||
public var coreAuthorizationPool: ConfigFixedSizeArray< | ||
ConfigLimitedSizeArray< | ||
Data32, | ||
ProtocolConfig.Int0, | ||
ProtocolConfig.MaxAuthorizationsPoolItems | ||
>, | ||
ProtocolConfig.TotalNumberOfCores | ||
> | ||
|
||
public init( | ||
coreAuthorizationPool: ConfigFixedSizeArray< | ||
ConfigLimitedSizeArray< | ||
Data32, | ||
ProtocolConfig.Int0, | ||
ProtocolConfig.MaxAuthorizationsPoolItems | ||
>, | ||
ProtocolConfig.TotalNumberOfCores | ||
> | ||
) { | ||
self.coreAuthorizationPool = coreAuthorizationPool | ||
} | ||
} | ||
|
||
public protocol Authorization { | ||
var coreAuthorizationPool: ConfigFixedSizeArray< | ||
ConfigLimitedSizeArray< | ||
Data32, | ||
ProtocolConfig.Int0, | ||
ProtocolConfig.MaxAuthorizationsPoolItems | ||
>, | ||
ProtocolConfig.TotalNumberOfCores | ||
> { get } | ||
|
||
var authorizationQueue: ConfigFixedSizeArray< | ||
ConfigFixedSizeArray< | ||
Data32, | ||
ProtocolConfig.MaxAuthorizationsQueueItems | ||
>, | ||
ProtocolConfig.TotalNumberOfCores | ||
> { get } | ||
|
||
mutating func mergeWith(postState: AuthorizationPostState) | ||
} | ||
|
||
extension Authorization { | ||
public func update( | ||
config _: ProtocolConfigRef, | ||
timeslot: TimeslotIndex, | ||
auths: [(core: CoreIndex, auth: Data32)] | ||
) throws -> AuthorizationPostState { | ||
var pool = coreAuthorizationPool | ||
|
||
// Create lookup for core authorizations | ||
let authsByCoreIndex = Dictionary(grouping: auths) { $0.core } | ||
|
||
for coreIndex in 0 ..< pool.count { | ||
var corePool = pool[coreIndex] | ||
let coreQueue = authorizationQueue[coreIndex] | ||
|
||
if coreQueue.isEmpty { | ||
continue | ||
} | ||
|
||
let newItem = coreQueue[Int(timeslot) % coreQueue.count] | ||
|
||
// Remove used authorizers from pool | ||
if let coreAuths = authsByCoreIndex[CoreIndex(coreIndex)] { | ||
for (_, auth) in coreAuths { | ||
if let idx = corePool.firstIndex(of: auth) { | ||
_ = try corePool.remove(at: idx) | ||
} else { | ||
throw AuthorizationError.invalidReportAuthorizer | ||
} | ||
} | ||
} | ||
|
||
// Add new item from queue | ||
corePool.safeAppend(newItem) | ||
pool[coreIndex] = corePool | ||
} | ||
|
||
return AuthorizationPostState(coreAuthorizationPool: pool) | ||
} | ||
} |
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,79 @@ | ||
import Blockchain | ||
import Codec | ||
import Foundation | ||
import Testing | ||
import Utils | ||
|
||
@testable import JAMTests | ||
|
||
struct CoreAuthorizer: Codable { | ||
var core: CoreIndex | ||
var auth: Data32 | ||
} | ||
|
||
struct AuthorizationsInput: Codable { | ||
var slot: TimeslotIndex | ||
var auths: [CoreAuthorizer] | ||
} | ||
|
||
struct AuthorizationsState: Equatable, Codable, Authorization { | ||
var coreAuthorizationPool: ConfigFixedSizeArray< | ||
ConfigLimitedSizeArray< | ||
Data32, | ||
ProtocolConfig.Int0, | ||
ProtocolConfig.MaxAuthorizationsPoolItems | ||
>, | ||
ProtocolConfig.TotalNumberOfCores | ||
> | ||
|
||
var authorizationQueue: ConfigFixedSizeArray< | ||
ConfigFixedSizeArray< | ||
Data32, | ||
ProtocolConfig.MaxAuthorizationsQueueItems | ||
>, | ||
ProtocolConfig.TotalNumberOfCores | ||
> | ||
|
||
mutating func mergeWith(postState: AuthorizationPostState) { | ||
coreAuthorizationPool = postState.coreAuthorizationPool | ||
} | ||
} | ||
|
||
struct AuthorizationsTestcase: Codable { | ||
var input: AuthorizationsInput | ||
var preState: AuthorizationsState | ||
var postState: AuthorizationsState | ||
} | ||
|
||
struct AuthorizationsTests { | ||
static func loadTests(variant: TestVariants) throws -> [Testcase] { | ||
try TestLoader.getTestcases(path: "authorizations/\(variant)", extension: "bin") | ||
} | ||
|
||
func authorizationsTests(_ testcase: Testcase, variant: TestVariants) throws { | ||
let config = variant.config | ||
let decoder = JamDecoder(data: testcase.data, config: config) | ||
let testcase = try decoder.decode(AuthorizationsTestcase.self) | ||
|
||
var state = testcase.preState | ||
let result = try state.update( | ||
config: config, | ||
timeslot: testcase.input.slot, | ||
auths: testcase.input.auths.map { ($0.core, $0.auth) } | ||
) | ||
|
||
state.mergeWith(postState: result) | ||
|
||
#expect(state == testcase.postState) | ||
} | ||
|
||
@Test(arguments: try AuthorizationsTests.loadTests(variant: .tiny)) | ||
func tinyTests(_ testcase: Testcase) throws { | ||
try authorizationsTests(testcase, variant: .tiny) | ||
} | ||
|
||
@Test(arguments: try AuthorizationsTests.loadTests(variant: .full)) | ||
func fullTests(_ testcase: Testcase) throws { | ||
try authorizationsTests(testcase, variant: .full) | ||
} | ||
} |