从零初始用 Python 打造自己的区块链
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/wvkocF2MXjUB0iaia5nk3IQJuVKG6CZ1xKkNDpFBGygGxdWibF4gcIGEEicicedRqKrW1JIAX72mYV2zMcxJO5siaFcA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">本文来自作者</span><span style="color: black;">Frank</span><span style="color: black;">在</span></span><span style="color: black;"><strong style="color: blue;">GitChat</strong></span><span style="color: black;">上分享 「从零<span style="color: black;">起始</span>用 Python 打造自己的区块链」</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">编辑 | 毛利</span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">前言</span></strong></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2018 年 4 月<span style="color: black;">败兴</span>,随着加密货币价格的回升,<span style="color: black;">各样</span>关于区块链的<span style="color: black;">专题</span>甚嚣尘上,各大<span style="color: black;">媒介</span>对其<span style="color: black;">商场</span>性质、技术原理、发展前景各个方面,都有<span style="color: black;">各样</span>非常深入的探讨。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">同期</span>,新的项目、概念<span style="color: black;">亦</span>层出不穷,让人眼花缭乱。笔者认为,“区块链” 本质上还是一种软件算计 / 架构上的创新。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">仅有</span>自己搭建环境,编写代码,实现从<span style="color: black;">创立</span>账本到完成交易的全过程,<span style="color: black;">才可</span>真正体会区块链的精髓。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNek1XcEpWGjnpBibTqnrPSJ0Iv9ZvgkHlkU6L58KOpTqrSic8ebdWhkxVqw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">比特币近十年来的价格逐波<span style="color: black;">提升</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本场 Chat 的目的是,<span style="color: black;">便是</span><span style="color: black;">帮忙</span>广大的区块链<span style="color: black;">兴趣</span>者<span style="color: black;">或</span><span style="color: black;">研发</span>人员,从零<span style="color: black;">起始</span>打造属于您自己的区块链应用。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">力求用最简单的语言,介绍这个最热门的技术,<span style="color: black;">无</span>任何<span style="color: black;">基本</span>的读者<span style="color: black;">亦</span>能按图索骥,一窥<span style="color: black;">到底</span>,消除<span style="color: black;">各样</span>专家为其增添的<span style="color: black;">非常多</span>迷思。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本文<span style="color: black;">首要</span>将介绍<span style="color: black;">有些</span>区块链的背景知识,而后将循序渐进地介绍在本地<span style="color: black;">研发</span>区块链应用所需要的模块和算法,做出一个简单的区块链交易程序。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这里</span>过程中,会穿插<span style="color: black;">非常多</span>关于<span style="color: black;">关联</span>名字、术语、算法的解释。<span style="color: black;">关联</span>源代码都<span style="color: black;">保留</span>在笔者的 GitHub 项目中</span><span style="color: black;">(https://github.com/marmul2/blockchain)</span><span style="color: black;">,欢迎读者直接<span style="color: black;">运用</span><span style="color: black;">或</span>提交修改申请(pull request)。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">GitHub 是一个面向开源及个人软件项目的托管平台,<span style="color: black;">由于</span>只支持 git <span style="color: black;">做为</span><span style="color: black;">独一</span>的版本管理格式,故名 GitHub。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">由于</span>这里聚集了超过 350 万<span style="color: black;">研发</span>者,其中又以男性程序员为主,<span style="color: black;">因此</span>被戏<span style="color: black;">叫作</span>为 “<span style="color: black;">全世界</span>最大同性交友网站”。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">它<span style="color: black;">不但</span>支持代码的发布、管理,还<span style="color: black;">供给</span>了订阅、讨论组、文本渲染、在线文件编辑器、协作图谱、代码片段分享等功能。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">任何用户都<span style="color: black;">能够</span>在其上<span style="color: black;">保留</span>和发布自己的个人项目,<span style="color: black;">非常多</span>知名的开源项目如 Ruby on Rails、jQuery、python 等都托管在该平台上。</span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">1. 区块链简介</strong></span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一位名为 “中本聪” (Satoshi Nakamoto) 的人于 2008 年 11 月 1 日,在 metzdowd.com网站的<span style="color: black;">秘码</span>学邮件列表中,<span style="color: black;">发布</span>了一篇名为《比特币:一种点对点式的电子现金系统》(Bitcoin: A Peer to Peer Electronic Cash System) 的论文,首次提出了 “区块链”(Blockchain)的概念。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">白皮书发布之后不久,中本聪于 2009 年 1 月 3 日,<span style="color: black;">研发</span>出首个实现了比特币算法的客户端程序,并进行了首次 “采矿”,<span style="color: black;">得到</span>了<span style="color: black;">第1</span>批 50 个比特币。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这标志着比特币的正式诞生。中本聪出于知识分享和推广的理念,把比特币程序的代码<span style="color: black;">所有</span>开源,与世界各地的软件<span style="color: black;">研发</span>人员<span style="color: black;">一起</span>分享。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNek4eickdcHCQT8NxGEVmxKjnibccEro3bFp8jwobQsIDichiczYhvOrfrnmg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">中本聪的原始论文</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过这十年的发展,区块链<span style="color: black;">已然</span>发展<span style="color: black;">作为</span><span style="color: black;">全世界</span>最具影响力的创新技术之一。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">从金融行业、制造<span style="color: black;">制品</span>到教育<span style="color: black;">公司</span>的各行各业,都可能会被这项技术全面改造。它的三大最<span style="color: black;">重点</span>的特点是:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">去中心化交易</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区块链的本质是一种分散在所有用户电脑上(即<span style="color: black;">所说</span>的 “分布式”)的计算机账本,<span style="color: black;">每一个</span>分散的账本会记录区块链上进行的所有交易活动的信息。<span style="color: black;">因此</span>,它不需要一个集中的<span style="color: black;">公司</span>、网站、<span style="color: black;">机构</span>来管理这些信息。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">法国大作家大仲马在 1884 这年出版的小说《三个火枪手》中有这么一句著名的话,“人人为我,我为人人”(All for one, one for all)。用这句话来形容区块链的去中心化特征非常贴切。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">信息不可篡改,一旦写入<span style="color: black;">没法</span>改变</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">做为</span>一个记录交易的账本,人们最不<span style="color: black;">期盼</span>的是它被<span style="color: black;">暴徒</span>恶意篡改。任何一个用户,都<span style="color: black;">能够</span><span style="color: black;">经过</span>交易编号,<span style="color: black;">拜访</span>区块链上<span style="color: black;">出现</span>的所有交易记录和注释。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因为</span>中本聪巧妙的算法设计,<span style="color: black;">协同</span><span style="color: black;">秘码</span>限制和共识机制,<span style="color: black;">倘若</span>要修改区块链中的某一个数据,就必须更改其后<span style="color: black;">出现</span>的所有数据记录,计算量无可想象,非常庞大,几乎不可能实现。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">实质</span>上,比特币诞生到<span style="color: black;">此刻</span><span style="color: black;">已然</span>接近 10 年,<span style="color: black;">每日</span>都<span style="color: black;">有没有</span>数的黑客绞尽脑汁攻击这个系统,<span style="color: black;">然则</span>从来<span style="color: black;">无</span><span style="color: black;">出现</span>过<span style="color: black;">一块</span>交易记录被篡改的事件,这足以证明了它的安全性。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">完全匿名</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在互联网诞生初期,有句话说:“在互联网上,没人知道你是一只狗。” 这强调的是在互联网上的匿名<span style="color: black;">拜访</span>性。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在区块链世界里,所有的账户(<span style="color: black;">或</span>说 “钱包”)都是<span style="color: black;">经过</span>一个<span style="color: black;">秘码</span>来<span style="color: black;">拜访</span>。<span style="color: black;">倘若</span>你失去了<span style="color: black;">秘码</span>,<span style="color: black;">亦</span>就失去了账户里面的所有货币。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在现实生活中,<span style="color: black;">倘若</span>你忘记了<span style="color: black;">秘码</span><span style="color: black;">或</span>丢失了银行卡,你<span style="color: black;">能够</span>去银行柜台申请补办,手续很简单。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">然则</span>在 “去中心化” 的区块链世界里,<span style="color: black;">无</span>这样的 “银行柜台”,谁<span style="color: black;">亦</span>不<span style="color: black;">晓得</span>你是谁,你<span style="color: black;">亦</span><span style="color: black;">没法</span>向别人证明你是某个钱包的主人。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当然,与大部分技术<span style="color: black;">同样</span>,区块链<span style="color: black;">亦</span>是一个 “双刃剑”,有它的不足之处:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">过度消耗能源</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">想要生成一个新的区块,必须要<span style="color: black;">海量</span>服务器资源进行<span style="color: black;">海量</span>无谓的尝试性计算进行 “挖矿”,严重耗费电能(后文将<span style="color: black;">仔细</span>介绍 “挖矿” 的过程)。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">信息的网络延迟</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以比特币为例,任何一笔交易数据都需要同步到其他所有节点,同步过程中难免会受到网络传输延迟的影响,带来较长的耗时。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区块链应用的<span style="color: black;">实质</span><span style="color: black;">研发</span>,<span style="color: black;">能够</span>分为以下几步。下面<span style="color: black;">咱们</span>将针对<span style="color: black;">每一个</span><span style="color: black;">过程</span><span style="color: black;">仔细</span>介绍。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">除了<span style="color: black;">重点</span>介绍环境配置的第二节和介绍交易<span style="color: black;">过程</span>的第七节以外,其他<span style="color: black;">过程</span>都分为概念介绍、代码实现、深入讲解这三个部分。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">所有代码均已放在我的 GitHub 项目之中,您<span style="color: black;">能够</span>直接下载<span style="color: black;">运用</span>。<span style="color: black;">倘若</span>对<span style="color: black;">详细</span>的代码实现不感兴趣,您<span style="color: black;">亦</span><span style="color: black;">能够</span>直接只关注每节中的概念介绍和深入讲解这两个部分。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>您对<span style="color: black;">详细</span>的算法<span style="color: black;">亦</span>不感兴趣,<span style="color: black;">不碍事</span>,您<span style="color: black;">能够</span>直接跳过这些部分,进入第七节,逐步尝试自己完成 “虚拟交易”。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNekFmkbl8gfrLicUnz2SzGqh5nHlIPG3n4z7Nlrpuyg9j5P2iau5iadBicSpg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">2. 环境配置:<span style="color: black;">创立</span> “交易所”</strong></span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为了便于初学者学习,笔者会尽可能<span style="color: black;">仔细</span>地记录<span style="color: black;">研发</span>环境配置的每一个<span style="color: black;">过程</span>,<span style="color: black;">倘若</span>您<span style="color: black;">已然</span><span style="color: black;">熟练</span> Python 环境和<span style="color: black;">关联</span>模块的<span style="color: black;">运用</span>,您<span style="color: black;">能够</span><span style="color: black;">选取</span>跳过这一部分。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1. <span style="color: black;">首要</span>,您需要一台安装了 Win7 <span style="color: black;">或</span> Win10 系统的电脑,<span style="color: black;">能够</span>是台式机<span style="color: black;">或</span>是笔记本电脑。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当然,不<span style="color: black;">必定</span>非要用 Windows 系统来<span style="color: black;">研发</span>区块链应用,Linux <span style="color: black;">或</span> Mac 系统<span style="color: black;">亦</span>是很好的<span style="color: black;">选取</span>,<span style="color: black;">这儿</span><span style="color: black;">咱们</span>为了照顾大部分读者,只介绍 Windows 系统下的操作。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2. <span style="color: black;">拜访</span> www.python.org,下载并安装 Python 3.6.5 <span style="color: black;">或</span>将来的更高版本。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNekwguQ6TwUMhuvIa2Wic9GtIcJsEZRYfiaXypn3qSibvM6FP0eph5mplawQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下载Python</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3. 安装完毕 Python 之后,从 Windows 的<span style="color: black;">起始</span>菜单输入 “cmd”:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNekwzMHO9oIBPb9ImtDEe1LmCPqespunHtXhCqV4xib5HLIM74GY1o8ibIw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在命令行窗口输入以下指令,安装 Flask 和 requests 库。Flask 库将用于在本地构建一个 Web 服务器,把 Python 的功能映射到<span style="color: black;">必定</span>的端口,便于<span style="color: black;">咱们</span>稍后<span style="color: black;">经过</span>针对<span style="color: black;">区别</span>的<span style="color: black;">位置</span>终端(endpoint)发送 HTTP 请求,执行各项交易,从而<span style="color: black;">形成</span><span style="color: black;">咱们</span>自己的 “虚拟交易所” ;requests 库则用于自动发送 HTTP 请求到服务器。</span></p>c:\>easy_install pip
c:\>pip install Flask==0.12.2 requests==2.18.4<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. <span style="color: black;">拜访</span> https://www.getpostman.com/apps, 下载并安装 Postman,它<span style="color: black;">能够</span>把<span style="color: black;">咱们</span>定制的交易内容,发送到<span style="color: black;">咱们</span>的 “虚拟交易所”。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNek4Pv8AreDmVCpskrwCZsv4dvr2A3sup3m9Tch7sGWG62rQkicJaoJYAQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Postman的<span style="color: black;">起始</span>界面</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5. <span style="color: black;">此刻</span>,打开电脑在带的写字板编辑器,<span style="color: black;">保留</span><span style="color: black;">这般</span>一个文件到 D:\hello.py:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">from</span> flask <span style="color: black;">import</span> Flask
app = Flask(__name__)<span style="color: black;">@app.route(/)<span style="color: black;">def</span> <span style="color: black;">hello_world</span><span style="color: black;">()</span>:</span>
<span style="color: black;">return</span> <span style="color: black;">您好,欢迎来到我的虚拟货币交易所!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span><span style="color: black;">实质</span>上用的 Flask 模块的路由功能,<span style="color: black;">详细</span>语法信息请参阅 Flask 介绍页面</span><span style="color: black;">(http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application)</span><span style="color: black;">。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6. 回到命令行<span style="color: black;">掌控</span>界面,输入下列指令:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">c:\>set FLASK_APP=D:\hello.py
c:\>python -m flask run</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">你<span style="color: black;">能够</span>看到屏幕<span style="color: black;">表示</span> “Running on https://127.0.0.1:5000/”。 <span style="color: black;">此时</span>,打开你的浏览器,<span style="color: black;">拜访</span> https://127.0.0.1:5000/, 你<span style="color: black;">能够</span>看到<span style="color: black;">这般</span>的<span style="color: black;">表示</span>结果:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNek6rd6fCiaydc8KIObXMarB2JLibKqibVK7UTPCa4O5X41cM0oRt4DblXwg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">交易所<span style="color: black;">起始</span>运行</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这就<span style="color: black;">寓意</span>着,<span style="color: black;">咱们</span>的运行环境<span style="color: black;">已然</span>搭建成功,您的 “虚拟交易所” <span style="color: black;">已然</span>准备好接受交易了!</span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">3. 构建区块链:把 “区块” ,“链” 到<span style="color: black;">一块</span></strong></span></h3><strong style="color: blue;"><span style="color: black;">3.1 概念介绍</span></strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">“区块链”(Blockchina)<span style="color: black;">能够</span>理解为把一个个符合特定格式的区块(Block),<span style="color: black;">根据</span><span style="color: black;">必定</span>的<span style="color: black;">办法</span> “链”(chain)到<span style="color: black;">一块</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNek9P6rP3ItPib0hiaXNg0glJicjbbzEBBzVrgDZkZ28wQnVmA0znnNGIxaw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>先预习几个需要<span style="color: black;">触及</span>的概念:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">“类” (class):类是面向对象程序设计中的概念,是面向对象编程的<span style="color: black;">基本</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">类是对现实生活中一类<span style="color: black;">拥有</span><span style="color: black;">一起</span>特征的事物的抽象,譬如区块链这个概念,<span style="color: black;">便是</span>一类<span style="color: black;">拥有</span><span style="color: black;">一起</span>特征的事物,<span style="color: black;">咱们</span><span style="color: black;">能够</span>用一个类来<span style="color: black;">表率</span>它。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">类<span style="color: black;">能够</span>描述一个对象(在本文中即某个区块链)能够做什么,以及做的<span style="color: black;">办法</span>(method)。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">哈希值(Hash):<span style="color: black;">所说</span> “哈希值” <span style="color: black;">便是</span>计算机<span style="color: black;">能够</span>对任意内容,计算出一个长度相同的特征值。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区块链的哈希值长度是 256 位,这<span style="color: black;">亦</span><span style="color: black;">便是</span>说,不管原始内容是什么,最后都会计算出一个 256 位的二进制数字。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">况且</span><span style="color: black;">能够</span><span style="color: black;">保准</span>,只要原始内容<span style="color: black;">区别</span>,对应的哈希<span style="color: black;">必定</span>是<span style="color: black;">区别</span>的。举例<span style="color: black;">来讲</span>,字符串 123 的哈希值是 a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0(十六进制),转成二进制<span style="color: black;">便是</span> 256 位,<span style="color: black;">况且</span><span style="color: black;">仅有</span> 123 能得到这个哈希。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(理论上,其他字符串<span style="color: black;">亦</span>有可能得到这个哈希,<span style="color: black;">然则</span>概率极低,<span style="color: black;">能够</span>近似认为不可能<span style="color: black;">出现</span>。) 由此可以得到两个重要的结论:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">结论 1:<span style="color: black;">每一个</span>区块的哈希都是不<span style="color: black;">同样</span>的,<span style="color: black;">能够</span><span style="color: black;">经过</span>哈希标识一个区块。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">结论 2:<span style="color: black;">倘若</span>区块的内容变了,它的哈希<span style="color: black;">必定</span>会改变。</span></p><span style="color: black;"><strong style="color: blue;"><span style="color: black;">3.2 代码实现</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>要用一个构造函数来创建一个区块链类 ,其中<span style="color: black;">包含</span>两个表:一个用于存储区块,一个用于记录交易。<span style="color: black;">另一</span>,<span style="color: black;">咱们</span>还要定义一个<span style="color: black;">办法</span>,用于生成区块的哈希值。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">另外</span>,<span style="color: black;">咱们</span>还为这个类定义了一个属性 last_block, <span style="color: black;">这般</span><span style="color: black;">能够</span><span style="color: black;">经过</span>调用该类<span style="color: black;">得到</span>区块链中最后一个区块的信息。下面是这个类的初步结构:</span></p><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Blockchain</span><span style="color: black;">(object)</span>:</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self)</span>:</span>self.chain = []
self.current_transactions = []<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">new_block</span><span style="color: black;">(self)</span>:</span>
<span style="color: black;"># 创建一个新的区块</span>
<span style="color: black;">pass</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">new_transaction</span><span style="color: black;">(self)</span>:</span>
<span style="color: black;"># 把新的交易添加到交易列表中</span>
<span style="color: black;">pass</span>
<span style="color: black;">@staticmethod</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">hash</span><span style="color: black;">(block)</span>:</span>
<span style="color: black;"># 生成一个区块的哈希值</span>
<span style="color: black;">pass</span>
<span style="color: black;">@property</span>
<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">last_block</span><span style="color: black;">(self)</span>:</span>
<span style="color: black;"># 返回链中的最后一个区块</span>
<span style="color: black;">pass</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这个类负责管理<span style="color: black;">全部</span>区块链,<span style="color: black;">包含</span>存储交易信息,把新的区块添加到<span style="color: black;">全部</span>区块链之中。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面,<span style="color: black;">咱们</span>再来<span style="color: black;">瞧瞧</span>一个典型的区块是什么样的,以及它们是怎么<span style="color: black;">形成</span>一条区块链的。<span style="color: black;">根据</span>中本聪的原始定义,下面是一个典型的区块:</span></p>block = { <span style="color: black;">index</span>: len(self.chain) +<span style="color: black;">1</span>, <span style="color: black;"># 区块编号,即区块链之前长度+1</span>
<span style="color: black;">timestamp</span>: time(), <span style="color: black;"># 区块生成时间的UNIX时间戳</span>
<span style="color: black;">previous_hash</span>: previous_hash <span style="color: black;">or</span> self.hash(self.chain[-<span style="color: black;">1</span>]), <span style="color: black;">#上一个区块的哈希值,<span style="color: black;">另一</span>需要<span style="color: black;">思虑</span><span style="color: black;">第1</span>个区块的<span style="color: black;">状况</span></span>
<span style="color: black;">proof</span>:proof, <span style="color: black;"># 工作量证明(PoW),稍后会<span style="color: black;">仔细</span>介绍</span>
<span style="color: black;">transactions</span>: [
{ <span style="color: black;">sender</span>: sender_hash, <span style="color: black;">#付款人钱包<span style="color: black;">位置</span></span>
<span style="color: black;">recipient</span>: recipient_hash, <span style="color: black;">#收款人钱包<span style="color: black;">位置</span></span>
<span style="color: black;">amount</span>: tranactions_ammount,<span style="color: black;">#交易金额</span>
}
], <span style="color: black;"># 区块中的交易信息</span>
}<span style="color: black;"><strong style="color: blue;"><span style="color: black;">3.3 深入讲解</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由上面的定义<span style="color: black;">能够</span>看出,一个区块<span style="color: black;">包含</span>下面几项内容:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区块索引 / 编号</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区块产生时间</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上一个区块的哈希值</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">工作量证明(PoW)</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">交易信息</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/0vU1ia3htaaP4oPV35qK5GfW5OcYSLNekT7vcficIytOkyKDg5F3Cz7yTxgrBmeib4cIcCTibrddRib91u9KNTkySKw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这揭示了区块链的核心理念:<span style="color: black;">每一个</span>区块中<span style="color: black;">包括</span>了上一个区块的哈希值<span style="color: black;">或</span>说信息。这一点对区块链有重大<span style="color: black;">道理</span>:<span style="color: black;">倘若</span>有人修改了一个区块,该区块的哈希值就变了。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为了让后面的区块还能连到它,就必须依次修改后面所有的区块,否则被改掉的区块就脱离区块链了。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因为</span>哈希值的计算非常耗时(后面将解释<span style="color: black;">原由</span>),<span style="color: black;">短期</span>内修改多个区块几乎不可能<span style="color: black;">出现</span>,除非有人真的<span style="color: black;">把握</span>了<span style="color: black;">全世界</span>网络中 51% 以上的计算能力。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">正是<span style="color: black;">经过</span>这种联动机制,区块链<span style="color: black;">保准</span>了<span style="color: black;">自己</span>的<span style="color: black;">靠谱</span>性,数据一旦写入,就<span style="color: black;">没法</span>被篡改。这就像历史<span style="color: black;">同样</span>,<span style="color: black;">出现</span>了<span style="color: black;">便是</span><span style="color: black;">出现</span>了,从此再<span style="color: black;">没法</span>改变。<span style="color: black;">每一个</span>区块都连着上一个区块,这<span style="color: black;">亦</span>是 “区块链” 这个名字的由来。</span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">4. 记录交易: 怎么给区块添加交易记录?</strong></span></h3><span style="color: black;"><strong style="color: blue;"><span style="color: black;">4.1 概念介绍</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">创立</span>起区块链之后,接下来<span style="color: black;">咱们</span><span style="color: black;">瞧瞧</span>怎么为其添加交易信息。<span style="color: black;">所说</span> “交易”(Transaction)的过程,<span style="color: black;">实质</span>上<span style="color: black;">便是</span>给区块链加一笔数据更新的记录,其中<span style="color: black;">包括</span>了付款人的钱包<span style="color: black;">位置</span>、收款人的钱包<span style="color: black;">位置</span>、交易金额。<span style="color: black;">倘若</span>把区块链<span style="color: black;">做为</span>一个状态机,则每次交易<span style="color: black;">便是</span>试图改变一次状态,而每次生成的区块,<span style="color: black;">便是</span>参与者<span style="color: black;">针对</span>区块中交易<span style="color: black;">引起</span>状态改变的结果进行确认。</span></p><span style="color: black;"><strong style="color: blue;"><span style="color: black;">4.2 代码实现</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">详细</span>的添加过程,是<span style="color: black;">经过</span> new_transaction 这个<span style="color: black;">办法</span>来实现的。下面是对这个<span style="color: black;">办法</span>的定义实例。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">......</span></strong></span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">5. “挖矿” 解密</strong></span></h3><span style="color: black;"><strong style="color: blue;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6. 在网络中传播区块链:共识算法</p>
</strong></span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">7. 执行交易</strong></span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">后记</strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本 Chat 中所有的代码都<span style="color: black;">安置</span>在我的 GitHub 项目 “Frank—Blockchain” 上,请自由索取<span style="color: black;">或</span>提交修改。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">或</span>您<span style="color: black;">亦</span><span style="color: black;">能够</span>分叉(fork)一个自己的分支,创建您自己的区块链项目。开句玩笑话,这就像是在麻将的基本原则<span style="color: black;">基本</span>上,开辟出<span style="color: black;">各样</span>分支:四川麻将、上海麻将等等。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">期盼</span>本 Chat 能<span style="color: black;">帮忙</span>您<span style="color: black;">认识</span>并实践区块链的基本概念和实现<span style="color: black;">办法</span>。请<span style="color: black;">重视</span>,<span style="color: black;">这儿</span><span style="color: black;">说到</span>的只是区块链最核心的部分,还有<span style="color: black;">非常多</span><span style="color: black;">非常多</span>其他的技术细节和算法,譬如非对<span style="color: black;">叫作</span>加密、点对点网络等,我将会在以后的其他 Chat 中<span style="color: black;">仔细</span>说明。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>有任何其他的问题,欢迎到我的读者圈中继续提问。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">扫描下方二维码</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">【阅读完整原文】</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">并在读者圈与作者交流</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
期待你更多的精彩评论,一起交流学习。 楼主的文章深得我心,表示由衷的感谢! 外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢! 你的见解独到,让我受益匪浅,期待更多交流。 期待楼主的下一次分享!” 你的话语真是温暖如春,让我心生感激。 论坛的成果是显著的,但我们不能因为成绩而沾沾自喜。
页:
[1]