Skip to content

Commit

Permalink
add unique id to logger label
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 22, 2024
1 parent 83d7bb4 commit 2017156
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Blockchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions Blockchain/Sources/Blockchain/Validator/ExtrinsicPoolService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<TicketItemAndOutput> = .init()
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions Blockchain/Sources/Blockchain/Validator/ServiceBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions Blockchain/Sources/Blockchain/Validator/ServiceBase2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class ServiceBase2: ServiceBase, @unchecked Sendable {
private let scheduler: Scheduler
private let cancellables: ThreadSafeContainer<Set<IdCancellable>> = .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 {
Expand Down

0 comments on commit 2017156

Please sign in to comment.