diff --git a/Blockchain/.gitignore b/Blockchain/.gitignore new file mode 100644 index 0000000..95c4320 --- /dev/null +++ b/Blockchain/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ diff --git a/Blockchain/Package.swift b/Blockchain/Package.swift new file mode 100644 index 0000000..892d1df --- /dev/null +++ b/Blockchain/Package.swift @@ -0,0 +1,22 @@ +// swift-tools-version:5.1 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Blockchain", + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "Blockchain", + dependencies: []), + .testTarget( + name: "BlockchainTests", + dependencies: ["Blockchain"]), + ] +) diff --git a/Blockchain/README.md b/Blockchain/README.md new file mode 100644 index 0000000..466c4a1 --- /dev/null +++ b/Blockchain/README.md @@ -0,0 +1,3 @@ +# Blockchain + +A description of this package. diff --git a/Blockchain/Models/Block.swift b/Blockchain/Sources/Blockchain/Models/Block.swift similarity index 75% rename from Blockchain/Models/Block.swift rename to Blockchain/Sources/Blockchain/Models/Block.swift index 4573654..cf3764d 100644 --- a/Blockchain/Models/Block.swift +++ b/Blockchain/Sources/Blockchain/Models/Block.swift @@ -7,7 +7,6 @@ // import Foundation -import CryptoKit class Block { var hash: String! @@ -16,10 +15,6 @@ class Block { var index: Int! func generateHash() -> String { - if let hashData = data.data(using: .utf8) { - _ = SHA256.hash(data: hashData) - } - return NSUUID().uuidString.replacingOccurrences(of: "-", with: "") } } diff --git a/Blockchain/Models/Blockchain.swift b/Blockchain/Sources/Blockchain/Models/Blockchain.swift similarity index 100% rename from Blockchain/Models/Blockchain.swift rename to Blockchain/Sources/Blockchain/Models/Blockchain.swift diff --git a/Blockchain/main.swift b/Blockchain/Sources/Blockchain/main.swift similarity index 95% rename from Blockchain/main.swift rename to Blockchain/Sources/Blockchain/main.swift index e0d7c02..07965d5 100644 --- a/Blockchain/main.swift +++ b/Blockchain/Sources/Blockchain/main.swift @@ -24,7 +24,7 @@ repeat { } if command == "spam" { - for _ in 1...1000000 { + for _ in 1...1000 { blockchain.createBlock(data: "\((blockchain.chain.last?.index ?? 0)+1)") } } diff --git a/Blockchain/Tests/BlockchainTests/BlockchainTests.swift b/Blockchain/Tests/BlockchainTests/BlockchainTests.swift new file mode 100644 index 0000000..dc36b32 --- /dev/null +++ b/Blockchain/Tests/BlockchainTests/BlockchainTests.swift @@ -0,0 +1,47 @@ +import XCTest +import class Foundation.Bundle + +final class BlockchainTests: XCTestCase { + func testExample() throws { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct + // results. + + // Some of the APIs that we use below are available in macOS 10.13 and above. + guard #available(macOS 10.13, *) else { + return + } + + let fooBinary = productsDirectory.appendingPathComponent("Blockchain") + + let process = Process() + process.executableURL = fooBinary + + let pipe = Pipe() + process.standardOutput = pipe + + try process.run() + process.waitUntilExit() + + let data = pipe.fileHandleForReading.readDataToEndOfFile() + let output = String(data: data, encoding: .utf8) + + XCTAssertEqual(output, "Hello, world!\n") + } + + /// Returns path to the built products directory. + var productsDirectory: URL { + #if os(macOS) + for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { + return bundle.bundleURL.deletingLastPathComponent() + } + fatalError("couldn't find the products directory") + #else + return Bundle.main.bundleURL + #endif + } + + static var allTests = [ + ("testExample", testExample), + ] +} diff --git a/Blockchain/Tests/BlockchainTests/XCTestManifests.swift b/Blockchain/Tests/BlockchainTests/XCTestManifests.swift new file mode 100644 index 0000000..7bcccdc --- /dev/null +++ b/Blockchain/Tests/BlockchainTests/XCTestManifests.swift @@ -0,0 +1,9 @@ +import XCTest + +#if !canImport(ObjectiveC) +public func allTests() -> [XCTestCaseEntry] { + return [ + testCase(BlockchainTests.allTests), + ] +} +#endif diff --git a/Blockchain/Tests/LinuxMain.swift b/Blockchain/Tests/LinuxMain.swift new file mode 100644 index 0000000..6e9e04a --- /dev/null +++ b/Blockchain/Tests/LinuxMain.swift @@ -0,0 +1,7 @@ +import XCTest + +import BlockchainTests + +var tests = [XCTestCaseEntry]() +tests += BlockchainTests.allTests() +XCTMain(tests)