Sample code to create a crytocurrency using python

//To create a crytocurrency, first we need to import the hashlib and datetime library.

import hashlib as hasher
import datetime as date

//Next, we are going to create our hash block structure which included the timestamp, data, hash values.

class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.hash_block()

def hash_block(self):
sha = hasher.sha256()
sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash))
return sha.hexdigest()

//Every crytocurrency must has their first block called genesis block, so we are going to create it.

def create_genesis_block():
return Block(0,date.datetime.now(),"Genesis Block","0")

//Next, we are going to complete our program.

def next_block(last_block):
this_index = last_block.index + 1
this_timestamp = date.datetime.now()
this_data = "Hey! I'm block " + str(this_index)
this_hash = last_block.hash
return Block(this_index, this_timestamp, this_data, this_hash)

blockchain = [create_genesis_block()]
previous_block = blockchain[0]

//Let's create our first 50 coin!

num_of_blocks_to_add = 50

for i in range(0,num_of_blocks_to_add):
block_to_add = next_block(previous_block)
blockchain.append(block_to_add)
previous_block = block_to_add

print "Block #{} has been added to the blockchain!".format(block_to_add.index)
print "Hash: {}\n".format(block_to_add.hash)

Popular posts from this blog

Accuracy for Logistic Regression model is always lower than using Convolutional neural network

Using mini batch SGD Neural Network in Alphabet recognition!

96.24% accuracy with higher epoch numbers for Convolutional Neural Network

Tower Defense (Endless TD Beta)

Simple data curation practices using Numpy (Udacity Deep Learning Assignment 1)

Accuracy 96% with Simple Deep NN using Keras Theano backend for nonMNist alphabet recognition

Python to read and record SQM-LE data, search saved data and find IP address..

Alphabet recognition by using LogisticRegression model from sklearn.linear_model. (Udacity Assignment 1)

First try to solve Polynomial equations by Tensorflow

Endless Falling