请选择 进入手机版 | 继续访问电脑版

4Ameta

 找回密码
 立即注册
搜索
查看: 98|回复: 0

write a blockchain in Python【3】consensus algorithm

[复制链接]

26

主题

26

帖子

119

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
119
发表于 2022-9-5 13:17:12 | 显示全部楼层 |阅读模式
2. Write the consensus algorithm

>We have created a blockchain, but this blockchain is not complete and has many problems:
-People can quickly create any block to add to the blockchain, which will cause our blockchain to be full of junk data, leading to blockchain overload and failure to work normally.
-It is too easy to create blocks, so we can easily tamper with data without much computational power and cost, and recalculate all hash values to make our tampering effective.
>So we need a consensus mechanism to solve this problem. Here we introduce the * * * POW * * * workload proof mechanism used by bitcoin.


2.1 what is POW
POW is a consensus mechanism, which obtains the specified result through a certain amount of complex and time-consuming operations, and this result can be verified quickly.
Take the time, equipment and energy consumed as the guarantee cost, so as to prevent data abuse.
***Because a slight change in the input of the hash hash will generate a completely different hash value, it is almost impossible to derive the original data from the hash value. We let the nodes of the blockchain find the specified value through a large number of exhaustive operations to realize pow***
Bitcoin implements POW by requiring hashes to have a specific number of 0s.
This is also called difficulty.
How do we change the hash value of the block? Because the creation time of the block and the data contained in the block are not expected to be changed, we need to introduce a new value to change the hash value of the block through the change of this value.
We add a 'nonce' value to the block` Nonce ` is the number of times to find a valid hash.
Since it is impossible to predict the output of the hash function, we can only find an effective hash value and create a new block through a lot of attempts before obtaining a hash that meets the difficulty condition - this is called mining.
Bitcoin stipulates that only one block can be added every 10 minutes.
You can imagine how much computational power it takes to create a block. It is almost impossible to tamper with a block.


2.2 implementation of pow
-First, we add the 'nonce' random number attribute to the block class and initialize it to '0'.

  1. def __init__(self, timestamp, data, previous_hash=''):
  2.         '''
  3.         Initialization of blocks
  4.         :param timestamp: Timestamp at creation
  5.         :param data: Block data
  6.         :param previous_hash: Hash of the previous block
  7.         :param hash: the hash of this block
  8.         '''
  9.         self.previous_hash = previous_hash
  10.         self.timestamp = timestamp
  11.         self.data = data
  12.         self.nonce = 0
  13.         self.hash = self.calculate_hash()
复制代码


Then we modify ` calculate_ Hash ` method,
Include nonce when calculating hash value.

  1. def calculate_hash(self):
  2.         '''
  3.         Calculate block hash value
  4.         :return:
  5.         '''
  6.         # Concatenate the block information and generate the hash value of sha256
  7.         raw_str = self.previous_hash + str(self.timestamp) + json.dumps(self.data, ensure_ascii=False) + str(self.nonce)
  8.         sha256 = hashlib.sha256()
  9.         sha256.update(raw_str.encode('utf-8'))
  10.         hash = sha256.hexdigest()
  11.         return hash
复制代码


nextLesson>>>

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|4ameta

GMT+8, 2022-10-4 12:25 , Processed in 0.034858 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表