Python Blockchain 简明教程

Python Creating Blockchain

区块链包含一个相互链接的区块列表。为了存储整个列表,我们将创建一个称为 TPCoins 的列表变量 −

A blockchain contains a list of blocks chained to each other. To store the entire list, we will create a list variable called TPCoins −

TPCoins = []

我们还将编写一个名为 dump_blockchain 的实用方法,用于转储整个区块链的内容。我们首先打印区块链的长度,以便了解当前区块链中有多少个区块。

We will also write a utility method called dump_blockchain for dumping the contents of the entire blockchain. We first print the length of the blockchain so that we know how many blocks are currently present in the blockchain.

def dump_blockchain (self):
   print ("Number of blocks in the chain: " + str(len (self)))

请注意,随着时间的流逝,区块链中的区块数量将高得惊人,无法打印。因此,在打印区块链内容时,你可能必须确定想要检查的范围。在下面的代码中,我们打印了整个区块链,因为我们在当前演示中不会添加太多区块。

Note that as the time passes, the number of blocks in the blockchain would be extraordinarily high for printing. Thus, when you print the contents of the blockchain you may have to decide on the range that you would like to examine. In the code below, we have printed the entire blockchain as we would not be adding too many blocks in the current demo.

为了遍历区块链,我们设置一个 for 循环,如下所示 −

To iterate through the chain, we set up a for loop as follows −

for x in range (len(TPCoins)):
   block_temp = TPCoins[x]

将每个引用区块复制到称为 block_temp 的临时变量中。

Each referenced block is copied to a temporary variable called block_temp.

我们为每一个区块打印一个区块号码作为标题。请注意,这些号码将从零开始,第一个区块是编号为零的创世区块。

We print the block number as a heading for each block. Note that the numbers would start with zero, the first block is a genesis block that is numbered zero.

print ("block # " + str(x))

在每一个区块内,我们都存储了一个变量为 verified_transactions 的三个交易的列表(创世区块除外)。我们用一个 for 循环遍历此列表,并针对每一个检索到的项目,我们调用 display_transaction 函数来显示交易详情。

Within each block, we have stored a list of three transactions (except for the genesis block) in a variable called verified_transactions. We iterate this list in a for loop and for each retrieved item, we call display_transaction function to display the transaction details.

for transaction in block_temp.verified_transactions:
   display_transaction (transaction)

整个函数定义如下所示——

The entire function definition is shown below −

def dump_blockchain (self):
   print ("Number of blocks in the chain: " + str(len (self)))
   for x in range (len(TPCoins)):
      block_temp = TPCoins[x]
      print ("block # " + str(x))
      for transaction in block_temp.verified_transactions:
         display_transaction (transaction)
         print ('--------------')
      print ('=====================================')

请注意,我们在代码中适当的位置插入了分隔符,以便于标记其中的区块和交易。

Note that here we have inserted the separators at appropriate points in the code to demarcate the blocks and transactions within it.

既然我们已经创建了一个用于存储区块的区块链,我们的下一任务就是创建区块并开始将它们添加到区块链。为此,我们将添加一个你在前一步中已经创建的创世区块。

As we have now created a blockchain for storing blocks, our next task is to create blocks and start adding it to the blockchain. For this purpose, we will add a genesis block that you have already created in the earlier step.