chaobi【Vaubecourt】构筑Blockchnano币

财经资讯3年前 (2023)发布 EditorY
7,856 0

chaobi【Vaubecourt】构筑Blockchnano币

ut官方网站

构筑Blockchain

关上你讨厌的图形界面器或IDE,我较为讨厌采用 PyCharm。接着建立两个名叫blockchain.py的新文档。只采用这两个文档,但假如搞丢了此文档,你能始终提及源代码:https://github.com/dvf/blockchain

(1)区块链愿景

他们将建立两个区块链 类,它的缺省会建立两个如上所述空条目用作储存区块链,另两个用作储存买卖。这是他们建立的区块链class 的源代码:

1.             class Blockchain(object):

2.                 def __init__(self):

3.                     self.chain = []

4.                     self.current_transactions = []

5.

6.                 def new_block(self):

7.                     # Creates a new Block and adds it to the chain

8.                     pass比特币创下新高

9.

10.              def new_transaction(self):

11.                   # Adds a new transaction to the list of transactions

12.                  pass

13.

14.              @staticmethod

15.              def hash(block):

16.                  # Hashes a Block

17.                  pass

18.

19.              @property

20.              def last_block(self):

21.                  # Returns the last Block in the chain

22.                  pass

Blueprint of our Blockchain Class

区块链 class 负责管理链。它将储存买卖,并有一些辅助方法来为链添加新的区块。让他们开始充实一些方法。比特币创下新高

两个区块会是什么样子?

每个块都有两个索引、两个时间戳(Unix时间)、两个买卖条目、两个证明和前两个块的哈希值。

区块源代码例子:

1.             block = {

ceg

2.                 ‘index’: 1,

3.                 ‘timestamp’: 1506057125.900785,

4.                 ‘transactions’: [

5.                     {

6.                         ‘sender’: “8527147fe1f5426f9dd545de4b27ee00”,

7.                         ‘recipient’: “a77f5cdfa2934df3954a5c7c7da5df1f”,

8.                         ‘amount’: 5,

9.                     }

10.              ],

11.               ‘proof’: 324984774000,比特币创下新高

12.              ‘previous_hash’: “2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824”

13.          }

链的概念应该很明显:每个新块都包含在其内部的前两个块的哈希。这点是至关重要的,因为它使 Blockchain 不可篡改:假如攻击者破坏了链中较早的区块,那么随后所有的块都将包含不正确的哈希值。

请花一些时间好好去理解它——这是区块链设计的的核心理念。

(2)在区块中添加买卖

他们需要一种将买卖添加到块中的方法。new_transaction() 方法能实现这个功能,而且非常简单:

1.             class Blockchain(object):

2.                 …

3.

4.                 def new_transaction(self, sender, recipient, amount):

5.                     “””

6.                     Creates a new transaction to go into the next mined Block比特币创下新高

7.

8.                     :param sender: Address of the Sender

9.                     :param recipient: Address of the Recipient

10.                  :param amount: Amount

11.                   :return: The index of the Block that will hold this transaction

12.                  “””

13.

14.                  self.current_transactions.append({

15.                      ‘sender’: sender,

16.                      ‘recipient’: recipient,

17.                      ‘amount’: amount,比特币创下新高

18.                  })

19.

20.                  return self.last_block[‘index’] + 1

在new_transaction()将买卖添加到条目之后,它将返回这个买卖会被添加到下两个块的索引。这对稍后提交买卖的用户有用。

(3)建立新区块

当 区块链被实例化时,需要将它与两个没有前辈的创世区块一起连接起来。他们还需要向他们的创世区块添加两个“证明”,这是挖矿的结果。

除了在他们的缺省中建立创世区块之外,他们还将为new_block()、new_transaction()和hash()添加方法:

1.             import hashlib

2.             import on

3.             from time import time

4.

5.

6.             class Blockchain(object):

7.                 def __init__(self):

8.                     self.current_transactions = []比特币创下新高

9.                     self.chain = []

10.

11.                   # Create the genesis block

12.                  self.new_block(previous_hash=1, proof=100)

13.

14.              def new_block(self, proof, previous_hash=None):

15.                  “””

16.                  Create a new Block in the Blockchain

17.

18.                  :param proof: The proof given by the Proof of Work algorithm

19.                  :param previous_hash: (Optional) Hash of previous Block

20.                  :return: New Block比特币创下新高

21.                  “””

22.

23.                  block = {

24.                      ‘index’: len(self.chain) + 1,

25.                      ‘timestamp’: time(),

26.                      ‘transactions’: self.current_transactions,

27.                      ‘proof’: proof,

28.                      ‘previous_hash’: previous_hash or self.hash(self.chain[-1]),

29.                  }

30.

31.                  # Reset the current list of transactions

32.                  self.current_transactions = []比特币创下新高

33.

34.                  self.chain.append(block)

35.                  return block

共生币

36.

37.              def new_transaction(self, sender, recipient, amount):

38.                  “””

39.                  Creates a new transaction to go into the next mined Block

40.

41.                  :param sender: Address of the Sender

42.                  :param recipient: Address of the Recipient

43.                  :param amount: Amount

44.                  :return: The index of the Block that will hold this transaction比特币创下新高

45.                  “””

46.                  self.current_transactions.append({

47.                      ‘sender’: sender,

48.                      ‘recipient’: recipient,

49.                      ‘amount’: amount,

50.                  })

51.

52.                  return self.last_block[‘index’] + 1

53.

54.              @property

55.              def last_block(self):

56.                  return self.chain[-1]

57.

58.              @staticmethod

59.              def hash(block):

60.                  “””比特币创下新高

61.                  Creates a SHA-256 hash of a Block

62.

63.                  :param block: Block

64.                  :return:

65.                  “””

66.

67.                  # We must make sure that the Dictionary is Ordered, or we’ll have inconsistent hashes

68.                  block_string = on.dumps(block, sort_keys=True).encode()

69.                  return hashlib.sha256(block_string).hexdigest()

70.

至此,他们几乎完成了 Blockchain 的代码化表现。但新的区块是如何被建立、挖掘的?

(4)理解PoW工作量证明

工作量证明,也就是新的区块如何在 Blockchain 上被建立或挖掘出来。它的目标是发现两个解决问题的数字,这个数字一定很难找到,但却很容易被验证——在网络上的任何人都能通过计算来验证,这是工作证明PoW背后的核心思想。比特币创下新高

他们来看两个非常简单的例子:他们想找到这样两个数值,将整数x与另两个数值y的乘积进行hash运算,使得运算的结果是一串字符串的结尾必须是数字0 。用数学表达式表示出来就是:

hash(x * y) = ac23dc…0

他们假定x = 5。在Python中实现,代码如下:

1.             from hashlib import sha256

2.             x = 5

3.             y = 0  # We don’t know what y should be yet…

4.             while sha256(f{x*y}’.encode()).hexdigest()[-1] != “0”:

5.                 y += 1

6.             print(f’The solution is y = {y}’)

这里的解是y = 21。因为,生成的hash值是以0结尾的:比特币创下新高

1. hash(5 * 21) = 1253e9373e…5e3600155e860

在比特币中,工作量证明被称为Hashcash 。它和上面举出的简单例子基本没有太大区别。这是为了建立两个新的区块,矿工们竞相解决问题的算法。一般来说,难度取决于字符串中搜索的字符数。

矿工会因为在两个买卖中找到了那个难题的解,而获得系统给出的激励:该网络的一定量的数字货币。该网络能够很容易地验证他们的解是否正确。

(5)实现基本的工作量证明

为区块链实现两个类似的算法,规则与上面类似:找到两个数字p,当与上两个区块的解进行哈希运算时,产生两个前4位都是0的哈希值。

为了调整算法的难度,他们能修改前几位零的个数。但4个0就足够了。你将发现,添加两个前导零就会对找到解所需的时间造成很大的不同。

1.             import hashlib

2.             import on

3.

4.             from time import time

5.             from uuid import uuid4

6.

7.

8.             class Blockchain(object):比特币创下新高

9.                 …

10.

11.               def proof_of_work(self, last_proof):

12.                  “””

13.                  Simple Proof of Work Algorithm:

14.                   – Find a number p’ such that hash(pp’) contains leading 4 zeroes, where p is the previous p’

15.                   – p is the previous proof, and p’ is the new proof

16.

17.                  :param last_proof:

18.                  :return:

19.                  “””

20.

21.                  proof = 0

22.                  while self.valid_proof(last_proof, proof) is False:比特币创下新高

23.                      proof += 1

24.

25.                  return proof

26.

27.              @staticmethod

28.              def valid_proof(last_proof, proof):

29.                  “””

30.                  Validates the Proof: Does hash(last_proof, proof) contain 4 leading zeroes?

31.

32.                  :param last_proof: Previous Proof

33.                  :param proof: Current Proof

34.                  :return: True if correct, False if not.比特币创下新高

35.                  “””

36.

37.                  guess = f{last_proof}{proof}’.encode()

38.                  guess_hash = hashlib.sha256(guess).hexdigest()

39.                  return guess_hash[:4] == “0000”

他们的类接近完成,他们已经准备好采用HTTP请求开始与它交互。

新闻排行榜

  • 1全球各国区块链、数字货币等政策汇总
  • 2斯坦福大学终身教授张首晟:区块链最核心的理念,必然是「 In Math We Trust 」
  • 3人民日报:让行业协会走上前台
  • 4洪门发布洪币白皮书**价格1美元,谁敢砸盘?
  • 5区块链热潮下,BAT也坐不住了 百度上线**区块链应用“莱茨狗”
  • 6习主席首提“区块链”,蕴含“区块链强国”战略
  • 7**中央关于制定国民经济和社会发展第十四个五年规划和二〇三五年远景目标的建议
  • 8区块链为什么上升为国家战略技术的原因解析
  • 9区块链在国家治理与公共事务中的现实应用
  • 102019年是区块链行业跌宕起伏的一年

比特币挖矿机

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时删除。

© 版权声明

相关文章