前言了解区块链的基本操作什么是区块链?区块链是不可变的、有序的记录链,称为块。它们可以包含事务、文件或您喜欢的任何数据。但重要的是,他们需要准备什么才能用哈希链接在一起?php5.6+1和Block长什么样?每个块都有一个索引、一个时间戳(Unix时间戳)、一个交易列表、一个校验和(由工作量证明算法生成的工作量证明)和前一个块的哈希值。block={'index':2,'timestamp':1506057125,'transactions':[{'sender':"8527147fe1f5426f9dd545de4b27ee00",'recipient':"a77f5cdfa2934df3954a5c7c7da5df1f",'amount}':324984774000,'previous_hash':"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"}在这一点上,区块链的概念应该是显而易见的——每个新区块都包含前一个区块的哈希值。这很关键,因为这就是区块链不可变的原因:如果攻击者破坏了区块链中较早的区块,则所有后续区块都将包含不正确的哈希值。创建一个新的块类。区块链由N个区块组成。在区块链中,价值信息存储在区块中。例如,比特币的区块存储交易记录,这是任何加密货币的核心。此外,区块还包含技术信息,例如版本号、当前时间戳和前一个区块的哈希值。hash;}公共函数__construct($index,$timestamp,$transactions,$previousHash,$proof){$this->index=$index;$this->timestamp=$timestamp;$this->transactions=$transactions;$this->previousHash=$previousHash;$this->proof=$proof;$this->hash=$this->块散列();}/***当前区块签名*@returnstring*/privatefunctionblockHash(){//我们必须确保这个字典(区块)是有序的,否则我们会得到不一致的哈希值$blockArray=['index'=>$this->index,'timestamp'=>$this->timestamp,'transactions'=>$this->transactions,'proof'=>$this->proof,'previous_hash'=>$this->previousHash];$blockString=json_encode($blockArray);返回散列('sha256',$blockString);}}index是当前区块的索引timestamp是当前区块的生成时间transactions是当前区块的交易列表(多个或一个交易)previousHash是前一个区块的签名哈希hash是当前区块的签名哈希proof是当前区块的矿工工作量证明proof使用工作量证明(PoW)算法,证明新区块是如何在区块链上创建或挖掘的。PoW的目标是计算出一个满足一定条件的数字,这个数字一定是计算难度高但容易验证的。这是工作量证明背后的核心思想。在比特币中,工作量证明算法被称为Hashcash,与上述问题非常相似,但计算难度很大。这就是矿工争先恐后地计算出块权的问题。通常,计算难度与目标字符串需要满足的特定字符数成正比。矿工计算出结果后,会得到一定数量的比特币奖励(通过交易)2.创建区块链我们需要创建一个Blockchain类,它的构造函数创建一个初始化的空列表(用来存放我们的区块链)并创建世纪块,以及初始化交易列表。下面是我们类的一个例子:第一步:初始化区块链表,创建创世区块/***@vararrayblocklist*/private$chain;/***@var数组交易列表*/private$currentTransactions;公共函数__construct(){$this->chain=[$this->createGenesisBlock()];$this->currentTransactions=[];}/***创建创世块*@returnarray*/privatefunctioncreateGenesisBlock(){$block=['index'=>1,'timestamp'=>time(),'transactions'=>[],'proof'=>100,'previous_hash'=>'0000000000000000000000000000000000000000000000参考BTC第一个创世块];$block['hash']=(newBlock($block['index'],$block['timestamp'],$block['transactions'],$block['previous_hash'],$block['proof']))->getHash();返回$块;}第二步:添加新交易创建新交易,等待新区块打包到交易列表中。生成新块后清除列表/***添加新交易*@param$senderPrivate键*@param$senderAddress*@param$recipientAddress*@param$amount*@returnbool*/publicfunctioncreateTransaction($senderPrivateKey,$senderAddress,$recipientAddress,$amount){$row=['from'=>$senderAddress,'to'=>$recipientAddress,'amount'=>$amount,'timestamp'=>time()];//TODO私钥签名(如校验签名)//TODO区块链节点可以使用发送方通过公钥签名推导出公钥,然后通过公钥验证签名并比对数据$this->currentTransactions[]=$行;返回真;}Step3:创建新区块当前示例创建新区块快速操作只能通过挖矿Miner运行成功,挖矿说明在Step4/***添加新区块*@paramint$proof*@returnbool*/publicfunctionaddBlock(int$proof){//前一个区块的信息$preBlockInfo=$this->chain[count($this->chain)-1];//验证工作证明if($this->checkProof($proof,$preBlockInfo['proof'])==false){returnfalse;}//TODO奖励矿工(在transaction交易中)$block=['index'=>count($this->chain)+1,'timestamp'=>time(),'transactions'=>$this->currentTransactions,'proof'=>$proof,'previous_hash'=>$preBlockInfo['hash'],'hash'=>''];$block['hash']=(newBlock($block['index'],$block['timestamp'],$block['transactions'],$block['previous_hash'],$block['proof']))->getHash();//新增区块$this->chain[]=$block;//重新设置交易事务$this->currentTransactions=[];返回真;}/***校验算力*@paramstring$proof*@paramstring$preProof*@returnbool*/privatefunctioncheckProof(string$proof,string$preProof){$string=$proof.$preProof;$hash=hash('sha256',$string);if(substr($hash,0,4)=='0000'){返回真;}else{返回错误;}}Step4:Mining挖矿是神奇的地方,它很简单,它做了三件事:计算工作量证明PoW授予矿工(自己)一个币来构建一个新的区域,通过添加一个新的交易块并添加到链在比特币中,工作量证明算法称为Hashcash,它与上述问题类似,只是计算难度很大。这就是矿工争夺创建区块的权利的原因。计算题。通常,计算难度与目标字符串需要满足的特定字符数成正比。矿工计算结果后,将获得一定数量的比特币奖励(通过交易)。让我们实现一个类似的PoW算法来找到一个数字P,使得它与前一个块的Proof拼接形成的字符串的Hash值以4个零开头。/***挖矿*@returnvoid*/publicfunctionmine(){//while(true)//{$proof=0;//最新区块$blockInfo=$this->chain[count($this->chain)-1];$preProof=$blockInfo['proof'];while(true){$string=$proof.$preProof;$hash=hash('sha256',$string);if(substr($hash,0,4)=='0000'){//添加一个新块$this->addBlock($proof);休息;$证明++;}//}}第五步:运行测试$blockChainObj=newBlockchain();//添加交易$blockChainObj->createTransaction('','8527147fe1f5426f9dd545de4b27ee00','a77f5cdfa2934df3954a5c7c7da5df1f',1);//开始挖矿(一个新的挖出区块就会产生)$blockChainObj->mine();//查看当前区块列表$blockList=$blockChainObj->getChainList();var_dump($blockList);//result:$phpBlockchain.phparray(2){[0]=>数组(6){["在dex"]=>int(1)["timestamp"]=>int(1580717292)["transactions"]=>array(0){}["proof"]=>int(100)["previous_hash"]=>string(64)"0000000000000000000000000000000000000000000000000000000000000000"["hash"]=>string(64)"567b2848f3ff87a614b3ba5ddc13389d4d7440699b1857935412561721d86d05"}[1]=>array(6){["index"]=>int(2)["timestamp"]=>int(1580717292)["transactions"]=>array(1){[0]=>array(4){["from"]=>string(32)"8527147fe1f5426f9dd545de4b27ee00"["to"]=>string(32)"a77f5cdfa2934df3954a5c7c7da5df1f"["金额"]=>int(1)["时间戳"]=>int(1580717292)}}["proof"]=>int(28)["previous_hash"]=>string(64)"567b2848f3ff87a614b3ba5ddc13389d4d7440699b1857935412561721d86d05"["hash"]=>string(64)"3a599c88ddd60fb25605df33d33b19252117c3d7d0e70c66dbc45ed81ab295a9"}}Setp6:完整代码ar414-com/phpblock
