没有合适的资源?快使用搜索试试~ 我知道了~
用Python实现简单的区块链系统1
需积分: 0 21 下载量 179 浏览量
2022-08-04
15:57:35
上传
评论 7
收藏 516KB PDF 举报
温馨提示
用Python实现简单的区块链系统1
资源详情
资源评论
资源推荐
Python
P2P
https://www.zhihu.com/question/26762707
HTTP
HTTP
PostmancURL
Python3.6+
"5Python"
https://github.com/dvf/blockchain
IDEPythonSublineEditPlusPyCharm
VSCodeAtom
"py_blockchain_demo"python
'blockchain.py'
Python
1
2
2.1
Blockchain
class Blockchain(object):
def __init__(self):
self.chain = []
self.current_transactions = []
def new_block(self):
# Creates a new Block and adds it to the chain
pass
def new_transaction(self):
# Adds a new transaction to the list of transactions
pass
@staticmethod
def hash(block):
# Hashes a Block
pass
@property
def last_block(self):
# Returns the last Block in the chain
pass
Blockchain
Method
Unix ()
Block
block = {
'index': 1,
'timestamp': 1506057125.900785,
'transactions': [
{
'sender': "8527147fe1f5426f9dd545de4b27ee00",
'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
'amount': 5,
}
2.2
],
'proof': 324984774000,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e730433
62938b9824"
}
-
-
new_transaction()
:
class Blockchain(object):
...
def new_transaction(self, sender, recipient, amount):
"""
Creates a new transaction to go into the next mined Block
:param sender: <str> Address of the Sender
:param recipient: <str> Address of the Recipient
:param amount: <int> Amount
:return: <int> The index of the Block that will hold this transactio
n
"""
self.current_transactions.append({
'sender': sender,
'recipient': recipient,
'amount': amount,
})
return self.last_block['index'] + 1
new_transaction() ---
Blockchain
()
2.3
2.4
new_block() new_transaction()
hash()
import hashlib
import json
from time import time
class Blockchain(object):
def __init__(self):
self.current_transactions = []
self.chain = []
#
self.new_block(previous_hash=1, proof=100)
def new_block(self, proof, previous_hash=None):
"""
:param proof: <int>
:param previous_hash: (Optional) <str> hash
:return: <dict>
"""
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
}
#
self.current_transactions = []
self.chain.append(block)
return block
def new_transaction(self, sender, recipient, amount):
"""
:param sender: <str>
:param recipient: <str>
:param amount: <int>
:return: <int>
"""
剩余25页未读,继续阅读
神康不是狗
- 粉丝: 39
- 资源: 336
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 大模型AI典型示范应用案例集
- AI指令合集-微头条10种框架创作指令
- 好看的邀请函PSD源文件(18个).zip
- Nvidia GeForce GTX 1080 TI显卡驱动(Win7、Win8驱动)
- AI指令合集-爆款文案优化助手
- Nvidia GeForce GTX 1080 TI显卡驱动(Win10、Win11驱动)
- GJB150A-2009军用装备实验室环境试验方法(共19份标准文件)
- 浩辰CAD看图王8.6.0最新版本下载,轻量化CAD看图软件,无需下载专业CAD软件,即可实现CAD看图、CAD图纸编辑、格式转换、三维览图等
- SW materials
- 好看的票券PSD源文件(15个).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0