Implement new difficulty algorithm (#931)

The algorithm is based on DigiShield v3/v4.
This commit is contained in:
Jack Grigg
2016-06-08 12:30:32 +12:00
parent 542da618df
commit f2c48e15e5
4 changed files with 45 additions and 36 deletions

View File

@@ -33,7 +33,7 @@ using namespace std;
/**
* Return average network hashes per second based on the last 'lookup' blocks,
* or from the last difficulty change if 'lookup' is nonpositive.
* or over the difficulty averaging window if 'lookup' is nonpositive.
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
*/
Value GetNetworkHashPS(int lookup, int height) {
@@ -45,9 +45,13 @@ Value GetNetworkHashPS(int lookup, int height) {
if (pb == NULL || !pb->nHeight)
return 0;
// If lookup is -1, then use blocks since last difficulty change.
// If lookup is -1, then use difficulty averaging window.
if (lookup <= 0)
lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
lookup = pb->nHeight - Params().GetConsensus().nPowAveragingWindow;
// If lookup is still negative, then use blocks since genesis.
if (lookup <= 0)
lookup = pb->nHeight;
// If lookup is larger than chain, then set it to chain length.
if (lookup > pb->nHeight)
@@ -79,10 +83,10 @@ Value getnetworkhashps(const Array& params, bool fHelp)
throw runtime_error(
"getnetworkhashps ( blocks height )\n"
"\nReturns the estimated network hashes per second based on the last n blocks.\n"
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
"Pass in [blocks] to override # of blocks, -1 specifies over difficulty averaging window.\n"
"Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
"\nArguments:\n"
"1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
"1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks over difficulty averaging window.\n"
"2. height (numeric, optional, default=-1) To estimate at the time of the given height.\n"
"\nResult:\n"
"x (numeric) Hashes per second estimated\n"