前言 最近由于比特币的暴涨,导致背后的区块链技术被人给当作热点拿出来。我也不例外的对区块链技术进行了分析。至于比特币,我个人是没有过多的关注。这点见仁见智吧。 区块链含义
百度百科给出的含义定义是这样的,通过这个,我们知道了,区块链其实就是一种数据结构,和二叉树、图等一样。那么其实比特币就是基于这种数据结构搞出的一种虚拟货币罢了。 中心化与去中心化 中心化(Centralization),与之对立的是去中心化(Decentralization)。为了简单的理解,我举一个例子。 中心化 那么什么是去中心化呢?很简单,就是账本不给账房先生管了,因为这个账房先生可能老是把账本丢了,或者说账本自己跑了等等借口,导致自己乱改账。于是每个人都有一个账本了,任何人改动了自己的账本都要告诉其他所有人,其他人也会在自己的账本记上一笔。但是怎么知道你是不是做假账呢?于是规定只要有人发现新的账目不对,可以拒绝接受。最后以大多数人一致的账目为准。 区块链去中心化那么一个既是数据结构,又是一个特殊的分布式数据库有什么特别之处呢?这个特点就是所谓的“去中心化”。那么我们可以发现,去中心化是需要资源的,为什么呢?因为比如上面的例子,人人都记账了。但是有人天天记,有人偷懒不记,会产生不公平。于是大家决定每天早上掷骰子,根据点数决定谁来记当天的账,其他人核对一下,没问题就复制过来。并且,当天的记账者会获得一点点奖励(这就是挖矿)。 区块链算法 因为区块链的去中心化,所以是没有管理员,也不需要人工审核和干涉,它是彻底无中心的。那么怎么保证数据的安全呢?这时候就请出了共识算法和加密算法。 环境Laravel 5.5 DEMO道理讲了一大堆,那么现在我们来模拟一个demo看看,由于去中心化的机制,每个人既要成为服务器,也要成为客户端。所以对PHP来说,每一个人等于要装一个apache的环境成为服务器一样,同时还要有发送广播之类等机制来进行传播。所以这里的demo只是模仿出最基本的原理。具体来说的话,编程语言不是问题,主要是思想。 DEMO—区块的结构首先我们要定义一下区块的结构。 /* 一个区块的结构: block = { 'index': 1, 'timestamp': 1506057125.900785, 'transactions': [ { 'sender': '8527147fe1f5426f9dd545de4b27ee00', 'recipient': 'a77f5cdfa2934df3954a5c7c7da5df1f', 'amount': 5, } ], 'proof': 324984774000, 'previous_hash': '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824' }*/ 这里面每个区块包含属性:索引(index),Unix时间戳(timestamp),交易列表(transactions),工作量证明以及前一个区块的Hash值。每个新的区块都包含上一个区块的Hash,这是关键的一点,它保障了区块链不可变性。如果攻击者破坏了前面的某个区块,那么后面所有区块的Hash都会变得不正确。 DEMO—区块类我直接定义了一个区块类,用于对区块的操作等。同时我采用了Redis进行对一些变量进行存储。 class Blockchain{ # 区块链 public $chain; # 交易 public $current_transactions; # 节点 public $node; public function __construct() { $this->chain = json_decode( Redis::get('BlockChain'),true); $this->node = json_decode(Redis::get('BlockNode'),true); $this->current_transactions = array(); # 创建创世块 if( count($this->last_block()) == 0 ){ $this->new_block('1',100); } } # 获取区块链信息 public function getChain() { return $this->chain; } # 获取交易信息 public function getTran(Type $var = null) { return $this->current_transactions; } # 获取节点信息 public function getNode(Type $var = null) { return $this->node; } /* 将新节点添加到节点列表中 :param address: DEMO—Controllerclass BlockController extends Controller{ # index public function Index(Request $request) { # 获取最新的节点信息 return view('block'); } # 挖掘新区域块 public function Mine(Request $request) { $BC = new Blockchain(); $node_identifier = Uuid::uuid4(time())->toString(); # 我们运行工作证明算法来获得下一个证明。 $last_block = $BC->last_block(); $last_proof = $last_block['proof']; $proof = $BC->proof_of_work($last_proof); # 给工作量证明的节点提供奖励. # 发送者为 '0' 表明是新挖出的币 $BC->new_transaction('0',$node_identifier,1); # 通过将其添加到链中来锻造新块 $block = $BC->new_block('0',$proof); $response = array( 'message' => '新区块', 'index' => $block['index'], 'transactions' => $block['transactions'], 'proof' => $block['proof'], 'previous_hash' => $block['previous_hash'] ); $str = array( 'message' => '新区块'.$block['index'].': '.$block['previous_hash'] ); # 添加节点记录 $BC->register_node(explode('/',$request->url())[2]); echo json_encode($str); } # 新的交易记录 public function TransactionsNew(Request $request) { $BC = new Blockchain(); $sender = $request->post('sender'); $recipient = $request->post('recipient'); $amount = $request->post('amount'); if($sender == null || $recipient == null || $amount == null){ return false; } # 创建一个新的交易记录 $index = $BC->new_transaction($sender,$recipient,$amount); $response = array( 'message' => '交易将被添加到块 ' . $index ); print_r($response); } # 显示整个区块链 public function Chain() { $BC = new Blockchain(); $response = array( 'chain' => $BC->getChain(), 'length' => count($BC->getChain()) ); echo json_encode($response); } # 注册节点 public function NodesRegister(Request $request) { $BC = new Blockchain(); $nodes = $request->post('nodes'); if($nodes == null){ return '错误:请提供一个有效的节点列表'; } foreach($nodes as $node){ $BC->register_node($node); } $response = array( 'message' => '新的节点已被添加', 'total_nodes' => $BC->getNode(), ); print_r($response); } # 解决节点冲突 public function NodesResolve() { $BC = new Blockchain(); $replaced = $BC->resolve_conflicts(); if($replaced){ $response = array( 'message' => '我们的链已被取代', 'new_chain' => $BC->getChain() ); }else{ $response = array( 'message' => '我们的链是权威的', 'new_chain' => $BC->getChain() ); } echo json_encode($response); // print_r($response); }} DEMO—Route# 区块链Route::get('/block','BlockController@Index');Route::get('/block/mine','BlockController@Mine');Route::post('/block/transactions/new','BlockController@TransactionsNew');Route::get('/block/chain','BlockController@Chain');Route::post('/block/nodes/register','BlockController@NodesRegister');Route::get('/block/nodes/resolve','BlockController@NodesResolve'); |
|