Skip to content

Commit

Permalink
main updates
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii committed Nov 20, 2024
1 parent b05f062 commit 5c035ec
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift
Original file line number Diff line number Diff line change
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
30 changes: 25 additions & 5 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ public final class Runtime {
throw Error.invalidHeaderStateRoot
}

let expectedExtrinsicHash = try Result { try JamEncoder.encode(block.extrinsic).blake2b256hash() }
let expectedExtrinsicHash = try Result { try JamEncoder.encode([
JamEncoder.encode(block.extrinsic.tickets).blake2b256hash(),
JamEncoder.encode(block.extrinsic.preimages).blake2b256hash(),
JamEncoder.encode(block.extrinsic.reports.guarantees.array.map { item in
try JamEncoder.encode(item.workReport.hash()) + JamEncoder.encode(item.timeslot) + JamEncoder.encode(item.credential)
}).blake2b256hash(),
JamEncoder.encode(block.extrinsic.availability).blake2b256hash(),
JamEncoder.encode(block.extrinsic.disputes).blake2b256hash(),
]).blake2b256hash() }
.mapError(Error.encodeError).get()

guard block.header.extrinsicsHash == expectedExtrinsicHash else {
Expand Down Expand Up @@ -297,7 +305,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 +387,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 +396,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: 1 addition & 1 deletion Blockchain/Sources/Blockchain/State/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,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
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ public struct AvailabilitySpecifications: Sendable, Equatable, Codable {
// e
public var segmentRoot: Data32

// n
public var segmentCount: Int

public init(
workPackageHash: Data32,
length: DataLength,
erasureRoot: Data32,
segmentRoot: Data32
segmentRoot: Data32,
segmentCount: Int
) {
self.workPackageHash = workPackageHash
self.length = length
self.erasureRoot = erasureRoot
self.segmentRoot = segmentRoot
self.segmentCount = segmentCount
}
}

Expand All @@ -34,14 +39,15 @@ extension AvailabilitySpecifications: Dummy {
workPackageHash: Data32(),
length: 0,
erasureRoot: Data32(),
segmentRoot: Data32()
segmentRoot: Data32(),
segmentCount: 0
)
}
}

extension AvailabilitySpecifications: EncodedSize {
public var encodedSize: Int {
workPackageHash.encodedSize + length.encodedSize + erasureRoot.encodedSize + segmentRoot.encodedSize
workPackageHash.encodedSize + length.encodedSize + erasureRoot.encodedSize + segmentRoot.encodedSize + segmentCount.encodedSize
}

public static var encodeedSizeHint: Int? {
Expand Down
16 changes: 8 additions & 8 deletions Blockchain/Sources/Blockchain/Types/Extrinsic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ public struct Extrinsic: Sendable, Equatable, Codable {
// permissioning of block authoring
public var tickets: ExtrinsicTickets

// ED: Votes, by validators, on dispute(s) arising between them presently taking place
public var judgements: ExtrinsicDisputes

// EP: Static data which is presently being requested to be available for workloads to be able to fetch on demand
public var preimages: ExtrinsicPreimages

// EG: Reports of newly completed workloads whose accuracy is guaranteed by specific validators
public var reports: ExtrinsicGuarantees

// EA: Assurances by each validator concerning which of the input data of workloads they have
// correctly received and are storing locally
public var availability: ExtrinsicAvailability

// EG: Reports of newly completed workloads whose accuracy is guaranteed by specific validators
public var reports: ExtrinsicGuarantees
// ED: Votes, by validators, on dispute(s) arising between them presently taking place
public var disputes: ExtrinsicDisputes

public init(
tickets: ExtrinsicTickets,
judgements: ExtrinsicDisputes,
disputes: ExtrinsicDisputes,
preimages: ExtrinsicPreimages,
availability: ExtrinsicAvailability,
reports: ExtrinsicGuarantees
) {
self.tickets = tickets
self.judgements = judgements
self.disputes = disputes
self.preimages = preimages
self.availability = availability
self.reports = reports
Expand All @@ -42,7 +42,7 @@ extension Extrinsic: Dummy {
public static func dummy(config: Config) -> Extrinsic {
Extrinsic(
tickets: ExtrinsicTickets.dummy(config: config),
judgements: ExtrinsicDisputes.dummy(config: config),
disputes: ExtrinsicDisputes.dummy(config: config),
preimages: ExtrinsicPreimages.dummy(config: config),
availability: ExtrinsicAvailability.dummy(config: config),
reports: ExtrinsicGuarantees.dummy(config: config)
Expand Down
6 changes: 3 additions & 3 deletions Blockchain/Sources/Blockchain/Types/PrivilegedServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import Utils

public struct PrivilegedServices: Sendable, Equatable, Codable {
// m
public var empower: ServiceIndex
public var blessed: ServiceIndex
// a
public var assign: ServiceIndex
// v
public var designate: ServiceIndex
// g
@CodingAs<SortedKeyValues<ServiceIndex, Gas>> public var basicGas: [ServiceIndex: Gas]

public init(empower: ServiceIndex, assign: ServiceIndex, designate: ServiceIndex, basicGas: [ServiceIndex: Gas]) {
self.empower = empower
public init(blessed: ServiceIndex, assign: ServiceIndex, designate: ServiceIndex, basicGas: [ServiceIndex: Gas]) {
self.blessed = blessed
self.assign = assign
self.designate = designate
self.basicGas = basicGas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public class Empower: HostCall {

if basicGas.count != 0 {
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.OK.rawValue)
x.accumulateState.privilegedServices.empower = regs[0]
x.accumulateState.privilegedServices.blessed = regs[0]
x.accumulateState.privilegedServices.assign = regs[1]
x.accumulateState.privilegedServices.designate = regs[2]
x.accumulateState.privilegedServices.basicGas = basicGas
Expand Down
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Validator/BlockAuthor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class BlockAuthor: ServiceBase2, @unchecked Sendable {

let extrinsic = try Extrinsic(
tickets: ExtrinsicTickets(tickets: ConfigLimitedSizeArray(config: config, array: Array(tickets))),
judgements: ExtrinsicDisputes.dummy(config: config), // TODO:
disputes: ExtrinsicDisputes.dummy(config: config), // TODO:
preimages: ExtrinsicPreimages.dummy(config: config), // TODO:
availability: ExtrinsicAvailability.dummy(config: config), // TODO:
reports: ExtrinsicGuarantees.dummy(config: config) // TODO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ struct ExtrinsicPoolServiceTests {
let blockTickets = Array(tickets[0 ..< 2])
let extrinsic = try Extrinsic(
tickets: ExtrinsicTickets(tickets: ConfigLimitedSizeArray(config: config, array: blockTickets.map(\.ticket))),
judgements: ExtrinsicDisputes.dummy(config: config),
disputes: ExtrinsicDisputes.dummy(config: config),
preimages: ExtrinsicPreimages.dummy(config: config),
availability: ExtrinsicAvailability.dummy(config: config),
reports: ExtrinsicGuarantees.dummy(config: config)
Expand Down
2 changes: 1 addition & 1 deletion JAMTests/Tests/JAMTests/CodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ struct CodecTests {
if let value = value as? Extrinsic {
return [
"tickets": transform(json["tickets"]!, value: value.tickets),
"disputes": transform(json["judgements"]!, value: value.judgements),
"disputes": transform(json["disputes"]!, value: value.disputes),
"preimages": transform(json["preimages"]!, value: value.preimages),
"assurances": transform(json["availability"]!, value: value.availability),
"guarantees": transform(json["reports"]!, value: value.reports),
Expand Down

0 comments on commit 5c035ec

Please sign in to comment.