一、开发环境搭建

1、安装pipenv
pipenv可以帮助我们管理Python和第三方库的版本。
**pip3 install pipenv**
在项目文件夹中生成Pipfile文件
**pipenv --python=python3.10 **
此时生成Pipfile文件
2、 在pipenv环境下安装flask、requests
**pipenv install flask==0.12.2**
**pipenv install requests==2.18.4**

二、确定区块的结构

  1. {
  2. "index": 0, #块的索引
  3. "timestamp": "", #时间戳
  4. "transactions": [ #交易的信息
  5. {
  6. "sender":"", #交易的发送者
  7. "recipient":"", #交易的接收者
  8. "amount": 5 #交易的金额
  9. }
  10. ],
  11. "proof": "", #工作量证明
  12. "previous_hash":"" #上一个区块的哈希值
  13. }

三、定义类进行区块的封装

  1. class Blockchain:
  2. def __init__(self):
  3. self.chain = [] #包含每一个区块
  4. self.current_transactions = [] #保存当前的交易信息
  5. def new_block(self): #建立新区块
  6. pass
  7. def new_transactions(self, sender, recipient, amount) -> int: #建立新交易
  8. self.current_transactions.append(
  9. {
  10. "sender": sender, #交易的发送者
  11. "recipient": recipient, #交易的接收者
  12. "amount": amount #交易的金额
  13. }
  14. )
  15. return self.last_block['index'] + 1
  16. @staticmethod
  17. def hash(block): #计算区块的哈希值
  18. pass
  19. @property
  20. def last_block(self): #获取当前区块链里最后一个块
  21. pass