Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Jan 10, 2025
1 parent b85254f commit 5c68e4a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public actor BlockchainDataProvider: Sendable {
if header.value.timeslot > bestHead.timeslot {
let number = try await dataProvider.getBlockNumber(hash: head).unwrap()
bestHead = HeadInfo(hash: head, timeslot: header.value.timeslot, number: number)
logger.info("best head with timeslot \(header.value.timeslot) updated to \(bestHead.hash)")
} else {
logger.warning("Found a block with timeslot \(header.value.timeslot) but best head is \(bestHead.timeslot)")
}
Expand Down Expand Up @@ -103,7 +102,7 @@ extension BlockchainDataProvider {

// add forks of finalized head is not allowed
public func add(block: BlockRef) async throws {
logger.info("adding block: \(block.hash)")
logger.debug("adding block: \(block.hash)")

// require parent exists (i.e. not purged) and block is not fork of any finalized block
guard try await hasBlock(hash: block.header.parentHash), block.header.timeslot > finalizedHead.timeslot else {
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 @@ -163,7 +163,7 @@ public final class BlockAuthor: ServiceBase2, @unchecked Sendable {
await withSpan("BlockAuthor.newBlock", logger: logger) { _ in
// TODO: add timeout
let block = try await createNewBlock(timeslot: timeslot, claim: claim)
logger.info("New block created: #\(block.header.timeslot) \(block.hash) on parent #\(block.header.parentHash)")
logger.debug("New block created: #\(block.header.timeslot) \(block.hash) on parent #\(block.header.parentHash)")
publish(RuntimeEvents.BlockAuthored(block: block))
}
}
Expand Down
2 changes: 1 addition & 1 deletion Database/Sources/Database/RocksDBBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extension RocksDBBackend: BlockchainDataProviderProtocol {
}

public func add(block: BlockRef) async throws {
logger.info("add(block:) \(block.hash)")
logger.debug("add(block:) \(block.hash)")

// TODO: batch put

Expand Down
16 changes: 8 additions & 8 deletions Networking/Sources/MsQuicSwift/QuicConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public final class QuicConnection: Sendable {
}

public func connect(to address: NetAddr) throws {
logger.info("connecting to \(address)")
logger.debug("connecting to \(address)")
try storage.write { storage in
guard var storage2 = storage else {
throw QuicError.alreadyClosed
Expand Down Expand Up @@ -131,7 +131,7 @@ public final class QuicConnection: Sendable {
}

public func shutdown(errorCode: QuicErrorCode = .success) throws {
logger.info("closing connection")
logger.debug("closing connection")
try storage.write { storage in
guard let storage2 = storage else {
throw QuicError.alreadyClosed
Expand All @@ -152,7 +152,7 @@ public final class QuicConnection: Sendable {
}

public func createStream() throws -> QuicStream {
logger.info("creating stream")
logger.debug("creating stream")

return try storage.read { storage in
guard let storage, storage.state == .started else {
Expand Down Expand Up @@ -210,7 +210,7 @@ private class ConnectionHandle {
fileprivate func callbackHandler(event: UnsafePointer<QUIC_CONNECTION_EVENT>) -> QuicStatus {
switch event.pointee.Type {
case QUIC_CONNECTION_EVENT_PEER_CERTIFICATE_RECEIVED:
logger.info("Peer certificate received")
logger.debug("Peer certificate received")
if let connection {
let evtData = event.pointee.PEER_CERTIFICATE_RECEIVED
let data: Data?
Expand Down Expand Up @@ -239,7 +239,7 @@ private class ConnectionHandle {
connection.handler.shutdownInitiated(connection, reason: .idle)
}
} else {
logger.info("Shut down by transport. Status: \(status) Error: \(evtData.ErrorCode)")
logger.debug("Shut down by transport. Status: \(status) Error: \(evtData.ErrorCode)")
if let connection {
connection.handler.shutdownInitiated(
connection,
Expand All @@ -249,14 +249,14 @@ private class ConnectionHandle {
}

case QUIC_CONNECTION_EVENT_SHUTDOWN_INITIATED_BY_PEER:
logger.info("Shut down by peer. Error: \(event.pointee.SHUTDOWN_INITIATED_BY_PEER.ErrorCode)")
logger.debug("Shut down by peer. Error: \(event.pointee.SHUTDOWN_INITIATED_BY_PEER.ErrorCode)")
if let connection {
let errorCode = QuicErrorCode(event.pointee.SHUTDOWN_INITIATED_BY_PEER.ErrorCode)
connection.handler.shutdownInitiated(connection, reason: .byPeer(code: errorCode))
}

case QUIC_CONNECTION_EVENT_SHUTDOWN_COMPLETE:
logger.info("Shutdown complete")
logger.debug("Shutdown complete")
if let connection {
connection.handler.shutdownComplete(connection)
}
Expand All @@ -270,7 +270,7 @@ private class ConnectionHandle {
Unmanaged.passUnretained(self).release() // !! release -1

case QUIC_CONNECTION_EVENT_PEER_STREAM_STARTED:
logger.info("Peer stream started")
logger.debug("Peer stream started")
let streamPtr = event.pointee.PEER_STREAM_STARTED.Stream
if let connection, let streamPtr, connection.api != nil {
let stream = QuicStream(connection: connection, stream: streamPtr, handler: connection.handler)
Expand Down
16 changes: 8 additions & 8 deletions Networking/Sources/MsQuicSwift/QuicStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class QuicStream: Sendable {
}

public func shutdown(errorCode: QuicErrorCode = .success) throws {
logger.info("closing stream \(errorCode)")
logger.debug("closing stream \(errorCode)")

try storage.write { storage in
guard let storage2 = storage else {
Expand All @@ -94,7 +94,7 @@ public final class QuicStream: Sendable {
}

public func send(data: Data, start: Bool = false, finish: Bool = false) throws {
logger.info("Sending \(data.count) bytes")
logger.debug("Sending \(data.count) bytes")

try storage.read { storage in
guard let storage, let api = storage.connection.api else {
Expand All @@ -104,7 +104,7 @@ public final class QuicStream: Sendable {
let messageLength = data.count

if messageLength == 0 {
logger.info("No data to send.")
logger.debug("No data to send.")
throw SendError.emptyData // Throw a specific error or return
}

Expand Down Expand Up @@ -173,7 +173,7 @@ private class StreamHandle {
fileprivate func callbackHandler(event: UnsafePointer<QUIC_STREAM_EVENT>) -> QuicStatus {
switch event.pointee.Type {
case QUIC_STREAM_EVENT_SEND_COMPLETE:
logger.info("Stream send completed")
logger.debug("Stream send completed")
if let clientContext = event.pointee.SEND_COMPLETE.ClientContext {
clientContext.deallocate() // !! deallocate
}
Expand All @@ -188,7 +188,7 @@ private class StreamHandle {
totalSize += Int(buffer.Length)
}

logger.info("Stream received \(totalSize) bytes")
logger.debug("Stream received \(totalSize) bytes")

var receivedData = Data(capacity: totalSize)

Expand All @@ -207,16 +207,16 @@ private class StreamHandle {
}

case QUIC_STREAM_EVENT_PEER_SEND_SHUTDOWN:
logger.info("Peer send shutdown")
logger.trace("Peer send shutdown")
if let stream {
stream.handler.dataReceived(stream, data: nil)
}

case QUIC_STREAM_EVENT_PEER_SEND_ABORTED:
logger.info("Peer send aborted")
logger.trace("Peer send aborted")

case QUIC_STREAM_EVENT_SHUTDOWN_COMPLETE:
logger.info("Stream shutdown complete")
logger.trace("Stream shutdown complete")

let evtData = event.pointee.SHUTDOWN_COMPLETE
if let stream {
Expand Down
4 changes: 2 additions & 2 deletions Node/Sources/Node/NetworkingProtocol/SyncManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public actor SyncManager: Sendable {
}

private func importBlock(currentTimeslot: TimeslotIndex, newHeader: HeaderRef, peer: PeerId) {
logger.info("importing block", metadata: ["hash": "\(newHeader.hash)", "remote": "\(peer)"])
logger.debug("importing block", metadata: ["hash": "\(newHeader.hash)", "remote": "\(peer)"])
let blockchain = blockchain
let network = network
Task.detached {
Expand All @@ -163,7 +163,7 @@ public actor SyncManager: Sendable {
}
// reverse to import old block first
for block in blocks.reversed() {
logger.info("blocks reversed", metadata: ["hash": "\(String(describing: block.hash))"])
logger.debug("blocks reversed", metadata: ["hash": "\(String(describing: block.hash))"])
try await blockchain.importBlock(block)
}
} catch {
Expand Down
10 changes: 3 additions & 7 deletions Node/Tests/NodeTests/NodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ final class NodeTests {
// Verify connections for a sample of non-validator nodes
#expect(node1.network.peersCount == 19)
#expect(node2.network.peersCount == 19)
// Advance time and verify sync
// Advance time to produce blocks
for _ in 0..<20 {
await scheduler.advance(
by: TimeInterval(validator1.blockchain.config.value.slotPeriodSeconds))
Expand All @@ -175,8 +175,6 @@ final class NodeTests {
}
}

try await Task.sleep(for: .milliseconds(nodes.count * 100))

let validator1BestHead = await validator1.dataProvider.bestHead
let validator2BestHead = await validator2.dataProvider.bestHead

Expand Down Expand Up @@ -218,8 +216,8 @@ final class NodeTests {
// Verify connections for a sample of non-validator nodes
#expect(node1.network.peersCount == 19)
#expect(node2.network.peersCount == 19)
// Advance time and verify sync
for _ in 0..<3 {
// Advance time to produce blocks
for _ in 0..<20 {
await scheduler.advance(
by: TimeInterval(validator1.blockchain.config.value.slotPeriodSeconds))
await validator1StoreMiddlware.wait()
Expand All @@ -230,8 +228,6 @@ final class NodeTests {
}
}

try await Task.sleep(for: .milliseconds(nodes.count * 500))

let validator1BestHead = await validator1.dataProvider.bestHead
let validator2BestHead = await validator2.dataProvider.bestHead

Expand Down
2 changes: 1 addition & 1 deletion Utils/Sources/Utils/EventBus/StoreMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct StoreMiddleware: MiddlewareProtocol {

@discardableResult
public func wait() async -> [Sendable] {
try? await Task.sleep(for: .milliseconds(5))
try? await Task.sleep(for: .milliseconds(50))

let value = storage.value

Expand Down

0 comments on commit 5c68e4a

Please sign in to comment.