Skip to content

Commit 354b830

Browse files
committed
test(w3f): update accumulate tests structures and encoding
- Refactor `PreimageStatusMapEntry` to `PreimageRequestMapEntry`, incorporating `length` into the key. - Update `Output` enum to support error codes and switch to unkeyed container encoding. - Modify `FullAccumulateState` to index preimage info by `HashAndLength`. - Adjust test setup and assertions to align with the updated data structures.
1 parent b9a9791 commit 354b830

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

JAMTests/Tests/JAMTests/w3f/AccumulateTests.swift

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ private struct PreimageMapEntry: Codable, Equatable {
1919
var blob: Data
2020
}
2121

22-
private struct PreimageStatusMapEntry: Codable, Equatable {
22+
private struct PreimageRequestKey: Codable, Equatable {
2323
var hash: Data32
24-
var status: StateKeys.ServiceAccountPreimageInfoKey.Value
24+
var length: DataLength
25+
}
26+
27+
private struct PreimageRequestMapEntry: Codable, Equatable {
28+
var key: PreimageRequestKey
29+
var value: StateKeys.ServiceAccountPreimageInfoKey.Value
2530
}
2631

2732
private struct StorageMapEntry: Codable, Equatable {
@@ -33,7 +38,7 @@ private struct Account: Codable, Equatable {
3338
var service: ServiceAccountDetails
3439
var storage: [StorageMapEntry]
3540
var preimagesBlobs: [PreimageMapEntry]
36-
var preimagesStatus: [PreimageStatusMapEntry]
41+
var preimageRequests: [PreimageRequestMapEntry]
3742
}
3843

3944
private struct AccountsMapEntry: Codable, Equatable {
@@ -64,29 +69,40 @@ private struct AccumulateState: Equatable, Codable {
6469

6570
private enum Output: Codable {
6671
case ok(Data32)
67-
case err
72+
case err(UInt8)
73+
74+
private enum CodingKeys: String, CodingKey {
75+
case ok
76+
case err
77+
}
6878

6979
init(from decoder: Decoder) throws {
70-
let container = try decoder.singleValueContainer()
71-
let val = try container.decode(UInt8.self)
72-
switch val {
80+
var container = try decoder.unkeyedContainer()
81+
let variant = try container.decode(UInt8.self)
82+
switch variant {
7383
case 0:
7484
self = try .ok(container.decode(Data32.self))
7585
case 1:
76-
self = .err
86+
self = try .err(container.decode(UInt8.self))
7787
default:
78-
throw DecodingError.dataCorruptedError(in: container, debugDescription: "invalid output")
88+
throw DecodingError.dataCorrupted(
89+
DecodingError.Context(
90+
codingPath: decoder.codingPath,
91+
debugDescription: "invalid output variant \(variant)"
92+
)
93+
)
7994
}
8095
}
8196

8297
func encode(to encoder: Encoder) throws {
83-
var container = encoder.singleValueContainer()
98+
var container = encoder.unkeyedContainer()
8499
switch self {
85100
case let .ok(data):
86-
try container.encode(0)
101+
try container.encode(UInt8(0))
87102
try container.encode(data)
88-
case .err:
89-
try container.encode(1)
103+
case let .err(code):
104+
try container.encode(UInt8(1))
105+
try container.encode(code)
90106
}
91107
}
92108
}
@@ -112,7 +128,7 @@ private struct FullAccumulateState: Accumulation {
112128
var accounts: [ServiceIndex: ServiceAccountDetails] = [:]
113129
var storages: [ServiceIndex: [Data: Data]] = [:]
114130
var preimages: [ServiceIndex: [Data32: Data]] = [:]
115-
var preimageInfo: [ServiceIndex: [Data32: StateKeys.ServiceAccountPreimageInfoKey.Value]] = [:]
131+
var preimageInfo: [ServiceIndex: [HashAndLength: StateKeys.ServiceAccountPreimageInfoKey.Value]] = [:]
116132

117133
func copy() -> ServiceAccounts {
118134
self
@@ -130,10 +146,12 @@ private struct FullAccumulateState: Accumulation {
130146
preimages[index]?[hash]
131147
}
132148

133-
func get(serviceAccount index: ServiceIndex, preimageHash hash: Data32, length _: UInt32) async throws -> StateKeys
134-
.ServiceAccountPreimageInfoKey.Value?
135-
{
136-
preimageInfo[index]?[hash]
149+
func get(
150+
serviceAccount index: ServiceIndex,
151+
preimageHash hash: Data32,
152+
length: UInt32
153+
) async throws -> StateKeys.ServiceAccountPreimageInfoKey.Value? {
154+
preimageInfo[index]?[HashAndLength(hash: hash, length: length)]
137155
}
138156

139157
func historicalLookup(serviceAccount _: ServiceIndex, timeslot _: TimeslotIndex, preimageHash _: Data32) async throws -> Data? {
@@ -165,14 +183,15 @@ private struct FullAccumulateState: Accumulation {
165183
length: UInt32,
166184
value: StateKeys.ServiceAccountPreimageInfoKey.Value?
167185
) {
186+
let key = HashAndLength(hash: hash, length: length)
168187
// update footprint
169-
let oldValue = preimageInfo[index]?[hash]
188+
let oldValue = preimageInfo[index]?[key]
170189
logger.debug("preimage footprint before: \(accounts[index]?.itemsCount ?? 0) items, \(accounts[index]?.totalByteLength ?? 0) bytes")
171190
accounts[index]?.updateFootprintPreimage(oldValue: oldValue, newValue: value, length: length)
172191
logger.debug("preimage footprint after: \(accounts[index]?.itemsCount ?? 0) items, \(accounts[index]?.totalByteLength ?? 0) bytes")
173192

174193
// update value
175-
preimageInfo[index, default: [:]][hash] = value
194+
preimageInfo[index, default: [:]][key] = value
176195
}
177196

178197
mutating func remove(serviceAccount index: ServiceIndex) async throws {
@@ -213,8 +232,9 @@ struct AccumulateTests {
213232
for preimage in entry.data.preimagesBlobs {
214233
fullState.preimages[entry.index, default: [:]][preimage.hash] = preimage.blob
215234
}
216-
for preimageStatus in entry.data.preimagesStatus {
217-
fullState.preimageInfo[entry.index, default: [:]][preimageStatus.hash] = preimageStatus.status
235+
for preimageRequest in entry.data.preimageRequests {
236+
let key = HashAndLength(hash: preimageRequest.key.hash, length: preimageRequest.key.length)
237+
fullState.preimageInfo[entry.index, default: [:]][key] = preimageRequest.value
218238
}
219239
}
220240

@@ -247,10 +267,11 @@ struct AccumulateTests {
247267
for preimage in entry.data.preimagesBlobs {
248268
#expect(fullState.preimages[entry.index]?[preimage.hash] == preimage.blob, "Preimage mismatch")
249269
}
250-
for preimageStatus in entry.data.preimagesStatus {
270+
for preimageRequest in entry.data.preimageRequests {
271+
let key = HashAndLength(hash: preimageRequest.key.hash, length: preimageRequest.key.length)
251272
#expect(
252-
fullState.preimageInfo[entry.index]?[preimageStatus.hash] == preimageStatus.status,
253-
"Preimage status mismatch"
273+
fullState.preimageInfo[entry.index]?[key] == preimageRequest.value,
274+
"Preimage request mismatch"
254275
)
255276
}
256277
}

0 commit comments

Comments
 (0)