Skip to content

Commit 7517701

Browse files
authored
fixes 0.7.0 (#362)
* parse isnt on demand * enable logstep only in debug build * disable some instuction log * add new traces * fix encode * fix gas * fix parallel updates * update tests * change transfer cash case back * fix lint
1 parent 03a06fc commit 7517701

Some content is hidden

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

48 files changed

+9169
-119
lines changed

Blockchain/Sources/Blockchain/RuntimeProtocols/Accumulation.swift

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -77,37 +77,53 @@ public struct AccumulationResult: Sendable {
7777
}
7878

7979
public struct AccountChanges: Sendable {
80+
public enum UpdateKind: Sendable {
81+
case newAccount(ServiceIndex, ServiceAccount)
82+
case removeAccount(ServiceIndex)
83+
case updateAccount(ServiceIndex, ServiceAccountDetails)
84+
case updateStorage(ServiceIndex, Data, Data?)
85+
case updatePreimage(ServiceIndex, Data32, Data?)
86+
case updatePreimageInfo(ServiceIndex, Data32, UInt32, StateKeys.ServiceAccountPreimageInfoKey.Value?)
87+
}
88+
89+
// records for checking conflicts
8090
public var newAccounts: [ServiceIndex: ServiceAccount]
8191
public var altered: Set<ServiceIndex>
82-
public var accountUpdates: [ServiceIndex: ServiceAccountDetails]
83-
public var storageUpdates: [ServiceIndex: [Data: Data?]]
84-
public var preimageUpdates: [ServiceIndex: [Data32: Data?]]
85-
public var preimageInfoUpdates: [ServiceIndex: [Data32: (UInt32, StateKeys.ServiceAccountPreimageInfoKey.Value?)]]
8692
public var removed: Set<ServiceIndex>
8793

94+
// array for apply sequential updates
95+
public var updates: [UpdateKind]
96+
8897
public init() {
8998
newAccounts = [:]
9099
altered = []
91-
accountUpdates = [:]
92-
storageUpdates = [:]
93-
preimageUpdates = [:]
94-
preimageInfoUpdates = [:]
95100
removed = []
101+
updates = []
102+
}
103+
104+
public mutating func addNewAccount(index: ServiceIndex, account: ServiceAccount) {
105+
newAccounts[index] = account
106+
updates.append(.newAccount(index, account))
107+
}
108+
109+
public mutating func addRemovedAccount(index: ServiceIndex) {
110+
removed.insert(index)
111+
updates.append(.removeAccount(index))
96112
}
97113

98114
public mutating func addAccountUpdate(index: ServiceIndex, account: ServiceAccountDetails) {
99-
accountUpdates[index] = account
100115
altered.insert(index)
116+
updates.append(.updateAccount(index, account))
101117
}
102118

103119
public mutating func addStorageUpdate(index: ServiceIndex, key: Data, value: Data?) {
104-
storageUpdates[index, default: [:]][key] = value
105120
altered.insert(index)
121+
updates.append(.updateStorage(index, key, value))
106122
}
107123

108124
public mutating func addPreimageUpdate(index: ServiceIndex, hash: Data32, value: Data?) {
109-
preimageUpdates[index, default: [:]][hash] = value
110125
altered.insert(index)
126+
updates.append(.updatePreimage(index, hash, value))
111127
}
112128

113129
public mutating func addPreimageInfoUpdate(
@@ -116,32 +132,24 @@ public struct AccountChanges: Sendable {
116132
length: UInt32,
117133
value: StateKeys.ServiceAccountPreimageInfoKey.Value?
118134
) {
119-
preimageInfoUpdates[index, default: [:]][hash] = (length, value)
120135
altered.insert(index)
136+
updates.append(.updatePreimageInfo(index, hash, length, value))
121137
}
122138

123139
public func apply(to accounts: ServiceAccountsMutRef) async throws {
124-
for (index, account) in newAccounts {
125-
try await accounts.addNew(serviceAccount: index, account: account)
126-
}
127-
for index in removed {
128-
accounts.remove(serviceAccount: index)
129-
}
130-
for (index, account) in accountUpdates {
131-
accounts.set(serviceAccount: index, account: account)
132-
}
133-
for (index, storage) in storageUpdates {
134-
for (key, value) in storage {
140+
for update in updates {
141+
switch update {
142+
case let .newAccount(index, account):
143+
try await accounts.addNew(serviceAccount: index, account: account)
144+
case let .removeAccount(index):
145+
accounts.remove(serviceAccount: index)
146+
case let .updateAccount(index, account):
147+
accounts.set(serviceAccount: index, account: account)
148+
case let .updateStorage(index, key, value):
135149
try await accounts.set(serviceAccount: index, storageKey: key, value: value)
136-
}
137-
}
138-
for (index, preimages) in preimageUpdates {
139-
for (hash, value) in preimages {
150+
case let .updatePreimage(index, hash, value):
140151
accounts.set(serviceAccount: index, preimageHash: hash, value: value)
141-
}
142-
}
143-
for (index, preimageInfos) in preimageInfoUpdates {
144-
for (hash, (length, value)) in preimageInfos {
152+
case let .updatePreimageInfo(index, hash, length, value):
145153
try await accounts.set(serviceAccount: index, preimageHash: hash, length: length, value: value)
146154
}
147155
}
@@ -166,25 +174,7 @@ public struct AccountChanges: Sendable {
166174
}
167175
altered.formUnion(other.altered)
168176
removed.formUnion(other.removed)
169-
170-
for (index, account) in other.accountUpdates {
171-
accountUpdates[index] = account
172-
}
173-
for (index, storage) in other.storageUpdates {
174-
for (key, value) in storage {
175-
storageUpdates[index, default: [:]][key] = value
176-
}
177-
}
178-
for (index, preimages) in other.preimageUpdates {
179-
for (hash, value) in preimages {
180-
preimageUpdates[index, default: [:]][hash] = value
181-
}
182-
}
183-
for (index, preimageInfos) in other.preimageInfoUpdates {
184-
for (hash, info) in preimageInfos {
185-
preimageInfoUpdates[index, default: [:]][hash] = info
186-
}
187-
}
177+
updates.append(contentsOf: other.updates)
188178
}
189179
}
190180

@@ -255,7 +245,7 @@ extension Accumulation {
255245
}
256246

257247
/// parallelized accumulate function ∆*
258-
private mutating func parallelizedAccumulate(
248+
private func parallelizedAccumulate(
259249
config: ProtocolConfigRef,
260250
state: AccumulateState,
261251
workReports: [WorkReport],
@@ -297,11 +287,9 @@ extension Accumulation {
297287
) { group in
298288
for service in serviceBatch {
299289
group.addTask { [batchState] in
300-
let parallelState = batchState.copy()
301-
302290
return try await Self.singleAccumulate(
303291
config: config,
304-
state: parallelState,
292+
state: batchState.copy(),
305293
workReports: workReports,
306294
service: service,
307295
alwaysAcc: alwaysAcc,
@@ -377,7 +365,7 @@ extension Accumulation {
377365
return batches
378366
}
379367

380-
private mutating func mergeParallelBatchResults(
368+
private func mergeParallelBatchResults(
381369
batchResults: [(ServiceIndex, AccumulationResult)],
382370
currentState: inout AccumulateState,
383371
overallAccountChanges: inout AccountChanges,
@@ -437,7 +425,7 @@ extension Accumulation {
437425
}
438426

439427
/// outer accumulate function ∆+
440-
private mutating func outerAccumulate(
428+
private func outerAccumulate(
441429
config: ProtocolConfigRef,
442430
state: AccumulateState,
443431
workReports: [WorkReport],
@@ -589,7 +577,7 @@ extension Accumulation {
589577
}
590578

591579
// accumulate execution
592-
private mutating func execution(
580+
private func execution(
593581
config: ProtocolConfigRef,
594582
workReports: [WorkReport],
595583
state: AccumulateState,

Blockchain/Sources/Blockchain/State/ServiceAccounts.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public class ServiceAccountsMutRef: @unchecked Sendable {
8080
for (key, value) in account.preimageInfos {
8181
try await ref.value.set(serviceAccount: index, preimageHash: key.hash, length: key.length, value: value)
8282
}
83-
changes.newAccounts[index] = account
83+
changes.addNewAccount(index: index, account: account)
8484
}
8585

8686
public func remove(serviceAccount index: ServiceIndex) {
8787
ref.value.set(serviceAccount: index, account: nil)
88-
changes.removed.insert(index)
88+
changes.addRemovedAccount(index: index)
8989
}
9090

9191
public func clearRecordedChanges() {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ public protocol HostCall {
1313
extension HostCall {
1414
public func call(config: ProtocolConfigRef, state: VMState) async -> ExecOutcome {
1515
logger.debug("===== host call: \(Self.self) =====")
16+
state.consumeGas(gasCost(state: state))
17+
logger.debug("consumed \(gasCost(state: state)) gas, \(state.getGas()) left")
18+
1619
guard hasEnoughGas(state: state) else {
1720
logger.debug("not enough gas")
1821
return .exit(.outOfGas)
1922
}
20-
state.consumeGas(gasCost(state: state))
21-
logger.debug("consumed \(gasCost(state: state)) gas, \(state.getGas()) left")
2223

2324
do {
2425
try await _callImpl(config: config, state: state)

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,11 @@ public class Read: HostCall {
294294
nil
295295
}
296296

297-
logger.debug("value: \(value?.toDebugHexString() ?? "nil")")
298-
299297
guard let value else {
300298
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.NONE.rawValue)
301299
return
302300
}
303301

304-
logger.debug("raw val: \(value.toDebugHexString())")
305-
306302
let reg11: UInt64 = state.readRegister(Registers.Index(raw: 11))
307303
let reg12: UInt64 = state.readRegister(Registers.Index(raw: 12))
308304

Codec/Sources/Codec/JamEncoder.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ private class EncodeContext: Encoder {
116116
data.append(UInt8(1)) // Encode presence flag
117117
try encode(value, key: key)
118118
} else {
119-
data.append(UInt8(0)) // Encode absence flag
119+
if key == nil, codingPath.isEmpty {
120+
// top-level nil encoding: do nothing (empty data)
121+
return
122+
} else {
123+
data.append(UInt8(0)) // Encode absence flag
124+
}
120125
}
121126
}
122127

Codec/Tests/CodecTests/EncoderTests.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ struct EncoderTests {
376376
let encodedNone = try JamEncoder.encode(Int?.none)
377377

378378
#expect(encodedSome == Data([1, 1, 0, 0, 0, 0, 0, 0, 0])) // Optional with value encoded
379-
#expect(encodedNone == Data([0])) // None encoded as 1 byte (0)
379+
#expect(encodedNone == Data()) // None encoded as empty data
380380
}
381381

382382
@Test func encodeOptionalData() throws {
@@ -386,7 +386,7 @@ struct EncoderTests {
386386
let encodedNone = try JamEncoder.encode(Data?.none)
387387

388388
#expect(encodedSome == Data([1, 1, 2, 3])) // Optional with value encoded
389-
#expect(encodedNone == Data([0])) // None encoded as 1 byte (0)
389+
#expect(encodedNone == Data()) // None encoded as empty data
390390
}
391391

392392
@Test func encodeInt() throws {
@@ -413,11 +413,6 @@ struct EncoderTests {
413413
let int64Value: Int64 = 1
414414
let uintValue: UInt = 65535
415415
let uint16Value: UInt16 = 65535
416-
let nilValue: Int64? = nil
417-
418-
let encodedNil = try JamEncoder.encode(nilValue)
419-
#expect(encodedNil == Data([0]))
420-
try #expect(JamDecoder.decode(Int64?.self, from: encodedNil) == nilValue)
421416

422417
let encodedInt16 = try JamEncoder.encode(int16Value)
423418
#expect(encodedInt16 == Data([1, 0]))
@@ -443,12 +438,19 @@ struct EncoderTests {
443438
@Test func encodeArrayOfNils() throws {
444439
let arrayOfNils: [UInt8?] = [nil, nil, nil]
445440
let encoded = try JamEncoder.encode(arrayOfNils)
446-
#expect(encoded == Data([3, 0, 0, 0]))
441+
#expect(encoded == Data([3]))
447442
}
448443

449444
@Test func encodeEmptyArray() throws {
450445
let emptyArray: [Int] = []
451446
let encoded = try JamEncoder.encode(emptyArray)
452447
#expect(encoded == Data([0]))
453448
}
449+
450+
@Test func encodeSingleNil() throws {
451+
let nilValue: Int64? = nil
452+
453+
let encodedNil = try JamEncoder.encode(nilValue)
454+
#expect(encodedNil == Data([]))
455+
}
454456
}

JAMTests/Tests/JAMTests/jamtestnet/FuzzTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct FuzzTests {
5858
expectFailure: [
5959
],
6060
ignore: [
61+
("0.7.0/1756548583", "00000009"), // TODO: one storage / preimage mismatch
62+
("0.7.0/1756548706", "00000094"), // TODO: backend
6163
]
6264
))
6365
func v070(_ input: TestInput) async throws {
1.24 MB
Binary file not shown.

JAMTests/fuzz/0.7.0/1756548459/00000041.json

Lines changed: 660 additions & 0 deletions
Large diffs are not rendered by default.
1.24 MB
Binary file not shown.

0 commit comments

Comments
 (0)