Skip to content

Commit

Permalink
Merge branch 'master' into dev_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI authored Nov 25, 2024
2 parents 271e267 + eb7a122 commit f83b7d1
Show file tree
Hide file tree
Showing 43 changed files with 652 additions and 291 deletions.
4 changes: 2 additions & 2 deletions Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
// P = 6: The slot period, in seconds.
public var slotPeriodSeconds: Int

// Q = 80: The maximum number of items in the authorizations queue.
// Q = 80: The number of items in the authorizations queue.
public var maxAuthorizationsQueueItems: Int

// R = 10: The rotation period of validator-core assignments, in timeslots.
Expand Down Expand Up @@ -107,7 +107,7 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
// ZI = 2^24: The standard pvm program initialization input data size.
public var pvmProgramInitInputDataSize: Int

// ZP = 2^14: The standard pvm program initialization page size.
// ZG = 2^14: The standard pvm program initialization page size.
public var pvmProgramInitPageSize: Int

// ZQ = 2^16: The standard pvm program initialization segment size.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ extension Accumulation {
}

switch service {
case privilegedServices.empower:
case privilegedServices.blessed:
newPrivilegedServices = singleOutput.state.privilegedServices
case privilegedServices.assign:
newAuthorizationQueue = singleOutput.state.authorizationQueue
Expand Down
25 changes: 17 additions & 8 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ public final class Runtime {
throw Error.invalidHeaderStateRoot
}

let expectedExtrinsicHash = try Result { try JamEncoder.encode(block.extrinsic).blake2b256hash() }
.mapError(Error.encodeError).get()

guard block.header.extrinsicsHash == expectedExtrinsicHash else {
guard block.header.extrinsicsHash == block.extrinsic.hash() else {
throw Error.invalidExtrinsicHash
}

Expand Down Expand Up @@ -297,7 +294,7 @@ public final class Runtime {
}

public func updateDisputes(block: BlockRef, state newState: inout State) throws {
let (posState, offenders) = try newState.update(config: config, disputes: block.extrinsic.judgements)
let (posState, offenders) = try newState.update(config: config, disputes: block.extrinsic.disputes)
newState.mergeWith(postState: posState)

guard offenders == block.header.offendersMarkers else {
Expand Down Expand Up @@ -379,7 +376,7 @@ public final class Runtime {
return availableReports
}

public func updatePreimages(block: BlockRef, state newState: inout State) async throws {
public func updatePreimages(block: BlockRef, state newState: inout State, prevState: StateRef) async throws {
let preimages = block.extrinsic.preimages.preimages

guard preimages.isSortedAndUnique() else {
Expand All @@ -388,14 +385,26 @@ public final class Runtime {

for preimage in preimages {
let hash = preimage.data.blake2b256hash()

// check prior state
let prevPreimageData: Data? = try await prevState.value.get(serviceAccount: preimage.serviceIndex, preimageHash: hash)
let prevInfo = try await prevState.value.get(
serviceAccount: preimage.serviceIndex, preimageHash: hash, length: UInt32(preimage.data.count)
)
guard prevPreimageData == nil, prevInfo == nil else {
throw Error.duplicatedPreimage
}

// disregard no longer useful ones in new state
let preimageData: Data? = try await newState.get(serviceAccount: preimage.serviceIndex, preimageHash: hash)
let info = try await newState.get(
serviceAccount: preimage.serviceIndex, preimageHash: hash, length: UInt32(preimage.data.count)
)
guard preimageData == nil, info == nil else {
throw Error.duplicatedPreimage
if preimageData != nil || info != nil {
continue
}

// update state
newState[serviceAccount: preimage.serviceIndex, preimageHash: hash] = preimage.data
newState[
serviceAccount: preimage.serviceIndex, preimageHash: hash, length: UInt32(preimage.data.count)
Expand Down
2 changes: 0 additions & 2 deletions Blockchain/Sources/Blockchain/State/State+Genesis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@ extension State {

return (StateRef(state), block)
}
// TODO: add file genesis
// public static func fileGenesis(config: ProtocolConfigRef) throws -> State
}
10 changes: 9 additions & 1 deletion Blockchain/Sources/Blockchain/State/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ public struct State: Sendable {
await backend.rootHash
}
}

public func read(key: Data32) async throws -> Data? {
let res = try layer[key].map { try JamEncoder.encode($0) }
if let res {
return res
}
return try await backend.readRaw(key)
}
}

extension State {
Expand Down Expand Up @@ -274,7 +282,7 @@ extension State: Dummy {
let authorizationQueue: StateKeys.AuthorizationQueueKey.Value =
try! ConfigFixedSizeArray(config: config, defaultValue: ConfigFixedSizeArray(config: config, defaultValue: Data32()))
let privilegedServices: StateKeys.PrivilegedServicesKey.Value = PrivilegedServices(
empower: ServiceIndex(),
blessed: ServiceIndex(),
assign: ServiceIndex(),
designate: ServiceIndex(),
basicGas: [:]
Expand Down
8 changes: 6 additions & 2 deletions Blockchain/Sources/Blockchain/State/StateBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ public final class StateBackend: Sendable {
return ret
}

public func write(_ values: any Sequence<(key: any StateKey, value: (Codable & Sendable)?)>) async throws {
try await trie.update(values.map { try (key: $0.key.encode(), value: $0.value.map { try JamEncoder.encode($0) }) })
public func write(_ values: any Sequence<(key: Data32, value: (Codable & Sendable)?)>) async throws {
try await trie.update(values.map { try (key: $0.key, value: $0.value.map { try JamEncoder.encode($0) }) })
try await trie.save()
}

public func readRaw(_ key: Data32) async throws -> Data? {
try await trie.read(key: key)
}

public func writeRaw(_ values: [(key: Data32, value: Data?)]) async throws {
try await trie.update(values)
try await trie.save()
Expand Down
26 changes: 13 additions & 13 deletions Blockchain/Sources/Blockchain/State/StateKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ private func constructKey(_ idx: UInt8, _ service: ServiceIndex) -> Data32 {
return Data32(data)!
}

private func constructKey(_ service: ServiceIndex, _ val: UInt32, _: Data) -> Data32 {
var data = Data(capacity: 32)
private func constructKey(_ service: ServiceIndex, _ val: UInt32, _ data: Data) -> Data32 {
var stateKey = Data(capacity: 32)

withUnsafeBytes(of: service) { servicePtr in
withUnsafeBytes(of: val) { valPtr in
data.append(servicePtr.load(as: UInt8.self))
data.append(valPtr.load(as: UInt8.self))
data.append(servicePtr.load(fromByteOffset: 1, as: UInt8.self))
data.append(valPtr.load(fromByteOffset: 1, as: UInt8.self))
data.append(servicePtr.load(fromByteOffset: 2, as: UInt8.self))
data.append(valPtr.load(fromByteOffset: 2, as: UInt8.self))
data.append(servicePtr.load(fromByteOffset: 3, as: UInt8.self))
data.append(valPtr.load(fromByteOffset: 3, as: UInt8.self))
stateKey.append(servicePtr.load(as: UInt8.self))
stateKey.append(valPtr.load(as: UInt8.self))
stateKey.append(servicePtr.load(fromByteOffset: 1, as: UInt8.self))
stateKey.append(valPtr.load(fromByteOffset: 1, as: UInt8.self))
stateKey.append(servicePtr.load(fromByteOffset: 2, as: UInt8.self))
stateKey.append(valPtr.load(fromByteOffset: 2, as: UInt8.self))
stateKey.append(servicePtr.load(fromByteOffset: 3, as: UInt8.self))
stateKey.append(valPtr.load(fromByteOffset: 3, as: UInt8.self))
}
}
data.append(contentsOf: data[relative: 0 ..< 24])
return Data32(data)!
stateKey.append(contentsOf: data[relative: 0 ..< 24])
return Data32(stateKey)!
}

public enum StateKeys {
Expand Down Expand Up @@ -316,7 +316,7 @@ public enum StateKeys {
}

public func encode() -> Data32 {
constructKey(index, length, hash.blake2b256hash().data)
constructKey(index, length, hash.blake2b256hash().data[2...])
}
}
}
Loading

0 comments on commit f83b7d1

Please sign in to comment.