diff --git a/Blockchain/Sources/Blockchain/Blockchain.swift b/Blockchain/Sources/Blockchain/Blockchain.swift index a83cb5e5..b2284727 100644 --- a/Blockchain/Sources/Blockchain/Blockchain.swift +++ b/Blockchain/Sources/Blockchain/Blockchain.swift @@ -15,7 +15,7 @@ public final class Blockchain: ServiceBase, @unchecked Sendable { self.dataProvider = dataProvider self.timeProvider = timeProvider - super.init(logger: Logger(label: "Blockchain"), config: config, eventBus: eventBus) + super.init(id: "Blockchain", config: config, eventBus: eventBus) await subscribe(RuntimeEvents.BlockAuthored.self, id: "Blockchain.BlockAuthored") { [weak self] event in try await self?.on(blockAuthored: event) diff --git a/Blockchain/Sources/Blockchain/Validator/BlockAuthor.swift b/Blockchain/Sources/Blockchain/Validator/BlockAuthor.swift index 2fe15743..728c11b2 100644 --- a/Blockchain/Sources/Blockchain/Validator/BlockAuthor.swift +++ b/Blockchain/Sources/Blockchain/Validator/BlockAuthor.swift @@ -23,7 +23,7 @@ public final class BlockAuthor: ServiceBase2, @unchecked Sendable { self.keystore = keystore self.extrinsicPool = extrinsicPool - super.init(logger: Logger(label: "BlockAuthor"), config: config, eventBus: eventBus, scheduler: scheduler) + super.init(id: "BlockAuthor", config: config, eventBus: eventBus, scheduler: scheduler) await subscribe(RuntimeEvents.SafroleTicketsGenerated.self, id: "BlockAuthor.SafroleTicketsGenerated") { [weak self] event in try await self?.on(safroleTicketsGenerated: event) diff --git a/Blockchain/Sources/Blockchain/Validator/ExtrinsicPoolService.swift b/Blockchain/Sources/Blockchain/Validator/ExtrinsicPoolService.swift index 6a9f0522..7a03f478 100644 --- a/Blockchain/Sources/Blockchain/Validator/ExtrinsicPoolService.swift +++ b/Blockchain/Sources/Blockchain/Validator/ExtrinsicPoolService.swift @@ -4,7 +4,7 @@ import Utils private typealias TicketItem = ExtrinsicTickets.TicketItem private actor ServiceStorage { - let logger: Logger + var logger: Logger! // sorted array ordered by output var pendingTickets: SortedUniqueArray = .init() @@ -13,11 +13,14 @@ private actor ServiceStorage { var entropy: Data32 = .init() let ringContext: Bandersnatch.RingContext - init(logger: Logger, ringContext: Bandersnatch.RingContext) { - self.logger = logger + init(ringContext: Bandersnatch.RingContext) { self.ringContext = ringContext } + func setLogger(_ logger: Logger) { + self.logger = logger + } + func add(tickets: [TicketItem], config: ProtocolConfigRef) { for ticket in tickets { if (try? ticket.validate(config: config)) == nil { @@ -77,12 +80,12 @@ public final class ExtrinsicPoolService: ServiceBase, @unchecked Sendable { ) async { self.dataProvider = dataProvider - let logger = Logger(label: "ExtrinsicPoolService") - let ringContext = try! Bandersnatch.RingContext(size: UInt(config.value.totalNumberOfValidators)) - storage = ServiceStorage(logger: logger, ringContext: ringContext) + storage = ServiceStorage(ringContext: ringContext) + + super.init(id: "ExtrinsicPoolService", config: config, eventBus: eventBus) - super.init(logger: logger, config: config, eventBus: eventBus) + await storage.setLogger(logger) await subscribe(RuntimeEvents.SafroleTicketsGenerated.self, id: "ExtrinsicPool.SafroleTicketsGenerated") { [weak self] event in try await self?.on(safroleTicketsGenerated: event) diff --git a/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift b/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift index 2c58c0ac..b893bd6f 100644 --- a/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift +++ b/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift @@ -28,7 +28,7 @@ public final class GuaranteeingService: ServiceBase2, @unchecked Sendable { self.runtime = runtime self.extrinsicPool = extrinsicPool - super.init(logger: Logger(label: "BlockAuthor"), config: config, eventBus: eventBus, scheduler: scheduler) + super.init(id: "BlockAuthor", config: config, eventBus: eventBus, scheduler: scheduler) } public func on(genesis _: StateRef) async {} diff --git a/Blockchain/Sources/Blockchain/Validator/SafroleService.swift b/Blockchain/Sources/Blockchain/Validator/SafroleService.swift index c6bbff44..646c4d69 100644 --- a/Blockchain/Sources/Blockchain/Validator/SafroleService.swift +++ b/Blockchain/Sources/Blockchain/Validator/SafroleService.swift @@ -27,7 +27,7 @@ public final class SafroleService: ServiceBase, @unchecked Sendable { self.keystore = keystore ringContext = try! Bandersnatch.RingContext(size: UInt(config.value.totalNumberOfValidators)) - super.init(logger: Logger(label: "SafroleService"), config: config, eventBus: eventBus) + super.init(id: "SafroleService", config: config, eventBus: eventBus) await subscribe(RuntimeEvents.BlockImported.self, id: "SafroleService.BlockImported") { [weak self] event in try await self?.on(blockImported: event) diff --git a/Blockchain/Sources/Blockchain/Validator/ServiceBase.swift b/Blockchain/Sources/Blockchain/Validator/ServiceBase.swift index 84effa38..3eb81503 100644 --- a/Blockchain/Sources/Blockchain/Validator/ServiceBase.swift +++ b/Blockchain/Sources/Blockchain/Validator/ServiceBase.swift @@ -2,13 +2,15 @@ import TracingUtils import Utils public class ServiceBase { - public let logger: Logger + public let id: UniqueId + let logger: Logger public let config: ProtocolConfigRef private let eventBus: EventBus private let subscriptionTokens: ThreadSafeContainer<[EventBus.SubscriptionToken]> = .init([]) - init(logger: Logger, config: ProtocolConfigRef, eventBus: EventBus) { - self.logger = logger + init(id: UniqueId, config: ProtocolConfigRef, eventBus: EventBus) { + self.id = id + logger = Logger(label: id) self.config = config self.eventBus = eventBus } diff --git a/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift b/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift index 2e62af7c..eaae3717 100644 --- a/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift +++ b/Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift @@ -20,9 +20,9 @@ public class ServiceBase2: ServiceBase, @unchecked Sendable { private let scheduler: Scheduler private let cancellables: ThreadSafeContainer> = .init([]) - public init(logger: Logger, config: ProtocolConfigRef, eventBus: EventBus, scheduler: Scheduler) { + public init(id: UniqueId, config: ProtocolConfigRef, eventBus: EventBus, scheduler: Scheduler) { self.scheduler = scheduler - super.init(logger: logger, config: config, eventBus: eventBus) + super.init(id: id, config: config, eventBus: eventBus) } deinit {