How to Create Your Own Cryptocurrency Blockchain in Python
Want to learn how to build your own cryptocurrency blockchain? Check out this tutorial on how to create your own blockchain in Python.
Join the DZone community and get the full member experience.
Join For Free
Cryptocurrencies and their underlying blockchain technology have taken the world by surprise —from their humble beginnings a few years ago to current everyday conversation point.
Typically, a blockchain refers to a distributed ledger technology that constitutes a “chain of blocks.” Every block in the blockchain has a hash of the previous block, a timestamp, and transaction data which makes it tamper-proof.
According to Elliot Minns, who has more than six years of software development experience and uses practical projects to teach people how to create cryptocurrencies, “Learning how to create a blockchain will help you to understand how digital currencies like Bitcoin and Ethereum operate and how you can extrapolate the technology to accelerate the capabilities of your applications.”
In this article, we are going to explain how you can create a simple blockchain using the Python programming language.
Here is the basic blueprint of the Python class we’ll use for creating the blockchain:
class Block(object):
def __init__():
pass
#initial structure of the block class
def compute_hash():
pass
#producing the cryptographic hash of each block
class BlockChain(object):
def __init__(self):
#building the chain
def build_genesis(self):
pass
#creating the initial block
def build_block(self, proof_number, previous_hash):
pass
#builds new block and adds to the chain
@staticmethod
def confirm_validity(block, previous_block):
pass
#checks whether the blockchain is valid
def get_data(self, sender, receiver, amount):
pass
# declares data of transactions
@staticmethod
def proof_of_work(last_proof):
pass
#adds to the security of the blockchain
@property
def latest_block(self):
pass
#returns the last block in the chain
Now, let’s explain how the blockchain class works.
Initial Structure of the Block Class
Here is the code for our initial block class:
import hashlib
import time
class Block(object):
def __init__(self, index, proof_number, previous_hash, data, timestamp=None):
self.index = index
self.proof_number = proof_number
self.previous_hash = previous_hash
self.data = data
self.timestamp = timestamp or time.time()
@property
def compute_hash(self):
string_block = "{}{}{}{}{}".format(self.index, self.proof_number, self.previous_hash, self.data, self.timestamp)
return hashlib.sha256(string_block.encode()).hexdigest()
As you can see above, the class constructor or initiation method ( __init__()
) above takes the following parameters:
- self — just like any other Python class, this parameter is used to refer to the class itself. Any variable associated with the class can be accessed using it.
- index — it’s used to track the position of a block within the blockchain.
- previous_hash — it used to reference the hash of the previous block within the blockchain.
- data—it gives details of the transactions done, for example, the amount bought.
- timestamp—it inserts a timestamp for all the transactions performed.
The second method in the class, compute_hash
, is used to produce the cryptographic hash of each block based on the above values.
As you can see, we imported the SHA-256 algorithm into the cryptocurrency blockchain project to help in getting the hashes of the blocks.
Once the values have been placed inside the hashing module, the algorithm will return a 256-bit string denoting the contents of the block.
So, this is what gives the blockchain immutability. Since each block will be represented by a hash, which will be computed from the hash of the previous block, corrupting any block in the chain will make the other blocks have invalid hashes, resulting in breakage of the whole blockchain network.
Building the Chain
The whole concept of a blockchain is based on the fact that the blocks are “chained” to each other. Now, we’ll create a blockchain class that will play the critical role of managing the entire chain.
It will keep the transactions data and include other helper methods for completing various roles, such as adding new blocks.
Let’s talk about the helper methods.
Adding the Constructor Method
Here is the code:
class BlockChain(object):
def __init__(self):
self.chain = []
self.current_data = []
self.nodes = set()
self.build_genesis()
The __init__()
constructor method is what instantiates the blockchain.
Here are the roles of its attributes:
- self.chain — this variable stores all the blocks.
- self.current_data — this variable stores information about the transactions in the block.
- self.build_genesis() — this method is used to create the initial block in the chain.
Building the Genesis Block
The build_genesis()
method is used for creating the initial block in the chain, that is, a block without any predecessors. The genesis block is what represents the beginning of the blockchain.
To create it, we’ll call the build_block()
method and give it some default values. The parameters proof_number
and previous_hash
are both given a value of zero, though you can give them any value you desire.
Here is the code:
def build_genesis(self):
self.build_block(proof_number=0, previous_hash=0)
def build_block(self, proof_number, previous_hash):
block = Block(
index=len(self.chain),
proof_number=proof_number,
previous_hash=previous_hash,
data=self.current_data
)
self.current_data = []
self.chain.append(block)
return block
Confirming Validity of the Blockchain
The confirm_validity
method is critical in examining the integrity of the blockchain and making sure inconsistencies are lacking.
As explained earlier, hashes are pivotal for realizing the security of the cryptocurrency blockchain, because any slight alteration in an object will result in the creation of an entirely different hash.
Thus, the confirm_validity
method utilizes a series of if statements to assess whether the hash of each block has been compromised.
Furthermore, it also compares the hash values of every two successive blocks to identify any anomalies. If the chain is working properly, it returns true; otherwise, it returns false.
Here is the code:
def confirm_validity(block, previous_block):
if previous_block.index + 1 != block.index:
return False
elif previous_block.compute_hash != block.previous_hash:
return False
elif block.timestamp <= previous_block.timestamp:
return False
return True
Declaring Data of Transactions
The get_data
method is important in declaring the data of transactions on a block. This method takes three parameters (sender’s information, receiver’s information, and amount) and adds the transaction data to the self.current_data
list.
Here is the code:
def get_data(self, sender, receiver, amount):
self.current_data.append({
'sender': sender,
'receiver': receiver,
'amount': amount
})
return True
Effecting the Proof of Work
In blockchain technology, Proof of Work (PoW) refers to the complexity involved in mining or generating new blocks on the blockchain.
For example, the PoW can be implemented by identifying a number that solves a problem whenever a user completes some computing work. Anyone on the blockchain network should find the number complex to identify but easy to verify — this is the main concept of PoW.
This way, it discourages spamming and compromising the integrity of the network.
In this article, we’ll illustrate how to include a Proof of Work algorithm in a blockchain cryptocurrency project.
Finalizing With the Last Block
Finally, the latest_block()
helper method is used for retrieving the last block on the network, which is actually the current block.
Here is the code:
def latest_block(self):
return self.chain[-1]
Implementing Blockchain Mining
Now, this is the most exciting section!
Initially, the transactions are kept in a list of unverified transactions. Mining refers to the process of placing the unverified transactions in a block and solving the PoW problem. It can be referred to as the computing work involved in verifying the transactions.
If everything has been figured out correctly, a block is created or mined and joined together with the others in the blockchain. If users have successfully mined a block, they are often rewarded for using their computing resources to solve the PoW problem.
Here is the mining method in this simple cryptocurrency blockchain project:
def block_mining(self, details_miner):
self.get_data(
sender="0", #it implies that this node has created a new block
receiver=details_miner,
quantity=1, #creating a new block (or identifying the proof number) is awarded with 1
)
last_block = self.latest_block
last_proof_number = last_block.proof_number
proof_number = self.proof_of_work(last_proof_number)
last_hash = last_block.compute_hash
block = self.build_block(proof_number, last_hash)
return vars(block)
Summary
Here is the whole code for our crypto blockchain class in Python:
import hashlib
import time
class Block(object):
def __init__(self, index, proof_number, previous_hash, data, timestamp=None):
self.index = index
self.proof_number = proof_number
self.previous_hash = previous_hash
self.data = data
self.timestamp = timestamp or time.time()
@property
def compute_hash(self):
string_block = "{}{}{}{}{}".format(self.index, self.proof_number, self.previous_hash, self.data, self.timestamp)
return hashlib.sha256(string_block.encode()).hexdigest()
def __repr__(self):
return "{} - {} - {} - {} - {}".format(self.index, self.proof_number, self.previous_hash, self.data, self.timestamp)
class BlockChain(object):
def __init__(self):
self.chain = []
self.current_data = []
self.nodes = set()
self.build_genesis()
def build_genesis(self):
self.build_block(proof_number=0, previous_hash=0)
def build_block(self, proof_number, previous_hash):
block = Block(
index=len(self.chain),
proof_number=proof_number,
previous_hash=previous_hash,
data=self.current_data
)
self.current_data = []
self.chain.append(block)
return block
@staticmethod
def confirm_validity(block, previous_block):
if previous_block.index + 1 != block.index:
return False
elif previous_block.compute_hash != block.previous_hash:
return False
elif block.timestamp <= previous_block.timestamp:
return False
return True
def get_data(self, sender, receiver, amount):
self.current_data.append({
'sender': sender,
'receiver': receiver,
'amount': amount
})
return True
@staticmethod
def proof_of_work(last_proof):
pass
@property
def latest_block(self):
return self.chain[-1]
def chain_validity(self):
pass
def block_mining(self, details_miner):
self.get_data(
sender="0", #it implies that this node has created a new block
receiver=details_miner,
quantity=1, #creating a new block (or identifying the proof number) is awared with 1
)
last_block = self.latest_block
last_proof_number = last_block.proof_number
proof_number = self.proof_of_work(last_proof_number)
last_hash = last_block.compute_hash
block = self.build_block(proof_number, last_hash)
return vars(block)
def create_node(self, address):
self.nodes.add(address)
return True
@staticmethod
def get_block_object(block_data):
return Block(
block_data['index'],
block_data['proof_number'],
block_data['previous_hash'],
block_data['data'],
timestamp=block_data['timestamp']
)
blockchain = BlockChain()
print("GET READY MINING ABOUT TO START")
print(blockchain.chain)
last_block = blockchain.latest_block
last_proof_number = last_block.proof_number
proof_number = blockchain.proof_of_work(last_proof_number)
blockchain.get_data(
sender="0", #this means that this node has constructed another block
receiver="LiveEdu.tv",
amount=1, #building a new block (or figuring out the proof number) is awarded with 1
)
last_hash = last_block.compute_hash
block = blockchain.build_block(proof_number, last_hash)
print("WOW, MINING HAS BEEN SUCCESSFUL!")
print(blockchain.chain)
Now, let’s try to run our code to see if we can generate some digital coins...
Wow, it worked!
Conclusion
That is it!
We hope that this article has assisted you to understand the underlying technology that powers cryptocurrencies such as Bitcoin and Ethereum.
We just illustrated the basic ideas for making your feet wet in the innovative blockchain technology. The project above can still be enhanced by incorporating other features to make it more useful and robust.
Of course, if you need something advanced, you can always grab a tutorial and fully immerse yourself in the world of cryptos.
Do you have any comments or questions? Please share them below.
Opinions expressed by DZone contributors are their own.
Comments