Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recent history test #85

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 19 additions & 31 deletions Blockchain/Sources/Blockchain/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,9 @@ public final class Runtime {
var newState = prevState.value

do {
newState.recentHistory = try updateRecentHistory(block: block, state: prevState)
try updateRecentHistory(block: block, state: &newState)

let safroleResult = try newState.updateSafrole(
config: config, slot: block.header.timeslot, entropy: newState.entropyPool.t0, extrinsics: block.extrinsic.tickets
)
newState.mergeWith(postState: safroleResult.state)

guard safroleResult.epochMark == block.header.epoch else {
throw Error.invalidHeaderEpochMarker
}

guard safroleResult.ticketsMark == block.header.winningTickets else {
throw Error.invalidHeaderWinningTickets
}
try updateSafrole(block: block, state: &newState)

newState.coreAuthorizationPool = try updateAuthorizationPool(
block: block, state: prevState
Expand All @@ -106,30 +95,29 @@ public final class Runtime {
return StateRef(newState)
}

public func updateRecentHistory(block: BlockRef, state: StateRef) throws -> RecentHistory {
var history = state.value.recentHistory
if history.items.count >= 0 { // if this is not block #0
// write the state root of last block
history.items[history.items.endIndex - 1].stateRoot = state.stateRoot
}

let workReportHashes = block.extrinsic.reports.guarantees.map(\.workReport.packageSpecification.workPackageHash)
public func updateSafrole(block: BlockRef, state newState: inout State) throws {
let safroleResult = try newState.updateSafrole(
config: config, slot: block.header.timeslot, entropy: newState.entropyPool.t0, extrinsics: block.extrinsic.tickets
)
newState.mergeWith(postState: safroleResult.state)

let accumulationResult = Data32() // TODO: calculate accumulation result
guard safroleResult.epochMark == block.header.epoch else {
throw Error.invalidHeaderEpochMarker
}

var mmr = history.items.last?.mmr ?? .init([])
mmr.append(accumulationResult, hasher: Keccak.self)
guard safroleResult.ticketsMark == block.header.winningTickets else {
throw Error.invalidHeaderWinningTickets
}
}

let newItem = try RecentHistory.HistoryItem(
public func updateRecentHistory(block: BlockRef, state newState: inout State) throws {
let workReportHashes = block.extrinsic.reports.guarantees.map(\.workReport.packageSpecification.workPackageHash)
try newState.recentHistory.update(
headerHash: block.header.parentHash,
mmr: mmr,
stateRoot: Data32(), // empty and will be updated upon next block
parentStateRoot: block.header.priorStateRoot,
accumulateRoot: Data32(), // TODO: calculate accumulation result
workReportHashes: ConfigLimitedSizeArray(config: config, array: workReportHashes)
)

history.items.safeAppend(newItem)

return history
}

// TODO: add tests
Expand Down
26 changes: 26 additions & 0 deletions Blockchain/Sources/Blockchain/Types/RecentHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,29 @@ extension RecentHistory: Dummy {
RecentHistory(items: try! ConfigLimitedSizeArray(config: config))
}
}

extension RecentHistory {
public mutating func update(
headerHash: Data32,
parentStateRoot: Data32,
accumulateRoot: Data32,
workReportHashes: ConfigLimitedSizeArray<Data32, ProtocolConfig.Int0, ProtocolConfig.TotalNumberOfCores>
) {
if items.count > 0 { // if this is not block #0
// write the state root of last block
items[items.endIndex - 1].stateRoot = parentStateRoot
}

var mmr = items.last?.mmr ?? .init([])
mmr.append(accumulateRoot, hasher: Keccak.self)

let newItem = RecentHistory.HistoryItem(
headerHash: headerHash,
mmr: mmr,
stateRoot: Data32(), // empty and will be updated upon next block
workReportHashes: workReportHashes
)

items.safeAppend(newItem)
}
}
7 changes: 5 additions & 2 deletions Codec/Sources/Codec/JamDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ private struct JamUnkeyedDecodingContainer: UnkeyedDecodingContainer {
let decoder: DecodeContext

mutating func decodeNil() throws -> Bool {
guard let byte = decoder.data.next() else {
throw DecodingError.dataCorruptedError(in: self, debugDescription: "Unexpected end of data")
}
currentIndex += 1
return decoder.data.count > 0
return byte == 0
}

mutating func decode(_: Bool.Type) throws -> Bool {
Expand Down Expand Up @@ -439,7 +442,7 @@ private struct JamSingleValueDecodingContainer: SingleValueDecodingContainer {
let decoder: DecodeContext

func decodeNil() -> Bool {
decoder.data.count > 0
decoder.data.next() == 0
}

func decode(_: Bool.Type) throws -> Bool {
Expand Down
12 changes: 10 additions & 2 deletions JAMTests/Sources/JAMTests/TestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import Foundation
// somehow without this the GH Actions CI fails
extension Foundation.Bundle: @unchecked @retroactive Sendable {}

struct Testcase: CustomStringConvertible {
var description: String
var data: Data
}

enum TestLoader {
static func getTestFiles(path: String, extension ext: String) throws -> [(path: String, description: String)] {
static func getTestcases(path: String, extension ext: String) throws -> [Testcase] {
let prefix = Bundle.module.resourcePath! + "/jamtestvectors/\(path)"
let files = try FileManager.default.contentsOfDirectory(atPath: prefix)
var filtered = files.filter { $0.hasSuffix(".\(ext)") }
filtered.sort()
return filtered.map { (path: prefix + "/" + $0, description: $0) }
return try filtered.map {
let data = try Data(contentsOf: URL(fileURLWithPath: prefix + "/" + $0))
return Testcase(description: $0, data: data)
}
}
}
13 changes: 5 additions & 8 deletions JAMTests/Tests/JAMTests/PVMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,14 @@ struct PolkaVMTestcase: Codable, CustomStringConvertible {
}

struct PVMTests {
static func loadTests() throws -> [PolkaVMTestcase] {
let tests = try TestLoader.getTestFiles(path: "pvm/programs", extension: "json")
return try tests.map {
let data = try Data(contentsOf: URL(fileURLWithPath: $0.path))
let decoder = JSONDecoder()
return try decoder.decode(PolkaVMTestcase.self, from: data)
}
static func loadTests() throws -> [Testcase] {
try TestLoader.getTestcases(path: "pvm/programs", extension: "json")
}

@Test(arguments: try loadTests())
func testPVM(testCase: PolkaVMTestcase) throws {
func testPVM(testCase: Testcase) throws {
let decoder = JSONDecoder()
let testCase = try decoder.decode(PolkaVMTestcase.self, from: testCase.data)
let program = try ProgramCode(Data(testCase.program))
let memory = Memory(
pageMap: testCase.initialPageMap.map { (address: $0.address, length: $0.length, writable: $0.isWritable) },
Expand Down
44 changes: 44 additions & 0 deletions JAMTests/Tests/JAMTests/RecentHistoryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Blockchain
import Codec
import Foundation
import Testing
import Utils

@testable import JAMTests

struct RecentHistoryInput: Codable {
var headerHash: Data32
var parentStateRoot: Data32
var accumulateRoot: Data32
var workPackages: [Data32]
}

struct RecentHisoryTestcase: Codable {
var input: RecentHistoryInput
var preState: RecentHistory
var postState: RecentHistory
}

struct RecentHistoryTests {
static func loadTests() throws -> [Testcase] {
try TestLoader.getTestcases(path: "history/data", extension: "scale")
}

@Test(arguments: try loadTests())
func recentHistory(_ testcase: Testcase) throws {
let config = ProtocolConfigRef.mainnet
let testcase = try JamDecoder.decode(RecentHisoryTestcase.self, from: testcase.data, withConfig: config)

var state = testcase.preState
try state.update(
headerHash: testcase.input.headerHash,
parentStateRoot: testcase.input.parentStateRoot,
accumulateRoot: testcase.input.accumulateRoot,
workReportHashes: ConfigLimitedSizeArray(config: config, array: testcase.input.workPackages)
)

withKnownIssue("need to figure out why the hash generation doesn't match", isIntermittent: true) {
#expect(state == testcase.postState)
}
}
}
13 changes: 2 additions & 11 deletions JAMTests/Tests/JAMTests/SafroleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ struct SafroleTestcase: Codable {
var postState: SafroleState
}

struct Testcase: CustomStringConvertible {
var description: String
var data: Data
}

enum SafroleTestVariants: String, CaseIterable {
case tiny
case full
Expand All @@ -121,13 +116,9 @@ enum SafroleTestVariants: String, CaseIterable {
}
}

final class SafroleTests {
struct SafroleTests {
static func loadTests(variant: SafroleTestVariants) throws -> [Testcase] {
let tests = try TestLoader.getTestFiles(path: "safrole/\(variant)", extension: "scale")
return try tests.map { path, description in
let data = try Data(contentsOf: URL(fileURLWithPath: path))
return Testcase(description: description, data: data)
}
try TestLoader.getTestcases(path: "safrole/\(variant)", extension: "scale")
}

func safroleTests(_ input: Testcase, variant: SafroleTestVariants) throws {
Expand Down
13 changes: 5 additions & 8 deletions JAMTests/Tests/JAMTests/TrieTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ struct TrieElement: Codable {
typealias TrieTestCase = [TrieElement]

struct TrieTests {
static func loadTests() throws -> [TrieTestCase] {
let tests = try TestLoader.getTestFiles(path: "trie", extension: "json")
return try tests.map {
let data = try Data(contentsOf: URL(fileURLWithPath: $0.path))
let decoder = JSONDecoder()
return try decoder.decode(TrieTestCase.self, from: data)
}
static func loadTests() throws -> [Testcase] {
try TestLoader.getTestcases(path: "trie", extension: "json")
}

@Test(arguments: try loadTests())
func trieTests(_ testcase: TrieTestCase) throws {
func trieTests(_ testcase: Testcase) throws {
let decoder = JSONDecoder()
let testcase = try decoder.decode(TrieTestCase.self, from: testcase.data)
for element in testcase {
let kv = element.input.reduce(into: [Data32: Data]()) { result, entry in
let keyData = Data(fromHexString: entry.key)
Expand Down
2 changes: 1 addition & 1 deletion JAMTests/jamtestvectors
Loading