比特币nBits计算
转载:比特币源码分析(二十二) - 挖矿和共识
https://blog.csdn.net/yzpbright/article/details/81231351
CalculateNextWorkRequired()方法:
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{
if (params.fPowNoRetargeting)
return pindexLast->nBits; // Limit adjustment step
// 计算生成最近的2016个区块实际花费了多少时间
int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
//这里需要限制调整的步长,即把实际花费的时间限制在0.5周和8周之间
if (nActualTimespan < params.nPowTargetTimespan/)//params.nPowTargetTimespan是2周,即20160分钟
nActualTimespan = params.nPowTargetTimespan/;
if (nActualTimespan > params.nPowTargetTimespan*)
nActualTimespan = params.nPowTargetTimespan*; // Retarget
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
arith_uint256 bnNew;
bnNew.SetCompact(pindexLast->nBits);//旧的难度目标值
bnNew *= nActualTimespan;
bnNew /= params.nPowTargetTimespan; if (bnNew > bnPowLimit)
bnNew = bnPowLimit; return bnNew.GetCompact();
}
计算公式:新的难度目标值 = 旧的难度目标值 * 生成最近2016个区块所花费的实际时间 / 系统期望生成2016个区块的时间
其中代码中:nBits 即 旧的难度目标值,nActualTimespa 即 生成最近2016个区块所花费的实际时间 ,
params.nPowTargetTimespan 即 系统期望生成2016个区块的时间 。
2.1.3.3难度目标的表示
上面讲了难度目标的计算方法,这里再进一步讲一下难度目标的表示方法,难度目标值用nBits表示,nBits是一个无符号的32位整数,定义在src/chain.h的CBlockIndex类中:
uint32_t nBits;
这个无符号整数的最高位的1个字节代表指数(exponent),低位的3个字节代表系数(coefficient),这个记法将工作量证明的target表示为系数/指数(coefficient/exponent)的格式。
计算难度目标target的公式为:target = coefficient * 2^(8 * (exponent – 3))
例如在区块277,316中,nBits的值为 0x1903a30c,在这个区块里,0x19为指数,而 0x03a30c为系数,计算难度值:
target = 0x03a30c * 2^(0x08 * (0x19 - 0x03))
=> target = 0x03a30c * 2^(0x08 * 0x16)
=> target = 0x03a30c * 2^0xB0
按十进制计算为:
=> target = 238,348 * 2^176
=> target = 22,829,202,948,393,929,850,749,706,076,701,368,331,072,452,018,388,575,715,328
转化回十六进制后为:
=> target = 0x0000000000000003A30C00000000000000000000000000000000000000000000
上述过程就是由无符号的32位整数nBits转为难度值的详细步骤。
由无符号的32位整数nBits转为难度值的函数
(如:0x1903a30c 转为 0x0000000000000003A30C00000000000000000000000000000000000000000000 ):
// This implementation directly uses shifts instead of going
// through an intermediate MPI representation.
arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
{
int nSize = nCompact >> 24;
uint32_t nWord = nCompact & 0x007fffff;
if (nSize <= 3) {
nWord >>= 8 * (3 - nSize);
*this = nWord;
} else {
*this = nWord;
*this <<= 8 * (nSize - 3);
}
if (pfNegative)
*pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
if (pfOverflow)
*pfOverflow = nWord != 0 && ((nSize > 34) ||
(nWord > 0xff && nSize > 33) ||
(nWord > 0xffff && nSize > 32));
return *this;
}
由难度值转为无符号的32位整数nBits的函数
(如:0x0000000000000003A30C00000000000000000000000000000000000000000000 转为 0x1903a30c ):
uint32_t arith_uint256::GetCompact(bool fNegative) const
{
int nSize = (bits() + 7) / 8;
uint32_t nCompact = 0;
if (nSize <= 3) {
nCompact = GetLow64() << 8 * (3 - nSize);
} else {
arith_uint256 bn = *this >> 8 * (nSize - 3);
nCompact = bn.GetLow64();
}
// The 0x00800000 bit denotes the sign.
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
if (nCompact & 0x00800000) {
nCompact >>= 8;
nSize++;
}
assert((nCompact & ~0x007fffff) == 0);
assert(nSize < 256);
nCompact |= nSize << 24;
nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
return nCompact;
}
这两个方法定义在src/arith_uint256.h的 arith_uint256类中。
比特币nBits计算的更多相关文章
- 为什么用GPU挖比特币?
http://www.leiphone.com/gpubitcoin.html 呵呵,这么红火的东东,不了解就长不了见识. 转一下两个东东,这挖矿机天天在算什么内容,还有,当前为什么GPU比CPU有优 ...
- php小数加减精度问题,比特币计算精度问题
php小数加减精度问题,比特币计算精度问题 在php开发时,有小数加减的场景.结果发现不能够等于预想的值,bccomp比较二个高精确度数字.语法: int bccomp(string left ope ...
- 比特币pow算法介绍
Proof Of Work 工作量证明 借鉴了 哈希现金(Hashcash)-1997年 英国密码学专家亚当.贝克(Adam Back) 用工作量证明系统解决了互联网垃圾邮件问题,它要求计算机在获得发 ...
- 比特币PoW
比特币区块头结构 字段 大小(Byte) 说明 nVersion 4 区块版本号,表示本区块遵守的验证规则 hashPrevBlock 32 前一区块的哈希值,使用SHA256(SHA256(父区块头 ...
- 比特币_Bitcoin 简介
2008-11 Satoshi Nakamoto Bitcoin: A Peer-to-Peer Electronic Cash System http://p2pbucks.com/?p=99 ...
- 我被比特币撞了一下腰——记OKCoin试用体验
本博客还有大量的.NET开源技术文章,您可能感兴趣: 1.开源Math.NET基础数学类库使用系列文章:链接 2.开源C#彩票数据资料库系列文章:链接 3.开源的.NET平台ORM组件文章:链接 4. ...
- GPU---并行计算利器
转载请引用:GPU---并行计算利器 源于阿里巴巴CCO<猿来如此>分享 1 GPU是什么 如图1所示,这台PC机与普通PC机不同的是这里插了7张显卡,左下角是显卡,在中间的就是GPU芯片 ...
- 什么是比特币(bitcoin)
一.什么是比特币? 比特币是一种由开源的P2P软件产生的电子货币,是一种网络虚拟货币.比特币使用遍布整个P2P网络节点的分布式数据库来记录货币的交易,并使用密码学的设计来确保货币流通各个环节安全性.比 ...
- 将P2P虚拟货币(比特币、莱特币....)的算力用于公共的分布式计算的猜想
比特币最近几年非常火爆.发明者中本聪设计了一个特定的算法用于生成(发行)比特币,让各位玩家(矿工)用自己的CPU.显卡,或者更加专业的矿机,通过无聊的并行计算算出比特币的特定密码(挖矿).为了保证全网 ...
随机推荐
- Why do we name variables in Tensorflow?
Reference:Stack Overflow. The name parameter is optional (you can create variables and constants wit ...
- 计算广告(5)----query意图识别
目录: 一.简介: 1.用户意图识别概念 2.用户意图识别难点 3.用户意图识别分类 4.意图识别方法: (1)基于规则 (2)基于穷举 (3)基于分类模型 二.意图识别具体做法: 1.数据集 2.数 ...
- 第九届蓝桥杯国赛+第二天的第11届acm省赛的总结
第九届蓝桥杯国赛+第二天的第11届acm省赛的总结 25号坐的去北京的火车,10个小时的火车,然后挤了快两个小时的地铁,最终达到了中国矿业大学旁边的订的房间.12个小时很难受,晕车症状有点严重,吃了快 ...
- vim 操作命令大全
转子:https://www.cnblogs.com/yangjig/p/6014198.html 和 https://blog.csdn.net/u010956473/article/detail ...
- 大数据之路week06--day01(VMware的下载与安装、安装CentOS)
好了,从今天开始就开始正式的进入大数据道路的轨道上了,当然了,Java 也是需要不断地在日后进行反复地学习,熟练掌握.(这里我要说一下,Java种还有一些I/O流.Lambda表达式和一些常用工具类有 ...
- 收集Nginx-access,Nginx-error日志
1.配置Logstash [root@Logstash logstash]# vim /usr/local/logstash/config/nginx_log.conf input { beats ...
- django安装好之后,django-admin仍然不能使用的问题
我使用的是python3,python3不能找到django的正确位置.需要下面这样才能正确建立mysite python3 /usr/lib/python2./site-packages/djang ...
- C# 任务、线程、同步(二)
取消架构 1.Parallel.For()方法的取消 static void CancelParallelLoop() { var cts = new CancellationTokenSource( ...
- 5、组件注册-@Scope-设置组件作用域
5.组件注册-@Scope-设置组件作用域 IOC容器默认都是单实例的 /** * * {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SIN ...
- Ubuntu16安装fabric1.4.4环境
安装流程依照官网地址 https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html 如果需要安装最新的版本,可 ...