This time, I took a course on building a blockchain with Python at Udemy, so I would like to share the contents. In this course, you can understand the concept of blockchain by actually moving your hands, and it will be a good practice in Python coding. In this article, I will introduce the touching part of this course!
I will explain the implementation of an application that sends money using blockchain in Python. The development environment used Pycharm. First of all, create a list of chains that will contain several blocks, a list of pools that describe the contents of the transaction, and an initial block.
def __init__(self):
self.transaction_pool = [] #Contains transaction information
self.chain = [] #Contains blockchain information
self.create_block(0, "init hash") #Creating an initial block
The information contained in one block is
・ prev hash: A hash containing information related to the previous block
-Timestamp: Information on block generation time
・ Nonce: Value obtained from the calculation result of mining
・ Transactions: Information on transaction details
It will be these four. Create a block, create a block to be used next time, and empty the pool containing transaction details.
def create_block(self, nonce, previous_hash): #Method to make a block
block = {
'timestamp': time.time(), #Store timestamp
'transactions': self.transaction_pool, #Store transaction
'nonce': nonce, #Store nonce
'previous_hash': previous_hash #Stores the hash associated with the previous block
}
self.chain.append(block) #Make a new block
self.transaction_pool = [] #Empty the contents of the transaction pool
return block```
It also performs processing to make the execution result easier to see.
#### **` #Processing to make blocks easier to see`**
```def pprint(chains)
for i, chain in enumerate(chains):
print(f'{"="*12} Chain {i} {"="*12}')
for k, v in chain.items():
print(f'{k:15}{v}')
Finally, add the process of calling the blockchain.
if __name__ == '__main__': #Call the blockchain
block_chain = BlockChain()
pprint(block_chain.chain) #First block
block_chain.create_block(5, 'hash 1')
pprint(block_chain.chain) #Second block
The following is a summary of all the descriptions so far.
import logging
import sys
import time #To use timestamps
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
class BlockChain(object): #Blockchain class
def __init__(self):
self.transaction_pool = [] #Contains transaction information
self.chain = [] #Contains blockchain information
self.create_block(0, "init hash") #Creating an initial block
def create_block(self, nonce, previous_hash): #Method to make a block
block = {
'timestamp': time.time(), #Store timestamp
'transactions': self.transaction_pool, #Store transaction
'nonce': nonce, #Store nonce
'previous_hash': previous_hash #Stores the hash associated with the previous block
}
self.chain.append(block) #Make a new block
self.transaction_pool = [] #Empty the contents of the transaction pool
return block
def pprint(chains): #Processing to make blocks easier to see
for i, chain in enumerate(chains):
print(f'{"="*12} Chain {i} {"="*12}')
for k, v in chain.items():
print(f'{k:15}{v}')
if __name__ == '__main__': #Call the blockchain
block_chain = BlockChain()
pprint(block_chain.chain) #First block
block_chain.create_block(5, 'hash 1')
pprint(block_chain.chain) #Second block
Click here to continue the course "Introduction to blockchain development from scratch starting with Python taught by active Silicon Valley engineers" https://www.udemy.com/course/python-blockchain/learn/lecture/15381372#overview
Recommended Posts