Skip to content

Commit 817baea

Browse files
committed
separate config limited decode and validate
1 parent 986e50f commit 817baea

File tree

7 files changed

+44
-28
lines changed

7 files changed

+44
-28
lines changed

Blockchain/Sources/Blockchain/RuntimeProtocols/Validate.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,24 @@ extension Array where Element: Validate {
6262

6363
extension ConfigLimitedSizeArray: Validate where T: Validate {
6464
public func validate(config: Config) throws {
65+
try validateThrowing()
6566
try array.validate(config: config)
6667
}
6768
}
6869

6970
extension LimitedSizeArray: Validate where T: Validate {
7071
public func validate(config: Config) throws {
72+
try validateThrowing()
7173
try array.validate(config: config)
7274
}
7375
}
7476

77+
extension ConfigSizeBitString: Validate {
78+
public func validate(config _: Config) throws {
79+
try validateThrowing()
80+
}
81+
}
82+
7583
extension Ref: Validate where T: Validate {
7684
public func validate(config: Config) throws {
7785
try value.validate(config: config)

Blockchain/Sources/Blockchain/Types/EpochMarker.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ extension EpochMarker: Dummy {
3838
)
3939
}
4040
}
41+
42+
extension EpochMarker: Validate {}

Blockchain/Sources/Blockchain/Types/WorkReport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ extension WorkReport: Validate {
8181
case tooManyDependencies
8282
}
8383

84-
public func validate(config: Config) throws(WorkReportError) {
84+
public func validateSelf(config: Config) throws(WorkReportError) {
8585
guard refinementContext.prerequisiteWorkPackages.count + lookup.count <= config.value.maxDepsInWorkReport else {
8686
throw .tooManyDependencies
8787
}

JAMTests/Tests/JAMTests/jamtestnet/FuzzTests.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,10 @@ struct FuzzTests {
3838
// empty to include all
3939
],
4040
ignore: [
41-
("0.7.1/1763371098", "00000006"), // tooFewElements (block decode fail, expected to fail)
42-
("0.7.1/1763371531", "00000006"), // tooFewElements
43-
("0.7.1/1763371531", "00000008"), // tooFewElements
44-
("0.7.1/1763371531", "00000011"), // tooFewElements
45-
("0.7.1/1763371531", "00000014"), // tooFewElements
46-
("0.7.1/1763371531", "00000023"), // tooFewElements
47-
("0.7.1/1763371531", "00000028"), // tooFewElements
48-
("0.7.1/1763371531", "00000030"), // tooFewElements
49-
("0.7.1/1763371531", "00000032"), // tooFewElements
50-
("0.7.1/1763371531", "00000038"), // tooFewElements
5141
("0.7.1/1763371531", "00000042"), // missing "keyvals": [] in prestate (expected to fail)
52-
("0.7.1/1763372314", "00000094"), // tooFewElements
42+
("0.7.1/1763487981", "00000050"), // missing "keyvals": [] in prestate (expected to fail)
43+
("0.7.1/1763488328", "00000050"), // missing "keyvals": [] in prestate (expected to fail)
44+
("0.7.1/1763489287", "00000872"), // missing "keyvals": [] in prestate (expected to fail)
5345
]
5446
))
5547
func v071(input: Testcase) async throws {

Utils/Sources/Utils/ConfigLimitedSizeArray.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public struct ConfigLimitedSizeArray<T, TMinLength: ReadInt, TMaxLength: ReadInt
4040
try self.init(array, minLength: minLength, maxLength: maxLength)
4141
}
4242

43-
private init(_ array: [T], minLength: Int, maxLength: Int) throws(ConfigLimitedSizeArrayError) {
43+
private init(_ array: [T], minLength: Int, maxLength: Int, validate: Bool = true) throws(ConfigLimitedSizeArrayError) {
4444
guard minLength >= 0 else {
4545
throw ConfigLimitedSizeArrayError.invalidMinLength
4646
}
@@ -52,15 +52,17 @@ public struct ConfigLimitedSizeArray<T, TMinLength: ReadInt, TMaxLength: ReadInt
5252
self.minLength = minLength
5353
self.maxLength = maxLength
5454

55-
try validateThrowing()
55+
if validate {
56+
try validateThrowing()
57+
}
5658
}
5759

5860
private func validate() {
5961
assert(array.count >= minLength, "count \(array.count) >= minLength \(minLength)")
6062
assert(array.count <= maxLength, "count \(array.count) <= maxLength \(maxLength)")
6163
}
6264

63-
private func validateThrowing() throws(ConfigLimitedSizeArrayError) {
65+
public func validateThrowing() throws(ConfigLimitedSizeArrayError) {
6466
guard array.count >= minLength else {
6567
throw ConfigLimitedSizeArrayError.tooFewElements
6668
}
@@ -231,19 +233,19 @@ extension ConfigLimitedSizeArray: Decodable where T: Decodable {
231233
for _ in 0 ..< minLength {
232234
try arr.append(container.decode(T.self))
233235
}
234-
try self.init(arr, minLength: minLength, maxLength: maxLength)
236+
try self.init(arr, minLength: minLength, maxLength: maxLength, validate: false)
235237
} else {
236238
// variable size array
237239
var container = try decoder.unkeyedContainer()
238240
if decoder.isJamCodec {
239241
let array = try container.decode([T].self)
240-
try self.init(array, minLength: minLength, maxLength: maxLength)
242+
try self.init(array, minLength: minLength, maxLength: maxLength, validate: false)
241243
} else {
242244
var array = [T]()
243245
while !container.isAtEnd {
244246
try array.append(container.decode(T.self))
245247
}
246-
try self.init(array, minLength: minLength, maxLength: maxLength)
248+
try self.init(array, minLength: minLength, maxLength: maxLength, validate: false)
247249
}
248250
}
249251
}

Utils/Sources/Utils/ConfigSizeBitString.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public struct ConfigSizeBitString<TBitLength: ReadInt>: Equatable, Sendable, Cod
1717
(length + 7) / 8
1818
}
1919

20-
public init(config: TBitLength.TConfig, data: Data) throws(ConfigSizeBitStringError) {
20+
public init(config: TBitLength.TConfig, data: Data, validate: Bool = true) throws(ConfigSizeBitStringError) {
2121
length = TBitLength.read(config: config)
2222
bytes = data
2323

24-
if byteLength != data.count {
25-
throw .invalidData
24+
if validate {
25+
try validateThrowing()
2626
}
2727
}
2828

@@ -32,6 +32,12 @@ public struct ConfigSizeBitString<TBitLength: ReadInt>: Equatable, Sendable, Cod
3232
bytes = Data(repeating: 0, count: byteLength)
3333
}
3434

35+
public func validateThrowing() throws(ConfigSizeBitStringError) {
36+
if byteLength != bytes.count {
37+
throw .invalidData
38+
}
39+
}
40+
3541
private func at(unchecked index: Int) -> Bool {
3642
let byteIndex = index / 8
3743
let bitIndex = index % 8
@@ -129,7 +135,7 @@ extension ConfigSizeBitString: FixedLengthData {
129135
guard let config = decoder.getConfig(TBitLength.TConfig.self) else {
130136
throw ConfigSizeBitStringError.missingConfig
131137
}
132-
try self.init(config: config, data: data)
138+
try self.init(config: config, data: data, validate: false)
133139
}
134140
}
135141

@@ -150,3 +156,7 @@ extension ConfigSizeBitString: DataPtrRepresentable {
150156
try data.withUnsafeBytes(cb)
151157
}
152158
}
159+
160+
extension ConfigSizeBitString: HasConfig {
161+
public typealias Config = TBitLength.TConfig
162+
}

Utils/Sources/Utils/LimitedSizeArray.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ public struct LimitedSizeArray<T, TMinLength: ConstInt, TMaxLength: ConstInt> {
1919
self.init(Array(repeating: defaultValue, count: Self.minLength))
2020
}
2121

22-
public init(_ array: [T]) {
22+
public init(_ array: [T], validate: Bool = true) {
2323
assert(Self.minLength >= 0)
2424
assert(Self.maxLength >= Self.minLength)
2525
self.array = array
2626

27-
validate()
27+
if validate {
28+
self.validate()
29+
}
2830
}
2931

3032
private func validate() {
3133
assert(array.count >= Self.minLength)
3234
assert(array.count <= Self.maxLength)
3335
}
3436

35-
private func validateThrowing() throws(LimitedSizeArrayError) {
37+
public func validateThrowing() throws(LimitedSizeArrayError) {
3638
guard array.count >= Self.minLength else {
3739
throw LimitedSizeArrayError.tooFewElements
3840
}
@@ -149,7 +151,7 @@ extension LimitedSizeArray: Encodable where T: Encodable {
149151

150152
extension LimitedSizeArray: Decodable where T: Decodable {
151153
public init(from decoder: any Decoder) throws {
152-
array = []
154+
var arr = [T]()
153155
var container = try decoder.unkeyedContainer()
154156
var length = TMaxLength.value
155157

@@ -168,10 +170,10 @@ extension LimitedSizeArray: Decodable where T: Decodable {
168170
}
169171

170172
for _ in 0 ..< length {
171-
try array.append(container.decode(T.self))
173+
try arr.append(container.decode(T.self))
172174
}
173175

174-
try validateThrowing()
176+
self.init(arr, validate: false)
175177
}
176178
}
177179

0 commit comments

Comments
 (0)