diff --git a/src/governance-classes.cpp b/src/governance-classes.cpp index 26b11a07e6..eb2cfd90ad 100644 --- a/src/governance-classes.cpp +++ b/src/governance-classes.cpp @@ -546,27 +546,24 @@ CAmount CSuperblock::GetPaymentsLimit(int nBlockHeight) return nPaymentsLimit; } -int CSuperblock::GetSuperblockHeight(bool last) +void CSuperblock::GetSuperblockHeight(int& nBlockHeight, int& nLastSuperblock, int& nNextSuperblock) { - // Get current block height - int nBlockHeight = (int)chainActive.Height(); - - // Get last superblock - int nLastSuperBlock = nBlockHeight - ((nBlockHeight - Params().GetConsensus().nSuperblockStartBlock) % Params().GetConsensus().nSuperblockCycle); - - // Get next superblock - int nNextSuperBlock = nLastSuperBlock + Params().GetConsensus().nSuperblockCycle; + // Get current block height if nothing was provided + if(nBlockHeight == 0) + nBlockHeight = (int)chainActive.Height(); // Special case: return first superblock if the current block height is below the first superblock during sync if(nBlockHeight < Params().GetConsensus().nSuperblockStartBlock){ - nLastSuperBlock = 0; - nNextSuperBlock = Params().GetConsensus().nSuperblockStartBlock; + nLastSuperblock = 0; + nNextSuperblock = Params().GetConsensus().nSuperblockStartBlock; + return; } - if(last) - return nLastSuperBlock; - else - return nNextSuperBlock; + // Get last superblock + nLastSuperblock = nBlockHeight - ((nBlockHeight - Params().GetConsensus().nSuperblockStartBlock) % Params().GetConsensus().nSuperblockCycle); + + // Get next superblock + nNextSuperblock = nLastSuperblock + Params().GetConsensus().nSuperblockCycle; } diff --git a/src/governance-classes.h b/src/governance-classes.h index 1c93d3b3b8..e1c4b17c73 100644 --- a/src/governance-classes.h +++ b/src/governance-classes.h @@ -178,7 +178,7 @@ public: static bool IsValidBlockHeight(int nBlockHeight); static CAmount GetPaymentsLimit(int nBlockHeight); - static int GetSuperblockHeight(bool last); + static void GetSuperblockHeight(int& nBlockHeight, int& nLastSuperblock, int& nNextSuperblock); int GetStatus() { return nStatus; } void SetStatus(int nStatusIn) { nStatus = nStatusIn; } diff --git a/src/rpcgovernance.cpp b/src/rpcgovernance.cpp index 8bcc59204d..7d68150330 100644 --- a/src/rpcgovernance.cpp +++ b/src/rpcgovernance.cpp @@ -657,19 +657,33 @@ UniValue getsuperblockbudget(const UniValue& params, bool fHelp) UniValue getsuperblockheight(const UniValue& params, bool fHelp) { - if (fHelp) { + int nBlockHeight = 0; + int nLastSuperblock, nNextSuperblock; + + if (fHelp || params.size() > 1) { throw runtime_error( "getsuperblockheight\n" "\nReturns the block height of the last/next superblock.\n" + "\nArguments:\n" + "1. index (numeric, optional, default = current blockchain height) The block index\n" "\nResult:\n" - "lastsuperblock (numeric) the height of the last superblock\n" - "nextsuperblock (numeric) the height of the next superblock\n" + "lastsuperblock (numeric) the height of the last superblock\n" + "nextsuperblock (numeric) the height of the next superblock\n" ); } + if(params.size() == 1) + nBlockHeight = params[0].get_int(); + + if (nBlockHeight < 0) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); + } + + CSuperblock::GetSuperblockHeight(nBlockHeight, nLastSuperblock, nNextSuperblock); + UniValue obj(UniValue::VOBJ); - obj.push_back(Pair("lastsuperblock", CSuperblock::GetSuperblockHeight(true))); - obj.push_back(Pair("nextsuperblock", CSuperblock::GetSuperblockHeight(false))); + obj.push_back(Pair("lastsuperblock", nLastSuperblock)); + obj.push_back(Pair("nextsuperblock", nNextSuperblock)); return obj;