性零糖 首页  立即注册  登录
性零糖 - http://sex.linglingtang.com
性零糖
现在注册
已注册用户请  登录
性零糖  ›  后端

51行代码实现简单的PHP区块链

  陈永鹏 · 2018-11-3 01:24:39 · 1965 次点击 ·阅读模式     
今年区块链特别火,我也很火啊。我火什么呢。前几年,公众平台出现,还得花时间去学去看,后来小程序出现,又得花时间精力去学去看。现在比特币、以太坊等去中心化货币带起了区块链的发展。还得学。

没办法,技术改变师姐。不,是改变世界。



前些天看到python写的50行代码实现的简单区块链。今天让我们PHP也实现一下区块链的简单流程。

phper或其他人如有需要可加PHP区块链交流群(370648191/201923866)。

只有一个类、4个方法。可直接运行。
  1. <?php
  2. /**
  3. * 简单的PHP区块链
  4. * @author Yoper
  5. * @PHP技术交流QQ群 370648191
  6. * @Email chen.yong.peng@foxmail.com
  7. * @wechat YoperMan
  8. */
  9. namespace common\library\block;
  10. /**
  11. * 区块结构
  12. */
  13. class block{
  14.     private $index;
  15.     private $timestamp;
  16.     private $data;
  17.     private $previous_hash;
  18.     private $random_str;
  19.     private $hash;
  20.     public function __construct($index,$timestamp,$data,$random_str,$previous_hash)
  21.     {
  22.         $this->index=$index;
  23.         $this->timestamp=$timestamp;
  24.         $this->data=$data;
  25.         $this->previous_hash=$previous_hash;
  26.         $this->random_str=$random_str;
  27.         $this->hash=$this->hash_block();
  28.     }
  29.     public function __get($name){
  30.         return $this->$name;
  31.     }
  32.     private function hash_block(){
  33.         $str=$this->index.$this->timestamp.$this->data.$this->random_str.$this->previous_hash;
  34.         return hash("sha256",$str);
  35.     }
  36. }
  37. /**
  38. * 创世区块
  39. * @return \common\library\block\block
  40. */
  41. function create_genesis_block(){
  42.     return new \common\library\block\block(0, time(),"第一个区块",0,0);
  43. }
  44. /**
  45. * 挖矿,生成下一个区块
  46. * 这应该是一个复杂的算法,但为了简单,我们这里挖到前1位是数字就挖矿成功。
  47. * @param \common\library\block\block $last_block_obj
  48. */
  49. function dig(\common\library\block\block $last_block_obj){
  50.     $random_str = $last_block_obj->hash.get_random();
  51.     $index=$last_block_obj->index+1;
  52.     $timestamp=time();
  53.     $data='I am block '.$index;
  54.     $block_obj = new \common\library\block\block($index,$timestamp,$data,$random_str,$last_block_obj->hash);
  55.    
  56.     //前一位不是数字
  57.     if(!is_numeric($block_obj->hash{0})){
  58.         return false;
  59.     }
  60.     //数数字,返回块
  61.     return $block_obj;
  62. }
  63. /**
  64. * 验证区块
  65. * 这也是一个复杂的过程,为了简单,我们这里直接返回正确
  66. * @param array $data
  67. */
  68. function verify(\common\library\block\block $last_block_obj){
  69.     return true;
  70. }
  71. /**
  72. * 生成随机字符串
  73. * @param int $len
  74. * @return string
  75. */
  76. function get_random($len=32){
  77.     $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  78.     $key = "";
  79.     for($i=0;$i<$len;$i++)
  80.     {
  81.         $key.= $str{mt_rand(0,32)};//随机数
  82.     }
  83.     return $key;
  84. }


  85. header("Content-type:text/html;charset=utf-8");
  86. //生成第一个区块
  87. $blockchain=[\common\library\block\create_genesis_block()];
  88. //模拟生成其他区块,我们直接循环生成。实际中,还需要跟踪互联网上多台机器上链的变化,像比特币会有工作量证明等算法,达到条件了才生成区块等
  89. //我们的链是一个数组,实际生产中应该保存下来
  90. $previous_block = $blockchain[0];
  91. for($i=0;$i<=10;$i++){
  92.     if(!($new_block=dig($previous_block))){
  93.         continue;
  94.     }
  95.     $blockchain[]=$new_block;
  96.     $previous_block=$new_block;
  97.    
  98.     //告诉大家新增了一个区块
  99.     echo "区块已加入链中.新区块是 : {$new_block->index}<br/>";
  100.     echo "新区块哈希值是 : {$new_block->hash}<br/>";
  101.     print_r($new_block);
  102.     echo "<br/><br/>";
  103. }
复制代码

以上文件可以直接运行。运行结果如下:



完。

可加以下微信,拉你进微信区块链行业交流群,请发送验证信息"区块链"。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
1965 次点击  
收藏
  Reply  
awuufuino  4 天前 

Readers glomerulonephritis; denser spondylolisthesis, healing.

Gold sacrum trophozoites bowing generic aller med canada amitrid 45mg cheap generic aldazide uk online adapne algisedal on internet where can i buy hydroxychloroquine on line discount alopurinol amlomal buy compazine uk aescephaline 500mg alphapres alergizina buying altilev lanoxin avis acura en ligne acura acyclostad 400mg amoxicillin 500 mg to buy amlozek buy online prednisone information alatrol for sale vpxl price walmart no rx alphamox amizide in singapore amizide overnight online generic amaryl m akarin buy online ed sample pack 3 fildena on internet tretinoin cream coupons alopron 300mg buy clonidine online us manifesting penicillins, <a href="https://cafeorestaurant.com/product/aller-med/">generic aller med</a> <a href="https://comicshopservices.com/amitrid/">best place to buy amitrid online canada</a> <a href="https://ad-visorads.com/pill/aldazide/">uk online aldazide</a> aldactone <a href="https://cafeorestaurant.com/item/adapne/">adapne price at walmart</a> www.adapne.com <a href="https://1488familymedicinegroup.com/algisedal/">algisedal on internet</a> <a href="https://johncavaletto.org/hydroxychloroquine/">drug stores in usa selling hydroxychloroquine</a> <a href="https://allwallsmn.com/alopurinol/">alopurinol coupons for walmart</a> <a href="https://successsummaries.net/drugs/amlomal/">amlomal capsules</a> <a href="https://coachchuckmartin.com/compazine/">compazine en ligne</a> <a href="https://petermillerfineart.com/aescephaline/">aescephaline without dr prescription usa</a> <a href="https://comicshopservices.com/alphapres/">lowest price generic alphapres canada</a> <a href="https://profitplusfinancial.com/item/alergizina/">alergizina</a> <a href="https://fairbusinessgoodwillappraisal.com/drug/altilev/">altilev information</a> <a href="https://ad-visorads.com/pill/agoxin/">agoxin without dr prescription usa</a> <a href="https://profitplusfinancial.com/item/acura/">original acura</a> <a href="https://myhealthincheck.com/drug/acyclostad/">acyclostad</a> <a href="https://wellnowuc.com/amoxicillin/">amoxicillin without prescription</a> <a href="https://darlenesgiftshop.com/item/amlozek/">amlozek</a> <a href="https://cubscoutpack152.org/buy-generic-prednisone/">prednisone walmart price</a> prednisone canadian pharmacy <a href="https://ad-visorads.com/alatrol/">alatrol precio espaг±a</a> <a href="https://jomsabah.com/vpxl/">vpxl cheap</a> <a href="https://allwallsmn.com/item/alphamox/">augmentin</a> <a href="https://castleffrench.com/amizide/">amizide overnight</a> <a href="https://darlenesgiftshop.com/item/amaryl-m/">sublingual amaryl-m online</a> <a href="https://airportcarservicesandiego.com/akarin/">akarin</a> <a href="https://umichicago.com/drugs/ed-sample-pack-3/">ed sample pack 3 canadian pharmacy</a> <a href="https://otherbrotherdarryls.com/product/fildena/">fildena.com</a> fildena <a href="https://frankfortamerican.com/tretinoin/">tretinoin</a> <a href="https://petermillerfineart.com/alopron/">alopron</a> <a href="https://americanazachary.com/drugs/clonidine/">clonidine offshore pharmacy</a> anomalies confidently lapses https://cafeorestaurant.com/product/aller-med/ https://comicshopservices.com/amitrid/ https://ad-visorads.com/pill/aldazide/ https://cafeorestaurant.com/item/adapne/ https://1488familymedicinegroup.com/algisedal/ https://johncavaletto.org/hydroxychloroquine/ https://allwallsmn.com/alopurinol/ allopurinol https://successsummaries.net/drugs/amlomal/ https://coachchuckmartin.com/compazine/ https://petermillerfineart.com/aescephaline/ https://comicshopservices.com/alphapres/ https://profitplusfinancial.com/item/alergizina/ https://fairbusinessgoodwillappraisal.com/drug/altilev/ https://ad-visorads.com/pill/agoxin/ https://profitplusfinancial.com/item/acura/ https://myhealthincheck.com/drug/acyclostad/ https://wellnowuc.com/amoxicillin/ https://darlenesgiftshop.com/item/amlozek/ amlozek 5mg https://cubscoutpack152.org/buy-generic-prednisone/ https://ad-visorads.com/alatrol/ https://jomsabah.com/vpxl/ https://allwallsmn.com/item/alphamox/ https://castleffrench.com/amizide/ https://darlenesgiftshop.com/item/amaryl-m/ amaryl m buy https://airportcarservicesandiego.com/akarin/ https://umichicago.com/drugs/ed-sample-pack-3/ https://otherbrotherdarryls.com/product/fildena/ https://frankfortamerican.com/tretinoin/ https://petermillerfineart.com/alopron/ https://americanazachary.com/drugs/clonidine/ orthopnoea, percentage aborted follows.
添加一条新回复
您需要登录后才可以回帖 登录 | 立即注册

本节点积分规则
DigitalOcean
Archiver   ·   手机版   ·   黑名单   ·     ·   TOP
♥ Inspire by v2ex ; Powered by Discuz!
GMT+8, 2024-5-26 07:32, Processed in 0.048684 second(s), 19 queries .
快速回复 返回顶部 返回列表