Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next target difficulty to RPC output #1615

Merged
merged 2 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ extern double GetTotalBalance();
extern std::string PubKeyToAddress(const CScript& scriptPubKey);
extern const CBlockIndex* GetHistoricalMagnitude(const NN::MiningId mining_id);
std::string GetCommandNonce(std::string command);
double GetDifficulty(const CBlockIndex* blockindex);

unsigned int nNodeLifespan;

Expand Down Expand Up @@ -240,6 +239,60 @@ double GetEstimatedNetworkWeight(unsigned int nPoSInterval)
return result;
}

double GetDifficulty(const CBlockIndex* blockindex)
{
// Floating point number that is a multiple of the minimum difficulty,
// minimum difficulty = 1.0.
if (blockindex == NULL)
{
if (pindexBest == NULL)
return 1.0;
else
blockindex = GetLastBlockIndex(pindexBest, false);
}

int nShift = (blockindex->nBits >> 24) & 0xff;

double dDiff =
(double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);

while (nShift < 29)
{
dDiff *= 256.0;
nShift++;
}
while (nShift > 29)
{
dDiff /= 256.0;
nShift--;
}

return dDiff;
}

double GetBlockDifficulty(unsigned int nBits)
{
// Floating point number that is a multiple of the minimum difficulty,
// minimum difficulty = 1.0.
int nShift = (nBits >> 24) & 0xff;

double dDiff =
(double)0x0000ffff / (double)(nBits & 0x00ffffff);

while (nShift < 29)
{
dDiff *= 256.0;
nShift++;
}
while (nShift > 29)
{
dDiff /= 256.0;
nShift--;
}

return dDiff;
}

double GetAverageDifficulty(unsigned int nPoSInterval)
{
/*
Expand Down
3 changes: 2 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ void PrintBlockTree();
bool ProcessMessages(CNode* pfrom);
bool SendMessages(CNode* pto, bool fSendTrickle);
bool LoadExternalBlockFile(FILE* fileIn);
double GetBlockDifficulty(unsigned int nBits);
std::string ExtractXML(const std::string& XMLdata, const std::string& key, const std::string& key_end);

std::string GetCurrentOverviewTabPoll();
Expand All @@ -259,6 +258,8 @@ bool OutOfSyncByAge();
bool IsSuperBlock(CBlockIndex* pIndex);

double GetEstimatedNetworkWeight(unsigned int nPoSInterval = 40);
double GetDifficulty(const CBlockIndex* blockindex = NULL);
double GetBlockDifficulty(unsigned int nBits);
double GetAverageDifficulty(unsigned int nPoSInterval = 40);
// Note that dDiff cannot be = 0 normally. This is set as default because you can't specify the output of
// GetAverageDifficulty(nPosInterval) = to dDiff here.
Expand Down
1 change: 0 additions & 1 deletion src/qt/clientmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <QTimer>

static const int64_t nClientStartupTime = GetTime();
double GetDifficulty(const CBlockIndex* blockindex = NULL);
extern ConvergedScraperStats ConvergedScraperStatsCache;
extern CCriticalSection cs_ConvergedScraperStatsCache;

Expand Down
76 changes: 10 additions & 66 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,63 +55,6 @@ extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&

BlockFinder RPCBlockFinder;

double GetDifficulty(const CBlockIndex* blockindex)
{
// Floating point number that is a multiple of the minimum difficulty,
// minimum difficulty = 1.0.
if (blockindex == NULL)
{
if (pindexBest == NULL)
return 1.0;
else
blockindex = GetLastBlockIndex(pindexBest, false);
}

int nShift = (blockindex->nBits >> 24) & 0xff;

double dDiff =
(double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);

while (nShift < 29)
{
dDiff *= 256.0;
nShift++;
}
while (nShift > 29)
{
dDiff /= 256.0;
nShift--;
}

return dDiff;
}




double GetBlockDifficulty(unsigned int nBits)
{
// Floating point number that is a multiple of the minimum difficulty,
// minimum difficulty = 1.0.
int nShift = (nBits >> 24) & 0xff;

double dDiff =
(double)0x0000ffff / (double)(nBits & 0x00ffffff);

while (nShift < 29)
{
dDiff *= 256.0;
nShift++;
}
while (nShift > 29)
{
dDiff /= 256.0;
nShift--;
}

return dDiff;
}

namespace {
UniValue ClaimToJson(const NN::Claim& claim)
{
Expand Down Expand Up @@ -310,8 +253,9 @@ UniValue getdifficulty(const UniValue& params, bool fHelp)
LOCK(cs_main);

UniValue obj(UniValue::VOBJ);
obj.pushKV("proof-of-work", GetDifficulty());
obj.pushKV("proof-of-stake", GetDifficulty(GetLastBlockIndex(pindexBest, true)));
obj.pushKV("current", GetDifficulty(GetLastBlockIndex(pindexBest, true)));
obj.pushKV("target", GetBlockDifficulty(GetNextTargetRequired(pindexBest)));

return obj;
}

Expand Down Expand Up @@ -1843,13 +1787,13 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)

UniValue res(UniValue::VOBJ), diff(UniValue::VOBJ);

res.pushKV("blocks", nBestHeight);
res.pushKV("moneysupply", ValueFromAmount(pindexBest->nMoneySupply));
diff.pushKV("proof-of-work", GetDifficulty());
diff.pushKV("proof-of-stake", GetDifficulty(GetLastBlockIndex(pindexBest, true)));
res.pushKV("difficulty", diff);
res.pushKV("testnet", fTestNet);
res.pushKV("errors", GetWarnings("statusbar"));
res.pushKV("blocks", nBestHeight);
res.pushKV("moneysupply", ValueFromAmount(pindexBest->nMoneySupply));
diff.pushKV("current", GetDifficulty(GetLastBlockIndex(pindexBest, true)));
diff.pushKV("target", GetBlockDifficulty(GetNextTargetRequired(pindexBest)));
res.pushKV("difficulty", diff);
res.pushKV("testnet", fTestNet);
res.pushKV("errors", GetWarnings("statusbar"));

return res;
}
Expand Down
9 changes: 6 additions & 3 deletions src/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@ UniValue getmininginfo(const UniValue& params, bool fHelp)
int64_t nTime = GetAdjustedTime();
uint64_t nWeight = 0;
double nNetworkWeight = 0;
double nDifficulty = 0;
double nCurrentDiff = 0;
double nTargetDiff = 0;
uint64_t nExpectedTime = 0;
{
LOCK2(cs_main, pwalletMain->cs_wallet);
pwalletMain->GetStakeWeight(nWeight);

nNetworkWeight = GetEstimatedNetworkWeight();
nDifficulty = GetDifficulty(GetLastBlockIndex(pindexBest, true));
nCurrentDiff = GetDifficulty(GetLastBlockIndex(pindexBest, true));
nTargetDiff = GetBlockDifficulty(GetNextTargetRequired(pindexBest));
nExpectedTime = GetEstimatedTimetoStake();
}

obj.pushKV("blocks", nBestHeight);
diff.pushKV("proof-of-stake", nDifficulty);
diff.pushKV("current", nCurrentDiff);
diff.pushKV("target", nTargetDiff);

{ LOCK(MinerStatus.lock);
// not using real weigh to not break calculation
Expand Down
1 change: 0 additions & 1 deletion src/rpcserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ extern const CRPCTable tableRPC;
extern int64_t nWalletUnlockTime;
extern int64_t AmountFromValue(const UniValue& value);
extern UniValue ValueFromAmount(int64_t amount);
extern double GetDifficulty(const CBlockIndex* blockindex = NULL);

extern std::string HelpRequiringPassphrase();
extern void EnsureWalletIsUnlocked();
Expand Down
5 changes: 3 additions & 2 deletions src/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ UniValue getinfo(const UniValue& params, bool fHelp)
obj.pushKV("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string()));
obj.pushKV("ip", addrSeenByPeer.ToStringIP());

diff.pushKV("proof-of-stake", GetDifficulty(GetLastBlockIndex(pindexBest, true)));
diff.pushKV("current", GetDifficulty(GetLastBlockIndex(pindexBest, true)));
diff.pushKV("target", GetBlockDifficulty(GetNextTargetRequired(pindexBest)));
obj.pushKV("difficulty", diff);

obj.pushKV("testnet", fTestNet);
Expand Down Expand Up @@ -385,7 +386,7 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp)
CWalletTx wtx;
if (params.size() > 2 && !params[2].isNull() && !params[2].get_str().empty())
wtx.mapValue["comment"] = params[2].get_str();
if (params.size() > 3 && !params[3].isNull() && !params[3].get_str().empty())
if (params.size() > 3 && !params[3].isNull() && !params[3].get_str().empty())
wtx.mapValue["to"] = params[3].get_str();
if (params.size() > 4 && !params[4].isNull() && !params[4].get_str().empty())
wtx.hashBoinc += "<MESSAGE>" + MakeSafeMessage(params[4].get_str()) + "</MESSAGE>";
Expand Down