// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include "bitcoinrpc.h"
+#include "rpcserver.h"
#include "chainparams.h"
-#include "db.h"
#include "init.h"
#include "net.h"
#include "main.h"
#include "miner.h"
+#ifdef ENABLE_WALLET
+#include "db.h"
#include "wallet.h"
-
+#endif
#include <stdint.h>
+#include <boost/assign/list_of.hpp>
#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"
using namespace json_spirit;
using namespace std;
-// Key used by getwork/getblocktemplate miners.
+#ifdef ENABLE_WALLET
+// Key used by getwork miners.
// Allocated in InitRPCMining, free'd in ShutdownRPCMining
static CReserveKey* pMiningKey = NULL;
delete pMiningKey; pMiningKey = NULL;
}
+#else
+void InitRPCMining()
+{
+}
+void ShutdownRPCMining()
+{
+}
+#endif
// Return average network hashes per second based on the last 'lookup' blocks,
// or from the last difficulty change 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) {
- CBlockIndex *pb = chainActive[height];
+ CBlockIndex *pb = chainActive.Tip();
+
+ if (height >= 0 && height < chainActive.Height())
+ pb = chainActive[height];
if (pb == NULL || !pb->nHeight)
return 0;
uint256 workDiff = pb->nChainWork - pb0->nChainWork;
int64_t timeDiff = maxTime - minTime;
- return (boost::int64_t)(workDiff.getdouble() / timeDiff);
+ return (int64_t)(workDiff.getdouble() / timeDiff);
}
Value getnetworkhashps(const Array& params, bool fHelp)
return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
}
-
+#ifdef ENABLE_WALLET
Value getgenerate(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
"\nArguments:\n"
"1. generate (boolean, required) Set to true to turn on generation, off to turn off.\n"
"2. genproclimit (numeric, optional) Set the processor limit for when generation is on. Can be -1 for unlimited.\n"
+ " Note: in -regtest mode, genproclimit controls how many blocks are generated immediately.\n"
"\nExamples:\n"
"\nSet the generation on with a limit of one processor\n"
+ HelpExampleCli("setgenerate", "true 1") +
+ HelpExampleRpc("setgenerate", "true, 1")
);
+ if (pwalletMain == NULL)
+ throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
+
bool fGenerate = true;
if (params.size() > 0)
fGenerate = params[0].get_bool();
+ int nGenProcLimit = -1;
if (params.size() > 1)
{
- int nGenProcLimit = params[1].get_int();
- mapArgs["-genproclimit"] = itostr(nGenProcLimit);
+ nGenProcLimit = params[1].get_int();
if (nGenProcLimit == 0)
fGenerate = false;
}
- mapArgs["-gen"] = (fGenerate ? "1" : "0");
- assert(pwalletMain != NULL);
- GenerateBitcoins(fGenerate, pwalletMain);
+ // -regtest mode: don't return until nGenProcLimit blocks are generated
+ if (fGenerate && Params().NetworkID() == CChainParams::REGTEST)
+ {
+ int nHeightStart = 0;
+ int nHeightEnd = 0;
+ int nHeight = 0;
+ int nGenerate = (nGenProcLimit > 0 ? nGenProcLimit : 1);
+ { // Don't keep cs_main locked
+ LOCK(cs_main);
+ nHeightStart = chainActive.Height();
+ nHeight = nHeightStart;
+ nHeightEnd = nHeightStart+nGenerate;
+ }
+ int nHeightLast = -1;
+ while (nHeight < nHeightEnd)
+ {
+ if (nHeightLast != nHeight)
+ {
+ nHeightLast = nHeight;
+ GenerateBitcoins(fGenerate, pwalletMain, 1);
+ }
+ MilliSleep(1);
+ { // Don't keep cs_main locked
+ LOCK(cs_main);
+ nHeight = chainActive.Height();
+ }
+ }
+ }
+ else // Not -regtest: start generate thread, return immediately
+ {
+ mapArgs["-gen"] = (fGenerate ? "1" : "0");
+ mapArgs ["-genproclimit"] = itostr(nGenProcLimit);
+ GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);
+ }
+
return Value::null;
}
-
Value gethashespersec(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
);
if (GetTimeMillis() - nHPSTimerStart > 8000)
- return (boost::int64_t)0;
- return (boost::int64_t)dHashesPerSec;
+ return (int64_t)0;
+ return (int64_t)dHashesPerSec;
}
+#endif
Value getmininginfo(const Array& params, bool fHelp)
obj.push_back(Pair("currentblocktx", (uint64_t)nLastBlockTx));
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
obj.push_back(Pair("errors", GetWarnings("statusbar")));
- obj.push_back(Pair("generate", getgenerate(params, false)));
obj.push_back(Pair("genproclimit", (int)GetArg("-genproclimit", -1)));
- obj.push_back(Pair("hashespersec", gethashespersec(params, false)));
obj.push_back(Pair("networkhashps", getnetworkhashps(params, false)));
obj.push_back(Pair("pooledtx", (uint64_t)mempool.size()));
obj.push_back(Pair("testnet", TestNet()));
+#ifdef ENABLE_WALLET
+ obj.push_back(Pair("generate", getgenerate(params, false)));
+ obj.push_back(Pair("hashespersec", gethashespersec(params, false)));
+#endif
return obj;
}
+#ifdef ENABLE_WALLET
Value getwork(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 1)
char phash1[64];
FormatHashBuffers(pblock, pmidstate, pdata, phash1);
- uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
+ uint256 hashTarget = uint256().SetCompact(pblock->nBits);
Object result;
result.push_back(Pair("midstate", HexStr(BEGIN(pmidstate), END(pmidstate)))); // deprecated
return CheckWork(pblock, *pwalletMain, *pMiningKey);
}
}
-
+#endif
Value getblocktemplate(const Array& params, bool fHelp)
{
Object aux;
aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
- uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
+ uint256 hashTarget = uint256().SetCompact(pblock->nBits);
static Array aMutable;
if (aMutable.empty())
return Value::null;
}
+
+Value estimatefee(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 1)
+ throw runtime_error(
+ "estimatefee nblocks\n"
+ "\nEstimates the approximate fee per kilobyte\n"
+ "needed for a transaction to get confirmed\n"
+ "within nblocks blocks.\n"
+ "\nArguments:\n"
+ "1. nblocks (numeric)\n"
+ "\nResult:\n"
+ "n : (numeric) estimated fee-per-kilobyte\n"
+ "\n"
+ "-1.0 is returned if not enough transactions and\n"
+ "blocks have been observed to make an estimate.\n"
+ "\nExample:\n"
+ + HelpExampleCli("estimatefee", "6")
+ );
+
+ RPCTypeCheck(params, boost::assign::list_of(int_type));
+
+ int nBlocks = params[0].get_int();
+ if (nBlocks < 1)
+ nBlocks = 1;
+
+ CFeeRate feeRate = mempool.estimateFee(nBlocks);
+ if (feeRate == CFeeRate(0))
+ return -1.0;
+
+ return ValueFromAmount(feeRate.GetFeePerK());
+}
+
+Value estimatepriority(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 1)
+ throw runtime_error(
+ "estimatepriority nblocks\n"
+ "\nEstimates the approximate priority\n"
+ "a zero-fee transaction needs to get confirmed\n"
+ "within nblocks blocks.\n"
+ "\nArguments:\n"
+ "1. nblocks (numeric)\n"
+ "\nResult:\n"
+ "n : (numeric) estimated priority\n"
+ "\n"
+ "-1.0 is returned if not enough transactions and\n"
+ "blocks have been observed to make an estimate.\n"
+ "\nExample:\n"
+ + HelpExampleCli("estimatepriority", "6")
+ );
+
+ RPCTypeCheck(params, boost::assign::list_of(int_type));
+
+ int nBlocks = params[0].get_int();
+ if (nBlocks < 1)
+ nBlocks = 1;
+
+ return mempool.estimatePriority(nBlocks);
+}