Python区块链块添加的实现及详细过程是什么
Admin 2022-08-23 群英技术资讯 708 次浏览
last_transaction_index = 0
我们现在让我们的第一个矿工在区块链中添加一个区块.
到添加一个新块,我们首先创建一个 Block 类的实例
block = Block()
我们从队列中选取前3个交易并减去;
for i in range(3): temp_transaction = transactions[last_transaction_index] # validate transaction
在将事务添加到块之前矿工将验证交易的有效性.通过测试发送方提供的哈希与矿工使用发送方公钥生成的哈希的相等性来验证交易有效性.此外,矿工将验证发件人是否有足够的余额来支付当前交易.
为简洁起见,我们未在教程中包含此功能.验证交易后,我们将其添加到块实例中的 verified_transactions 列表中.
block.verified_transactions.append(temp_transaction)
我们递增最后一个事务索引,以便下一个矿工将获取队列中的后续事务
last_transaction_index + = 1
我们在块中添加了三个交易.完成此操作后,我们将初始化 Block 类的其余实例变量.我们首先添加最后一个块的哈希值.
block.previous_block_hash = last_block_hash
接下来,我们挖掘难度等级为2的区块.
block.Nonce = mine(block,2)
请注意, mine 函数的第一个参数是二进制对象.我们现在对整个块进行哈希并在其上创建摘要.
digest = hash(block)
最后,我们将创建的块添加到区块链中,并重新初始化全局变量 last_block_hash ,以便在下一个区块中使用.
添加块的整个代码显示在下面 :
block = Block() for i in range(3): temp_transaction = transactions[last_transaction_index] # validate transaction # if valid block.verified_transactions.append (temp_transaction) last_transaction_index += 1 block.previous_block_hash = last_block_hash block.Nonce = mine (block, 2) digest = hash (block) TPCoins.append (block) last_block_hash = digest
现在我们将向区块链添加两个块.添加下两个块的代码在下面和下面给出;
# Miner 2 adds a block block = Block() for i in range(3): temp_transaction = transactions[last_transaction_index] # validate transaction # if valid block.verified_transactions.append (temp_transaction) last_transaction_index += 1 block.previous_block_hash = last_block_hash block.Nonce = mine (block, 2)digest = hash (block) TPCoins.append (block)last_block_hash = digest # Miner 3 adds a block block = Block() for i in range(3): temp_transaction = transactions[last_transaction_index] #display_transaction (temp_transaction) # validate transaction # if valid block.verified_transactions.append (temp_transaction) last_transaction_index += 1 block.previous_block_hash = last_block_hash block.Nonce = mine (block, 2) digest = hash (block) TPCoins.append (block) last_block_hash = digest
当你添加这两个块时,您还将看到查找Nonce所需的迭代次数.此时,我们的区块链共包含4个区块,包括创世区块.
您可以验证整个区块链的内容使用以下语句 :
dump_blockchain(TPCoins)
你会看到输出类似于下面显示的那个 :
Number of blocks in the chain: 4 block # 0 sender: Genesis ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100ed272b52ccb539e2cd779 c6cc10ed1dfadf5d97c6ab6de90ed0372b2655626fb79f62d0e01081c163b0864cc68d426bbe943 8e8566303bb77414d4bfcaa3468ab7febac099294de10273a816f7047d4087b4bafa11f141544d4 8e2f10b842cab91faf33153900c7bf6c08c9e47a7df8aa7e60dc9e0798fb2ba3484bbdad2e44302 03010001 ----- value: 500.0 ----- time: 2019-01-14 16:18:02.042739 ----- -------------- ===================================== block # 1 sender: 30819f300d06092a864886f70d010101050003818d0030818902818100bb064c99c492144a9f463 480273aba93ac1db1f0da3cb9f3c1f9d058cf499fd8e54d244da0a8dd6ddd329ec86794b04d773e b4841c9f935ea4d9ccc2821c7a1082d23b6c928d59863407f52fa05d8b47e5157f8fe56c2ce3279 c657f9c6a80500073b0be8093f748aef667c03e64f04f84d311c4d866c12d79d3fc3034563dfb02 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100be93b516b28c6e674abe7 abdb11ce0fdf5bb728b75216b73f37a6432e4b402b3ad8139b8c0ba541a72c8add126b6e1a1308f b98b727beb63c6060356bb177bb7d54b54dbe87aee7353d0a6baa9397704de625d1836d3f42c7ee 5683f6703259592cc24b09699376807f28fe0e00ff882974484d805f874260dfc2d1627473b9102 03010001 ----- value: 15.0 ----- time: 2019-01-14 16:18:01.859915 ----- -------------- sender: 30819f300d06092a864886f70d010101050003818d0030818902818100bb064c99c492144a9f463 480273aba93ac1db1f0da3cb9f3c1f9d058cf499fd8e54d244da0a8dd6ddd329ec86794b04d773e b4841c9f935ea4d9ccc2821c7a1082d23b6c928d59863407f52fa05d8b47e5157f8fe56c2ce3279 c657f9c6a80500073b0be8093f748aef667c03e64f04f84d311c4d866c12d79d3fc3034563dfb02 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100a070c82b34ae143cbe59b 3a2afde7186e9d5bc274955d8112d87a00256a35369acc4d0edfe65e8f9dc93fbd9ee74b9e7ea12 334da38c8c9900e6ced1c4ce93f86e06611e656521a1eab561892b7db0961b4f212d1fd5b5e49ae 09cf8c603a068f9b723aa8a651032ff6f24e5de00387e4d062375799742a359b8f22c5362e56502 03010001 ----- value: 6.0 ----- time: 2019-01-14 16:18:01.860966 ----- -------------- sender: 30819f300d06092a864886f70d010101050003818d0030818902818100be93b516b28c6e674abe7 abdb11ce0fdf5bb728b75216b73f37a6432e4b402b3ad8139b8c0ba541a72c8add126b6e1a1308f b98b727beb63c6060356bb177bb7d54b54dbe87aee7353d0a6baa9397704de625d1836d3f42c7ee 5683f6703259592cc24b09699376807f28fe0e00ff882974484d805f874260dfc2d1627473b9102 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100cba097c0854876f41338c 62598c658f545182cfa4acebce147aedf328181f9c4930f14498fd03c0af6b0cce25be99452a81d f4fa30a53eddbb7bb7b203adf8764a0ccd9db6913a576d68d642d8fd47452590137869c25d9ff83 d68ebe6d616056a8425b85b52e69715b8b85ae807b84638d8f00e321b65e4c33acaf6469e18e302 03010001 ----- value: 2.0 ----- time: 2019-01-14 16:18:01.861958 ----- -------------- ===================================== block # 2 sender: 30819f300d06092a864886f70d010101050003818d0030818902818100a070c82b34ae143cbe59b 3a2afde7186e9d5bc274955d8112d87a00256a35369acc4d0edfe65e8f9dc93fbd9ee74b9e7ea12 334da38c8c9900e6ced1c4ce93f86e06611e656521a1eab561892b7db0961b4f212d1fd5b5e49ae 09cf8c603a068f9b723aa8a651032ff6f24e5de00387e4d062375799742a359b8f22c5362e56502 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100be93b516b28c6e674abe7 abdb11ce0fdf5bb728b75216b73f37a6432e4b402b3ad8139b8c0ba541a72c8add126b6e1a1308f b98b727beb63c6060356bb177bb7d54b54dbe87aee7353d0a6baa9397704de625d1836d3f42c7ee 5683f6703259592cc24b09699376807f28fe0e00ff882974484d805f874260dfc2d1627473b9102 03010001 ----- value: 4.0 ----- time: 2019-01-14 16:18:01.862946 ----- -------------- sender: 30819f300d06092a864886f70d010101050003818d0030818902818100cba097c0854876f41338c 62598c658f545182cfa4acebce147aedf328181f9c4930f14498fd03c0af6b0cce25be99452a81d f4fa30a53eddbb7bb7b203adf8764a0ccd9db6913a576d68d642d8fd47452590137869c25d9ff83 d68ebe6d616056a8425b85b52e69715b8b85ae807b84638d8f00e321b65e4c33acaf6469e18e302 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100a070c82b34ae143cbe59b 3a2afde7186e9d5bc274955d8112d87a00256a35369acc4d0edfe65e8f9dc93fbd9ee74b9e7ea12 334da38c8c9900e6ced1c4ce93f86e06611e656521a1eab561892b7db0961b4f212d1fd5b5e49ae 09cf8c603a068f9b723aa8a651032ff6f24e5de00387e4d062375799742a359b8f22c5362e56502 03010001 ----- value: 7.0 ----- time: 2019-01-14 16:18:01.863932 ----- -------------- sender: 30819f300d06092a864886f70d010101050003818d0030818902818100be93b516b28c6e674abe7 abdb11ce0fdf5bb728b75216b73f37a6432e4b402b3ad8139b8c0ba541a72c8add126b6e1a1308f b98b727beb63c6060356bb177bb7d54b54dbe87aee7353d0a6baa9397704de625d1836d3f42c7ee 5683f6703259592cc24b09699376807f28fe0e00ff882974484d805f874260dfc2d1627473b9102 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100a070c82b34ae143cbe59b 3a2afde7186e9d5bc274955d8112d87a00256a35369acc4d0edfe65e8f9dc93fbd9ee74b9e7ea12 334da38c8c9900e6ced1c4ce93f86e06611e656521a1eab561892b7db0961b4f212d1fd5b5e49ae 09cf8c603a068f9b723aa8a651032ff6f24e5de00387e4d062375799742a359b8f22c5362e56502 03010001 ----- value: 3.0 ----- time: 2019-01-14 16:18:01.865099 ----- -------------- ===================================== block # 3 sender: 30819f300d06092a864886f70d010101050003818d0030818902818100a070c82b34ae143cbe59b 3a2afde7186e9d5bc274955d8112d87a00256a35369acc4d0edfe65e8f9dc93fbd9ee74b9e7ea12 334da38c8c9900e6ced1c4ce93f86e06611e656521a1eab561892b7db0961b4f212d1fd5b5e49ae 09cf8c603a068f9b723aa8a651032ff6f24e5de00387e4d062375799742a359b8f22c5362e56502 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100bb064c99c492144a9f463 480273aba93ac1db1f0da3cb9f3c1f9d058cf499fd8e54d244da0a8dd6ddd329ec86794b04d773e b4841c9f935ea4d9ccc2821c7a1082d23b6c928d59863407f52fa05d8b47e5157f8fe56c2ce3279 c657f9c6a80500073b0be8093f748aef667c03e64f04f84d311c4d866c12d79d3fc3034563dfb02 03010001 ----- value: 8.0 ----- time: 2019-01-14 16:18:01.866219 ----- -------------- sender: 30819f300d06092a864886f70d010101050003818d0030818902818100a070c82b34ae143cbe59b 3a2afde7186e9d5bc274955d8112d87a00256a35369acc4d0edfe65e8f9dc93fbd9ee74b9e7ea12 334da38c8c9900e6ced1c4ce93f86e06611e656521a1eab561892b7db0961b4f212d1fd5b5e49ae 09cf8c603a068f9b723aa8a651032ff6f24e5de00387e4d062375799742a359b8f22c5362e56502 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100be93b516b28c6e674abe7 abdb11ce0fdf5bb728b75216b73f37a6432e4b402b3ad8139b8c0ba541a72c8add126b6e1a1308f b98b727beb63c6060356bb177bb7d54b54dbe87aee7353d0a6baa9397704de625d1836d3f42c7ee 5683f6703259592cc24b09699376807f28fe0e00ff882974484d805f874260dfc2d1627473b9102 03010001 ----- value: 1.0 ----- time: 2019-01-14 16:18:01.867223 ----- -------------- sender: 30819f300d06092a864886f70d010101050003818d0030818902818100cba097c0854876f41338c 62598c658f545182cfa4acebce147aedf328181f9c4930f14498fd03c0af6b0cce25be99452a81d f4fa30a53eddbb7bb7b203adf8764a0ccd9db6913a576d68d642d8fd47452590137869c25d9ff83 d68ebe6d616056a8425b85b52e69715b8b85ae807b84638d8f00e321b65e4c33acaf6469e18e302 03010001 ----- recipient: 30819f300d06092a864886f70d010101050003818d0030818902818100bb064c99c492144a9f463 480273aba93ac1db1f0da3cb9f3c1f9d058cf499fd8e54d244da0a8dd6ddd329ec86794b04d773e b4841c9f935ea4d9ccc2821c7a1082d23b6c928d59863407f52fa05d8b47e5157f8fe56c2ce3279 c657f9c6a80500073b0be8093f748aef667c03e64f04f84d311c4d866c12d79d3fc3034563dfb02 03010001 ----- value: 5.0 ----- time: 2019-01-14 16:18:01.868241 ----- -------------- =====================================
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Python基础之赋值,浅拷贝,深拷贝的区别,文中有非常详细的代码示例,对正在学习python基础的小伙伴们也有非常好的帮助,需要的朋友可以参考下
这篇文章主要为大家详细介绍了8个实用的Python程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
IndentationError:unexpectedindent Python中强制缩进,,IndentationError:unexpectedindent缩进错误这类错误非常常见,一般都是由于tab在不同的平台上占用长度不同导致,有些事程序员自己直接使用空格或其他来顶替tab。解决办法非常简单,在所在平台上使用标准的tab进行缩进,就OK了。Unicode
这篇文章主要为大家详细介绍了python实现人人对战的五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了python爬虫scrapy框架用法的用法,对于新手学习scrapy框架有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章能有所收获,接下来小编带着大家一起了解看看。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008