From e603d922978a33b3cd67a4efaeda4670fd45384e Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Thu, 27 Jun 2024 16:55:08 +1200 Subject: [PATCH] replace h256 with data32 (#28) --- .../Sources/Blockchain/Blockchain.swift | 4 +-- .../Types/AvailabilitySpecifications.swift | 18 +++++------ .../Types/ExtrinsicAvailability.swift | 4 +-- .../Blockchain/Types/ExtrinsicJudgement.swift | 4 +-- .../Sources/Blockchain/Types/Header.swift | 30 +++++++++---------- .../Blockchain/Types/JudgementsState.swift | 8 ++--- .../Blockchain/Types/RefinementContext.swift | 28 ++++++++--------- .../Blockchain/Types/ServiceAccount.swift | 18 +++++------ .../Sources/Blockchain/Types/State.swift | 16 +++++----- .../Sources/Blockchain/Types/Ticket.swift | 4 +-- .../Sources/Blockchain/Types/WorkReport.swift | 6 ++-- .../Sources/Blockchain/Types/WorkResult.swift | 12 ++++---- .../Sources/Blockchain/Types/primitives.swift | 9 +++--- .../xcshareddata/swiftpm/Package.resolved | 20 ++++++++++++- 14 files changed, 99 insertions(+), 82 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Blockchain.swift b/Blockchain/Sources/Blockchain/Blockchain.swift index 08af4939..f0ff385d 100644 --- a/Blockchain/Sources/Blockchain/Blockchain.swift +++ b/Blockchain/Sources/Blockchain/Blockchain.swift @@ -8,7 +8,7 @@ public class Blockchain { public private(set) var heads: [StateRef] public private(set) var finalizedHead: StateRef - private var stateByBlockHash: [H256: StateRef] = [:] + private var stateByBlockHash: [Data32: StateRef] = [:] private var stateByTimeslot: [TimeslotIndex: [StateRef]] = [:] public init(heads: [StateRef], finalizedHead: StateRef) { @@ -40,7 +40,7 @@ public class Blockchain { } extension Blockchain { - public subscript(hash: H256) -> StateRef? { + public subscript(hash: Data32) -> StateRef? { stateByBlockHash[hash] } diff --git a/Blockchain/Sources/Blockchain/Types/AvailabilitySpecifications.swift b/Blockchain/Sources/Blockchain/Types/AvailabilitySpecifications.swift index efc6afde..f19174bd 100644 --- a/Blockchain/Sources/Blockchain/Types/AvailabilitySpecifications.swift +++ b/Blockchain/Sources/Blockchain/Types/AvailabilitySpecifications.swift @@ -3,22 +3,22 @@ import Utils public struct AvailabilitySpecifications: Sendable { // h - public var workPackageHash: H256 + public var workPackageHash: Data32 // l public var length: DataLength // u - public var erasureRoot: H256 + public var erasureRoot: Data32 // e - public var segmentRoot: H256 + public var segmentRoot: Data32 public init( - workPackageHash: H256, + workPackageHash: Data32, length: DataLength, - erasureRoot: H256, - segmentRoot: H256 + erasureRoot: Data32, + segmentRoot: Data32 ) { self.workPackageHash = workPackageHash self.length = length @@ -31,10 +31,10 @@ extension AvailabilitySpecifications: Dummy { public typealias Config = ProtocolConfigRef public static func dummy(withConfig _: Config) -> AvailabilitySpecifications { AvailabilitySpecifications( - workPackageHash: H256(), + workPackageHash: Data32(), length: 0, - erasureRoot: H256(), - segmentRoot: H256() + erasureRoot: Data32(), + segmentRoot: Data32() ) } } diff --git a/Blockchain/Sources/Blockchain/Types/ExtrinsicAvailability.swift b/Blockchain/Sources/Blockchain/Types/ExtrinsicAvailability.swift index 3a2e2f31..fc05e422 100644 --- a/Blockchain/Sources/Blockchain/Types/ExtrinsicAvailability.swift +++ b/Blockchain/Sources/Blockchain/Types/ExtrinsicAvailability.swift @@ -5,7 +5,7 @@ import Utils public struct ExtrinsicAvailability: Sendable { public struct AssuranceItem: Sendable { // a - public var parentHash: H256 + public var parentHash: Data32 // f public var assurance: Data // bit string with length of Constants.TotalNumberOfCores TODO: use a BitString type // v @@ -14,7 +14,7 @@ public struct ExtrinsicAvailability: Sendable { public var signature: Ed25519Signature public init( - parentHash: H256, + parentHash: Data32, assurance: Data, validatorIndex: ValidatorIndex, signature: Ed25519Signature diff --git a/Blockchain/Sources/Blockchain/Types/ExtrinsicJudgement.swift b/Blockchain/Sources/Blockchain/Types/ExtrinsicJudgement.swift index a695715d..f93a446a 100644 --- a/Blockchain/Sources/Blockchain/Types/ExtrinsicJudgement.swift +++ b/Blockchain/Sources/Blockchain/Types/ExtrinsicJudgement.swift @@ -19,14 +19,14 @@ public struct ExtrinsicJudgement: Sendable { } } - public var reportHash: H256 + public var reportHash: Data32 public var signatures: ConfigFixedSizeArray< SignatureItem, ProtocolConfig.TwoThirdValidatorsPlusOne > public init( - reportHash: H256, + reportHash: Data32, signatures: ConfigFixedSizeArray< SignatureItem, ProtocolConfig.TwoThirdValidatorsPlusOne diff --git a/Blockchain/Sources/Blockchain/Types/Header.swift b/Blockchain/Sources/Blockchain/Types/Header.swift index 18c8b0d6..1c8e5dd6 100644 --- a/Blockchain/Sources/Blockchain/Types/Header.swift +++ b/Blockchain/Sources/Blockchain/Types/Header.swift @@ -3,14 +3,14 @@ import Utils public struct Header: Sendable { public struct EpochMarker: Sendable { - public var randomness: H256 + public var randomness: Data32 public var keys: ConfigFixedSizeArray< BandersnatchPublicKey, ProtocolConfig.TotalNumberOfValidators > public init( - randomness: H256, + randomness: Data32, keys: ConfigFixedSizeArray< BandersnatchPublicKey, ProtocolConfig.TotalNumberOfValidators @@ -22,13 +22,13 @@ public struct Header: Sendable { } // Hp: parent hash - public var parentHash: H256 + public var parentHash: Data32 // Hr: prior state root - public var priorStateRoot: H256 // state root of the after parent block execution + public var priorStateRoot: Data32 // state root of the after parent block execution // Hx: extrinsic hash - public var extrinsicsRoot: H256 + public var extrinsicsRoot: Data32 // Ht: timeslot index public var timeslotIndex: TimeslotIndex @@ -50,7 +50,7 @@ public struct Header: Sendable { // Hj: The judgement marker must contain exactly the sequence of report hashes judged not as // confidently valid (i.e. either controversial or invalid). - public var judgementsMarkers: [H256] + public var judgementsMarkers: [Data32] // Hk: a Bandersnatch block author key Hk public var authorKey: BandersnatchPublicKey @@ -62,16 +62,16 @@ public struct Header: Sendable { public var seal: BandersnatchSignature public init( - parentHash: H256, - priorStateRoot: H256, - extrinsicsRoot: H256, + parentHash: Data32, + priorStateRoot: Data32, + extrinsicsRoot: Data32, timeslotIndex: TimeslotIndex, epoch: EpochMarker?, winningTickets: ConfigFixedSizeArray< Ticket, ProtocolConfig.EpochLength >?, - judgementsMarkers: [H256], + judgementsMarkers: [Data32], authorKey: BandersnatchPublicKey, vrfSignature: BandersnatchSignature, seal: BandersnatchSignature @@ -93,9 +93,9 @@ extension Header: Dummy { public typealias Config = ProtocolConfigRef public static func dummy(withConfig _: Config) -> Header { Header( - parentHash: H256(), - priorStateRoot: H256(), - extrinsicsRoot: H256(), + parentHash: Data32(), + priorStateRoot: Data32(), + extrinsicsRoot: Data32(), timeslotIndex: 0, epoch: nil, winningTickets: nil, @@ -152,7 +152,7 @@ extension Header.EpochMarker: ScaleCodec.Encodable { } extension Header { - public var hash: H256 { - H256() // TODO: implement this + public var hash: Data32 { + Data32() // TODO: implement this } } diff --git a/Blockchain/Sources/Blockchain/Types/JudgementsState.swift b/Blockchain/Sources/Blockchain/Types/JudgementsState.swift index 90b1e57b..645dbbb2 100644 --- a/Blockchain/Sources/Blockchain/Types/JudgementsState.swift +++ b/Blockchain/Sources/Blockchain/Types/JudgementsState.swift @@ -3,19 +3,19 @@ import Utils public struct JudgementsState: Sendable { // ψa: The allow-set contains the hashes of all work-reports which were disputed and judged to be accurate. - public var allowSet: Set + public var allowSet: Set // ψb: The ban-set contains the hashes of all work-reports which were disputed and whose accuracy // could not be confidently confirmed. - public var banSet: Set + public var banSet: Set // ψp; he punish-set is a set of keys of Bandersnatch keys which were found to have guaranteed // a report which was confidently found to be invalid. public var punishSet: Set public init( - allowSet: Set, - banSet: Set, + allowSet: Set, + banSet: Set, punishSet: Set ) { self.allowSet = allowSet diff --git a/Blockchain/Sources/Blockchain/Types/RefinementContext.swift b/Blockchain/Sources/Blockchain/Types/RefinementContext.swift index e49854e0..2a75f8f3 100644 --- a/Blockchain/Sources/Blockchain/Types/RefinementContext.swift +++ b/Blockchain/Sources/Blockchain/Types/RefinementContext.swift @@ -5,29 +5,29 @@ import Utils // at the point that the report’s corresponding work-package was evaluated. public struct RefinementContext: Sendable { public var anchor: ( - headerHash: H256, - stateRoot: H256, - beefyRoot: H256 + headerHash: Data32, + stateRoot: Data32, + beefyRoot: Data32 ) public var lokupAnchor: ( - headerHash: H256, + headerHash: Data32, timeslot: TimeslotIndex ) - public var prerequistieWorkPackage: H256? + public var prerequistieWorkPackage: Data32? public init( anchor: ( - headerHash: H256, - stateRoot: H256, - beefyRoot: H256 + headerHash: Data32, + stateRoot: Data32, + beefyRoot: Data32 ), lokupAnchor: ( - headerHash: H256, + headerHash: Data32, timeslot: TimeslotIndex ), - prerequistieWorkPackage: H256? + prerequistieWorkPackage: Data32? ) { self.anchor = anchor self.lokupAnchor = lokupAnchor @@ -40,12 +40,12 @@ extension RefinementContext: Dummy { public static func dummy(withConfig _: Config) -> RefinementContext { RefinementContext( anchor: ( - headerHash: H256(), - stateRoot: H256(), - beefyRoot: H256() + headerHash: Data32(), + stateRoot: Data32(), + beefyRoot: Data32() ), lokupAnchor: ( - headerHash: H256(), + headerHash: Data32(), timeslot: 0 ), prerequistieWorkPackage: nil diff --git a/Blockchain/Sources/Blockchain/Types/ServiceAccount.swift b/Blockchain/Sources/Blockchain/Types/ServiceAccount.swift index e109f6c0..21169161 100644 --- a/Blockchain/Sources/Blockchain/Types/ServiceAccount.swift +++ b/Blockchain/Sources/Blockchain/Types/ServiceAccount.swift @@ -4,20 +4,20 @@ import Utils public struct ServiceAccount: Sendable { public struct HashAndLength: Sendable, Hashable { - public var hash: H256 + public var hash: Data32 public var length: DataLength - public init(hash: H256, length: DataLength) { + public init(hash: Data32, length: DataLength) { self.hash = hash self.length = length } } // s - public var storage: [H256: Data] + public var storage: [Data32: Data] // p - public var preimages: [H256: Data] + public var preimages: [Data32: Data] // l public var preimageInfos: [ @@ -25,7 +25,7 @@ public struct ServiceAccount: Sendable { ] // c - public var codeHash: H256 + public var codeHash: Data32 // b public var balance: Balances @@ -37,10 +37,10 @@ public struct ServiceAccount: Sendable { public var onTransferGasLimit: Gas public init( - storage: [H256: Data], - preimages: [H256: Data], + storage: [Data32: Data], + preimages: [Data32: Data], preimageInfos: [HashAndLength: LimitedSizeArray], - codeHash: H256, + codeHash: Data32, balance: Balances, accumlateGasLimit: Gas, onTransferGasLimit: Gas @@ -62,7 +62,7 @@ extension ServiceAccount: Dummy { storage: [:], preimages: [:], preimageInfos: [:], - codeHash: H256(), + codeHash: Data32(), balance: 0, accumlateGasLimit: 0, onTransferGasLimit: 0 diff --git a/Blockchain/Sources/Blockchain/Types/State.swift b/Blockchain/Sources/Blockchain/Types/State.swift index a03da768..dafa26cd 100644 --- a/Blockchain/Sources/Blockchain/Types/State.swift +++ b/Blockchain/Sources/Blockchain/Types/State.swift @@ -21,7 +21,7 @@ public struct State: Sendable { // α: The core αuthorizations pool. public var coreAuthorizationPool: ConfigFixedSizeArray< ConfigLimitedSizeArray< - H256, + Data32, ProtocolConfig.Int0, ProtocolConfig.MaxAuthorizationsPoolItems >, @@ -38,7 +38,7 @@ public struct State: Sendable { public var serviceAccounts: [ServiceIdentifier: ServiceAccount] // η: The eηtropy accumulator and epochal raηdomness. - public var entropyPool: (H256, H256, H256, H256) + public var entropyPool: (Data32, Data32, Data32, Data32) // ι: The validator keys and metadata to be drawn from next. public var validatorQueue: ConfigFixedSizeArray< @@ -67,7 +67,7 @@ public struct State: Sendable { // φ: The authorization queue. public var authorizationQueue: ConfigFixedSizeArray< ConfigFixedSizeArray< - H256, + Data32, ProtocolConfig.MaxAuthorizationsQueueItems >, ProtocolConfig.TotalNumberOfCores @@ -86,7 +86,7 @@ public struct State: Sendable { public init( coreAuthorizationPool: ConfigFixedSizeArray< ConfigLimitedSizeArray< - H256, + Data32, ProtocolConfig.Int0, ProtocolConfig.MaxAuthorizationsPoolItems >, @@ -95,7 +95,7 @@ public struct State: Sendable { lastBlock: Block, safroleState: SafroleState, serviceAccounts: [ServiceIdentifier: ServiceAccount], - entropyPool: (H256, H256, H256, H256), + entropyPool: (Data32, Data32, Data32, Data32), validatorQueue: ConfigFixedSizeArray< ValidatorKey, ProtocolConfig.TotalNumberOfValidators >, @@ -112,7 +112,7 @@ public struct State: Sendable { timestamp: TimeslotIndex, authorizationQueue: ConfigFixedSizeArray< ConfigFixedSizeArray< - H256, + Data32, ProtocolConfig.MaxAuthorizationsQueueItems >, ProtocolConfig.TotalNumberOfCores @@ -150,7 +150,7 @@ extension State: Dummy { lastBlock: Block.dummy(withConfig: config), safroleState: SafroleState.dummy(withConfig: config), serviceAccounts: [:], - entropyPool: (H256(), H256(), H256(), H256()), + entropyPool: (Data32(), Data32(), Data32(), Data32()), validatorQueue: ConfigFixedSizeArray(withConfig: config, defaultValue: ValidatorKey.dummy(withConfig: config)), currentValidators: ConfigFixedSizeArray(withConfig: config, defaultValue: ValidatorKey.dummy(withConfig: config)), previousValidators: ConfigFixedSizeArray(withConfig: config, defaultValue: ValidatorKey.dummy(withConfig: config)), @@ -158,7 +158,7 @@ extension State: Dummy { timestamp: 0, authorizationQueue: ConfigFixedSizeArray( withConfig: config, - defaultValue: ConfigFixedSizeArray(withConfig: config, defaultValue: H256()) + defaultValue: ConfigFixedSizeArray(withConfig: config, defaultValue: Data32()) ), privilegedServiceIndices: ( empower: ServiceIdentifier(), diff --git a/Blockchain/Sources/Blockchain/Types/Ticket.swift b/Blockchain/Sources/Blockchain/Types/Ticket.swift index 2798fef4..7e4d82a6 100644 --- a/Blockchain/Sources/Blockchain/Types/Ticket.swift +++ b/Blockchain/Sources/Blockchain/Types/Ticket.swift @@ -2,14 +2,14 @@ import ScaleCodec import Utils public struct Ticket: Sendable { - public var identifier: H256 + public var identifier: Data32 public var entryIndex: TicketIndex } extension Ticket: Dummy { public typealias Config = ProtocolConfigRef public static func dummy(withConfig _: Config) -> Ticket { - Ticket(identifier: H256(), entryIndex: 0) + Ticket(identifier: Data32(), entryIndex: 0) } } diff --git a/Blockchain/Sources/Blockchain/Types/WorkReport.swift b/Blockchain/Sources/Blockchain/Types/WorkReport.swift index 4b17abdb..c5ee799c 100644 --- a/Blockchain/Sources/Blockchain/Types/WorkReport.swift +++ b/Blockchain/Sources/Blockchain/Types/WorkReport.swift @@ -4,7 +4,7 @@ import Utils public struct WorkReport: Sendable { // a: authorizer hash - public var authorizerHash: H256 + public var authorizerHash: Data32 // o: output public var output: Data @@ -23,7 +23,7 @@ public struct WorkReport: Sendable { > public init( - authorizerHash: H256, + authorizerHash: Data32, output: Data, refinementContext: RefinementContext, packageSpecification: AvailabilitySpecifications, @@ -45,7 +45,7 @@ extension WorkReport: Dummy { public typealias Config = ProtocolConfigRef public static func dummy(withConfig config: Config) -> WorkReport { WorkReport( - authorizerHash: H256(), + authorizerHash: Data32(), output: Data(), refinementContext: RefinementContext.dummy(withConfig: config), packageSpecification: AvailabilitySpecifications.dummy(withConfig: config), diff --git a/Blockchain/Sources/Blockchain/Types/WorkResult.swift b/Blockchain/Sources/Blockchain/Types/WorkResult.swift index e851ae34..ab3f30eb 100644 --- a/Blockchain/Sources/Blockchain/Types/WorkResult.swift +++ b/Blockchain/Sources/Blockchain/Types/WorkResult.swift @@ -7,10 +7,10 @@ public struct WorkResult: Sendable { public var serviceIdentifier: ServiceIdentifier // c: the hash of the code of the service at the time of being reported - public var codeHash: H256 + public var codeHash: Data32 // l: the hash of the payload - public var payloadHash: H256 + public var payloadHash: Data32 // g: the gas prioritization ratio // used when determining how much gas should be allocated to execute of this item’s accumulate @@ -22,8 +22,8 @@ public struct WorkResult: Sendable { public init( serviceIdentifier: ServiceIdentifier, - codeHash: H256, - payloadHash: H256, + codeHash: Data32, + payloadHash: Data32, gas: Gas, output: Result ) { @@ -40,8 +40,8 @@ extension WorkResult: Dummy { public static func dummy(withConfig _: Config) -> WorkResult { WorkResult( serviceIdentifier: ServiceIdentifier(), - codeHash: H256(), - payloadHash: H256(), + codeHash: Data32(), + payloadHash: Data32(), gas: 0, output: .success(Data()) ) diff --git a/Blockchain/Sources/Blockchain/Types/primitives.swift b/Blockchain/Sources/Blockchain/Types/primitives.swift index 2dc82520..e0055678 100644 --- a/Blockchain/Sources/Blockchain/Types/primitives.swift +++ b/Blockchain/Sources/Blockchain/Types/primitives.swift @@ -1,8 +1,7 @@ import Utils public typealias Balances = UInt64 -public typealias H256 = Data32 -public typealias ServiceIdentifier = H256 +public typealias ServiceIdentifier = Data32 public typealias TimeslotIndex = UInt32 public typealias Gas = UInt64 public typealias DataLength = UInt32 @@ -10,10 +9,10 @@ public typealias ValidatorIndex = UInt32 // TODO: confirm this public typealias CoreIndex = UInt32 // TODO: confirm this public typealias TicketIndex = UInt8 // TODO: confirm this -public typealias Ed25519PublicKey = H256 +public typealias Ed25519PublicKey = Data32 public typealias Ed25519Signature = Data64 -public typealias BandersnatchPublicKey = H256 +public typealias BandersnatchPublicKey = Data32 public typealias BandersnatchSignature = Data64 public typealias BandersnatchRintVRFProof = Data784 -public typealias BandersnatchRingVRFRoot = H256 +public typealias BandersnatchRingVRFRoot = Data32 public typealias BLSKey = Data144 diff --git a/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 71d53bdd..750a0863 100644 --- a/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "b578da78329a065abfa4d3423647483a61d7fc81ec6963d5b5a064c699c949c8", + "originHash" : "c80a60eaf06aa1bc3b9d48f7d3541282e34d308bcc613d98df75ba316c2ca443", "pins" : [ { "identity" : "async-channels", @@ -10,6 +10,15 @@ "revision" : "679ee7d35e493e181be844dadbe78636cefaace4" } }, + { + "identity" : "blake2.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/tesseract-one/Blake2.swift.git", + "state" : { + "revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e", + "version" : "0.2.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", @@ -46,6 +55,15 @@ "version" : "1.1.0" } }, + { + "identity" : "swift-crypto", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-crypto.git", + "state" : { + "revision" : "bc1c29221f6dfeb0ebbfbc98eb95cd3d4967868e", + "version" : "3.4.0" + } + }, { "identity" : "tuples.swift", "kind" : "remoteSourceControl",