初始区块链
特点:
区块链是由区块的记录构成的不可变,有序的链记录。主要有以下几个特点:
1:去中心化
由于使用分布式核算和存储,不存在中心化的硬件或管理机构,任意节点的权利和义务都是均等的,系统中的数据块由整个系统中具有维护功能的节点来共同维护。得益于区块链的去中心化特征,比特币也有去中心化的特征 。
2:开放性
系统是开放的,除了交易各方的私有信息被加密外,区块链的数据对所有人公开,任何人都可以通过公开的接口查询区块链数据和开发相关应用,因此整个系统信息高度透明。
3:自治性
区块链采用基于协商一致的规范和协议(比如一套公开透明的算法)使得整个系统中的所有节点能够在去信任的环境自由安全的交换数据,使得对“人”的信任改成了对机器的信任,任何人为的干预不起作用。
4:信息不可篡改
一旦信息经过验证并添加至区块链,就会永久的存储起来,除非能够同时控制住系统中超过51%的节点,否则单个节点上对数据库的修改是无效的,因此区块链的数据稳定性和可靠性极高。
5:匿名性
由于节点之间的交换遵循固定的算法,其数据交互是无需信任的(区块链中的程序规则会自行判断活动是否有效),因此交易对手无须通过公开身份的方式让对方对自己产生信任,对信用的累积非常有帮助。
与传统分布式数据库相比主要有以下两个区别:
1:传统分布式数据库支持增删查改,区块链只支持查找和插入,对区块不能进行删除和修改。
2:传统的分布式数据库一般都是主从结构:master和slaves的结构,为了保证高可用,通过备用master来实现,而区块链是一个去中心化的数据库。没有主从结构。
区块链和比特币:
说起区块链,大多数人都会谈起比特币。但区块链并不等于是比特币,现在已经是区块链3.0时代,而比特币只是区块链1.0时代的产物。
区块链的进化方式是:
- ▪ 区块链1.0——数字货币
- ▪ 区块链2.0——数字资产与智能合约
- ▪ 区块链3.0——各种行业分布式应用落地
区块链的分类:
公有区块链(PublicBlockChains)
公有区块链是指:世界上任何个体或者团体都可以发送交易,且交易能够获得该区块链的有效确认,任何人都可以参与其共识过程。公有区块链是最早的区块链,也是应用最广泛的区块链,各大bitcoins系列的虚拟数字货币均基于公有区块链,世界上有且仅有一条该币种对应的区块链。
联合(行业)区块链(ConsortiumBlockChains)
行业区块链:由某个群体内部指定多个预选的节点为记账人,每个块的生成由所有的预选节点共同决定(预选节点参与共识过程),其他接入节点可以参与交易,但不过问记账过程(本质上还是托管记账,只是变成分布式记账,预选节点的多少,如何决定每个块的记账者成为该区块链的主要风险点),其他任何人可以通过该区块链开放的API进行限定查询。
私有区块链(privateBlockChains)
私有区块链:仅仅使用区块链的总账技术进行记账,可以是一个公司,也可以是个人,独享该区块链的写入权限,本链与其他的分布式存储方案没有太大区别。(Dec2015)保守的巨头(传统金融)都是想实验尝试私有区块链,而公链的应用例如bitcoin已经工业化,私链的应用产品还在摸索当中。
通过python 实现简单的区块链
- 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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
对代码作出解释:
- Blockchain类:区块链类,在构造函数中有两个列表,一个是存储区块链,一个是存储交易
- block:区块,每个区块包含属性,索引(index),Unix时间戳(timestamp),交易列表,工作量证明,以及前一个区块的Hash值。
- new_block:生成一个新的区块方法
- new_transaction:生成新交易信息,信息将加入到下一个代挖的区块中。
- proof_of_word:简单的工作量证明,矿工通过工作量来得到奖励,对应的比特币,矿工挖矿,就是在运行这个方法,到啦proof,然后得到比特币奖励。我们的例子是要求开头为4个0,修改开头的0个数,你会发现多一个零会大大增加计算出结果所需的时间。
- valid_proof:验证得出的工作量证明是否正确。
- register_node:注册新的节点,这个是完成分布式区块链的主要方法。
- resolve_conflicts:解决区块链冲突问题,不同的客户端可能会有不同的链,通过该方法获取到最长的有效率作为客户端的区块链
通过Flask框架,将网络请求映射到python函数:
1:/transactions/new 创建一个交易并添加到区块
2:/mine 告诉服务器去挖掘新的区块
3:/chain 返回整个区块链
4:/nodes/register 注册节点
5:/nodes/resolve 解决冲突
通过Postman和API进行交互
1:打开8000,8001两个不同端口模拟两个客户端。
- python blockchain.py -p 8000
- python blockchain.py -p 8001
- 1
- 2
2:将8001注册到8000端口中:
3:在8001端口挖矿:
4:调用resolve接口,将8001端口的链同步到8000端口
5:查看8000端口的链:
到这里基本区块链就完成啦,生成交易,并将交易存到以下一个待挖的测试,这里就不在过多的掩饰,基本是一样的测试方法。测试过程中对应对应的命令行输出如下: