Skip to content

Commit

Permalink
update node test
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Jan 13, 2025
1 parent f0ab698 commit 3af4f9e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
12 changes: 12 additions & 0 deletions Blockchain/Sources/Blockchain/Types/EpochMarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ public struct EpochMarker: Sendable, Equatable, Codable {
self.validators = validators
}
}

extension EpochMarker: Dummy {
public typealias Config = ProtocolConfigRef

Check failure on line 27 in Blockchain/Sources/Blockchain/Types/EpochMarker.swift

View workflow job for this annotation

GitHub Actions / Swift Lint

Trailing Whitespace (trailing_whitespace)

Lines should not have trailing whitespace
public static func dummy(config: Config) -> EpochMarker {
EpochMarker(
entropy: Data32(),
ticketsEntropy: Data32(),
validators: try! ConfigFixedSizeArray(config: config, defaultValue: Data32())
)
}
}
4 changes: 2 additions & 2 deletions Blockchain/Sources/Blockchain/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ extension HeaderRef: Codable {

extension Header.Unsigned: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(config _: Config) -> Header.Unsigned {
public static func dummy(config: Config) -> Header.Unsigned {
Header.Unsigned(
parentHash: Data32(),
priorStateRoot: Data32(),
extrinsicsHash: Data32(),
timeslot: 0,
epoch: nil,
epoch: EpochMarker.dummy(config: config),
winningTickets: nil,
offendersMarkers: [],
authorIndex: 0,
Expand Down
31 changes: 15 additions & 16 deletions Node/Tests/NodeTests/NodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import Utils
@testable import Node

final class NodeTests {
var dataBaseIndex: Int = 0

let path = {
let tmpDir = FileManager.default.temporaryDirectory
return tmpDir.appendingPathComponent("\(UUID().uuidString)")
}()

func getDatabase() -> Database {
dataBaseIndex += 1
return Database.rocksDB(path: path.appendingPathComponent("\(dataBaseIndex)"))
func getDatabase(_ index: Int) -> Database {
return Database.rocksDB(path: path.appendingPathComponent("\(index)"))
}

deinit {
Expand Down Expand Up @@ -54,7 +52,7 @@ final class NodeTests {

@Test func validatorNodeRocksDB() async throws {
let (nodes, scheduler) = try await Topology(
nodes: [NodeDescription(isValidator: true, database: getDatabase())]
nodes: [NodeDescription(isValidator: true, database: getDatabase(0))]
).build(genesis: .preset(.minimal))

let (validatorNode, storeMiddlware) = nodes[0]
Expand Down Expand Up @@ -86,8 +84,8 @@ final class NodeTests {
// Create validator and full node
let (nodes, scheduler) = try await Topology(
nodes: [
NodeDescription(isValidator: true, database: getDatabase()),
NodeDescription(devSeed: 1, database: getDatabase()),
NodeDescription(isValidator: true, database: getDatabase(0)),
NodeDescription(devSeed: 1, database: getDatabase(1)),
],
connections: [(0, 1)]
).build(genesis: .preset(.minimal))
Expand Down Expand Up @@ -136,12 +134,12 @@ final class NodeTests {
@Test func multiplePeers() async throws {
// Create multiple nodes
var nodeDescriptions: [NodeDescription] = [
NodeDescription(isValidator: true, database: getDatabase()),
NodeDescription(isValidator: true, devSeed: 1, database: getDatabase()),
NodeDescription(isValidator: true, database: getDatabase(0)),
NodeDescription(isValidator: true, devSeed: 1, database: getDatabase(1)),
]
// Add 18 non-validator nodes
for i in 2...19 {
nodeDescriptions.append(NodeDescription(devSeed: UInt32(i), database: getDatabase()))
nodeDescriptions.append(NodeDescription(devSeed: UInt32(i), database: getDatabase(i)))
}

let (nodes, scheduler) = try await Topology(
Expand Down Expand Up @@ -187,8 +185,8 @@ final class NodeTests {
@Test func moreMultiplePeers() async throws {
// Create multiple nodes
var nodeDescriptions: [NodeDescription] = [
NodeDescription(isValidator: true, database: getDatabase()),
NodeDescription(isValidator: true, devSeed: 1, database: getDatabase()),
NodeDescription(isValidator: true, database: getDatabase(0)),
NodeDescription(isValidator: true, devSeed: 1, database: getDatabase(1)),
]

// Add 18 non-validator nodes
Expand All @@ -198,8 +196,8 @@ final class NodeTests {

let (nodes, scheduler) = try await Topology(
nodes: nodeDescriptions,
connections: (0..<20).flatMap { i in
(i + 1..<20).map { j in (i, j) } // Fully connected topology
connections: (0..<2).flatMap { i in
(2..<20).map { j in (i, j) } //connected topology

Check failure on line 200 in Node/Tests/NodeTests/NodeTests.swift

View workflow job for this annotation

GitHub Actions / Swift Lint

Comment Spacing (comment_spacing)

Prefer at least one space after slashes for comments
}
).build(genesis: .preset(.minimal))

Expand All @@ -213,8 +211,8 @@ final class NodeTests {
let (node1, _) = nonValidatorNodes[0]
let (node2, _) = nonValidatorNodes[1]
// Verify connections for a sample of non-validator nodes
#expect(node1.network.peersCount == 19)
#expect(node2.network.peersCount == 19)
#expect(node1.network.peersCount == 2)
#expect(node2.network.peersCount == 2)
// Advance time to produce blocks
for _ in 0..<30 {
await scheduler.advance(
Expand All @@ -225,6 +223,7 @@ final class NodeTests {
for (_, middleware) in nonValidatorNodes {
await middleware.wait()
}
try await Task.sleep(for: .milliseconds(500))
}
try await Task.sleep(for: .milliseconds(nodes.count * 100))
let validator1BestHead = await validator1.dataProvider.bestHead
Expand Down

0 comments on commit 3af4f9e

Please sign in to comment.