如果你还没有听说过 3 点钟区块链群,说明你还不是链圈的人;如果你还没有加入 3 点钟区块链群,说明你还不是链圈的大佬;如果你还没有被 3 点钟区块链群刷屏,说明你还体会不到什么是“币圈一天,人间一年”。
If you have not heard of the 3 o'clock chain & #xff0c; if you are not a chain & #xff1b; if you have not joined the 3 o'clock chain & #xff0c; if you are not a chain & #xff1b; if you have not been brushed by the 3 o'clock chain & #xff0c; if you don't have a currency ring & #xff0c; one year in person.
“三点钟区块链”无疑成为了大家春节期间焦虑的根源,而“区块链”注定是 2018 年被持续讨论、关注的行业性热点话题。
The "3 o'clock block chain" has undoubtedly become a source of anxiety during the spring festival #xff0c; the "block chain" is destined to be a topical subject of ongoing discussion in 2018.
3 月 1 日,朱啸虎对正翻涌不断的区块链热潮再次开炮,在朋友圈一张画满区块链应用的图上,朱啸虎质疑:所有这些应用加在一起,有多少日活用户?“2000 年的互联网泡沫至少还有 eyeball,今天的区块链除了炒币外还有什么”?
1 March xff0c; xff0c; ff0c ; ; ;
在此之前,朱啸虎在朋友圈转发了讽刺区块链投资热的文章《来,喝了这碗区块链的毒鸡汤!》,并声明:“不要拉我进任何 3 点钟群,有些风口宁愿错过,有些钱宁愿不赚,大家晚节保重。”朱啸虎还表示,说 ICO 是庞氏骗局是在侮辱庞氏骗局。
Prior to that, xff0c; xff0c; xff0c; xff01; xff0c; and xff1a; xff1a; “Don't drag me into any 3-point ffff0c; some of the wind would have missed xff0c; some of the money would have preferred not to earn xff0c; everyone take care in the evening.” xff0c; say ICO is Ponzi's scam is an insult to Ponzi.
作为程序员的你,再不懂这个技术,2018 可能会被淘汰!下面和小编一起从十个幽默段子入门区块链吧!
You're a programmer xff0c; you're no longer aware of this technology xff0c; 2018 is likely to be eliminated xff01; below, along with the editor-in-chief, enter the chain of blocks from ten humor segments xff01;
笑喷!区块链十个段子集锦
1、假如你是一位女性,你男朋友每次跟你说一句肉麻的话或者承诺给你买东西,你都立刻录下来并且发给你的和他的所有闺蜜、同学、同事,还有各种群和朋友圈,让他再也无法抵赖,这叫区块链。
If you are a woman #xff0c; every time your boyfriend tells you something or promises you something #xff0c; you record it immediately and send it to you and to all of his friends, classmates, colleagues #xff0c; there are groups and friends #xff0c; he can no longer deny #xff0c; this is called the chain of blocks.
2、麻将是中国传统的区块链项目:四个矿工一组,先碰撞出 13 个数字正确哈希值的矿工可以获得记账权并得到奖励。不可篡改。因为说服其他三个人需要消耗太多算力和体力。
2. mah will be the traditional Chinese block chain project xff1a; a group of four miners xff0c; miners who first collide 13 numerically correct Hashi values will be given the right to bookkeeping and reward. Unalterable. Because convincing the other three requires too much numeracy and strength.
3、玩夜店的小姐姐和玩虚拟币的小哥哥们有几处相似:
Three, the clubs' little sisters and the virtual coins' little brothers have a few similarities #xff1a.
- 都是自认聪明的优秀群体
- 不给自己赚钱的都是傻 X 屌丝
- 都认识很多大佬
- 都明白很多道理
- 都是在等自己涨价或者自己的虚拟币涨价被别人接盘
4、区块链是正经技术,各种币正不正经就不知道了。
4. The block chain is a real technology & #xff0c; various currencies are not known if they are serious.
5、吴三桂在山海关冲冠一怒,本质是为了争夺睡陈圆圆的权力;大佬们在区块链路上的互怼,本质是为了争夺割韭菜的权力。
U San-Gwee is angry at the mountain customs; in essence, he is fighting for power to sleep round & #xff1b; in fact, he is fighting for power to cut vegetables #xff0c in the chain of blocks.
6、新学期刚开始,儿子问老爸:「父亲工作一栏怎么填?是写币民吗?」老爸犹豫了一下说:「就写多家上市公司股东。」
6. The new semester is just beginning xff0c; the son asks Dad xff1a; "How does the father's work column fill xff1f; writing coins xff1f;" and the father hesitates to say xff1a; and "let's write about the shareholders in many listed companies."
7、最近数字货币很火,很多山寨币都是几倍几十倍的增长。很多炒币的开始飘飘然,叫嚣什么「一币一嫩模」。有朋友就问我要不要跟?我观点很简单:淘金热,一窝蜂淘金,风险很大。所以,让他们叫「一币一嫩模」去吧,不要跟风盲目炒币,我们应该赚他们的钱——去当嫩模!当嫩模!嫩模!
7 & #xff0c & recently digital currency & #xff0c; many bouncing coins are growing several dozen times more. Many of them start to float & #xff0c; yell "one dollar". Friends ask me if I want to talk to #xff1f; my point is simple #xff1a; gold-grabbing #xff0c; gold-grabbing #xff0c; risk is high. So #xff0c; let them call them "one dollar, one dollar" xff0c; don't follow blindly #xff0c; we should make their money — go to #xff01; #ff01; #ff01; xff01; ff01;
8、我昨天遇见一币友,问他:「近来币市暴降,睡觉质量怎么样?」
8. I met a friend of a coin yesterday #xff0c; asked him #xff1a; "Recently the currency fell #xff0c; how good the sleeping quality #xff1f;"
他说:「还行,像婴儿般睡觉!」
He said #xff1a; #xff0c; sleep like a baby #xff01;
我说:「羡慕了。」
I said #xff1a; "I envy."
他说:「是睡一个小时,醒了,然后哭一个小时,接着,再睡一个小时,起来再哭一个小时。」
He said xff1a; "It's one hour of sleep xff0c; wake up xff0c; then cry for one hour xff0c; then xff0c; sleep for another hour xff0c; wake up for another hour."
9、老同志语重心长地对 80、90 后说:「别玩那些比特币,那些虚拟的玩意,做点实事在北京买个房、娶个媳妇,多好!」90 后回答说:「你们都把几千块钱成本的房子搞到10万一平米了。我们不另寻出路,搞一串串数字 10 万一个卖给你们,我们拿什么买得起房子啊?」
"Don't play with those bitcoin and #xff0c; those virtual things #xff0c; do something real in Beijing to buy a house and marry a daughter-in-law #xff0c; how good #xff01;" 90 answers #xff1a; "You've got 10,000 square metres of houses for thousands of dollars. We're not looking for another way out #xff0c; do a bunch of numbers #xff0c; what can we get to buy a house #xff1f;"
10、首先感谢公司拿出价值 100 万的比特币作为给员工的奖励,其次我觉得自己很幸运能拿到这 95 万的奖励,然后我觉得我还是要好好规划一下这 86 万的用处,毕竟 70 万也不是一笔小钱,我打算拿出 20 万给父母,剩下的 36 万暂时还没想好怎么用,总之,感谢公司价值 30 万的比特币的奖励,谢谢,祝大家和我一样都能得到这 15 万的奖励。
10, first of all, thank the company for the $1 million bitcoin as a reward for its employees xff0c; second, I think I'm lucky I got the 950,000 reward xff0c; then I think I'm still going to have to plan for the 860,000 worth xff0c; after all, 700,000 is not a small sum xff0c; I'm going to give 200,000 to my parents xff0c; the remaining 360,000 are not yet ready to use xff0c; overall xff0c; thanks to the 300,000 dollars of the company's reward ff0c; thank you #xff0c; and wish everyone, like me, the 150,000.
图解:大白话了解到底区块链是个啥?
Diagrams #xff1a; big whites know exactly what a block chain is #xff1f;
what is 区块链
what is a block chain
“区块链仅仅是一门技术而已”,“比特币”仅仅是区块链技术的一种应用而已, 就好比,一个人会厨艺的技术,但是应用起他的厨艺可以做出“宫保鸡丁”、”鱼香肉丝”等各式的菜肴。
The “block chain” & #xff0c; “bitcoin” is only an application of block chain technology & #xff0c; it's like #xff0c; a person's cooking technique & #xff0c; but his cooking can be used to produce all kinds of dishes, such as “miya-paw chickens” and “fish flavours”.
那么,到底“区块链”是个啥?我们这里借助网上一个比较流行的段子,将它用图形的形式展示给大家。
so #xff0c; what the hell is a "block chain" xff1f; , we use a more popular segment #xff0c on the Internet to show it to you in graphic form.
我们可以将“比特币”抽象成“某荣的照片”如上图所示,如果网上很多用户想要得到某荣的照片,需要到一个固定的网站去搜索。
We can use the word “bitcoin” in abstraction as “photos of honour”, as shown in the figure above; if many users on the Internet want to get a photo of honour xff0c; they need to search a fixed website.
当然你也没其他地方可去嘛,那么好了,这个某榴网站天天给你弹出广告啊,小窗口啊,你都得忍着,没办法,因为就这一个地方可以获取嘛。
Of course you don't have anywhere else to go; well #xff0c; #xff0c; #xff0c; small window #xff0c; you have to put up with xff0c; you can't xff0c; because this is the only place that can be accessed.
再者,这个网站突然被警察叔叔封杀了咋办?或者断电?断网?管他啥的,反正就是服务器崩溃了,那么悲剧的“某荣”粉丝们心爱的 2100 张照片将全部失去提供资源的场所。
Moreover, #xff0c; the website was suddenly blocked by the police uncle; xff1f; xff1f; xff1f; whatever xff0c; anyway, the server crashed xff0c; then the tragic 2100 photos of the “Honor” fans lost all the places where the resources were made available.
这就是网上流传的新词“中心化”喽,他的弊端就是资源集中一起,抗风险容错性很弱。资源容易丢失。
That's the new word "centre" that's going on the Internet; his vice is pooling resources together & #xff0c; resistance to risk is weak. Resources are easily lost.
那么,怎么解决这个问题呢?我们试想,能不能让每个“某荣”粉丝,都拥有这 2100 张照片呢?比如下图:
#xff0c; how do we solve this? #xff1f; let's try #xff0c; let's see if every #xff0c fan has this 2100 picture. #xff1f; compared to #xff1a below;
这样的话,貌似就不用在依赖那个“某榴”网站了呢。即使某个粉丝突然电脑崩溃,他随便找一个其他粉丝来获得这 2100 张照片,不愁了,不愁了。
In that case, xff0c; there seems to be no need to rely on the "a grenade" web site. Even if a fan suddenly crashes a computer xff0c; he can find some other fan to get this 2100 photo xff0c; xff0c; xff0c;
后来,一个叫“某本聪”的一个虚拟人物提供了,一个拥有协议的某荣照片共享文件夹,用户可以从中获取照片,但是必须遵循协议。
Later on xff0c; a virtual figure called “Abstinence”, a privileged photo sharing folder with a protocol xff0c; the user can get a photo xff0c; however, the protocol has to be followed.
这样,每个粉丝都可以从这个文件夹中获取那 2100 张某荣的照片,但是全部获取的粉丝必须要遵循一个协议,当然是人家某本聪定义的协议啦。
This way xff0c; every fan can get from this folder the 2,100 photos of a winner xff0c; but all the fans must follow a protocol xff0c; of course it's an agreement defined by someone's own brain.
“不得复制,修改,共享文件中的任意照片,粉丝们在共享文件夹中的任何行为都会被记录,并且是按照时间去记录!”
“No reproduction of xff0c; modification of xff0c; any photograph of the shared file xff0c; any behavior of fans in the shared folder will be recorded xff0c; and xff01 will be recorded according to time;”
粉丝们这么喜爱某荣,而且不需要去某榴获取,当然就纷纷踊跃加入了~
#xff0c is so much loved by fans; there's no need to go to #xff0c; of course, there's a lot of them.
忽然,有一天,调皮的小 One 想要违背规则,在 2018 年 1 月 15 日中午 12 点删除编号为 1-100 的某荣照片。
Suddenly #xff0c; one day #xff0c; the naughty little One wants to break the rules & #xff0c; at noon on January 15th, 2018, delete a photo of honor number 1-100.
根据协议,这个行为会被记录,并且会广播给其他粉丝。
According to the protocol xff0c; the act will be recorded xff0c; and it will be broadcast to other fans.
照片到底被删除了么?当然不是,因为小露手里也有照片嘛,她收到广播后可以立刻恢复共享文件夹中已经删除的照片,小 One 永远别想对“共享文件夹”搞修改破坏,且所有行为都同步记录在其他用户的电脑里。
xff1f; of course not xff0c; because she also has photos in her hand xff0c; she can immediately resume sharing the deleted photos in her folder xff0c; Little One never tries to modify the "shared folder" xff0c; and all behaviors are recorded simultaneously on the computers of other users.
这就是区块链,数据分散存储,去中心化。按时间戳广播记录所有行为,无法修改、破坏数据源或造假。
This is the block chain & #xff0c; data decentralized storage & #xff0c; decentralisation.
除非同一时刻炸掉 100 万个用户的电脑,或互联网消失,或世界毁灭....否则数据将永远存在~~
Unless the computers of 1 million users are blown up at the same time, or the Internet disappears, or the world destroys... data will always exist.
如何增加区块链保护的资源?“某本聪”又来了。他说,你们是可以在文件中添加某荣照片的,但是呢,你们各位必须达到某种“共识”?啥是“共识”,就是我们都承认的规则喽。
那到底是个啥共识呢?啊,小 One 和小露立刻了解了某本聪的意思,在每年规定的时间内,尽快拍出 100 张某荣的照片,这样就可以添加到“某荣共享文件夹”了,我们的资源就扩充了。
What exactly is the consensus? #xff1f; #xff0c; Little One and Lulu immediately learned the meaning of a deaf & #xff0c; xff0c within the prescribed time frame of each year; 100 photos of a privileged person #xff0c as soon as possible; so that we can add to the #xff0c folder of a privileged shared folder; our resources are expanded.
但是,好景不长。某本聪发现大量的拍照,冲击前 100,那很快就能达成了,拍照就没有难度了。
But #xff0c; not for long. One of them found a lot of photographs #xff0c; before the shock 100xff0c; it was quick to reach #xff0c; there was no difficulty in taking pictures.
而且照片质量还差,好像谁都能轻易的添加照片资源,这样就保证不了某荣的照片质量了。于是,某本聪再次降临,增加拍照的共识难度。?
And the quality of the photos is poor xff0c; it's as if anyone can easily add a photo resource xff0c; that doesn't guarantee the quality of a proud photo. So xff0c; a deaf person comes back xff0c; making it harder to get a picture.
小 One 和小露作为忠实的粉丝,怎能放弃,他们买了高端相机,让某荣摆出各种姿势,花费大量的时间和汗水完成了高质量的照片,当然自己也非常的辛苦。
Little One and Lulu, as faithful fans & #xff0c; how can they give up #xff0c; they buy high-end cameras & #xff0c; allow a certain honour to pose various positions & #xff0c; spend a great deal of time and sweat on high-quality photographs & #xff0c; and of course, they are very hard themselves.
这样高质量的照片就可以添加到“某荣的文件夹”中了。
In this way, high-quality photos can be added to the "Holder's Folder".
何为 ICO?
what ICO #xff1f;
小 One 想:每张照片都是不可造假破坏的,所以具有唯一性,还有单独编号,给每一张照片估价,它不就值钱了吗?就像现实世界中无法复制的名画一样!
Little One wants #xff1a; each photo is unprovoked xff0c; so it is unique xff0c; there is a separate number xff0c; each photo is valued xff0c; it's not worth it xff1f; it's like a painting that cannot be copied in the real world xff01;
小 One 就把之前的某荣照片,构造出来相应的“某荣币”,当然这个行为就向我们的政府通过国库的黄金数额发行等价的人民币类似啦。
Little One has created a picture of a former glory & #xff0c; the corresponding #xff0c & #xf0c; of course, this is the same thing as issuing the equivalent of the renminbi to our government through the amount of gold in the treasury.
估值呢,当然小 One 说的算啦,人家是照片的拥有者嘛。
What about the valuation? #xff0c; of course, Little One says it's up to you #xff0c; he's the owner of the photo.
为了证实某荣币 5W 块一个,小 One 首先购买了 2100 个中的 1100 个,剩下的发行给吃瓜群众们啦,我们已经用 5W 买一个,说明他已经值这个价钱啦,剩下 1000 个大家一起买吧。
In order to prove the value of a five-W block xff0c; Little One first bought 1,100 xff0c out of 2100; the rest was distributed to the eating crowds xff0c; we used the five-W to buy a xff0c; that he was worth the price xff0c; and that there were 1,000 more to buy.
这样的话如果 2100 个全部认购出去,2100 个某荣币可以就估值 1.05 亿块哦~这种通过数字货币的发行而得到融资的过程就是 ICO 啦。
So if 2,100 full subscriptions & #xff0c; 2,100 medals can be valued at 105 million.
根据这个图的含义,如果小 One 和小露是一个可信任的机构或者公众人物,还是可以相信他们的,当然啦也会有很多不法分子恶意发行货币来套现的。
According to the meaning of this map xff0c; if Little One and Ziaoyu are a trusted institution or a public figure xff0c; or if they can be trusted xff0c; of course, there will also be a large number of outlaws who have issued money in bad faith.
这也是我们国家为什么禁止 ICO 发行的原因啦,因为目前没有一个完善的 ICO 监管条例能够保证发行货币的机构的可信任性和合法的监管他们,所以吃瓜群众们要自己承担风险找到一个可信的机构。
That is why our country has banned ICO distribution xff0c; because there is currently no perfect ICO regulation that can guarantee the trustworthiness and legitimacy of the money issuing agencies xff0c; so the eating crowds have to take the risk to themselves to find a credible institution.
这样 2100 个某荣币全部认购成功,基金成立,这就是 ICO。当然吃瓜群众也可以继续拍照创造某荣币,就是有点难罢了。
So 2,100 medals were fully subscribed to xff0c; the fund was founded xff0c; this is ICO. Of course, the eating people can continue to take pictures to create xff0c; it's just a bit difficult.
现在各位了解什么是区块链和 ICO 了吧~下面手把手教你如何用 Python 语言创建一个区块链?
Now you know what a block chain is and what an ICO is.
用 Python 从 0 开始创建一个区块链
Create a block chain with Python from 0
对数字货币的崛起感到新奇的我们,并且想知道其背后的技术——区块链是怎样实现的。本文通过 Python 构建一个区块链可以加深对区块链的理解。
We #xff0c, new to the rise of digital money; and want to know how the technology behind it – the block chain – works. By building a block chain through Python, this paper can deepen understanding of the block chain.
准备工作
Preparations
本文要求读者对 Python 有基本的理解,能读写基本的 Python,并且需要对 HTTP 请求有基本的了解。
This paper asks readers to have a basic understanding of Python & #xff0c; to read and write basic Python & #xff0c; and to have a basic understanding of the HTTP request.
我们知道区块链是由区块的记录构成的不可变、有序的链结构,记录可以是交易、文件或任何你想要的数据,重要的是它们是通过哈希值(hashes)链接起来的。
We know that the blocks chain is an immutable and orderly chain structure of blocks' records xff0c; records can be transactions, documents or any data you want xff0c; what is important is that they are linked by Hashi xff08; hashes) and xff09.
如果你还不是很了解哈希,可以查看这篇文章https://learncryptography.com/hash-functions/what-are-hash-functions。
If you do not know Hashi well #xff0c; you can see the article https://learncryptography.com/hash-functions/what-are-hash-functions.
环境准备:
Environmental Preparednessxff1a;
环境准备,确保已经安装 Python3.6+、pip、Flask、requests。
Environmental readiness & #xff0c; ensure that Python 3.6 & #43; pip, flask, réquests are installed.
安装方法:
- pip?install?Flask==0.12.2?requests==2.18.4?
同时还需要一个 HTTP 客户端,比如 Postman、cURL 或其他客户端。
There is also a need for a HTTP client & #xff0c; e.g. Postman, curL or other client.
参考源代码(原代码在我翻译的时候,无法运行,我 fork 了一份,修复了其中的错误,并添加了翻译,感谢 star)。
Reference source & #xff08; original code when I translated xff0c; could not run xff0c; I fork axff0c; bugs xff0c were repaired; translation xff0c was added; thanks starxff09;
开始创建 Blockchain
Starts to create Blockchain
新建一个文件 blockchain.py,本文所有的代码都写在这一个文件中,可以随时参考源代码。
Creates a new file Blockchain.py, all codes in this document are written in this file & #xff0c; the source code can be consulted at any time.
Blockchain 类
Blockchain class
首先创建一个 Blockchain 类,在构造函数中创建了两个列表,一个用于储存区块链,一个用于储存交易。
First, create a Blockchain class & #xff0c; create two lists & #xff0c in the construction function; one to store block chains & #xff0c; and one to store transactions.
以下是 Blockchain 类的框架:
The following is the framework for the Blockchain class & #xff1a;
- class?Blockchain(object):
- def?__init__(self):
- self.chain?=?[]
- self.current_transactions?=?[]
- def?new_block(self):
- #?Creates?a?new?Block?and?adds?it?to?the?chain
- pass
- def?new_transaction(self):
- #?Adds?a?new?transaction?to?the?list?of?transactions
- pass
- @staticmethod
- def?hash(block):
- #?Hashes?a?Block
- pass
- @property
- def?last_block(self):
- #?Returns?the?last?Block?in?the?chain
- pass?
Blockchain 类用来管理链条,它能存储交易、加入新块等,下面我们来进一步完善这些方法。
The Blockchai class is used to manage chains & #xff0c; it can store transactions, add new blocks, etc. & #xff0c; below we will further refine these methods.
块结构
block structure
每个区块包含属性:索引(index)、Unix 时间戳(timestamp)、交易列表(transactions)、工作量证明(稍后解释)以及前一个区块的 Hash 值。
Each block contains properties & #xff1a; index & #xff08; index & & #xff09; Unix time stamp & #xff08; Timestamp) trading list & #xff08; tradings & #xff09; workload certification & #xff08; later explanation & #xff09; and the Hash of the previous block.
以下是一个区块的结构:
The following is the structure of a block xff1a;
- block?=?{
- 'index':?1,
- 'timestamp':?1506057125.900785,
- 'transactions':?[
- {
- 'sender':?"8527147fe1f5426f9dd545de4b27ee00",
- 'recipient':?"a77f5cdfa2934df3954a5c7c7da5df1f",
- 'amount':?5,
- }
- ],
- 'proof':?324984774000,
- 'previous_hash':?"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
- }?
到这里区块链的概念就清楚了,每个新的区块都包含上一个区块的 Hash,这是关键的一点,它保障了区块链的不可变性。
The concept of the block chain here is clear xff0c; each new block contains the Hashxff0c of the previous block; this is the key point xff0c; it guarantees the non-variability of the block chain.
如果攻击者破坏了前面的某个区块,那么后面所有区块的Hash都会变得不正确。不理解的话,慢慢消化,可参考{% post_link whatbc 区块链技术原理 %}。
If the attacker destroys a block xff0c in front of it; then the Hash in all the back blocks becomes incorrect. If not understood, xff0c; slowly digesting xff0c; reference can be made to post_link whatbc block chain technical principles %}.
加入交易
joins the deal
接下来我们需要添加一个交易,来完善下 new_transaction 方法:
Next we need to add a transaction & #xff0c; to refine the new_transaction method & #xff1a;
- class?Blockchain(object):
- ...
- def?new_transaction(self,?sender,?recipient,?amount):
- """
- 生成新交易信息,信息将加入到下一个待挖的区块中
- :param?sender:?<str>?Address?of?the?Sender
- :param?recipient:?<str>?Address?of?the?Recipient
- :param?amount:?<int>?Amount
- :return:?<int>?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?
方法向列表中添加一个交易记录,并返回该记录将被添加到的区块(下一个待挖掘的区块)的索引,等下在用户提交交易时会有用。
Method to add a transaction log xff0c to the list; and to return the index of the block to which the record will be added (the next block to be excavated) xff0c; etc. will be useful when the user submits the transaction.
创建新块
Creates a new block
当 Blockchain 实例化后,我们需要构造一个创世块(没有前区块的第一个区块),并且给它加上一个工作量证明。每个区块都需要经过工作量证明,俗称挖矿,稍后会继续讲解。
When Blockchain exemplifies xff0c; we need to construct a creation block xff08; there is no first block of the former block xff09; xff0c; and add a workload certificate to it. Each block requires a workload certificate xff0c; commonly known as mining xff0c; it will continue to be explained later.
为了构造创世块,我们还需要完善 new_block(),new_transaction() 和hash() 方法:
#xff0c; we also need to refine new_block(), new_transaction() and ash() methodsxff1a;
- import?hashlib
- import?json
- from?time?import?time
- class?Blockchain(object):
- def?__init__(self):
- self.current_transactions?=?[]
- self.chain?=?[]
- #?Create?the?genesis?block
- self.new_block(previous_hash=1,?proof=100)
- def?new_block(self,?proof,?previous_hash=None):
- """
- 生成新块
- :param?proof:?<int>?The?proof?given?by?the?Proof?of?Work?algorithm
- :param?previous_hash:?(Optional)?<str>?Hash?of?previous?Block
- :return:?<dict>?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,?recipient,?amount):
- """
- 生成新交易信息,信息将加入到下一个待挖的区块中
- :param?sender:?<str>?Address?of?the?Sender
- :param?recipient:?<str>?Address?of?the?Recipient
- :param?amount:?<int>?Amount
- :return:?<int>?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):
- return?self.chain[-1]
- @staticmethod
- def?hash(block):
- """
- 生成块的?SHA-256?hash值
- :param?block:?<dict>?Block
- :return:?<str>
- """
- #?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()?
通过上面的代码和注释可以对区块链有直观的了解,接下来我们看看区块是怎么挖出来的。
The code and the notes above provide a visual understanding of the block chain #xff0c; then we look at how the block was dug up.
理解工作量证明
understands the proof of workload
新的区块依赖工作量证明算法(PoW)来构造。PoW 的目标是找出一个符合特定条件的数字,这个数字很难计算出来,但容易验证。这就是工作量证明的核心思想。
The new blocks are constructed by relying on workload proof algorithms & #xff08; PoW) the goal of PoW is to find a number that meets certain conditions & #xff0c; this number is difficult to calculate & #xff0c; but it is easy to verify. This is the core idea of workload proof.
为了方便理解,举个例子:
For ease of understanding #xff0c; for example #xff1a;
假设一个整数 x 乘以另一个整数 y 的积的 Hash 值必须以 0 结尾,即 hash(x * y) = ac23dc...0。设变量 x = 5,求 y 的值?
Assuming that an integer x multiplied by another integer y must end xff0c; i. e. hash(x*y) & #61; ac23dc...0. Set the variable x & #61; 5xff0c; value yxff1f;
用 Python 实现如下:
xff1a with Python;
- from?hashlib?import?sha256
- x?=?5
- y?=?0#?y未知
- while?sha256(f'{x*y}'.encode()).hexdigest()[-1]?!=?"0":
- y?+=?1
- print(f'The?solution?is?y?=?{y}')?
结果是:y = 21,因为:
Results xff1a; y & #61; 21xff0c; due to xff1a;
- hash(5?*?21)?=?1253e9373e...5e3600155e860?
在比特币中,使用称为 Hashcash 的工作量证明算法,它和上面的问题很类似,矿工们为了争夺创建区块的权利而争相计算结果。
In Bitcoin & #xff0c; using the workload proof algorithm known as Hashcash & #xff0c; it is very similar to the issue above & #xff0c; the miners competed for the right to create blocks.
通常,计算难度与目标字符串需要满足的特定字符的数量成正比,矿工算出结果后,会获得比特币奖励。当然,在网络上非常容易验证这个结果。
Usually xff0c; the number of specific characters to be met by calculating difficulty versus target string xff0c; the miner gets a bitcoin reward when the result is calculated. Of course xff0c; the result is very easy to verify on the network.
实现工作量证明
achieve workload certification
让我们来实现一个相似 PoW 算法,规则是:寻找一个数 p,使得它与前一个区块的 proof 拼接成的字符串的 Hash 值以 4 个零开头。
Let us achieve a similar Pow algorithm & #xff0c; rules & #xff1a; search for a number of p& #xff0c; Hash value that allows it to start with four zeros of the string that binds it to the proof of the previous block.
- import?hashlib
- import?json
- from?time?import?time
- from?uuid?import?uuid4
- class?Blockchain(object):
- ...
- def?proof_of_work(self,?last_proof):
- """
- 简单的工作量证明:
- ?-?查找一个?p'?使得?hash(pp')?以4个0开头
- ?-?p?是上一个块的证明,p'?是当前的证明
- :param?last_proof:?<int>
- :return:?<int>
- """
- proof?=?0
- while?self.valid_proof(last_proof,?proof)?is?False:
- proof?+=?1
- return?proof
- @staticmethod
- def?valid_proof(last_proof,?proof):
- """
- 验证证明:?是否hash(last_proof,?proof)以4个0开头?
- :param?last_proof:?<int>?Previous?Proof
- :param?proof:?<int>?Current?Proof
- :return:?<bool>?True?if?correct,?False?if?not.
- """
- guess?=?f'{last_proof}{proof}'.encode()
- guess_hash?=?hashlib.sha256(guess).hexdigest()
- return?guess_hash[:4]?==?"0000"?
衡量算法复杂度的办法是修改零开头的个数。使用 4 个零来用于演示,你会发现多一个零都会大大增加计算出结果所需的时间。
Arithmetic complexity is measured by changing the number of zeros. Four zeros are used to demonstrate xff0c; one more zero will significantly increase the time needed to calculate the results.
现在 Blockchain 类基本已经完成了,接下来使用 HTTP requests 来进行交互。
& #xff0c has been largely completed for the Blockchain class; HTTP is then used to interact with each other.
Blockchain 作为 API 接口
Blockchain as an API interface
我们将使用 Python Flask 框架,这是一个轻量 Web 应用框架,它方便将网络请求映射到 Python 函数,现在我们来让 Blockchain 运行在 Flask Web 上。
We will use the Python Flask framework & #xff0c; this is a light Web application frame & #xff0c; it facilitates the mapping of network requests to the Python function & #xff0c; now we will let Blockchain run on the Plask Web.
我们将创建三个接口:
We will create three interfaces #xff1a;
- /transactions/new 创建一个交易并添加到区块
- /mine 告诉服务器去挖掘新的区块
- /chain 返回整个区块链
创建节点
Creates nodes
我们的“Flask 服务器”将扮演区块链网络中的一个节点,我们先添加一些框架代码:
Our "Flask server" will play a node in the block chain network & #xff0c; we will add some frame code & #xff1a;
- import?hashlib
- import?json
- from?textwrap?import?dedent
- from?time?import?time
- from?uuid?import?uuid4
- from?flask?import?Flask
- class?Blockchain(object):
- ...
- #?Instantiate?our?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():
- return?"We'll?mine?a?new?Block"
- @app.route('/transactions/new',?methods=['POST'])
- def?new_transaction():
- return?"We'll?add?a?new?transaction"
- @app.route('/chain',?methods=['GET'])
- def?full_chain():
- response?=?{
- 'chain':?blockchain.chain,
- 'length':?len(blockchain.chain),
- }
- return?jsonify(response),?200
- if?__name__?==?'__main__':
- app.run(host='0.0.0.0',?port=5000)?
简单的说明一下以上代码:
A simple description of the above code xff1a;
- 第 15 行:创建一个节点。
- 第 18 行:为节点创建一个随机的名字。
- 第 21 行:实例 Blockchain 类。
- 第 24–26 行:创建 /mine GET 接口。
- 第 28–30 行:创建 /transactions/new POST 接口,可以给接口发送交易数据。
- 第 32–38 行:创建 /chain 接口, 返回整个区块链。
- 第 40–41 行:服务运行在端口 5000 上。
发送交易
Sends a transaction
发送到节点的交易数据结构如下:
The transaction data structure sent to nodes is as follows: xff1a;
- {
- ?"sender":?"my?address",
- ?"recipient":?"someone?else's?address",
- ?"amount":?5
- }?
之前已经有添加交易的方法,基于接口来添加交易就很简单了:
There is already a method for adding transactions xff0c; adding transactions based on interfaces is simple xff1a;
- import?hashlib
- import?json
- from?textwrap?import?dedent
- from?time?import?time
- from?uuid?import?uuid4
- from?flask?import?Flask,?jsonify,?request
- ...
- @app.route('/transactions/new',?methods=['POST'])
- def?new_transaction():
- values?=?request.get_json()
- #?Check?that?the?required?fields?are?in?the?POST'ed?data
- 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?
挖矿
digs
挖矿正是神奇所在,它很简单,做了以下三件事:
Mining is the magic of #xff0c; it's simple #xff0c; it's done three things #xff1a;
- 计算工作量证明 PoW。
- 通过新增一个交易授予矿工(自己)一个币。
- 构造新区块并将其添加到链中。?
- import?hashlib
- import?json
- from?textwrap?import?dedent
- from?time?import?time
- from?uuid?import?uuid4
- from?flask?import?Flask,?jsonify,?request
- ...
- import?hashlib
- import?json
- from?time?import?time
- from?uuid?import?uuid4
- from?flask?import?Flask,?jsonify,?request
- ...
- @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)
- response?=?{
- 'message':?"New?Block?Forged",?
- 'index':?block['index'],
- 'transactions':?block['transactions'],
- 'proof':?block['proof'],
- 'previous_hash':?block['previous_hash'],
- }
- return?jsonify(response),?200?
注意交易的接收者是我们自己的服务器节点,我们做的大部分工作都只是围绕 Blockchain 类方法进行交互。到此,我们的区块链就算完成了,我们来实际运行下。
The recipient of the transaction is our own server node & #xff0c; most of the work we do is just interacting around the Blockchain class approach. Here #xff0c; our block chain is complete xff0c; let's actually run it.
运行区块链
runs block chains
你可以使用 cURL 或 Postman 去和 API 进行交互。
You can use curl or Postman to interact with API.
启动 server:
Start server:
- $?python?blockchain.py
- *?Runing?on?http://127.0.0.1:5000/?(Press?CTRL+C?to?quit)?
让我们通过请求 http://localhost:5000/mine 来进行挖矿:
Let's ask http://localhost:5,000/mine for mining #xff1a;
通过 post 请求,添加一个新交易:
Request xff0c by post; add a new transaction xff1a;
如果不是使用 Postman,则用以下的 cURL 语句也是一样的:
If Postman, is not used, the following curL statement is the same xff1a;
- $?curl?-X?POST?-H?"Content-Type:?application/json"?-d?'{
- ?"sender":?"d4ee26eee15148ee92c6cd394edd974e",
- ?"recipient":?"someone-other-address",
- ?"amount":?5
- }'?"http://localhost:5000/transactions/new"?
在挖了两次矿之后,就有 3 个块了,通过请求 http://localhost:5000/chain 可以得到所有的块信息。
After digging two mines xff0c; there are three pieces xff0c; all the pieces are available on request at http://localhost:5,000/chain.
- {
- "chain":?[
- {
- "index":?1,
- "previous_hash":?1,
- "proof":?100,
- "timestamp":?1506280650.770839,
- "transactions":?[]
- },
- {
- "index":?2,
- "previous_hash":?"c099bc...bfb7",
- "proof":?35293,
- "timestamp":?1506280664.717925,
- "transactions":?[
- {
- "amount":?1,
- "recipient":?"8bbcb347e0634905b0cac7955bae152b",
- "sender":?"0"
- }
- ]
- },
- {
- "index":?3,
- "previous_hash":?"eff91a...10f2",
- "proof":?35089,
- "timestamp":?1506280666.1086972,
- "transactions":?[
- {
- "amount":?1,
- "recipient":?"8bbcb347e0634905b0cac7955bae152b",
- "sender":?"0"
- }
- ]
- }
- ],
- "length":?3
- }?
一致性(共识)
我们已经有了一个基本的区块链可以接受交易和挖矿,但是区块链系统应该是分布式的。
We already have a basic block chain that can be traded and mined xff0c; but the block chain system should be distributed.
既然是分布式的,那么我们究竟拿什么保证所有节点有同样的链呢?这就是一致性问题,我们要想在网络上有多个节点,就必须实现一个一致性的算法。
Now that it's distributed xff0c; what exactly do we have to guarantee that all nodes have the same chain xff1f; that's the issue of coherence xff0c; that we have to achieve a consistent algorithm if we want to have more than one node on the network xff0c.
注册节点
Registered node
在实现一致性算法之前,我们需要找到一种方式让一个节点知道它相邻的节点。
Before achieving consistency algorithms xff0c; we need to find a way to let a node know its adjacent nodes.
每个节点都需要保存一份包含网络中其他节点的记录,因此让我们新增几个接口:
Each node is required to keep a record containing other nodes in the network xff0c; so let us add several interfaces xff1a;
- /nodes/register 接收 URL 形式的新节点列表。
- /nodes/resolve 执行一致性算法,解决任何冲突,确保节点拥有正确的链。
我们修改下 Blockchain 的 init 函数并提供一个注册节点方法:
We modify the init function of Blockchai and provide a registered node method & #xff1a;
- ...
- from?urllib.parse?import?urlparse
- ...
- class?Blockchain(object):
- def?__init__(self):
- ...
- self.nodes?=?set()
- ...
- def?register_node(self,?address):
- """
- Add?a?new?node?to?the?list?of?nodes
- :param?address:?<str>?Address?of?node.?Eg.?'http://192.168.0.5:5000'
- :return:?None
- """
- parsed_url?=?urlparse(address)
- self.nodes.add(parsed_url.netloc)?
我们用 set 来储存节点,这是一种避免重复添加节点的简单方法。
We use set to store nodes & #xff0c; this is a simple way to avoid adding nodes.
实现共识算法
Consensus Algorithm
前面提到,冲突是指不同的节点拥有不同的链,为了解决这个问题,规定最长的、有效的链才是最终的链,换句话说,网络中有效最长链才是实际的链。
xff0c above; conflict means that different nodes have different chains xff0c; in order to resolve the problem xff0c; provide that the longest, effective chain is the final chain xff0c; in other words xff0c; and that the most effective chain in the network is the actual chain.
我们使用以下的算法,来达到网络中的共识:
We use the following algorithms xff0c; to reach a consensus in the network xff1a;
- ...
- import?requests
- class?Blockchain(object)
- ...
- def?valid_chain(self,?chain):
- """
- Determine?if?a?given?blockchain?is?valid
- :param?chain:?<list>?A?blockchain
- :return:?<bool>?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(" ----------- ")
- #?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):?
- """
- 共识算法解决冲突
- 使用网络中最长的链.
- :return:?<bool>?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?
第一个方法 valid_chain() 用来检查是否是有效链,遍历每个块验证 hash 和 proof。
The first method valid_chain() is used to check whether a valid chain is xff0c; hash and proof are verified through each block.
第二个方法 resolve_conflicts() 用来解决冲突,遍历所有的邻居节点,并用上一个方法检查链的有效性, 如果发现有效更长链,就替换掉自己的链。
The second method is resolve_confects() used to resolve conflicts & #xff0c; traveled through all neighbourhood nodes & #xff0c; and examined the validity of the chain & #xff0c using the previous method; replaced its own chain if found to be effective longer & #xff0c; replaced its own chain.
让我们添加两个路由,一个用来注册节点,一个用来解决冲突。
Let us add two routers xff0c; one for registration node xff0c; and one for conflict resolution.
- @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?
你可以在不同的机器运行节点,或在一台机机开启不同的网络端口来模拟多节点的网络。
You can run nodes & #xff0c in different machines; or a machine can open different network ports to simulate multi-node networks.
这里在同一台机器开启不同的端口演示,在不同的终端运行以下命令,就启动了两个节点:
This is a different port demonstration xff0c for the same machine; the following command xff0c for the different terminals; two nodes xff1a are activated;
- http://localhost:5000?
- http://localhost:5001?
- pipenv?run?python?blockchain.py?
- ?
- pipenv?run?python?blockchain.py?-p?5001?
然后在节点 2 上挖两个块,确保是更长的链,然后在节点 1 上访问接口 /nodes/resolve,这时节点 1 的链会通过共识算法被节点 2 的链取代。
Then dig two blocks at node 2 & #xff0c; ensure that it is a longer chain & #xff0c; then access the interface at node 1 /nodes/resolve, then the chain at node 1 is replaced by the chain at node 2 by a consensus algorithm.
好啦,你可以邀请朋友们一起来测试你的区块链。
Okay #xff0c; you can invite your friends to test your block chain.
编辑:陶家龙、孙淑娟
Editor xff1a; Tao Jialong, Sun Su-jung
来源:互联网综合整理
Source & #xff1a; Internet synthesis
-
《笑喷!区块链十个段子集锦》转载自刘兴亮时间微信公众号
Laughing #xff01; ten pieces of the block chain.
-
《图解:大白话了解到底区块链是个啥?》转载自黑马程序员视频库微信公众号
"Image #xff1a; big white words know what the chain of blocks is" #xff1f; reprinted from Black Horse Programr's video library, Weibo Public.
-
《用Python从零开始创建区块链》作者:Daniel van Flymen,Tiny熊译。
Python to Create Block Chains from Zero & #xff1a; Daniel van Flymen, Tiny Bear Translation.
原文:https://learnblockchain.cn/2017/10/27/build_blockchain_by_python/
Original #xff1a; https://learnblockchain.cn/2017/10/27/Build_blockchain_by_python/
源码:https://github.com/xilibi2003/blockchain
Source & #xff1a; https://github.com/xilibi2003/blockchain
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论