Here we are talking about blockchain. The protocol defined on the blockchain is not only to trade data but also to trade the value of data. The current primary method was invented before the Internet and requires the use of a centralized clearinghouse. The machines sometimes had to wait four days to transfer payments from one place to another, but in all other respects the speed of communication between the machines was staggering. For example, they can send micropayments to each other. However, transactions can only be carried out immediately when resources such as electricity, storage space, and computing are met.
Bitcoin and other cryptocurrencies serve as a fifth protocol layer for value transfer, with the underlying technology called blockchain. For the first time, blockchain allows machines to agree to the transfer of value without having to trust a central authority, in the real world if I create a “me”, only I own it, but if I create a digital image, then on the Internet everyone else can copy it, and then we all own it, then there is no way to prove ownership. But blockchain lets us achieve a consensus layer in a decentralized way, preventing a bad actor from falsely claiming ownership.
Let’s build our own simple blockchain in python to better understand how this incredible data structure works. For simplicity, let’s define what a block is, assuming each block represents a transaction, a value transferred between two computers. To see the code below and read it, one must define a block object in its own class, which will store some data and two data hashes, one for the previous hash in the blockchain and one for its own hash, the reason a block needs a hash is because the hash provides a unique id, and it has the ability to verify data integrity.
class Block():
def __init __(self,params):
self.index = params.index
self.previous_hash = params.previous_hash
self.timestamp = params.timestamp
self.data = params.data
self.hash = self.calc_hash()
We use the popular cryptographic hash algorithm SHA-256 to help generate a 256-bit signature from each block in the hash function. For this you have to import hashlib.
def calc_hash(self):
return hashlib.sha256(str(self.params())。encode())。hexdigest()
How are blocks generated?

The user provides some data in their transaction, so we can use that as the only argument to the generator function, since we have to fetch the last chunk to do the work. We’ll use the last block to help find this block index and hash, and we’ll also give it a timestamp. Thus, each generated block points to the previous block by storing its hash.
def generate_next_block(self,data):
index = len(self.blockchain_store)
previous_hash = self.latest_block()。hash
timestamp = int(time.time())
params = block_params.BlockParams(index,previous_hash,timestamp,data)
new_block = block.Block(params)
self.blockchain_store.append(new_block)
It is a block called blockchain
The first generated block is called the genesis block, and we will hardcode the predefined properties into its own function
def genesis_block(cls):
params = block_params.BlockParams.genesis_params()
return cls(params)
We will use this to initialize the blockchain, and store it as an in-memory list
def __init __(self):
self.blockchain_store = self.fetch_blockchain()
def latest_block(self):
return self.blockchain_store [-1]
def fetch_blockchain(self):
return [block.Block.genesis_block()]
Remember what I’m saying here about a block’s hash both as its identity and to verify its integrity. We can use the hash value to judge a newly generated block and see if it is valid given the connection between the new block and the previous block, which is the last block in the chain. We can first look at the correct ordering index, we can check the previous hash value attribute and match it to the previous block that has been verified, and finally we will calculate the hash value of the new block to ensure that it is accurate.
def receive_new_block(self, new_block):
previous_block = self.latest_block()
if not new_block.has_valid_index(previous_block):
print(‘invalid index’)
return
if not new_block.has_valid_previous_hash(previous_block):
print(‘invalid previous hash’)
return
if not new_block.has_valid_hash():
print(‘invalid hash’)
return
self.blockchain_store.append(new_block)
Assuming all nodes store a copy of the blockchain, we need a way to choose which blockchain to use in the event of a conflict. Assuming 2 nodes generate the same number of blocks, then our solution is to choose the chain with the longest number of blocks, since it’s been around longer so it’s more likely to be the real blockchain.
def replace_chain(self,block:self.blockchain_store):
if self.is_valid_chain(blocks)和len(blocks)》 self.length:
self.blockchain_store = blocks
else:
self.log(“Recieved blockchain invalid”)
After a new blockchain is given: we will check if it is valid programmatically that means it replaces our previous block with a new one, since each node stores a copy of the blockchain, and Blocks generate transactions continuously, so that each node can broadcast, and every time it generates a new block, it connects to a new node, which can add the block to its current chain or to the entire blockchain, So we can create an http server by encapsulating nodes so that users can query other nodes.
So the user will control the node via HTTP requests and it will communicate with other nodes via WebSockets. This is Simple Blockchain One of the key features we are going to discuss is the Proof of Work algorithm.
Bitcoin mining nodes complete a series of very expensive calculations to approve each transaction and earn bitcoins for the computational work required to generate a new block. But the premise of generating a new block must be to break through the 51% attack, which proves that the new block is valid and can be added to the blockchain, so a bad actor must have more computing power, not half Bitcoin network.
Read Also –
How Does Blockchain Technology Change The World Through Value Dissemination?
How Does A Cryptocurrency Wallet Work?
How Can Blockchain Help The Agriculture Sector?
How Can Artificial Intelligence Utilize Blockchain Technology?
Leave a Reply