Changeset View
Changeset View
Standalone View
Standalone View
src/pow/util.h
- This file was added.
// Copyright (c) 2020 The Bitcoin developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#ifndef BITCOIN_POW_UTIL_H | |||||
#define BITCOIN_POW_UTIL_H | |||||
#include <arith_uint256.h> | |||||
/** | |||||
* Compute a target based on the work require for the next block. | |||||
*/ | |||||
static arith_uint256 ComputeTargetFromWork(const arith_uint256 &work) { | |||||
// Special case 0 to avoid division by zero. | |||||
if (work == 0) { | |||||
return -arith_uint256(1); | |||||
} | |||||
/** | |||||
* 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; | |||||
} | |||||
#endif // BITCOIN_POW_UTIL_H |