Remove xcodeproj

This commit is contained in:
vbodinaud
2020-03-01 02:23:06 +01:00
parent 5c83114197
commit b32a6e6bbb
14 changed files with 14 additions and 325 deletions

View File

@@ -0,0 +1,20 @@
//
// Block.swift
// Blockchain
//
// Created by Victor BODINAUD on 27/02/2020.
// Copyright © 2020 Victor BODINAUD. All rights reserved.
//
import Foundation
class Block {
var hash: String!
var data: String!
var previousHash: String!
var index: Int!
func generateHash() -> String {
return NSUUID().uuidString.replacingOccurrences(of: "-", with: "")
}
}

View File

@@ -0,0 +1,34 @@
//
// Blockchain.swift
// Blockchain
//
// Created by Victor BODINAUD on 27/02/2020.
// Copyright © 2020 Victor BODINAUD. All rights reserved.
//
import Foundation
class Blockchain {
var chain = [Block]()
func createGenesisBlock(data: String) {
let genesisBlock = Block()
genesisBlock.data = data
genesisBlock.previousHash = "0000"
genesisBlock.index = 0
genesisBlock.hash = genesisBlock.generateHash()
chain.append(genesisBlock)
print("Genesis block created -- hash: \(genesisBlock.hash ?? "")")
}
func createBlock(data: String) {
let newBlock = Block()
newBlock.data = data
newBlock.previousHash = chain[chain.count-1].hash
newBlock.index = chain.count
newBlock.hash = newBlock.generateHash()
chain.append(newBlock)
print("Block \(newBlock.index ?? 0) created -- hash: \(newBlock.hash ?? "") previous hash: \(newBlock.previousHash ?? "") data: \(newBlock.data ?? "")")
}
}

View File

@@ -0,0 +1,32 @@
//
// main.swift
// Blockchain
//
// Created by Victor BODINAUD on 27/02/2020.
// Copyright © 2020 Victor BODINAUD. All rights reserved.
//
import Foundation
let blockchain = Blockchain()
var command: String?
blockchain.createGenesisBlock(data: "")
repeat {
print("Enter command:")
command = readLine()
if command == "create" {
print("data for the new block:")
let data = readLine()
blockchain.createBlock(data: data ?? "")
}
if command == "spam" {
for _ in 1...1000 {
blockchain.createBlock(data: "\((blockchain.chain.last?.index ?? 0)+1)")
}
}
} while command != "exit"