-
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
135 additions
and
24 deletions.
There are no files selected for viewing
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
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,84 @@ | ||
import Blockchain | ||
import Codec | ||
import Foundation | ||
import Testing | ||
import Utils | ||
|
||
@testable import JAMTests | ||
|
||
struct DisputesState: Equatable, Codable, Disputes { | ||
var judgements: JudgementsState | ||
var reports: ConfigFixedSizeArray< | ||
ReportItem?, | ||
ProtocolConfig.TotalNumberOfCores | ||
> | ||
var timeslot: TimeslotIndex | ||
var currentValidators: ConfigFixedSizeArray< | ||
ValidatorKey, ProtocolConfig.TotalNumberOfValidators | ||
> | ||
var previousValidators: ConfigFixedSizeArray< | ||
ValidatorKey, ProtocolConfig.TotalNumberOfValidators | ||
> | ||
|
||
mutating func mergeWith(postState: DisputesPostState) { | ||
judgements = postState.judgements | ||
reports = postState.reports | ||
} | ||
} | ||
|
||
struct DisputesTestcase: Codable { | ||
var input: ExtrinsicDisputes | ||
var preState: DisputesState | ||
var output: Either<[Ed25519PublicKey], UInt8> | ||
var postState: DisputesState | ||
} | ||
|
||
struct DisputesTests { | ||
static func loadTests(variant: TestVariants) throws -> [Testcase] { | ||
try TestLoader.getTestcases(path: "disputes/\(variant)", extension: "bin") | ||
} | ||
|
||
func disputesTests(_ testcase: Testcase, variant: TestVariants) throws { | ||
let config = variant.config | ||
let decoder = JamDecoder(data: testcase.data, config: config) | ||
let testcase = try decoder.decode(DisputesTestcase.self) | ||
|
||
var state = testcase.preState | ||
let result = Result { | ||
try testcase.input.validate(config: config) | ||
return try state.update( | ||
config: config, | ||
disputes: testcase.input | ||
) | ||
} | ||
switch result { | ||
case let .success((postState, offenders)): | ||
switch testcase.output { | ||
case let .left(output): | ||
state.mergeWith(postState: postState) | ||
#expect(state == testcase.postState) | ||
#expect(offenders == output) | ||
case .right: | ||
Issue.record("Expected error, got \(result)") | ||
} | ||
case .failure: | ||
switch testcase.output { | ||
case .left: | ||
Issue.record("Expected success, got \(result)") | ||
case .right: | ||
// ignore error code because it is unspecified | ||
break | ||
} | ||
} | ||
} | ||
|
||
@Test(arguments: try DisputesTests.loadTests(variant: .tiny)) | ||
func tinyTests(_ testcase: Testcase) throws { | ||
try disputesTests(testcase, variant: .tiny) | ||
} | ||
|
||
@Test(arguments: try DisputesTests.loadTests(variant: .full)) | ||
func fullTests(_ testcase: Testcase) throws { | ||
try disputesTests(testcase, variant: .full) | ||
} | ||
} |