diff --git a/SwiftChain/Models/Blockchain.swift b/SwiftChain/Models/Blockchain.swift index a48f6a3..a00ee33 100644 --- a/SwiftChain/Models/Blockchain.swift +++ b/SwiftChain/Models/Blockchain.swift @@ -16,6 +16,17 @@ class Blockchain { let maxDifficulty = 6 let targetBlockTime = 10.0 // en secondes + static let genesisBlock: Block = { + let block = Block() + block.previousHash = "0000genesis0000" + block.index = 0 + block.timestamp = 1701388800 // 1er Décembre 2023 00:00:00 GMT + block.difficulty = 4 + block.nonce = 12345 // Nonce précalculé qui donne un hash valide + block.hash = "000088c1731bed4996680d2c50ea3d9b573c1507d2d61866c0deff33a7f8cf5e" + return block + }() + init() { self.accountManager = AccountManager() self.memPool = MemPool(accountManager: accountManager) @@ -28,13 +39,8 @@ class Blockchain { - data: The datas of the block */ func createGenesisBlock() { - let genesisBlock = Block() - genesisBlock.previousHash = "0000" - genesisBlock.index = 0 - genesisBlock.timestamp = Int(Date().timeIntervalSince1970) - let _ = genesisBlock.mineBlock() - chain.append(genesisBlock) - print("Genesis block created -- hash: \(genesisBlock.hash)") + chain.append(Blockchain.genesisBlock) + print("Genesis block initialized -- hash: \(Blockchain.genesisBlock.hash)") } /** @@ -118,24 +124,23 @@ class Blockchain { } func validateChain(_ newChain: [Block]) -> Bool { - // Vérifier que la chaîne commence par notre genesis block - guard newChain.first?.hash == chain.first?.hash else { + // Vérifier que la chaîne commence par notre bloc genesis codé en dur + guard let firstBlock = newChain.first, + firstBlock.hash == Blockchain.genesisBlock.hash else { print("Genesis block mismatch") return false } - // Vérifier chaque bloc + // Vérifier les blocs suivants for i in 1..