Skip to content

Commit 16b9fbc

Browse files
authored
integrate and pass latest tests (#337)
* update submodules * remove jamixir * fix path * fix codec test * fix stats tests * update jamtestvectors * fix a few tests * fix encoding issue * comment out some test * update submodule * various fix and new logs * fix log * various fix * update submodules * pass fallback and safrole trace * various fix and comformance * fix decoder tests * service accounts fix * encode data fix * fix sbrk and int32 usage * fix info * fix memory test * fix accumulate stats * fix state trie * fix footprint * fix preimage update * fix some host calls * update trace tests * pass trace tests
1 parent e2bd8c4 commit 16b9fbc

File tree

67 files changed

+1162
-441
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1162
-441
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
[submodule "JAMTests/jamtestvectors"]
22
path = JAMTests/jamtestvectors
33
url = https://github.com/open-web3-stack/jamtestvectors.git
4-
[submodule "JAMTests/jamixir"]
5-
path = JAMTests/jamixir
6-
url = https://github.com/jamixir/jamtestnet.git
74
[submodule "JAMTests/javajam"]
85
path = JAMTests/javajam
96
url = https://github.com/javajamio/javajam-trace.git

Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,20 +727,20 @@ extension ProtocolConfig {
727727
UInt16(recentHistorySize),
728728
UInt16(maxWorkItems),
729729
UInt16(maxDepsInWorkReport),
730+
UInt16(maxTicketsPerExtrinsic),
730731
UInt32(maxLookupAnchorAge),
732+
UInt16(ticketEntriesPerValidator),
731733
UInt16(maxAuthorizationsPoolItems),
732734
UInt16(slotPeriodSeconds),
733735
UInt16(maxAuthorizationsQueueItems),
734736
UInt16(coreAssignmentRotationPeriod),
735-
UInt16(maxAccumulationQueueItems),
736737
UInt16(maxWorkPackageExtrinsics),
737738
UInt16(preimageReplacementPeriod),
738739
UInt16(totalNumberOfValidators),
739740
UInt32(maxIsAuthorizedCodeSize),
740741
UInt32(maxEncodedWorkPackageSize),
741742
UInt32(maxServiceCodeSize),
742743
UInt32(erasureCodedPieceSize),
743-
UInt32(segmentSize),
744744
UInt32(maxWorkPackageImports),
745745
UInt32(erasureCodedSegmentSize),
746746
UInt32(maxWorkReportBlobSize),

Blockchain/Sources/Blockchain/RuntimeProtocols/AccumulateFunction.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Codec
12
import Foundation
23
import Utils
34

@@ -9,14 +10,14 @@ public struct OperandTuple: Codable {
910
public var segmentRoot: Data32
1011
/// a
1112
public var authorizerHash: Data32
12-
/// o
13-
public var authorizerTrace: Data
1413
/// y
1514
public var payloadHash: Data32
1615
/// g
17-
public var gasLimit: Gas
16+
@CodingAs<Compact<Gas>> public var gasLimit: Gas
1817
/// d
1918
public var workResult: WorkResult
19+
/// o
20+
public var authorizerTrace: Data
2021
}
2122

2223
public struct DeferredTransfers: Codable {

Blockchain/Sources/Blockchain/RuntimeProtocols/Accumulation.swift

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public struct AccumulationOutput {
3939
public var state: AccumulateState
4040
public var transfers: [DeferredTransfers]
4141
public var commitments: Set<Commitment>
42-
public var gasUsed: [(seriveIndex: ServiceIndex, gas: Gas)]
42+
public var gasUsed: [(serviceIndex: ServiceIndex, gas: Gas)]
4343
}
4444

4545
/// parallelized accumulation function ∆* output
4646
public struct ParallelAccumulationOutput {
4747
public var state: AccumulateState
4848
public var transfers: [DeferredTransfers]
4949
public var commitments: Set<Commitment>
50-
public var gasUsed: [(seriveIndex: ServiceIndex, gas: Gas)]
50+
public var gasUsed: [(serviceIndex: ServiceIndex, gas: Gas)]
5151
}
5252

5353
/// single-service accumulation function ∆1 output
@@ -79,7 +79,7 @@ public struct AccumulationResult {
7979
public struct AccountChanges {
8080
public var newAccounts: [ServiceIndex: ServiceAccount]
8181
public var altered: Set<ServiceIndex>
82-
public var alterations: [(ServiceAccountsMutRef) -> Void]
82+
public var alterations: [(ServiceAccountsMutRef) async throws -> Void]
8383
public var removed: Set<ServiceIndex>
8484

8585
public init() {
@@ -89,20 +89,20 @@ public struct AccountChanges {
8989
removed = []
9090
}
9191

92-
public mutating func addAlteration(index: ServiceIndex, _ alteration: @escaping (ServiceAccountsMutRef) -> Void) {
92+
public mutating func addAlteration(index: ServiceIndex, _ alteration: @escaping (ServiceAccountsMutRef) async throws -> Void) {
9393
alterations.append(alteration)
9494
altered.insert(index)
9595
}
9696

97-
public func apply(to accounts: ServiceAccountsMutRef) {
97+
public func apply(to accounts: ServiceAccountsMutRef) async throws {
9898
for (index, account) in newAccounts {
99-
accounts.addNew(serviceAccount: index, account: account)
99+
try await accounts.addNew(serviceAccount: index, account: account)
100100
}
101101
for index in removed {
102102
accounts.remove(serviceAccount: index)
103103
}
104104
for alteration in alterations {
105-
alteration(accounts)
105+
try await alteration(accounts)
106106
}
107107
}
108108

@@ -112,7 +112,7 @@ public struct AccountChanges {
112112
throw .duplicatedNewService
113113
}
114114
guard altered.isDisjoint(with: other.altered) else {
115-
logger.debug("altered accounts have duplicates, self: \(altered), other: \(other.altered)")
115+
logger.debug("same service being altered in parallel, self: \(altered), other: \(other.altered)")
116116
throw .duplicatedContributionToService
117117
}
118118
guard removed.isDisjoint(with: other.removed) else {
@@ -170,10 +170,10 @@ extension Accumulation {
170170
packageHash: report.packageSpecification.workPackageHash,
171171
segmentRoot: report.packageSpecification.segmentRoot,
172172
authorizerHash: report.authorizerHash,
173-
authorizerTrace: report.authorizerTrace,
174173
payloadHash: digest.payloadHash,
175174
gasLimit: digest.gasLimit,
176175
workResult: digest.result,
176+
authorizerTrace: report.authorizerTrace,
177177
))
178178
}
179179
}
@@ -203,7 +203,7 @@ extension Accumulation {
203203
timeslot: TimeslotIndex
204204
) async throws -> ParallelAccumulationOutput {
205205
var services = [ServiceIndex]()
206-
var gasUsed: [(seriveIndex: ServiceIndex, gas: Gas)] = []
206+
var gasUsed: [(serviceIndex: ServiceIndex, gas: Gas)] = []
207207
var transfers: [DeferredTransfers] = []
208208
var commitments = Set<Commitment>()
209209
var newPrivilegedServices: PrivilegedServices?
@@ -234,7 +234,7 @@ extension Accumulation {
234234
var accountsRef = ServiceAccountsMutRef(state.accounts.value)
235235
var servicePreimageSet = Set<ServicePreimagePair>()
236236

237-
for service in services {
237+
for service in Set(services) {
238238
let singleOutput = try await singleAccumulate(
239239
config: config,
240240
state: AccumulateState(
@@ -376,7 +376,12 @@ extension Accumulation {
376376
}
377377

378378
if preimageInfo.isEmpty {
379-
accounts.set(serviceAccount: serviceIndex, preimageHash: preimageHash, length: UInt32(preimage.count), value: [timeslot])
379+
try await accounts.set(
380+
serviceAccount: serviceIndex,
381+
preimageHash: preimageHash,
382+
length: UInt32(preimage.count),
383+
value: [timeslot]
384+
)
380385
accounts.set(serviceAccount: serviceIndex, preimageHash: preimageHash, value: preimage)
381386
}
382387
}
@@ -527,7 +532,7 @@ extension Accumulation {
527532
transfersStats[service] = (count, gasUsed)
528533
}
529534

530-
self = accountsMutRef.value as! Self
535+
self = accumulateOutput.state.accounts.value as! Self
531536

532537
// update accumulation history
533538
let accumulated = accumulatableReports[0 ..< accumulateOutput.numAccumulated]
@@ -570,14 +575,13 @@ extension Accumulation {
570575
for (service, _) in accumulateOutput.gasUsed {
571576
if accumulateStats[service] != nil { continue }
572577

573-
let num = accumulated.filter { report in
574-
report.digests.contains { $0.serviceIndex == service }
575-
}.count
578+
let digests = accumulated.compactMap(\.digests).flatMap { $0 }
579+
let num = digests.filter { $0.serviceIndex == service }.count
576580

577581
if num == 0 { continue }
578582

579583
let gasUsed = accumulateOutput.gasUsed
580-
.filter { $0.seriveIndex == service }
584+
.filter { $0.serviceIndex == service }
581585
.reduce(Gas(0)) { $0 + $1.gas }
582586

583587
accumulateStats[service] = (gasUsed, UInt32(num))

Blockchain/Sources/Blockchain/RuntimeProtocols/ActivityStatistics.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ extension ActivityStatistics {
6161
config: config,
6262
defaultValue: .dummy(config: config)
6363
)
64-
var serviceStats = [UInt: Statistics.Service]()
64+
var serviceStats = [UInt32: Statistics.Service]()
6565
for index in indices {
66-
serviceStats[UInt(index)] = .dummy(config: config)
66+
serviceStats[UInt32(index)] = .dummy(config: config)
6767
}
6868

6969
for guaranteeItem in extrinsic.reports.guarantees {
@@ -77,7 +77,7 @@ extension ActivityStatistics {
7777
coreStats[index].extrinsicsCount += digest.extrinsicsCount
7878
coreStats[index].extrinsicsSize += digest.extrinsicsSize
7979

80-
let serviceIndex = UInt(digest.serviceIndex)
80+
let serviceIndex = UInt32(digest.serviceIndex)
8181
serviceStats[serviceIndex]!.importsCount += digest.importsCount
8282
serviceStats[serviceIndex]!.exportsCount += digest.exportsCount
8383
serviceStats[serviceIndex]!.extrinsicsCount += digest.extrinsicsCount
@@ -97,17 +97,17 @@ extension ActivityStatistics {
9797
}
9898
}
9999
for preimageItem in extrinsic.preimages.preimages {
100-
let index = UInt(preimageItem.serviceIndex)
100+
let index = UInt32(preimageItem.serviceIndex)
101101
serviceStats[index]!.preimages.count += 1
102102
serviceStats[index]!.preimages.size += UInt(preimageItem.data.count)
103103
}
104104
for accumulateItem in accumulateStats {
105-
let index = UInt(accumulateItem.key)
105+
let index = UInt32(accumulateItem.key)
106106
serviceStats[index]!.accumulates.count += UInt(accumulateItem.value.1)
107107
serviceStats[index]!.accumulates.gasUsed += UInt(accumulateItem.value.0.value)
108108
}
109109
for transferItem in transfersStats {
110-
let index = UInt(transferItem.key)
110+
let index = UInt32(transferItem.key)
111111
serviceStats[index]!.transfers.count += UInt(transferItem.value.0)
112112
serviceStats[index]!.transfers.gasUsed += UInt(transferItem.value.1.value)
113113
}

Blockchain/Sources/Blockchain/RuntimeProtocols/Guaranteeing.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public protocol Guaranteeing {
4949
ProtocolConfig.EpochLength
5050
> { get }
5151

52-
func serviceAccount(index: ServiceIndex) -> ServiceAccountDetails?
52+
func serviceAccount(index: ServiceIndex) async throws -> ServiceAccountDetails?
5353
}
5454

5555
extension Guaranteeing {
@@ -89,7 +89,7 @@ extension Guaranteeing {
8989
config: ProtocolConfigRef,
9090
timeslot: TimeslotIndex,
9191
extrinsic: ExtrinsicGuarantees
92-
) throws(GuaranteeingError) -> (
92+
) async throws(GuaranteeingError) -> (
9393
newReports: ConfigFixedSizeArray<
9494
ReportItem?,
9595
ProtocolConfig.TotalNumberOfCores
@@ -109,9 +109,9 @@ extension Guaranteeing {
109109
let previousCoreAssignment = getCoreAssignment(
110110
config: config,
111111
randomness: previousRandomness,
112-
timeslot: timeslot - coreAssignmentRotationPeriod
112+
timeslot: UInt32(max(0, Int(timeslot) - Int(coreAssignmentRotationPeriod)))
113113
)
114-
let pareviousCoreKeys = withoutOffenders(keys: previousValidators.map(\.ed25519))
114+
let previousCoreKeys = withoutOffenders(keys: previousValidators.map(\.ed25519))
115115

116116
var workPackageHashes = Set<Data32>()
117117

@@ -131,12 +131,12 @@ extension Guaranteeing {
131131

132132
for credential in guarantee.credential {
133133
let isCurrent = (guarantee.timeslot / coreAssignmentRotationPeriod) == (timeslot / coreAssignmentRotationPeriod)
134-
let keys = isCurrent ? currentCoreKeys : pareviousCoreKeys
134+
let keys = isCurrent ? currentCoreKeys : previousCoreKeys
135135
let key = keys[Int(credential.index)]
136136
let reportHash = report.hash()
137137
workPackageHashes.insert(report.packageSpecification.workPackageHash)
138138
let payload = SigningContext.guarantee + reportHash.data
139-
let pubkey = try Result { try Ed25519.PublicKey(from: key) }
139+
let pubkey = try Result(catching: { try Ed25519.PublicKey(from: key) })
140140
.mapError { _ in GuaranteeingError.invalidPublicKey }
141141
.get()
142142
guard pubkey.verify(signature: credential.signature, message: payload) else {
@@ -164,7 +164,7 @@ extension Guaranteeing {
164164
}
165165

166166
for digest in report.digests {
167-
guard let acc = serviceAccount(index: digest.serviceIndex) else {
167+
guard let acc = try? await serviceAccount(index: digest.serviceIndex) else {
168168
throw .invalidServiceIndex
169169
}
170170

Blockchain/Sources/Blockchain/RuntimeProtocols/Preimages.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ public enum PreimagesError: Error {
55
case preimagesNotSorted
66
case duplicatedPreimage
77
case invalidServiceIndex
8-
case preimageNotUsed
8+
case preimageNotSolicited
9+
case preimageIsProvided
910
}
1011

1112
public struct PreimageUpdate: Sendable, Equatable {
@@ -37,7 +38,7 @@ public protocol Preimages {
3738
func get(serviceAccount index: ServiceIndex, preimageHash hash: Data32, length: UInt32) async throws
3839
-> LimitedSizeArray<TimeslotIndex, ConstInt0, ConstInt3>?
3940

40-
mutating func mergeWith(postState: PreimagesPostState)
41+
mutating func mergeWith(postState: PreimagesPostState) async throws
4142
}
4243

4344
extension Preimages {
@@ -65,9 +66,13 @@ extension Preimages {
6566
throw PreimagesError.duplicatedPreimage
6667
}
6768

68-
let isRequested = try? await get(serviceAccount: preimage.serviceIndex, preimageHash: hash, length: UInt32(preimage.data.count))
69-
guard isRequested != nil else {
70-
throw PreimagesError.preimageNotUsed
69+
let requested = try? await get(serviceAccount: preimage.serviceIndex, preimageHash: hash, length: UInt32(preimage.data.count))
70+
guard let requested else {
71+
throw PreimagesError.preimageNotSolicited
72+
}
73+
74+
guard requested.isEmpty else {
75+
throw PreimagesError.preimageIsProvided
7176
}
7277

7378
updates.append(PreimageUpdate(

Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public final class Runtime {
172172
try updateDisputes(block: block, state: &newState)
173173

174174
// depends on Safrole and Disputes
175-
let availableReports = try updateReports(block: block, state: &newState)
175+
let availableReports = try await updateAssurances(block: block, state: &newState)
176176

177177
// accumulate
178178
let (accumulateRoot, accumulateStats, transfersStats) = try await newState.update(
@@ -183,6 +183,14 @@ public final class Runtime {
183183
entropy: newState.entropyPool.t0
184184
)
185185

186+
newState.recentHistory.updatePartial(parentStateRoot: block.header.priorStateRoot)
187+
188+
try await updateGuarantees(block: block, state: &newState)
189+
190+
// after reports as it need old recent history
191+
try updateRecentHistory(block: block, state: &newState, accumulateRoot: accumulateRoot)
192+
193+
// update authorization pool and queue
186194
do {
187195
let authorizationResult = try newState.update(
188196
config: config,
@@ -206,9 +214,6 @@ public final class Runtime {
206214
transfersStats: transfersStats
207215
)
208216

209-
// after reports as it need old recent history
210-
try updateRecentHistory(block: block, state: &newState, accumulateRoot: accumulateRoot)
211-
212217
try await newState.save()
213218
} catch let error as Error {
214219
throw error
@@ -230,7 +235,6 @@ public final class Runtime {
230235
) })
231236
newState.recentHistory.update(
232237
headerHash: block.hash,
233-
parentStateRoot: block.header.priorStateRoot,
234238
accumulateRoot: accumulateRoot,
235239
lookup: lookup
236240
)
@@ -265,7 +269,7 @@ public final class Runtime {
265269
}
266270

267271
// returns available reports
268-
public func updateReports(block: BlockRef, state newState: inout State) throws -> [WorkReport] {
272+
public func updateAssurances(block: BlockRef, state newState: inout State) async throws -> [WorkReport] {
269273
let (
270274
newReports: newReports, availableReports: availableReports
271275
) = try newState.update(
@@ -276,19 +280,20 @@ public final class Runtime {
276280
)
277281

278282
newState.reports = newReports
279-
let result = try newState.update(
283+
return availableReports
284+
}
285+
286+
public func updateGuarantees(block: BlockRef, state newState: inout State) async throws {
287+
let result = try await newState.update(
280288
config: config, timeslot: newState.timeslot, extrinsic: block.extrinsic.reports
281289
)
282-
283290
newState.reports = result.newReports
284-
285-
return availableReports
286291
}
287292

288293
public func updatePreimages(block: BlockRef, state newState: inout State) async throws {
289294
let res = try await newState.updatePreimages(
290295
config: config, timeslot: newState.timeslot, preimages: block.extrinsic.preimages
291296
)
292-
newState.mergeWith(postState: res)
297+
try await newState.mergeWith(postState: res)
293298
}
294299
}

0 commit comments

Comments
 (0)