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

4Ameta

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

write a blockchain in Python【5】Modify blockchain

[复制链接]

26

主题

26

帖子

119

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
119
发表于 2022-9-10 15:20:43 | 显示全部楼层 |阅读模式
write a blockchain in Python【5】Modify blockchain




We add a 'difficulty' attribute to the blockchain class,
The initial difficulty is set to 5,
This means that the hash of the block must start with 5 zeros,
Of course, you can adjust the difficulty yourself,
The more difficult it is, the longer it takes to mine.

  1. def __ init__ (self):
  2. #Initialize the chain and add Genesis block
  3. self. chain = [self._create_genesis_block()]
  4. #Set initial difficulty
  5. self. difficulty = 5
复制代码



Then we modify ` add_ Block ` method,
Make sure the block is dug

  1. def add_ block(self, block):
  2. '''
  3. Add Block
  4. : param block: the block to be added
  5. :return:
  6. '''
  7. block. previous_ hash = self.get_ latest_ block().hash
  8. #Start mining
  9. block.mine_ block(self.difficulty)
  10. #Add to chain after mining
  11. self.chain.append(block)
复制代码

nextLesson>>>




回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2022-10-4 11:03 , Processed in 0.035076 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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