区块链是由区块的记录构成的不可变,有序的链记录。主要有以下几个特点:
1:去中心化
由于使用分布式核算和存储,不存在中心化的硬件或管理机构,任意节点的权利和义务都是均等的,系统中的数据块由整个系统中具有维护功能的节点来共同维护。得益于区块链的去中心化特征,比特币也有去中心化的特征 。
2:开放性
系统是开放的,除了交易各方的私有信息被加密外,区块链的数据对所有人公开,任何人都可以通过公开的接口查询区块链数据和开发相关应用,因此整个系统信息高度透明。
3:自治性
区块链采用基于协商一致的规范和协议(比如一套公开透明的算法)使得整个系统中的所有节点能够在去信任的环境自由安全的交换数据,使得对“人”的信任改成了对机器的信任,任何人为的干预不起作用。
4:信息不可篡改
一旦信息经过验证并添加至区块链,就会永久的存储起来,除非能够同时控制住系统中超过51%的节点,否则单个节点上对数据库的修改是无效的,因此区块链的数据稳定性和可靠性极高。
5:匿名性
由于节点之间的交换遵循固定的算法,其数据交互是无需信任的(区块链中的程序规则会自行判断活动是否有效),因此交易对手无须通过公开身份的方式让对方对自己产生信任,对信用的累积非常有帮助。
与传统分布式数据库相比主要有以下两个区别:
1:传统分布式数据库支持增删查改,区块链只支持查找和插入,对区块不能进行删除和修改。
2:传统的分布式数据库一般都是主从结构:master和slaves的结构,为了保证高可用,通过备用master来实现,而区块链是一个去中心化的数据库。没有主从结构。
说起区块链,大多数人都会谈起比特币。但区块链并不等于是比特币,现在已经是区块链3.0时代,而比特币只是区块链1.0时代的产物。
区块链的进化方式是:
区块链的分类:
公有区块链(PublicBlockChains)
公有区块链是指:世界上任何个体或者团体都可以发送交易,且交易能够获得该区块链的有效确认,任何人都可以参与其共识过程。公有区块链是最早的区块链,也是应用最广泛的区块链,各大bitcoins系列的虚拟数字货币均基于公有区块链,世界上有且仅有一条该币种对应的区块链。
联合(行业)区块链(ConsortiumBlockChains)
行业区块链:由某个群体内部指定多个预选的节点为记账人,每个块的生成由所有的预选节点共同决定(预选节点参与共识过程),其他接入节点可以参与交易,但不过问记账过程(本质上还是托管记账,只是变成分布式记账,预选节点的多少,如何决定每个块的记账者成为该区块链的主要风险点),其他任何人可以通过该区块链开放的API进行限定查询。
私有区块链(privateBlockChains)
私有区块链:仅仅使用区块链的总账技术进行记账,可以是一个公司,也可以是个人,独享该区块链的写入权限,本链与其他的分布式存储方案没有太大区别。(Dec2015)保守的巨头(传统金融)都是想实验尝试私有区块链,而公链的应用例如bitcoin已经工业化,私链的应用产品还在摸索当中。
import hashlib
import json
from time import time
from typing import Any, Dict, List, Optional
from urllib.parse import urlparse
from uuid import uuid4
from flask import Flask, jsonify, request
import requests
class Blockchain:
def __init__(self):
self.current_transactions = []
self.chain = []
self.nodes = set()
# 创建创世块
self.new_block(previous_hash='1', proof=100)
def register_node(self, address: str) -> None:
"""
Add a new node to the list of nodes
:param address: Address of node. Eg. 'http://192.168.0.5:5000'
"""
parsed_url = urlparse(address)
self.nodes.add(parsed_url.netloc)
def valid_chain(self, chain: List[Dict[str, Any]]) -> bool:
"""
Determine if a given blockchain is valid
:param chain: A blockchain
:return: True if valid, False if not
"""
last_block = chain[0]
current_index = 1
while current_index < len(chain):
block = chain[current_index]
print(f'{last_block}')
print(f'{block}')
print("\n-----------\n")
# Check that the hash of the block is correct
if block['previous_hash'] != self.hash(last_block):
return False
# Check that the Proof of Work is correct
if not self.valid_proof(last_block['proof'], block['proof']):
return False
last_block = block
current_index += 1
return True
def resolve_conflicts(self) -> bool:
"""
共识算法解决冲突
使用网络中最长的链.
:return: 如果链被取代返回 True, 否则为False
"""
neighbours = self.nodes
new_chain = None
# We're only looking for chains longer than ours
max_length = len(self.chain)
# Grab and verify the chains from all the nodes in our network
for node in neighbours:
response = requests.get(f'http://{node}/chain')
if response.status_code == 200:
length = response.json()['length']
chain = response.json()['chain']
# Check if the length is longer and the chain is valid
if length > max_length and self.valid_chain(chain):
max_length = length
new_chain = chain
# Replace our chain if we discovered a new, valid chain longer than ours
if new_chain:
self.chain = new_chain
return True
return False
def new_block(self, proof: int, previous_hash: Optional[str]) -> Dict[str, Any]:
"""
生成新块
:param proof: The proof given by the Proof of Work algorithm
:param previous_hash: Hash of previous Block
:return: New Block
"""
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
}
# Reset the current list of transactions
self.current_transactions = []
self.chain.append(block)
return block
def new_transaction(self, sender: str, recipient: str, amount: int) -> int:
"""
生成新交易信息,信息将加入到下一个待挖的区块中
:param sender: Address of the Sender
:param recipient: Address of the Recipient
:param amount: Amount
:return: The index of the Block that will hold this transaction
"""
self.current_transactions.append({
'sender': sender,
'recipient': recipient,
'amount': amount,
})
return self.last_block['index'] + 1
@property
def last_block(self) -> Dict[str, Any]:
return self.chain[-1]
@staticmethod
def hash(block: Dict[str, Any]) -> str:
"""
生成块的 SHA-256 hash值
:param block: Block
"""
# We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
def proof_of_work(self, last_proof: int) -> int:
"""
简单的工作量证明:
- 查找一个 p' 使得 hash(pp') 以4个0开头
- p 是上一个块的证明, p' 是当前的证明
"""
proof = 0
while self.valid_proof(last_proof, proof) is False:
proof += 1
return proof
@staticmethod
def valid_proof(last_proof: int, proof: int) -> bool:
"""
验证证明: 是否hash(last_proof, proof)以4个0开头
:param last_proof: Previous Proof
:param proof: Current Proof
:return: True if correct, False if not.
"""
guess = f'{last_proof}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"
# Instantiate the Node
app = Flask(__name__)
# Generate a globally unique address for this node
node_identifier = str(uuid4()).replace('-', '')
# Instantiate the Blockchain
blockchain = Blockchain()
@app.route('/mine', methods=['GET'])
def mine():
# We run the proof of work algorithm to get the next proof...
last_block = blockchain.last_block
last_proof = last_block['proof']
proof = blockchain.proof_of_work(last_proof)
# 给工作量证明的节点提供奖励.
# 发送者为 "0" 表明是新挖出的币
blockchain.new_transaction(
sender="0",
recipient=node_identifier,
amount=1,
)
# Forge the new Block by adding it to the chain
block = blockchain.new_block(proof, None)
response = {
'message': "New Block Forged",
'index': block['index'],
'transactions': block['transactions'],
'proof': block['proof'],
'previous_hash': block['previous_hash'],
}
return jsonify(response), 200
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
values = request.get_json()
# 检查POST数据
required = ['sender', 'recipient', 'amount']
if not all(k in values for k in required):
return 'Missing values', 400
# Create a new Transaction
index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])
response = {'message': f'Transaction will be added to Block {index}'}
return jsonify(response), 201
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
'chain': blockchain.chain,
'length': len(blockchain.chain),
}
return jsonify(response), 200
@app.route('/nodes/register', methods=['POST'])
def register_nodes():
values = request.get_json()
nodes = values.get('nodes')
if nodes is None:
return "Error: Please supply a valid list of nodes", 400
for node in nodes:
blockchain.register_node(node)
response = {
'message': 'New nodes have been added',
'total_nodes': list(blockchain.nodes),
}
return jsonify(response), 201
@app.route('/nodes/resolve', methods=['GET'])
def consensus():
replaced = blockchain.resolve_conflicts()
if replaced:
response = {
'message': 'Our chain was replaced',
'new_chain': blockchain.chain
}
else:
response = {
'message': 'Our chain is authoritative',
'chain': blockchain.chain
}
return jsonify(response), 200
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
app.run(host='127.0.0.1', port=port)
对代码作出解释:
通过Flask框架,将网络请求映射到python函数:
1:/transactions/new 创建一个交易并添加到区块
2:/mine 告诉服务器去挖掘新的区块
3:/chain 返回整个区块链
4:/nodes/register 注册节点
5:/nodes/resolve 解决冲突
1:打开8000,8001两个不同端口模拟两个客户端。
python blockchain.py -p 8000
python blockchain.py -p 8001
2:将8001注册到8000端口中:
3:在8001端口挖矿:
4:调用resolve接口,将8001端口的链同步到8000端口
5:查看8000端口的链:
到这里基本区块链就完成啦,生成交易,并将交易存到以下一个待挖的测试,这里就不在过多的掩饰,基本是一样的测试方法。测试过程中对应对应的命令行输出如下: