没有合适的资源?快使用搜索试试~ 我知道了~
用Python实现简单的区块链系统1
需积分: 0 18 下载量 185 浏览量
2022-08-04
15:57:35
上传
评论 5
收藏 516KB PDF 举报
温馨提示
试读
26页
用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>
"""
self.current_transactions.append({
'sender': sender,
'recipient': recipient,
'amount': amount,
})
return self.last_block['index'] + 1
@property
def last_block(self):
return self.chain[-1]
@staticmethod
def hash(block):
"""
SHA-256
:param block: <dict> Block
:return: <str>
"""
#
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
---
PoWPoW
x y Hash 0 hash(x * y) = ac23dc...0
x = 5 y Python
from hashlib import sha256
x = 5
y = 0 # We don't know what y should be yet...
while sha256(f'{x*y}'.encode()).hexdigest()[-1] != "0":
y += 1
3
3.1
剩余25页未读,继续阅读
神康不是狗
- 粉丝: 27
- 资源: 338
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0