Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 22, 2024
1 parent a6438ee commit 83d7bb4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
7 changes: 4 additions & 3 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public final class Runtime {
case preimagesNotSorted
case invalidPreimageServiceIndex
case duplicatedPreimage
case notBlockAuthor
case invalidAuthorTicket
case invalidAuthorKey
case invalidBlockSeal(any Swift.Error)
case invalidVrfSignature
case other(any Swift.Error)
Expand Down Expand Up @@ -95,7 +96,7 @@ public final class Runtime {
)
}.mapError(Error.invalidBlockSeal).get()
guard ticket.id == vrfOutput else {
throw Error.notBlockAuthor
throw Error.invalidAuthorTicket
}

entropyVRFInputData = SigningContext.entropyInputData(entropy: vrfOutput)
Expand All @@ -104,7 +105,7 @@ public final class Runtime {
let key = keys[Int(index)]
guard key == blockAuthorKey.data else {
logger.debug("expected key: \(key.toHexString()), got key: \(blockAuthorKey.data.toHexString())")
throw Error.notBlockAuthor
throw Error.invalidAuthorKey
}
let vrfInputData = SigningContext.fallbackSealInputData(entropy: state.entropyPool.t3)
vrfOutput = try Result {
Expand Down
4 changes: 1 addition & 3 deletions Blockchain/Sources/Blockchain/Validator/SafroleService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public final class SafroleService: ServiceBase, @unchecked Sendable {
continue
}

logger.debug("Generating tickets for validator \(pubkey)")

try withSpan("generateTickets") { _ in
let tickets = try SafroleService.generateTickets(
count: TicketIndex(config.value.ticketEntriesPerValidator),
Expand All @@ -86,7 +84,7 @@ public final class SafroleService: ServiceBase, @unchecked Sendable {
}

if events.isEmpty {
logger.debug("Not in next validators")
logger.trace("Not in next validators")
}

return events
Expand Down
2 changes: 1 addition & 1 deletion Blockchain/Tests/BlockchainTests/BlockAuthorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct BlockAuthorTests {
// setupTestLogger()

let services = await BlockchainServices()
let blockAuthor = await services.blockAuthor()
let blockAuthor = await services.blockAuthor
let runtime = Runtime(config: services.config)
return (services, blockAuthor, runtime)
}
Expand Down
67 changes: 50 additions & 17 deletions Blockchain/Tests/BlockchainTests/BlockchainServices.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Blockchain
import Utils

struct BlockchainServices {
class BlockchainServices {
let config: ProtocolConfigRef
let timeProvider: MockTimeProvider
let dataProvider: BlockchainDataProvider
Expand All @@ -12,6 +12,12 @@ struct BlockchainServices {
let genesisBlock: BlockRef
let genesisState: StateRef

private var _blockchain: Blockchain?
private weak var _blockchainRef: Blockchain?

private var _blockAuthor: BlockAuthor?
private weak var _blockAuthorRef: BlockAuthor?

init(
config: ProtocolConfigRef = .dev,
timeProvider: MockTimeProvider = MockTimeProvider(time: 988)
Expand All @@ -32,23 +38,50 @@ struct BlockchainServices {
keystore = try! await DevKeyStore()
}

func blockchain() async -> Blockchain {
try! await Blockchain(
config: config,
dataProvider: dataProvider,
timeProvider: timeProvider,
eventBus: eventBus
)
deinit {
_blockchain = nil
_blockAuthor = nil

if let _blockchainRef {
fatalError("BlockchainServices: blockchain still alive. retain count: \(_getRetainCount(_blockchainRef))")
}

if let _blockAuthorRef {
fatalError("BlockchainServices: blockAuthor still alive. retain count: \(_getRetainCount(_blockAuthorRef))")
}
}

var blockchain: Blockchain {
get async {
if let _blockchain {
return _blockchain
}
_blockchain = try! await Blockchain(
config: config,
dataProvider: dataProvider,
timeProvider: timeProvider,
eventBus: eventBus
)
_blockchainRef = _blockchain
return _blockchain!
}
}

func blockAuthor() async -> BlockAuthor {
await BlockAuthor(
config: config,
dataProvider: dataProvider,
eventBus: eventBus,
keystore: keystore,
scheduler: scheduler,
extrinsicPool: ExtrinsicPoolService(config: config, dataProvider: dataProvider, eventBus: eventBus)
)
var blockAuthor: BlockAuthor {
get async {
if let _blockAuthor {
return _blockAuthor
}
_blockAuthor = await BlockAuthor(
config: config,
dataProvider: dataProvider,
eventBus: eventBus,
keystore: keystore,
scheduler: scheduler,
extrinsicPool: ExtrinsicPoolService(config: config, dataProvider: dataProvider, eventBus: eventBus)
)
_blockAuthorRef = _blockAuthor
return _blockAuthor!
}
}
}
4 changes: 2 additions & 2 deletions Blockchain/Tests/BlockchainTests/ValidatorServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import Utils

struct ValidatorServiceTests {
func setup(time: TimeInterval = 988) async throws -> (BlockchainServices, ValidatorService) {
// setupTestLogger()
setupTestLogger()

let services = await BlockchainServices(
timeProvider: MockTimeProvider(time: time)
)
let validatorService = await ValidatorService(
blockchain: services.blockchain(),
blockchain: services.blockchain,
keystore: services.keystore,
eventBus: services.eventBus,
scheduler: services.scheduler,
Expand Down

0 comments on commit 83d7bb4

Please sign in to comment.