Launch 2 nodes

This commit is contained in:
Victor Bodinaud
2025-05-05 16:12:59 +02:00
parent 7fe82bde44
commit 7a71c6bbfe
2 changed files with 41 additions and 15 deletions

View File

@@ -11,7 +11,7 @@ import Network
public class Node {
// Network properties
private var peers: [NWConnection] = []
private let port: NWEndpoint.Port = 8333
private var port: NWEndpoint.Port
private var listener: NWListener?
private let queue = DispatchQueue(label: "com.blockchain.node")
@@ -21,7 +21,8 @@ public class Node {
private var pendingTransactions: [Transaction] = []
private let maxTransactionsPerBlock: Int = 10
public init() {
public init(port: NWEndpoint.Port = 8333) {
self.port = port
self.accountManager = AccountManager()
self.blockchain = Blockchain()
setupListener()
@@ -135,8 +136,9 @@ public class Node {
}
// Connexion à un pair
public func connectToPeer(host: String) {
let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host(host), port: port)
public func connectToPeer(host: String, port: NWEndpoint.Port? = nil) {
let peerPort = port ?? self.port
let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host(host), port: peerPort)
let connection = NWConnection(to: endpoint, using: .tcp)
connection.stateUpdateHandler = { [weak self] state in