Python区块链教程

创建创世纪块

Python区块链创建创世纪块详细操作教程
我们假设TPCoins的创建者最初向已知客户端 Dinesh 分发了500 TPCoins。为此,他首先创建了一个Dinesh实例-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
Dinesh = Client()
然后我们创建一个创世纪交易,并将500个TPCoins发送到Dinesh的公共地址。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
t0 = Transaction (
   "Genesis",
   Dinesh.identity,
   500.0
)
现在,我们创建一个 Block 类的实例,并将其命名为 block0
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
block0 = Block()
我们将 previous_block_hash Nonce 实例变量初始化为 None ,因为这是存储在区块链中的第一笔交易。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
block0.previous_block_hash = None
Nonce = None
接下来,我们将上述t0事务添加到块内维护的 verified_transactions 列表中-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
block0.verified_transactions.append (t0)
这时,该块已完全初始化,可以添加到我们的区块链中了。我们将为此创建区块链。在将区块添加到区块链之前,我们将对区块进行哈希处理并将其值存储在我们先前声明的名为 last_block_hash 的全局变量中。该值将由下一个矿工在其区块中使用。
我们使用以下两行编码对块进行哈希处理并存储摘要值。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
digest = hash (block0)
last_block_hash = digest
最后,我们将在下一章中创建一个区块链。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4