Skip to content

Commit 64f27c6

Browse files
committed
various fix and comformance
1 parent 9ee2f3a commit 64f27c6

File tree

12 files changed

+100
-89
lines changed

12 files changed

+100
-89
lines changed

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: 6 additions & 6 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
@@ -111,7 +111,7 @@ extension Guaranteeing {
111111
randomness: previousRandomness,
112112
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/Runtime.swift

Lines changed: 15 additions & 10 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,13 +280,14 @@ 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 {

Blockchain/Sources/Blockchain/State/State.swift

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -166,50 +166,6 @@ public struct State: Sendable {
166166
}
167167
}
168168

169-
// δ: The (prior) state of the service accounts.
170-
public subscript(serviceAccount index: ServiceIndex) -> StateKeys.ServiceAccountKey.Value? {
171-
get {
172-
layer[serviceAccount: index]
173-
}
174-
set {
175-
layer[serviceAccount: index] = newValue
176-
}
177-
}
178-
179-
// s
180-
public subscript(serviceAccount index: ServiceIndex, storageKey key: Data32) -> StateKeys.ServiceAccountStorageKey.Value? {
181-
get {
182-
layer[serviceAccount: index, storageKey: key]
183-
}
184-
set {
185-
layer[serviceAccount: index, storageKey: key] = newValue
186-
}
187-
}
188-
189-
// p
190-
public subscript(
191-
serviceAccount index: ServiceIndex, preimageHash hash: Data32
192-
) -> StateKeys.ServiceAccountPreimagesKey.Value? {
193-
get {
194-
layer[serviceAccount: index, preimageHash: hash]
195-
}
196-
set {
197-
layer[serviceAccount: index, preimageHash: hash] = newValue
198-
}
199-
}
200-
201-
// l
202-
public subscript(
203-
serviceAccount index: ServiceIndex, preimageHash hash: Data32, length length: UInt32
204-
) -> StateKeys.ServiceAccountPreimageInfoKey.Value? {
205-
get {
206-
layer[serviceAccount: index, preimageHash: hash, length: length]
207-
}
208-
set {
209-
layer[serviceAccount: index, preimageHash: hash, length: length] = newValue
210-
}
211-
}
212-
213169
public mutating func load(keys: [any StateKey]) async throws {
214170
let pairs = try await backend.batchRead(keys)
215171
for (key, value) in pairs {
@@ -502,8 +458,8 @@ extension State: Guaranteeing {
502458
judgements.punishSet
503459
}
504460

505-
public func serviceAccount(index: ServiceIndex) -> ServiceAccountDetails? {
506-
self[serviceAccount: index] ?? nil
461+
public func serviceAccount(index: ServiceIndex) async throws -> ServiceAccountDetails? {
462+
try await get(serviceAccount: index)
507463
}
508464
}
509465

@@ -518,9 +474,17 @@ extension State: ActivityStatistics {}
518474
extension State: Preimages {
519475
public mutating func mergeWith(postState: PreimagesPostState) {
520476
for update in postState.updates {
521-
self[serviceAccount: update.serviceIndex, preimageHash: update.hash] = update.data
522-
self[serviceAccount: update.serviceIndex, preimageHash: update.hash, length: update.length] =
523-
LimitedSizeArray([update.timeslot])
477+
set(
478+
serviceAccount: update.serviceIndex,
479+
preimageHash: update.hash,
480+
value: update.data
481+
)
482+
set(
483+
serviceAccount: update.serviceIndex,
484+
preimageHash: update.hash,
485+
length: update.length,
486+
value: StateKeys.ServiceAccountPreimageInfoKey.Value([update.timeslot])
487+
)
524488
}
525489
}
526490
}

Blockchain/Sources/Blockchain/Types/RecentHistory.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,20 @@ extension RecentHistory: Dummy {
5151
}
5252

5353
extension RecentHistory {
54-
public mutating func update(
55-
headerHash: Data32,
54+
public mutating func updatePartial(
5655
parentStateRoot: Data32,
57-
accumulateRoot: Data32,
58-
lookup: [Data32: Data32]
5956
) {
6057
if items.count > 0 { // if this is not block #0
6158
// write the state root of last block
6259
items[items.endIndex - 1].stateRoot = parentStateRoot
6360
}
61+
}
6462

63+
public mutating func update(
64+
headerHash: Data32,
65+
accumulateRoot: Data32,
66+
lookup: [Data32: Data32]
67+
) {
6568
var mmr = items.last?.mmr ?? .init([])
6669

6770
mmr.append(accumulateRoot, hasher: Keccak.self)

Blockchain/Sources/Blockchain/Types/Statistics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public struct Statistics: Sendable, Equatable, Codable {
103103
public var core: ConfigFixedSizeArray<Core, ProtocolConfig.TotalNumberOfCores>
104104

105105
// service statistics
106-
@CodingAs<SortedKeyValues<UInt, Service>> public var service: [UInt: Service]
106+
@CodingAs<SortedKeyValues<UInt32, Service>> public var service: [UInt32: Service]
107107
}
108108

109109
extension Statistics: Dummy {

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public class Info: HostCall {
401401
m = nil
402402
}
403403

404-
if !state.isMemoryWritable(address: o, length: Int(m!.count)) {
404+
if let m, !state.isMemoryWritable(address: o, length: Int(m.count)) {
405405
throw VMInvocationsError.panic
406406
} else if m == nil {
407407
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.NONE.rawValue)

Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public final class GuaranteeingService: ServiceBase2, @unchecked Sendable, OnBef
440440
let state = try await dataProvider.getState(hash: head.hash)
441441

442442
// Verify the service account exists
443-
guard let serviceAccount = state.value.serviceAccount(index: workPackage.authorizationServiceIndex) else {
443+
guard let serviceAccount = try? await state.value.serviceAccount(index: workPackage.authorizationServiceIndex) else {
444444
logger.debug("Service account does not exist",
445445
metadata: ["serviceIndex": "\(workPackage.authorizationServiceIndex)"])
446446
throw GuaranteeingServiceError.serviceAccountNotFound

Codec/Sources/Codec/DataInput.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public protocol DataInput {
55
/// throw when no more data
66
mutating func read(length: Int) throws -> Data
77

8+
mutating func readAll() throws -> Data
9+
810
var isEmpty: Bool { get }
911
}
1012

@@ -13,6 +15,10 @@ extension DataInput {
1315
try read(length: 1).first!
1416
}
1517

18+
public mutating func readAll() throws -> Data {
19+
try readAll()
20+
}
21+
1622
public mutating func decodeUInt64() throws -> UInt64 {
1723
// TODO: improve this by use `read(minLength: 8)` to avoid read byte by byte
1824
let res = try IntegerCodec.decode { try self.read() }
@@ -42,4 +48,10 @@ extension Data: DataInput {
4248
self = self[startIndex + length ..< endIndex]
4349
return res
4450
}
51+
52+
public mutating func readAll() throws -> Data {
53+
let res = self
54+
self = Data()
55+
return res
56+
}
4557
}

Codec/Sources/Codec/JamDecoder.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ extension Optional: OptionalWrapper where Wrapped: Decodable {
6767
}
6868
}
6969

70+
/// A default coding key used for types not belong to trivial encoding types
71+
private struct DefaultKey: CodingKey {
72+
let stringValue: String
73+
let intValue: Int? = nil
74+
75+
init?(stringValue: String) {
76+
self.stringValue = stringValue
77+
}
78+
79+
init?(intValue _: Int) { nil }
80+
81+
init(for type: (some Any).Type) {
82+
stringValue = "<\(type)>"
83+
}
84+
}
85+
7086
private class DecodeContext: Decoder {
7187
struct PushCodingPath: ~Copyable {
7288
let decoder: DecodeContext
@@ -134,12 +150,16 @@ private class DecodeContext: Decoder {
134150
}
135151

136152
fileprivate func decodeData(codingPath: @autoclosure () -> [CodingKey]) throws -> Data {
153+
let path = codingPath()
154+
if path.isEmpty {
155+
return try input.readAll()
156+
}
137157
let length = try input.decodeUInt64()
138158
// sanity check: length must be less than 4gb
139159
guard length < 0x1_0000_0000 else {
140160
throw DecodingError.dataCorrupted(
141161
DecodingError.Context(
142-
codingPath: codingPath(),
162+
codingPath: path,
143163
debugDescription: "Invalid data length"
144164
)
145165
)
@@ -149,12 +169,16 @@ private class DecodeContext: Decoder {
149169
}
150170

151171
fileprivate func decodeData(codingPath: @autoclosure () -> [CodingKey]) throws -> [UInt8] {
172+
let path = codingPath()
173+
if path.isEmpty {
174+
return try [UInt8](input.readAll())
175+
}
152176
let length = try input.decodeUInt64()
153177
// sanity check: length must be less than 4gb
154178
guard length < 0x1_0000_0000 else {
155179
throw DecodingError.dataCorrupted(
156180
DecodingError.Context(
157-
codingPath: codingPath(),
181+
codingPath: path,
158182
debugDescription: "Invalid data length"
159183
)
160184
)
@@ -220,7 +244,8 @@ private class DecodeContext: Decoder {
220244
} else if let type = type as? any ArrayWrapper.Type {
221245
try decodeArray(type, key: key) as! T
222246
} else {
223-
try withExtendedLifetime(PushCodingPath(decoder: self, key: key)) {
247+
// ensure a coding key is present non trivial encoding types
248+
try withExtendedLifetime(PushCodingPath(decoder: self, key: key ?? DefaultKey(for: type))) {
224249
try .init(from: self)
225250
}
226251
}

0 commit comments

Comments
 (0)