https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/proof-of-work-example.py

下面代码可以运行:

#!/usr/bin/env python
# example of proof-of-work algorithmimport hashlib
import timemax_nonce = 2 ** 32  # 4 billiondef proof_of_work(header, difficulty_bits):# calculate the difficulty targettarget = 2 ** (256-difficulty_bits)for nonce in range(max_nonce):encode_str = (str(header)+str(nonce)).encode('utf-8')hash_result = hashlib.sha256(encode_str).hexdigest()# check if this is a valid result, below the targetif int(hash_result, 16) < target: # 16进制字符串转成10进制intprint("Success with nonce %d" % nonce)print("Hash is %s" % hash_result)return (hash_result, nonce)print("Failed after %d (max_nonce) tries" % nonce)return nonceif __name__ == '__main__':nonce = 0hash_result = ''# difficulty from 0 to 31 bitsfor difficulty_bits in range(32):difficulty = 2 ** difficulty_bitsprint("Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits))print("Starting search...")# checkpoint the current timestart_time = time.time()# make a new block which includes the hash from the previous block# we fake a block of transactions - just a stringnew_block = 'test block with transactions' + hash_result# find a valid nonce for the new block(hash_result, nonce) = proof_of_work(new_block, difficulty_bits)# checkpoint how long it took to find a resultend_time = time.time()elapsed_time = end_time - start_timeprint("Elapsed Time: %.4f seconds" % elapsed_time)if elapsed_time > 0:# estimate the hashes per secondhash_power = float(nonce/elapsed_time)print("Hashing Power: %ld hashes per second" % hash_power)

(转)《精通比特币》原码分析: pow机制相关推荐

  1. MyBatis 源码分析 - 插件机制

    1.简介 一般情况下,开源框架都会提供插件或其他形式的拓展点,供开发者自行拓展.这样的好处是显而易见的,一是增加了框架的灵活性.二是开发者可以结合实际需求,对框架进行拓展,使其能够更好的工作.以 My ...

  2. Dubbo 源码分析 - SPI 机制

    1.简介 SPI 全称为 Service Provider Interface,是一种服务发现机制.SPI 的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加载实现类.这样可以 ...

  3. 源码分析Handler机制

    看下面例子: 在子线程发送一个消息,然后在主线程街道这个消息处理,这个消息是如何从子线程切换到主线程的呢?首先跟踪一下handler.sendMessage(new Message())如下: 从上面 ...

  4. JQuery源码分析 - 闭包机制在jQuery中的使用及冲突解决

    jQuery中的闭包机制 本系列中我们将基于jquery3.5.1版本对jQuery源码进行分析,分析以源码加注释的方式展示. 本节中将分析jQuery源码中的 14 ~ 40行:自执行函数定义.环境 ...

  5. SOFA 源码分析 — 扩展机制

    前言 我们在之前的文章中已经稍微了解过 SOFA 的扩展机制,我们也说过,一个好的框架,必然是易于扩展的.那么 SOFA 具体是怎么实现的呢? 一起来看看. 如何使用? 看官方的 demo: 1.定义 ...

  6. LinkedList原码分析(基于JDK1.6)

    <Java集合类>一文中已经最List的基本操作进行说明,并且比较了ArrayList和LinkedList的效率.本文将进一步解析LinkedList. LinkedList也和Arra ...

  7. Struts2 源码分析——拦截器的机制

    本章简言 上一章讲到关于action代理类的工作.即是如何去找对应的action配置信息,并执行action类的实例.而这一章笔者将讲到在执行action需要用到的拦截器.为什么要讲拦截器呢?可以这样 ...

  8. MyBatis 源码分析 - 内置数据源

    1.简介 本篇文章将向大家介绍 MyBatis 内置数据源的实现逻辑.搞懂这些数据源的实现,可使大家对数据源有更深入的认识.同时在配置这些数据源时,也会更清楚每种属性的意义和用途.因此,如果大家想知其 ...

  9. MyBatis 源码分析 - SQL 的执行过程

    本文速览 本篇文章较为详细的介绍了 MyBatis 执行 SQL 的过程.该过程本身比较复杂,牵涉到的技术点比较多.包括但不限于 Mapper 接口代理类的生成.接口方法的解析.SQL 语句的解析.运 ...

  10. MyBatis 源码分析 - 映射文件解析过程

    1.简介 在上一篇文章中,我详细分析了 MyBatis 配置文件的解析过程.由于上一篇文章的篇幅比较大,加之映射文件解析过程也比较复杂的原因.所以我将映射文件解析过程的分析内容从上一篇文章中抽取出来, ...

最新文章

  1. POJ-2251 Dungeon Master
  2. Google 团队效能研究 | 为什么雇用最聪明的人是远远不够的?
  3. Solaris 11的ip地址配置
  4. WINCE6.0 中文支持
  5. JasperMES.cn JasperMES.com.cn 我的MES网站
  6. linux c语言内核函数,2014-1-5_linux内核学习(1)_C语言基础
  7. 把VOC数据集转化成txt文件python
  8. Scrapy Crawl 运行出错 AttributeError: 'xxxSpider' object has no attribute '_rules' 的问题解决...
  9. Spring 框架 IOC 与 DI 的总结
  10. 第二周代码(wc项目)
  11. javaweb——Servlet开发
  12. 20220213:力扣第280场周赛(上)
  13. win11如何快速加密硬盘 Windows11快速加密硬盘的设置方法
  14. Knockout应用开发指南 第二章:监控属性(Observables)
  15. 软件项目管理知识点总结
  16. xp系统从u盘启动计算机,手把手教你u盘安装XP系统步骤
  17. 2021道路运输企业主要负责人安全考核试题
  18. 多元复合函数的求导法则
  19. 如何设计一个权限管理模块?
  20. 计算机中断/硬中断/软中断之理解(1)

热门文章

  1. 个人经常查阅的网站(顺序无先后,持续更新)
  2. hdu 5178 pairs (线性探查问题)
  3. 数组 , List互转
  4. 苹果傲慢,售后服务中外有别
  5. My first essay
  6. 黄聪:Linq初级班 Linq To XML体验(编程篇)
  7. SQL Server里面如何导出包含数据的SQL脚本
  8. Windows 的 80 端口被 System 进程占用解决方案
  9. LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)
  10. moment.js的方法总结