// // Blockchain.swift // Blockchain // // Created by Victor BODINAUD on 27/02/2020. // Copyright © 2020 Victor BODINAUD. All rights reserved. // import Foundation public class Blockchain { var chain = [Block]() let minDifficulty = 2 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 block.difficulty = 4 block.nonce = 12345 block.hash = "000088c1731bed4996680d2c50ea3d9b573c1507d2d61866c0deff33a7f8cf5e" return block }() init() { chain.append(Blockchain.genesisBlock) print("Genesis block initialized -- hash: \(Blockchain.genesisBlock.hash)") } func calculateNewDifficulty() -> Int { guard chain.count >= 2 else { return minDifficulty } let lastBlock = chain.last! let previousBlock = chain[chain.count - 2] let timeSpent = Double(lastBlock.timestamp - previousBlock.timestamp) if timeSpent < targetBlockTime / 2 { return min(lastBlock.difficulty + 1, maxDifficulty) } else if timeSpent > targetBlockTime * 2 { return max(lastBlock.difficulty - 1, minDifficulty) } return lastBlock.difficulty } func validateChain(_ chain: [Block]) -> Bool { guard let firstBlock = chain.first, firstBlock.hash == Blockchain.genesisBlock.hash else { print("Genesis block mismatch") return false } for i in 1.. Bool { // Vérifier le genesis block guard let firstBlock = chain.first, firstBlock.hash == Blockchain.genesisBlock.hash else { print("Chain: Invalid genesis block") return false } // Vérifier la séquence des blocs for i in 1.. Int { return chain[index].difficulty } }