✨ Launch 2 nodes
This commit is contained in:
@@ -9,11 +9,22 @@
|
||||
import Foundation
|
||||
import SwiftChainCore
|
||||
|
||||
let node = Node()
|
||||
// Create two nodes on different ports
|
||||
let node1 = Node(port: 8333)
|
||||
let node2 = Node(port: 8334)
|
||||
|
||||
// Track which node is active
|
||||
var activeNode = node1
|
||||
var activeNodeName = "Node 1"
|
||||
|
||||
var command: String?
|
||||
var currentMinerAddress = "MINER1"
|
||||
var wallets: [String: Wallet] = [:]
|
||||
|
||||
// Connect nodes to each other (add after node initialization)
|
||||
node1.connectToPeer(host: "localhost", port: 8334) // Connect to node2
|
||||
node2.connectToPeer(host: "localhost", port: 8333) // Connect to node1
|
||||
|
||||
print("""
|
||||
Blockchain CLI - Commandes disponibles:
|
||||
- createwallet : Créer un nouveau wallet
|
||||
@@ -27,7 +38,10 @@ Blockchain CLI - Commandes disponibles:
|
||||
- connect <host> : Se connecter à un pair
|
||||
- peers : Liste des pairs connectés
|
||||
- mempool : État du mempool
|
||||
- switchnode : Basculer entre Node 1 et Node 2
|
||||
- exit : Quitter
|
||||
|
||||
Node actif: \(activeNodeName)
|
||||
""")
|
||||
|
||||
repeat {
|
||||
@@ -38,16 +52,16 @@ repeat {
|
||||
case "connect":
|
||||
print("Entrez l'adresse du pair (ex: 192.168.1.100):")
|
||||
if let host = readLine() {
|
||||
node.connectToPeer(host: host)
|
||||
activeNode.connectToPeer(host: host)
|
||||
}
|
||||
|
||||
case "peers":
|
||||
node.listPeers()
|
||||
activeNode.listPeers()
|
||||
|
||||
case "mine":
|
||||
if let _ = node.mineBlock(minerAddress: currentMinerAddress) {
|
||||
if let _ = activeNode.mineBlock(minerAddress: currentMinerAddress) {
|
||||
print("Bloc miné avec succès. Récompense envoyée à \(currentMinerAddress)")
|
||||
print("Nouveau solde: \(node.getBalance(currentMinerAddress))")
|
||||
print("Nouveau solde: \(activeNode.getBalance(currentMinerAddress))")
|
||||
} else {
|
||||
print("Échec du minage")
|
||||
}
|
||||
@@ -55,7 +69,7 @@ repeat {
|
||||
case "balance":
|
||||
print("Entrer l'adresse:")
|
||||
if let address = readLine() {
|
||||
let balance = node.getBalance(address)
|
||||
let balance = activeNode.getBalance(address)
|
||||
print("Solde de \(address): \(balance)")
|
||||
}
|
||||
|
||||
@@ -67,7 +81,7 @@ repeat {
|
||||
}
|
||||
|
||||
case "pending":
|
||||
let transactions = node.getPendingTransactions()
|
||||
let transactions = activeNode.getPendingTransactions()
|
||||
print("Transactions en attente: \(transactions.count)")
|
||||
for (index, tx) in transactions.enumerated() {
|
||||
print("""
|
||||
@@ -79,7 +93,7 @@ repeat {
|
||||
}
|
||||
|
||||
case "validity":
|
||||
let isValid = node.isChainValid()
|
||||
let isValid = activeNode.isChainValid()
|
||||
print("Chaîne valide: \(isValid)")
|
||||
|
||||
case "exit":
|
||||
@@ -94,7 +108,7 @@ repeat {
|
||||
case "listwallet":
|
||||
print("\nWallets disponibles:")
|
||||
for (address, _) in wallets {
|
||||
print("- \(address) (Solde: \(node.getBalance(address)))")
|
||||
print("- \(address) (Solde: \(activeNode.getBalance(address)))")
|
||||
}
|
||||
|
||||
case "send":
|
||||
@@ -131,7 +145,7 @@ repeat {
|
||||
transaction.senderPublicKey = wallet.getPublicKeyData()
|
||||
if let signature = wallet.signTransaction(transaction) {
|
||||
transaction.signature = signature
|
||||
if node.submitTransaction(transaction) {
|
||||
if activeNode.submitTransaction(transaction) {
|
||||
print("Transaction signée et propagée au réseau!")
|
||||
} else {
|
||||
print("Erreur lors de l'envoi de la transaction")
|
||||
@@ -141,7 +155,17 @@ repeat {
|
||||
}
|
||||
|
||||
case "mempool":
|
||||
node.printMemPoolStatus()
|
||||
activeNode.printMemPoolStatus()
|
||||
|
||||
case "switchnode":
|
||||
if activeNode === node1 {
|
||||
activeNode = node2
|
||||
activeNodeName = "Node 2"
|
||||
} else {
|
||||
activeNode = node1
|
||||
activeNodeName = "Node 1"
|
||||
}
|
||||
print("Node actif: \(activeNodeName)")
|
||||
|
||||
default:
|
||||
print("Commande inconnue")
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user