🐛 Fix node connection & sync
This commit is contained in:
@@ -9,6 +9,9 @@ import Foundation
|
||||
import Network
|
||||
|
||||
public class Node {
|
||||
private let nodeId = UUID().uuidString // Identifiant unique pour chaque nœud
|
||||
private var processedMessages = Set<String>() // Pour traquer les messages déjà traités
|
||||
|
||||
// Network properties
|
||||
private var peers: [NWConnection] = []
|
||||
private var port: NWEndpoint.Port
|
||||
@@ -38,6 +41,15 @@ public class Node {
|
||||
struct NetworkMessage: Codable {
|
||||
let type: MessageType
|
||||
let data: Data
|
||||
let senderId: String // Identifiant du nœud émetteur
|
||||
let messageId: String // Identifiant unique du message
|
||||
|
||||
init(type: MessageType, data: Data, senderId: String) {
|
||||
self.type = type
|
||||
self.data = data
|
||||
self.senderId = senderId
|
||||
self.messageId = UUID().uuidString
|
||||
}
|
||||
}
|
||||
|
||||
// Configuration du listener
|
||||
@@ -178,7 +190,7 @@ public class Node {
|
||||
func broadcastTransaction(_ transaction: Transaction) {
|
||||
do {
|
||||
let transactionData = try JSONEncoder().encode(transaction)
|
||||
let message = NetworkMessage(type: .transaction, data: transactionData)
|
||||
let message = NetworkMessage(type: .transaction, data: transactionData, senderId: nodeId)
|
||||
let messageData = try JSONEncoder().encode(message)
|
||||
|
||||
print("Broadcasting transaction to \(peers.count) peers")
|
||||
@@ -220,6 +232,15 @@ public class Node {
|
||||
let message = try JSONDecoder().decode(NetworkMessage.self, from: data)
|
||||
print("Message decoded successfully, type: \(message.type)")
|
||||
|
||||
// Vérifier si le message a déjà été traité
|
||||
if processedMessages.contains(message.messageId) {
|
||||
print("Message déjà traité, ignoré")
|
||||
return
|
||||
}
|
||||
|
||||
// Ajouter le message à la liste des messages traités
|
||||
processedMessages.insert(message.messageId)
|
||||
|
||||
switch message.type {
|
||||
case .transaction:
|
||||
let transaction = try JSONDecoder().decode(Transaction.self, from: message.data)
|
||||
@@ -261,7 +282,7 @@ public class Node {
|
||||
|
||||
private func requestBlockchain(from peer: NWConnection) {
|
||||
do {
|
||||
let message = NetworkMessage(type: .blockchainRequest, data: Data())
|
||||
let message = NetworkMessage(type: .blockchainRequest, data: Data(), senderId: nodeId)
|
||||
let messageData = try JSONEncoder().encode(message)
|
||||
|
||||
peer.send(content: messageData, completion: .contentProcessed { error in
|
||||
@@ -279,7 +300,7 @@ public class Node {
|
||||
private func sendBlockchain(to peer: NWConnection) {
|
||||
do {
|
||||
let chainData = try JSONEncoder().encode(blockchain.chain)
|
||||
let message = NetworkMessage(type: .blockchainResponse, data: chainData)
|
||||
let message = NetworkMessage(type: .blockchainResponse, data: chainData, senderId: nodeId)
|
||||
let messageData = try JSONEncoder().encode(message)
|
||||
|
||||
peer.send(content: messageData, completion: .contentProcessed { error in
|
||||
@@ -331,6 +352,11 @@ public class Node {
|
||||
private func handleReceivedBlock(_ block: Block) {
|
||||
print("Received new block: \(block.hash)")
|
||||
|
||||
if blockchain.chain.contains(where: { $0.hash == block.hash }) {
|
||||
print("Bloc déjà dans la chaîne")
|
||||
return
|
||||
}
|
||||
|
||||
// Vérifier que le bloc suit bien notre dernier bloc
|
||||
guard let lastBlock = blockchain.chain.last else {
|
||||
print("No existing chain")
|
||||
@@ -338,7 +364,13 @@ public class Node {
|
||||
}
|
||||
|
||||
if block.previousHash != lastBlock.hash {
|
||||
print("Block does not connect to our chain")
|
||||
print("Le bloc ne se connecte pas à notre chaîne")
|
||||
|
||||
// Demander la chaîne complète aux pairs pour se synchroniser
|
||||
for peer in peers {
|
||||
requestBlockchain(from: peer)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -367,7 +399,7 @@ public class Node {
|
||||
private func broadcastBlock(_ block: Block) {
|
||||
do {
|
||||
let blockData = try JSONEncoder().encode(block)
|
||||
let message = NetworkMessage(type: .newBlock, data: blockData)
|
||||
let message = NetworkMessage(type: .newBlock, data: blockData, senderId: nodeId)
|
||||
let messageData = try JSONEncoder().encode(message)
|
||||
|
||||
print("Broadcasting block to peers")
|
||||
|
||||
Reference in New Issue
Block a user