Changeset View
Changeset View
Standalone View
Standalone View
src/pow/daa.cpp
// Copyright (c) 2017-2020 The Bitcoin developers | // Copyright (c) 2017-2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <pow/daa.h> | #include <pow/daa.h> | ||||
#include <arith_uint256.h> | #include <arith_uint256.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <consensus/params.h> | #include <consensus/params.h> | ||||
#include <pow/util.h> | |||||
/** | /** | ||||
* Compute a target based on the work done between 2 blocks and the time | * Compute a target based on the work done between 2 blocks and the time | ||||
* required to produce that work. | * required to produce that work. | ||||
*/ | */ | ||||
static arith_uint256 ComputeTarget(const CBlockIndex *pindexFirst, | static arith_uint256 ComputeTarget(const CBlockIndex *pindexFirst, | ||||
const CBlockIndex *pindexLast, | const CBlockIndex *pindexLast, | ||||
const Consensus::Params ¶ms) { | const Consensus::Params ¶ms) { | ||||
Show All 13 Lines | int64_t nActualTimespan = | ||||
int64_t(pindexLast->nTime) - int64_t(pindexFirst->nTime); | int64_t(pindexLast->nTime) - int64_t(pindexFirst->nTime); | ||||
if (nActualTimespan > 288 * params.nPowTargetSpacing) { | if (nActualTimespan > 288 * params.nPowTargetSpacing) { | ||||
nActualTimespan = 288 * params.nPowTargetSpacing; | nActualTimespan = 288 * params.nPowTargetSpacing; | ||||
} else if (nActualTimespan < 72 * params.nPowTargetSpacing) { | } else if (nActualTimespan < 72 * params.nPowTargetSpacing) { | ||||
nActualTimespan = 72 * params.nPowTargetSpacing; | nActualTimespan = 72 * params.nPowTargetSpacing; | ||||
} | } | ||||
work /= nActualTimespan; | work /= nActualTimespan; | ||||
return ComputeTargetFromWork(work); | |||||
/** | |||||
* We need to compute T = (2^256 / W) - 1 but 2^256 doesn't fit in 256 bits. | |||||
* By expressing 1 as W / W, we get (2^256 - W) / W, and we can compute | |||||
* 2^256 - W as the complement of W. | |||||
*/ | |||||
return (-work) / work; | |||||
} | } | ||||
/** | /** | ||||
* To reduce the impact of timestamp manipulation, we select the block we are | * To reduce the impact of timestamp manipulation, we select the block we are | ||||
* basing our computation on via a median of 3. | * basing our computation on via a median of 3. | ||||
*/ | */ | ||||
static const CBlockIndex *GetSuitableBlock(const CBlockIndex *pindex) { | static const CBlockIndex *GetSuitableBlock(const CBlockIndex *pindex) { | ||||
assert(pindex->nHeight >= 3); | assert(pindex->nHeight >= 3); | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |