From b05f062b6d87d4bfcfb2011be42c8679726c3e43 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 20 Nov 2024 14:42:17 +0800 Subject: [PATCH] small changes --- .../Blockchain/Config/ProtocolConfig.swift | 2 +- .../Blockchain/RuntimeProtocols/Safrole.swift | 3 ++- .../Sources/Blockchain/State/StateKeys.swift | 24 +++++++++---------- .../Blockchain/Types/EpochMarker.swift | 9 ++++--- .../Invocations/AccumulateInvocation.swift | 2 +- .../Invocations/OnTransferInvocation.swift | 2 +- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift b/Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift index 193f0cdf..7884d01c 100644 --- a/Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift +++ b/Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift @@ -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. diff --git a/Blockchain/Sources/Blockchain/RuntimeProtocols/Safrole.swift b/Blockchain/Sources/Blockchain/RuntimeProtocols/Safrole.swift index 76deae39..e478051a 100644 --- a/Blockchain/Sources/Blockchain/RuntimeProtocols/Safrole.swift +++ b/Blockchain/Sources/Blockchain/RuntimeProtocols/Safrole.swift @@ -301,7 +301,8 @@ extension Safrole { } let epochMark = try isEpochChange ? EpochMarker( - entropy: newEntropyPool.1, + entropyOne: newEntropyPool.1, + entropyTwo: newEntropyPool.2, validators: ConfigFixedSizeArray(config: config, array: newNextValidators.map(\.bandersnatch)) ) : nil diff --git a/Blockchain/Sources/Blockchain/State/StateKeys.swift b/Blockchain/Sources/Blockchain/State/StateKeys.swift index df80ebbe..556f5faf 100644 --- a/Blockchain/Sources/Blockchain/State/StateKeys.swift +++ b/Blockchain/Sources/Blockchain/State/StateKeys.swift @@ -33,22 +33,22 @@ 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]) + stateKey.append(contentsOf: data[relative: 0 ..< 24]) return Data32(data)! } @@ -316,7 +316,7 @@ public enum StateKeys { } public func encode() -> Data32 { - constructKey(index, length, hash.blake2b256hash().data) + constructKey(index, length, hash.blake2b256hash().data[2...]) } } } diff --git a/Blockchain/Sources/Blockchain/Types/EpochMarker.swift b/Blockchain/Sources/Blockchain/Types/EpochMarker.swift index 4506678e..01d0eb4c 100644 --- a/Blockchain/Sources/Blockchain/Types/EpochMarker.swift +++ b/Blockchain/Sources/Blockchain/Types/EpochMarker.swift @@ -1,20 +1,23 @@ import Utils public struct EpochMarker: Sendable, Equatable, Codable { - public var entropy: Data32 + public var entropyOne: Data32 + public var entropyTwo: Data32 public var validators: ConfigFixedSizeArray< BandersnatchPublicKey, ProtocolConfig.TotalNumberOfValidators > public init( - entropy: Data32, + entropyOne: Data32, + entropyTwo: Data32, validators: ConfigFixedSizeArray< BandersnatchPublicKey, ProtocolConfig.TotalNumberOfValidators > ) { - self.entropy = entropy + self.entropyOne = entropyOne + self.entropyTwo = entropyTwo self.validators = validators } } diff --git a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift index d65f7596..f129f0aa 100644 --- a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift +++ b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift @@ -40,7 +40,7 @@ extension AccumulateFunction { let (exitReason, gas, output) = await invokePVM( config: config, blob: accumulatingAccountDetails.codeHash.data, - pc: 10, + pc: 5, gas: gas, argumentData: argument, ctx: ctx diff --git a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift index 6271b5e2..fedc57e4 100644 --- a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift +++ b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift @@ -28,7 +28,7 @@ extension OnTransferFunction { _ = await invokePVM( config: config, blob: account.codeHash.data, - pc: 15, + pc: 10, gas: gasLimitSum, argumentData: argument, ctx: ctx