From 3af4f9e90ac9be85301758a9417d58969b0cc999 Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Mon, 13 Jan 2025 14:26:04 +0800 Subject: [PATCH] update node test --- .../Blockchain/Types/EpochMarker.swift | 12 +++++++ .../Sources/Blockchain/Types/Header.swift | 4 +-- Node/Tests/NodeTests/NodeTests.swift | 31 +++++++++---------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Types/EpochMarker.swift b/Blockchain/Sources/Blockchain/Types/EpochMarker.swift index f7c5a700..5f63a00d 100644 --- a/Blockchain/Sources/Blockchain/Types/EpochMarker.swift +++ b/Blockchain/Sources/Blockchain/Types/EpochMarker.swift @@ -21,3 +21,15 @@ public struct EpochMarker: Sendable, Equatable, Codable { self.validators = validators } } + +extension EpochMarker: Dummy { + public typealias Config = ProtocolConfigRef + + public static func dummy(config: Config) -> EpochMarker { + EpochMarker( + entropy: Data32(), + ticketsEntropy: Data32(), + validators: try! ConfigFixedSizeArray(config: config, defaultValue: Data32()) + ) + } +} diff --git a/Blockchain/Sources/Blockchain/Types/Header.swift b/Blockchain/Sources/Blockchain/Types/Header.swift index 8c742810..696fdf1c 100644 --- a/Blockchain/Sources/Blockchain/Types/Header.swift +++ b/Blockchain/Sources/Blockchain/Types/Header.swift @@ -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, diff --git a/Node/Tests/NodeTests/NodeTests.swift b/Node/Tests/NodeTests/NodeTests.swift index 001f047d..c0665947 100644 --- a/Node/Tests/NodeTests/NodeTests.swift +++ b/Node/Tests/NodeTests/NodeTests.swift @@ -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 { @@ -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] @@ -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)) @@ -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( @@ -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 @@ -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 } ).build(genesis: .preset(.minimal)) @@ -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( @@ -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