✨ Launch 2 nodes
This commit is contained in:
@@ -9,11 +9,22 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import SwiftChainCore
|
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 command: String?
|
||||||
var currentMinerAddress = "MINER1"
|
var currentMinerAddress = "MINER1"
|
||||||
var wallets: [String: Wallet] = [:]
|
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("""
|
print("""
|
||||||
Blockchain CLI - Commandes disponibles:
|
Blockchain CLI - Commandes disponibles:
|
||||||
- createwallet : Créer un nouveau wallet
|
- createwallet : Créer un nouveau wallet
|
||||||
@@ -27,7 +38,10 @@ Blockchain CLI - Commandes disponibles:
|
|||||||
- connect <host> : Se connecter à un pair
|
- connect <host> : Se connecter à un pair
|
||||||
- peers : Liste des pairs connectés
|
- peers : Liste des pairs connectés
|
||||||
- mempool : État du mempool
|
- mempool : État du mempool
|
||||||
|
- switchnode : Basculer entre Node 1 et Node 2
|
||||||
- exit : Quitter
|
- exit : Quitter
|
||||||
|
|
||||||
|
Node actif: \(activeNodeName)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
repeat {
|
repeat {
|
||||||
@@ -38,16 +52,16 @@ repeat {
|
|||||||
case "connect":
|
case "connect":
|
||||||
print("Entrez l'adresse du pair (ex: 192.168.1.100):")
|
print("Entrez l'adresse du pair (ex: 192.168.1.100):")
|
||||||
if let host = readLine() {
|
if let host = readLine() {
|
||||||
node.connectToPeer(host: host)
|
activeNode.connectToPeer(host: host)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "peers":
|
case "peers":
|
||||||
node.listPeers()
|
activeNode.listPeers()
|
||||||
|
|
||||||
case "mine":
|
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("Bloc miné avec succès. Récompense envoyée à \(currentMinerAddress)")
|
||||||
print("Nouveau solde: \(node.getBalance(currentMinerAddress))")
|
print("Nouveau solde: \(activeNode.getBalance(currentMinerAddress))")
|
||||||
} else {
|
} else {
|
||||||
print("Échec du minage")
|
print("Échec du minage")
|
||||||
}
|
}
|
||||||
@@ -55,7 +69,7 @@ repeat {
|
|||||||
case "balance":
|
case "balance":
|
||||||
print("Entrer l'adresse:")
|
print("Entrer l'adresse:")
|
||||||
if let address = readLine() {
|
if let address = readLine() {
|
||||||
let balance = node.getBalance(address)
|
let balance = activeNode.getBalance(address)
|
||||||
print("Solde de \(address): \(balance)")
|
print("Solde de \(address): \(balance)")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +81,7 @@ repeat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "pending":
|
case "pending":
|
||||||
let transactions = node.getPendingTransactions()
|
let transactions = activeNode.getPendingTransactions()
|
||||||
print("Transactions en attente: \(transactions.count)")
|
print("Transactions en attente: \(transactions.count)")
|
||||||
for (index, tx) in transactions.enumerated() {
|
for (index, tx) in transactions.enumerated() {
|
||||||
print("""
|
print("""
|
||||||
@@ -79,7 +93,7 @@ repeat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "validity":
|
case "validity":
|
||||||
let isValid = node.isChainValid()
|
let isValid = activeNode.isChainValid()
|
||||||
print("Chaîne valide: \(isValid)")
|
print("Chaîne valide: \(isValid)")
|
||||||
|
|
||||||
case "exit":
|
case "exit":
|
||||||
@@ -94,7 +108,7 @@ repeat {
|
|||||||
case "listwallet":
|
case "listwallet":
|
||||||
print("\nWallets disponibles:")
|
print("\nWallets disponibles:")
|
||||||
for (address, _) in wallets {
|
for (address, _) in wallets {
|
||||||
print("- \(address) (Solde: \(node.getBalance(address)))")
|
print("- \(address) (Solde: \(activeNode.getBalance(address)))")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "send":
|
case "send":
|
||||||
@@ -131,7 +145,7 @@ repeat {
|
|||||||
transaction.senderPublicKey = wallet.getPublicKeyData()
|
transaction.senderPublicKey = wallet.getPublicKeyData()
|
||||||
if let signature = wallet.signTransaction(transaction) {
|
if let signature = wallet.signTransaction(transaction) {
|
||||||
transaction.signature = signature
|
transaction.signature = signature
|
||||||
if node.submitTransaction(transaction) {
|
if activeNode.submitTransaction(transaction) {
|
||||||
print("Transaction signée et propagée au réseau!")
|
print("Transaction signée et propagée au réseau!")
|
||||||
} else {
|
} else {
|
||||||
print("Erreur lors de l'envoi de la transaction")
|
print("Erreur lors de l'envoi de la transaction")
|
||||||
@@ -141,7 +155,17 @@ repeat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "mempool":
|
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:
|
default:
|
||||||
print("Commande inconnue")
|
print("Commande inconnue")
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import Network
|
|||||||
public class Node {
|
public class Node {
|
||||||
// Network properties
|
// Network properties
|
||||||
private var peers: [NWConnection] = []
|
private var peers: [NWConnection] = []
|
||||||
private let port: NWEndpoint.Port = 8333
|
private var port: NWEndpoint.Port
|
||||||
private var listener: NWListener?
|
private var listener: NWListener?
|
||||||
private let queue = DispatchQueue(label: "com.blockchain.node")
|
private let queue = DispatchQueue(label: "com.blockchain.node")
|
||||||
|
|
||||||
@@ -21,7 +21,8 @@ public class Node {
|
|||||||
private var pendingTransactions: [Transaction] = []
|
private var pendingTransactions: [Transaction] = []
|
||||||
private let maxTransactionsPerBlock: Int = 10
|
private let maxTransactionsPerBlock: Int = 10
|
||||||
|
|
||||||
public init() {
|
public init(port: NWEndpoint.Port = 8333) {
|
||||||
|
self.port = port
|
||||||
self.accountManager = AccountManager()
|
self.accountManager = AccountManager()
|
||||||
self.blockchain = Blockchain()
|
self.blockchain = Blockchain()
|
||||||
setupListener()
|
setupListener()
|
||||||
@@ -135,8 +136,9 @@ public class Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connexion à un pair
|
// Connexion à un pair
|
||||||
public func connectToPeer(host: String) {
|
public func connectToPeer(host: String, port: NWEndpoint.Port? = nil) {
|
||||||
let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host(host), port: port)
|
let peerPort = port ?? self.port
|
||||||
|
let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host(host), port: peerPort)
|
||||||
let connection = NWConnection(to: endpoint, using: .tcp)
|
let connection = NWConnection(to: endpoint, using: .tcp)
|
||||||
|
|
||||||
connection.stateUpdateHandler = { [weak self] state in
|
connection.stateUpdateHandler = { [weak self] state in
|
||||||
|
|||||||
Reference in New Issue
Block a user