update packages

add some docs
This commit is contained in:
vbodinaud
2021-03-04 15:45:45 +01:00
parent 00bc56adc3
commit 33d508fb44
7 changed files with 216 additions and 5 deletions

View File

@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Blockchain"
BuildableName = "Blockchain"
BlueprintName = "Blockchain"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BlockchainTests"
BuildableName = "BlockchainTests"
BlueprintName = "BlockchainTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BlockchainTests"
BuildableName = "BlockchainTests"
BlueprintName = "BlockchainTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Blockchain"
BuildableName = "Blockchain"
BlueprintName = "Blockchain"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Blockchain"
BuildableName = "Blockchain"
BlueprintName = "Blockchain"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@@ -9,6 +9,27 @@
<key>orderHint</key> <key>orderHint</key>
<integer>0</integer> <integer>0</integer>
</dict> </dict>
<key>CryptoSwift (Playground) 1.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>CryptoSwift (Playground) 2.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>3</integer>
</dict>
<key>CryptoSwift (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict> </dict>
<key>SuppressBuildableAutocreation</key> <key>SuppressBuildableAutocreation</key>
<dict> <dict>

View File

@@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git", "repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"state": { "state": {
"branch": null, "branch": null,
"revision": "a44caef0550c346e0ab9172f7c9a3852c1833599", "revision": "5669f222e46c8134fb1f399c745fa6882b43532e",
"version": "1.3.0" "version": "1.3.8"
} }
} }
] ]

View File

@@ -5,6 +5,9 @@ import PackageDescription
let package = Package( let package = Package(
name: "Blockchain", name: "Blockchain",
platforms: [
.macOS(.v10_12),
],
dependencies: [ dependencies: [
// Dependencies declare other packages that this package depends on. // Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"), // .package(url: /* package url */, from: "1.0.0"),

View File

@@ -17,6 +17,19 @@ class Block {
var nonce: Int var nonce: Int
var timestamp: Int var timestamp: Int
/**
Initialize a block with the provided parts and specifications.
- parameters:
- hash: The hash of the block
- data: The data of the block
- previousHash: The hash of the previous block
- index: The index of the block
- nonce: The nonce of the block
- timestamp: The timestamp of the block
- returns: A beautiful new block for the blockchain.
*/
init(hash: String = "", data: String = "", previousHash: String = "", index: Int = 0, nonce: Int = 0, timestamp: Int = 0) { init(hash: String = "", data: String = "", previousHash: String = "", index: Int = 0, nonce: Int = 0, timestamp: Int = 0) {
self.data = data self.data = data
self.previousHash = previousHash self.previousHash = previousHash
@@ -26,10 +39,20 @@ class Block {
self.hash = hash self.hash = hash
} }
/**
Generate the hash of the block.
- returns: The hash of the block
*/
func generateHash() -> String { func generateHash() -> String {
return data.sha256() return data.sha256()
} }
/**
Generate the timestamp of the block
- returns: The timestamp of the block
*/
func generateTimestamp() -> Int { func generateTimestamp() -> Int {
return Int(Date().timeIntervalSince1970) return Int(Date().timeIntervalSince1970)
} }

View File

@@ -11,6 +11,12 @@ import Foundation
class Blockchain { class Blockchain {
var chain = [Block]() var chain = [Block]()
/**
Initialize the first block of the blockchain.
- Parameters:
- data: The datas of the block
*/
func createGenesisBlock(data: String) { func createGenesisBlock(data: String) {
let genesisBlock = Block() let genesisBlock = Block()
genesisBlock.data = data genesisBlock.data = data
@@ -20,19 +26,63 @@ class Blockchain {
genesisBlock.timestamp = genesisBlock.generateTimestamp() genesisBlock.timestamp = genesisBlock.generateTimestamp()
genesisBlock.hash = genesisBlock.generateHash() genesisBlock.hash = genesisBlock.generateHash()
chain.append(genesisBlock) chain.append(genesisBlock)
print("Genesis block created -- hash: \(genesisBlock.hash ?? "")") print("Genesis block created -- hash: \(genesisBlock.hash)")
} }
/**
Initialize a new block of the blockchain.
- Parameters:
- data: The datas of the block
*/
func createBlock(data: String) { func createBlock(data: String) {
let newBlock = Block() let newBlock = Block()
newBlock.data = data newBlock.data = data
newBlock.previousHash = chain[chain.count-1].hash newBlock.previousHash = chain.last!.hash
newBlock.index = chain.count newBlock.index = chain.count
newBlock.nonce = 0 newBlock.nonce = 0
newBlock.timestamp = newBlock.generateTimestamp() newBlock.timestamp = newBlock.generateTimestamp()
newBlock.hash = newBlock.generateHash() newBlock.hash = newBlock.generateHash()
chain.append(newBlock) chain.append(newBlock)
print("Block \(newBlock.index ?? 0) created -- hash: \(newBlock.hash ?? "") previous hash: \(newBlock.previousHash ?? "") data: \(newBlock.data ?? "")") print("-- Block \(newBlock.index) created --\n hash: \(newBlock.hash)\n previous hash: \(newBlock.previousHash)\n data: \(newBlock.data)")
}
/**
Insert a corrupted block in the blockhain.
(for testing purpose)
*/
func insertCorruptedBlock() {
let newCorruptedBlock = Block()
newCorruptedBlock.data = "Corrupted block"
newCorruptedBlock.previousHash = "1234567890"
newCorruptedBlock.index = chain.count
newCorruptedBlock.nonce = 0
newCorruptedBlock.timestamp = newCorruptedBlock.generateTimestamp()
newCorruptedBlock.hash = newCorruptedBlock.generateHash()
chain.append(newCorruptedBlock)
print("-- Corrupted block \(newCorruptedBlock.index) created --\n hash: \(newCorruptedBlock.hash)\n previous hash: \(newCorruptedBlock.previousHash)\n data: \(newCorruptedBlock.data)")
}
/**
Check validity of the blockchain.
*/
func chainValidity() {
var isChainValid = true
var corruptedBlock = Block()
for i in 1...chain.count-1 {
if chain[i].previousHash != chain[i-1].hash {
isChainValid = false
corruptedBlock = chain[i]
}
}
print("Chain is valid : \(isChainValid)")
if !isChainValid {
print("Corrupted block is : \(corruptedBlock.index)")
}
} }
} }

View File

@@ -17,16 +17,28 @@ repeat {
print("Enter command:") print("Enter command:")
command = readLine() command = readLine()
// Create a new block
if command == "create" { if command == "create" {
print("data for the new block:") print("data for the new block:")
let data = readLine() let data = readLine()
blockchain.createBlock(data: data ?? "") blockchain.createBlock(data: data ?? "")
} }
// Create a lot of blocks
if command == "spam" { if command == "spam" {
for _ in 1...1000 { for _ in 1...1000 {
blockchain.createBlock(data: "\((blockchain.chain.last?.index ?? 0)+1)") blockchain.createBlock(data: "\((blockchain.chain.last?.index ?? 0)+1)")
} }
} }
// Check validity of the blockchain
if command == "validity" {
blockchain.chainValidity()
}
// Insert a corrupted block
if command == "corrupt" {
blockchain.insertCorruptedBlock()
}
} while command != "exit" } while command != "exit"