Skip to content

Instantly share code, notes, and snippets.

@harshvishu
Created January 7, 2019 19:37
Show Gist options
  • Select an option

  • Save harshvishu/1c69e90c6fc7f5359fe07003d274f3c1 to your computer and use it in GitHub Desktop.

Select an option

Save harshvishu/1c69e90c6fc7f5359fe07003d274f3c1 to your computer and use it in GitHub Desktop.
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