Python Blockchain 简明教程
Python Blockchain - Block Class
一个区块包含数量不等的事务。为了简单起见,在我们案例中,我们将假设区块包含固定数量的事务,在本例中为三个。由于该区块需要存储这三个事务的列表,因此,我们将声明一个名为 verified_transactions 的实例变量,如下所示 −
A block consists of a varying number of transactions. For simplicity, in our case we will assume that the block consists of a fixed number of transactions, which is three in this case. As the block needs to store the list of these three transactions, we will declare an instance variable called verified_transactions as follows −
self.verified_transactions = []
我们已将该变量命名为 verified_transactions ,以表明仅经过验证的有效交易将被添加到区块。每个区块还保存前一个区块的哈希值,从而使区块链变得不可改变。
We have named this variable as verified_transactions to indicate that only the verified valid transactions will be added to the block. Each block also holds the hash value of the previous block, so that the chain of blocks becomes immutable.
为了存储前一个哈希,我们声明一个实例变量,如下所示 −
To store the previous hash, we declare an instance variable as follows −
self.previous_block_hash = ""
最后,我们声明另一个变量 Nonce ,用于存储挖掘者在挖掘过程中创建的随机数。
Finally, we declare one more variable called Nonce for storing the nonce created by the miner during the mining process.
self.Nonce = ""
Block 类的完整定义如下 −
The full definition of the Block class is given below −
class Block:
def __init__(self):
self.verified_transactions = []
self.previous_block_hash = ""
self.Nonce = ""
由于每个区块都需前一个区块哈希的值,我们声明一个全局变量 last_block_hash ,如下所示 −
As each block needs the value of the previous block’s hash we declare a global variable called last_block_hash as follows −
last_block_hash = ""
现在,让我们在区块链中创建我们的第一个区块。
Now let us create our first block in the blockchain.