Created
January 7, 2019 19:37
-
-
Save harshvishu/1c69e90c6fc7f5359fe07003d274f3c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Block: Codable { | |
| var index: Int64 | |
| var timestamp: Date | |
| var transactions: [Transaction] | |
| var proof: Int64 | |
| var previous_hash: String | |
| } | |
| struct Transaction: Codable { | |
| var sender: String | |
| var recipient: String | |
| var amount: Int64 | |
| } | |
| protocol Chain: class { | |
| var chain: [Block] {get} | |
| var current_transactions: [Transaction] {get} | |
| var nodes: Set<String> {get} | |
| /** | |
| # Creates a new block and adds it to the chain | |
| - Parameter proof: <int> The proof given by the Proof of Work algorithm | |
| - Parameter previous_hash: (Optional) <str> Hash of previous Block | |
| - returns: _dict_ New Block | |
| */ | |
| func newBlock(previous_hash: String?, proof: Int64) -> Block | |
| /** | |
| Creates a new transaction to go into the next mined Block | |
| - Parameter sender: Address of the Sender | |
| - Parameter recipient: Address of the Recipient | |
| - Parameter amount: Amount | |
| - returns: The index of the Block that will hold this transaction | |
| */ | |
| func newTransaction(sender: String, recipient: String, amount: Int64) -> Int64 | |
| /** | |
| Returns the last Block in the chain | |
| */ | |
| var last_block: Block {get} | |
| /** | |
| Creates a SHA-256 hash of a Block | |
| - Parameter block: <dict> Block | |
| - returns: String | |
| */ | |
| func hash(block: Block) -> String | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment