2012-08-21 08:21:33 +02:00
// Copyright (c) 2010 Satoshi Nakamoto
2016-12-31 19:01:21 +01:00
// Copyright (c) 2009-2016 The Bitcoin Core developers
2014-11-20 03:19:29 +01:00
// Distributed under the MIT software license, see the accompanying
2012-08-21 08:21:33 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2016-03-12 17:41:51 +01:00
# include "base58.h"
2014-10-23 02:05:11 +02:00
# include "amount.h"
2015-07-05 14:17:46 +02:00
# include "chain.h"
2013-05-07 15:16:25 +02:00
# include "chainparams.h"
2015-01-24 15:29:29 +01:00
# include "consensus/consensus.h"
2016-04-24 01:30:20 +02:00
# include "consensus/params.h"
2015-01-24 15:57:12 +01:00
# include "consensus/validation.h"
2014-09-14 12:43:56 +02:00
# include "core_io.h"
2012-08-21 08:21:33 +02:00
# include "init.h"
2016-12-02 01:06:41 +01:00
# include "validation.h"
2013-07-31 15:43:35 +02:00
# include "miner.h"
2015-01-24 15:29:29 +01:00
# include "net.h"
2014-03-10 16:46:53 +01:00
# include "pow.h"
2016-01-15 01:55:17 +01:00
# include "rpc/server.h"
2015-07-05 14:17:46 +02:00
# include "txmempool.h"
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
# include "util.h"
2015-07-05 14:17:46 +02:00
# include "utilstrencodings.h"
2015-03-24 22:14:44 +01:00
# include "validationinterface.h"
2014-05-10 14:54:20 +02:00
2016-06-18 19:38:28 +02:00
# include <memory>
2013-04-13 07:13:08 +02:00
# include <stdint.h>
2014-03-17 13:19:54 +01:00
# include <boost/assign/list_of.hpp>
2015-07-01 08:32:30 +02:00
# include <boost/shared_ptr.hpp>
2014-05-10 14:54:20 +02:00
2015-09-04 16:11:34 +02:00
# include <univalue.h>
2012-08-21 08:21:33 +02:00
using namespace std ;
2014-11-20 03:19:29 +01:00
/**
* 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 .
*/
2015-05-13 21:29:19 +02:00
UniValue GetNetworkHashPS ( int lookup , int height ) {
2013-12-29 12:14:06 +01:00
CBlockIndex * pb = chainActive . Tip ( ) ;
if ( height > = 0 & & height < chainActive . Height ( ) )
pb = chainActive [ height ] ;
2013-05-17 12:57:05 +02:00
if ( pb = = NULL | | ! pb - > nHeight )
return 0 ;
// If lookup is -1, then use blocks since last difficulty change.
if ( lookup < = 0 )
2015-04-10 18:35:09 +02:00
lookup = pb - > nHeight % Params ( ) . GetConsensus ( ) . DifficultyAdjustmentInterval ( ) + 1 ;
2013-05-17 12:57:05 +02:00
// If lookup is larger than chain, then set it to chain length.
if ( lookup > pb - > nHeight )
lookup = pb - > nHeight ;
CBlockIndex * pb0 = pb ;
2013-04-13 07:13:08 +02:00
int64_t minTime = pb0 - > GetBlockTime ( ) ;
int64_t maxTime = minTime ;
2013-05-17 12:57:05 +02:00
for ( int i = 0 ; i < lookup ; i + + ) {
pb0 = pb0 - > pprev ;
2013-04-13 07:13:08 +02:00
int64_t time = pb0 - > GetBlockTime ( ) ;
2013-05-17 12:57:05 +02:00
minTime = std : : min ( time , minTime ) ;
maxTime = std : : max ( time , maxTime ) ;
}
// In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
if ( minTime = = maxTime )
return 0 ;
2014-12-16 15:43:03 +01:00
arith_uint256 workDiff = pb - > nChainWork - pb0 - > nChainWork ;
2013-04-13 07:13:08 +02:00
int64_t timeDiff = maxTime - minTime ;
2013-05-17 12:57:05 +02:00
2016-02-08 16:49:27 +01:00
return workDiff . getdouble ( ) / timeDiff ;
2013-05-17 12:57:05 +02:00
}
2016-09-22 09:46:41 +02:00
UniValue getnetworkhashps ( const JSONRPCRequest & request )
2013-05-17 12:57:05 +02:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) > 2 )
2013-05-17 12:57:05 +02:00
throw runtime_error (
2016-11-21 14:03:09 +01:00
" getnetworkhashps ( nblocks height ) \n "
2013-10-29 12:29:44 +01:00
" \n Returns the estimated network hashes per second based on the last n blocks. \n "
2013-05-17 12:57:05 +02:00
" Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change. \n "
2013-10-29 12:29:44 +01:00
" Pass in [height] to estimate the network speed at the time when a certain block was found. \n "
" \n Arguments: \n "
2016-11-21 14:03:09 +01:00
" 1. nblocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change. \n "
2016-12-09 18:06:42 +01:00
" 2. height (numeric, optional, default=-1) To estimate at the time of the given height. \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" x (numeric) Hashes per second estimated \n "
" \n Examples: \n "
+ HelpExampleCli ( " getnetworkhashps " , " " )
+ HelpExampleRpc ( " getnetworkhashps " , " " )
) ;
2013-05-17 12:57:05 +02:00
2014-10-19 10:46:17 +02:00
LOCK ( cs_main ) ;
2016-09-22 09:46:41 +02:00
return GetNetworkHashPS ( request . params . size ( ) > 0 ? request . params [ 0 ] . get_int ( ) : 120 , request . params . size ( ) > 1 ? request . params [ 1 ] . get_int ( ) : - 1 ) ;
2013-05-17 12:57:05 +02:00
}
2016-03-12 17:41:51 +01:00
UniValue generateBlocks ( boost : : shared_ptr < CReserveScript > coinbaseScript , int nGenerate , uint64_t nMaxTries , bool keepScript )
2015-04-01 05:28:28 +02:00
{
2016-03-09 22:30:15 +01:00
static const int nInnerLoopCount = 0x10000 ;
2015-04-01 05:28:28 +02:00
int nHeightStart = 0 ;
int nHeightEnd = 0 ;
int nHeight = 0 ;
{ // Don't keep cs_main locked
LOCK ( cs_main ) ;
nHeightStart = chainActive . Height ( ) ;
nHeight = nHeightStart ;
nHeightEnd = nHeightStart + nGenerate ;
}
2015-04-10 07:33:06 +02:00
unsigned int nExtraNonce = 0 ;
2015-05-10 14:48:35 +02:00
UniValue blockHashes ( UniValue : : VARR ) ;
2015-04-10 07:33:06 +02:00
while ( nHeight < nHeightEnd )
{
2015-12-15 21:26:44 +01:00
std : : unique_ptr < CBlockTemplate > pblocktemplate ( BlockAssembler ( Params ( ) ) . CreateNewBlock ( coinbaseScript - > reserveScript ) ) ;
2015-04-10 07:33:06 +02:00
if ( ! pblocktemplate . get ( ) )
2015-04-10 12:49:01 +02:00
throw JSONRPCError ( RPC_INTERNAL_ERROR , " Couldn't create new block " ) ;
2015-04-10 07:33:06 +02:00
CBlock * pblock = & pblocktemplate - > block ;
{
LOCK ( cs_main ) ;
IncrementExtraNonce ( pblock , chainActive . Tip ( ) , nExtraNonce ) ;
}
2016-03-09 22:30:15 +01:00
while ( nMaxTries > 0 & & pblock - > nNonce < nInnerLoopCount & & ! CheckProofOfWork ( pblock - > GetHash ( ) , pblock - > nBits , Params ( ) . GetConsensus ( ) ) ) {
2015-04-10 07:33:06 +02:00
+ + pblock - > nNonce ;
2016-03-09 22:30:15 +01:00
- - nMaxTries ;
}
if ( nMaxTries = = 0 ) {
break ;
}
if ( pblock - > nNonce = = nInnerLoopCount ) {
continue ;
2015-04-10 07:33:06 +02:00
}
2016-12-04 09:17:30 +01:00
std : : shared_ptr < const CBlock > shared_pblock = std : : make_shared < const CBlock > ( * pblock ) ;
2016-12-04 09:23:17 +01:00
if ( ! ProcessNewBlock ( Params ( ) , shared_pblock , true , NULL ) )
2016-11-09 02:39:44 +01:00
throw JSONRPCError ( RPC_INTERNAL_ERROR , " ProcessNewBlock, block not accepted " ) ;
2015-04-01 05:28:28 +02:00
+ + nHeight ;
2015-04-10 07:33:06 +02:00
blockHashes . push_back ( pblock - > GetHash ( ) . GetHex ( ) ) ;
2015-07-01 08:32:30 +02:00
2016-03-12 17:41:51 +01:00
//mark script as important because it was used at least for one coinbase output if the script came from the wallet
if ( keepScript )
{
coinbaseScript - > KeepScript ( ) ;
}
2015-04-01 05:28:28 +02:00
}
return blockHashes ;
}
2012-08-21 08:21:33 +02:00
2016-09-22 09:46:41 +02:00
UniValue generate ( const JSONRPCRequest & request )
2016-03-12 17:41:51 +01:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 )
2016-03-12 17:41:51 +01:00
throw runtime_error (
2016-11-21 14:03:09 +01:00
" generate nblocks ( maxtries ) \n "
" \n Mine up to nblocks blocks immediately (before the RPC call returns) \n "
2016-03-12 17:41:51 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. nblocks (numeric, required) How many blocks are generated immediately. \n "
2016-03-12 17:41:51 +01:00
" 2. maxtries (numeric, optional) How many iterations to try (default = 1000000). \n "
2016-12-22 05:26:03 +01:00
" \n Result: \n "
2016-03-12 17:41:51 +01:00
" [ blockhashes ] (array) hashes of blocks generated \n "
" \n Examples: \n "
" \n Generate 11 blocks \n "
+ HelpExampleCli ( " generate " , " 11 " )
) ;
2016-09-22 09:46:41 +02:00
int nGenerate = request . params [ 0 ] . get_int ( ) ;
2016-03-12 17:41:51 +01:00
uint64_t nMaxTries = 1000000 ;
2016-09-22 09:46:41 +02:00
if ( request . params . size ( ) > 1 ) {
nMaxTries = request . params [ 1 ] . get_int ( ) ;
2016-03-12 17:41:51 +01:00
}
boost : : shared_ptr < CReserveScript > coinbaseScript ;
GetMainSignals ( ) . ScriptForMining ( coinbaseScript ) ;
// If the keypool is exhausted, no script is returned at all. Catch this.
if ( ! coinbaseScript )
throw JSONRPCError ( RPC_WALLET_KEYPOOL_RAN_OUT , " Error: Keypool ran out, please call keypoolrefill first " ) ;
//throw an error if no script was provided
if ( coinbaseScript - > reserveScript . empty ( ) )
throw JSONRPCError ( RPC_INTERNAL_ERROR , " No coinbase script available (mining requires a wallet) " ) ;
return generateBlocks ( coinbaseScript , nGenerate , nMaxTries , true ) ;
}
2016-09-22 09:46:41 +02:00
UniValue generatetoaddress ( const JSONRPCRequest & request )
2016-03-12 17:41:51 +01:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 2 | | request . params . size ( ) > 3 )
2016-03-12 17:41:51 +01:00
throw runtime_error (
2016-11-21 14:03:09 +01:00
" generatetoaddress nblocks address (maxtries) \n "
2016-03-12 17:41:51 +01:00
" \n Mine blocks immediately to a specified address (before the RPC call returns) \n "
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. nblocks (numeric, required) How many blocks are generated immediately. \n "
" 2. address (string, required) The address to send the newly generated bitcoin to. \n "
2016-03-12 17:41:51 +01:00
" 3. maxtries (numeric, optional) How many iterations to try (default = 1000000). \n "
2016-12-22 05:26:03 +01:00
" \n Result: \n "
2016-03-12 17:41:51 +01:00
" [ blockhashes ] (array) hashes of blocks generated \n "
" \n Examples: \n "
" \n Generate 11 blocks to myaddress \n "
+ HelpExampleCli ( " generatetoaddress " , " 11 \" myaddress \" " )
) ;
2016-09-22 09:46:41 +02:00
int nGenerate = request . params [ 0 ] . get_int ( ) ;
2016-03-12 17:41:51 +01:00
uint64_t nMaxTries = 1000000 ;
2016-09-22 09:46:41 +02:00
if ( request . params . size ( ) > 2 ) {
nMaxTries = request . params [ 2 ] . get_int ( ) ;
2016-03-12 17:41:51 +01:00
}
2016-09-22 09:46:41 +02:00
CBitcoinAddress address ( request . params [ 1 ] . get_str ( ) ) ;
2016-03-12 17:41:51 +01:00
if ( ! address . IsValid ( ) )
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Error: Invalid address " ) ;
boost : : shared_ptr < CReserveScript > coinbaseScript ( new CReserveScript ( ) ) ;
coinbaseScript - > reserveScript = GetScriptForDestination ( address . Get ( ) ) ;
return generateBlocks ( coinbaseScript , nGenerate , nMaxTries , false ) ;
}
2016-09-22 09:46:41 +02:00
UniValue getmininginfo ( const JSONRPCRequest & request )
2012-08-21 08:21:33 +02:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2012-08-21 08:21:33 +02:00
throw runtime_error (
" getmininginfo \n "
2013-10-29 12:29:44 +01:00
" \n Returns a json object containing mining-related information. "
" \n Result: \n "
" { \n "
" \" blocks \" : nnn, (numeric) The current block \n "
" \" currentblocksize \" : nnn, (numeric) The last block size \n "
2016-07-18 19:28:26 +02:00
" \" currentblockweight \" : nnn, (numeric) The last block weight \n "
2013-10-29 12:29:44 +01:00
" \" currentblocktx \" : nnn, (numeric) The last block transaction \n "
" \" difficulty \" : xxx.xxxxx (numeric) The current difficulty \n "
2016-08-04 23:33:59 +02:00
" \" errors \" : \" ... \" (string) Current errors \n "
" \" networkhashps \" : nnn, (numeric) The network hashes per second \n "
2016-10-27 12:13:03 +02:00
" \" pooledtx \" : n (numeric) The size of the mempool \n "
2016-08-04 23:33:59 +02:00
" \" chain \" : \" xxxx \" , (string) current network name as defined in BIP70 (main, test, regtest) \n "
2013-10-29 12:29:44 +01:00
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getmininginfo " , " " )
+ HelpExampleRpc ( " getmininginfo " , " " )
) ;
2012-08-21 08:21:33 +02:00
2014-10-19 10:46:17 +02:00
LOCK ( cs_main ) ;
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2013-10-10 23:07:44 +02:00
obj . push_back ( Pair ( " blocks " , ( int ) chainActive . Height ( ) ) ) ;
2013-04-28 17:37:50 +02:00
obj . push_back ( Pair ( " currentblocksize " , ( uint64_t ) nLastBlockSize ) ) ;
2016-07-18 19:28:26 +02:00
obj . push_back ( Pair ( " currentblockweight " , ( uint64_t ) nLastBlockWeight ) ) ;
2013-04-28 17:37:50 +02:00
obj . push_back ( Pair ( " currentblocktx " , ( uint64_t ) nLastBlockTx ) ) ;
obj . push_back ( Pair ( " difficulty " , ( double ) GetDifficulty ( ) ) ) ;
obj . push_back ( Pair ( " errors " , GetWarnings ( " statusbar " ) ) ) ;
2016-09-22 09:46:41 +02:00
obj . push_back ( Pair ( " networkhashps " , getnetworkhashps ( request ) ) ) ;
2013-04-28 17:37:50 +02:00
obj . push_back ( Pair ( " pooledtx " , ( uint64_t ) mempool . size ( ) ) ) ;
2014-06-12 14:52:12 +02:00
obj . push_back ( Pair ( " chain " , Params ( ) . NetworkIDString ( ) ) ) ;
2012-08-21 08:21:33 +02:00
return obj ;
}
2014-12-01 13:51:45 +01:00
// NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
2016-09-22 09:46:41 +02:00
UniValue prioritisetransaction ( const JSONRPCRequest & request )
2012-07-11 20:52:41 +02:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 3 )
2012-07-11 20:52:41 +02:00
throw runtime_error (
" prioritisetransaction <txid> <priority delta> <fee delta> \n "
2014-07-15 02:11:55 +02:00
" Accepts the transaction into mined blocks at a higher (or lower) priority \n "
" \n Arguments: \n "
" 1. \" txid \" (string, required) The transaction id. \n "
2016-12-09 18:06:42 +01:00
" 2. priority_delta (numeric, required) The priority to add or subtract. \n "
2014-07-15 02:11:55 +02:00
" The transaction selection algorithm considers the tx as it would have a higher priority. \n "
" (priority of a transaction is calculated: coinage * value_in_satoshis / txsize) \n "
2016-12-09 18:06:42 +01:00
" 3. fee_delta (numeric, required) The fee value (in satoshis) to add (or subtract, if negative). \n "
2014-07-15 02:11:55 +02:00
" The fee is not actually paid, only the algorithm for selecting transactions into a block \n "
" considers the transaction as it would have paid a higher (or lower) fee. \n "
2016-12-22 05:26:03 +01:00
" \n Result: \n "
2014-07-15 02:11:55 +02:00
" true (boolean) Returns true \n "
" \n Examples: \n "
2014-12-01 13:51:45 +01:00
+ HelpExampleCli ( " prioritisetransaction " , " \" txid \" 0.0 10000 " )
+ HelpExampleRpc ( " prioritisetransaction " , " \" txid \" , 0.0, 10000 " )
2014-07-15 02:11:55 +02:00
) ;
2012-07-11 20:52:41 +02:00
2014-10-19 10:46:17 +02:00
LOCK ( cs_main ) ;
2014-07-15 02:11:55 +02:00
2016-09-22 09:46:41 +02:00
uint256 hash = ParseHashStr ( request . params [ 0 ] . get_str ( ) , " txid " ) ;
CAmount nAmount = request . params [ 2 ] . get_int64 ( ) ;
2014-07-15 02:11:55 +02:00
2016-09-22 09:46:41 +02:00
mempool . PrioritiseTransaction ( hash , request . params [ 0 ] . get_str ( ) , request . params [ 1 ] . get_real ( ) , nAmount ) ;
2012-07-11 20:52:41 +02:00
return true ;
}
2014-10-30 03:56:33 +01:00
// NOTE: Assumes a conclusive result; if result is inconclusive, it must be handled by caller
2015-05-13 21:29:19 +02:00
static UniValue BIP22ValidationResult ( const CValidationState & state )
2014-10-30 03:56:33 +01:00
{
if ( state . IsValid ( ) )
2015-05-18 14:02:18 +02:00
return NullUniValue ;
2014-10-30 03:56:33 +01:00
std : : string strRejectReason = state . GetRejectReason ( ) ;
if ( state . IsError ( ) )
throw JSONRPCError ( RPC_VERIFY_ERROR , strRejectReason ) ;
if ( state . IsInvalid ( ) )
{
if ( strRejectReason . empty ( ) )
return " rejected " ;
return strRejectReason ;
}
// Should be impossible
return " valid? " ;
}
2016-04-24 01:30:20 +02:00
std : : string gbt_vb_name ( const Consensus : : DeploymentPos pos ) {
const struct BIP9DeploymentInfo & vbinfo = VersionBitsDeploymentInfo [ pos ] ;
std : : string s = vbinfo . name ;
2016-06-01 18:47:36 +02:00
if ( ! vbinfo . gbt_force ) {
s . insert ( s . begin ( ) , ' ! ' ) ;
}
2016-04-24 01:30:20 +02:00
return s ;
}
2016-09-22 09:46:41 +02:00
UniValue getblocktemplate ( const JSONRPCRequest & request )
2012-08-21 08:21:33 +02:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) > 1 )
2012-08-21 08:21:33 +02:00
throw runtime_error (
2016-10-18 09:17:19 +02:00
" getblocktemplate ( TemplateRequest ) \n "
2013-10-29 12:29:44 +01:00
" \n If the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'. \n "
" It returns data needed to construct a block to work on. \n "
2016-10-18 09:17:19 +02:00
" For full specification, see BIPs 22, 23, 9, and 145: \n "
2016-04-24 01:30:20 +02:00
" https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki \n "
2016-10-18 09:17:19 +02:00
" https://github.com/bitcoin/bips/blob/master/bip-0023.mediawiki \n "
2016-04-24 01:30:20 +02:00
" https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes \n "
2016-10-18 09:17:19 +02:00
" https://github.com/bitcoin/bips/blob/master/bip-0145.mediawiki \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2016-11-21 14:03:09 +01:00
" 1. template_request (json object, optional) A json object in the following spec \n "
2013-10-29 12:29:44 +01:00
" { \n "
2016-10-18 09:17:19 +02:00
" \" mode \" : \" template \" (string, optional) This must be set to \" template \" , \" proposal \" (see BIP 23), or omitted \n "
" \" capabilities \" :[ (array, optional) A list of strings \n "
" \" support \" (string) client side supported feature, 'longpoll', 'coinbasetxn', 'coinbasevalue', 'proposal', 'serverlist', 'workid' \n "
2013-10-29 12:29:44 +01:00
" ,... \n "
2016-10-18 09:17:19 +02:00
" ], \n "
" \" rules \" :[ (array, optional) A list of strings \n "
" \" support \" (string) client side supported softfork deployment \n "
" ,... \n "
" ] \n "
2013-10-29 12:29:44 +01:00
" } \n "
" \n "
" \n Result: \n "
" { \n "
2016-10-18 09:17:19 +02:00
" \" version \" : n, (numeric) The preferred block version \n "
2016-04-24 01:30:20 +02:00
" \" rules \" : [ \" rulename \" , ... ], (array of strings) specific block rules that are to be enforced \n "
" \" vbavailable \" : { (json object) set of pending, supported versionbit (BIP 9) softfork deployments \n "
2016-10-18 09:17:19 +02:00
" \" rulename \" : bitnumber (numeric) identifies the bit number as indicating acceptance and readiness for the named softfork rule \n "
2016-04-24 01:30:20 +02:00
" ,... \n "
" }, \n "
" \" vbrequired \" : n, (numeric) bit mask of versionbits the server requires set in submissions \n "
2016-10-18 09:17:19 +02:00
" \" previousblockhash \" : \" xxxx \" , (string) The hash of current highest block \n "
2013-10-29 12:29:44 +01:00
" \" transactions \" : [ (array) contents of non-coinbase transactions that should be included in the next block \n "
" { \n "
2016-10-18 09:17:19 +02:00
" \" data \" : \" xxxx \" , (string) transaction data encoded in hexadecimal (byte-for-byte) \n "
" \" txid \" : \" xxxx \" , (string) transaction id encoded in little-endian hexadecimal \n "
" \" hash \" : \" xxxx \" , (string) hash encoded in little-endian hexadecimal (including witness data) \n "
" \" depends \" : [ (array) array of numbers \n "
" n (numeric) transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is \n "
2013-10-29 12:29:44 +01:00
" ,... \n "
" ], \n "
2016-10-18 09:17:19 +02:00
" \" fee \" : n, (numeric) difference in value between transaction inputs and outputs (in Satoshis); for coinbase transactions, this is a negative Number of the total collected block fees (ie, not including the block subsidy); if key is not present, fee is unknown and clients MUST NOT assume there isn't one \n "
" \" sigops \" : n, (numeric) total SigOps cost, as counted for purposes of block limits; if key is not present, sigop cost is unknown and clients MUST NOT assume it is zero \n "
" \" weight \" : n, (numeric) total transaction weight, as counted for purposes of block limits \n "
" \" required \" : true|false (boolean) if provided and true, this transaction must be in the final block \n "
2013-10-29 12:29:44 +01:00
" } \n "
" ,... \n "
" ], \n "
2016-10-18 09:17:19 +02:00
" \" coinbaseaux \" : { (json object) data that should be included in the coinbase's scriptSig content \n "
" \" flags \" : \" xx \" (string) key name is to be ignored, and value included in scriptSig \n "
2013-10-29 12:29:44 +01:00
" }, \n "
2016-10-18 09:17:19 +02:00
" \" coinbasevalue \" : n, (numeric) maximum allowable input to coinbase transaction, including the generation award and transaction fees (in Satoshis) \n "
" \" coinbasetxn \" : { ... }, (json object) information for coinbase transaction \n "
" \" target \" : \" xxxx \" , (string) The hash target \n "
" \" mintime \" : xxx, (numeric) The minimum timestamp appropriate for next block time in seconds since epoch (Jan 1 1970 GMT) \n "
" \" mutable \" : [ (array of string) list of ways the block template may be changed \n "
" \" value \" (string) A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock' \n "
2013-10-29 12:29:44 +01:00
" ,... \n "
" ], \n "
2016-10-18 09:17:19 +02:00
" \" noncerange \" : \" 00000000ffffffff \" ,(string) A range of valid nonces \n "
" \" sigoplimit \" : n, (numeric) limit of sigops in blocks \n "
2013-10-29 12:29:44 +01:00
" \" sizelimit \" : n, (numeric) limit of block size \n "
2016-07-18 19:28:26 +02:00
" \" weightlimit \" : n, (numeric) limit of block weight \n "
2013-10-29 12:29:44 +01:00
" \" curtime \" : ttt, (numeric) current timestamp in seconds since epoch (Jan 1 1970 GMT) \n "
2016-10-18 09:17:19 +02:00
" \" bits \" : \" xxxxxxxx \" , (string) compressed target of next block \n "
2013-10-29 12:29:44 +01:00
" \" height \" : n (numeric) The height of the next block \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getblocktemplate " , " " )
+ HelpExampleRpc ( " getblocktemplate " , " " )
) ;
2012-08-21 08:21:33 +02:00
2014-10-19 10:46:17 +02:00
LOCK ( cs_main ) ;
2012-08-21 08:21:33 +02:00
std : : string strMode = " template " ;
2015-05-13 21:29:19 +02:00
UniValue lpval = NullUniValue ;
2016-06-01 18:47:36 +02:00
std : : set < std : : string > setClientRules ;
2016-06-01 18:51:54 +02:00
int64_t nMaxVersionPreVB = - 1 ;
2016-09-22 09:46:41 +02:00
if ( request . params . size ( ) > 0 )
2012-08-21 08:21:33 +02:00
{
2016-09-22 09:46:41 +02:00
const UniValue & oparam = request . params [ 0 ] . get_obj ( ) ;
2015-05-18 14:02:18 +02:00
const UniValue & modeval = find_value ( oparam , " mode " ) ;
2014-08-20 21:15:16 +02:00
if ( modeval . isStr ( ) )
2012-08-21 08:21:33 +02:00
strMode = modeval . get_str ( ) ;
2014-08-20 21:15:16 +02:00
else if ( modeval . isNull ( ) )
2012-09-01 09:12:50 +02:00
{
/* Do nothing */
}
2012-08-21 08:21:33 +02:00
else
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid mode " ) ;
2012-05-13 06:43:24 +02:00
lpval = find_value ( oparam , " longpollid " ) ;
2012-09-10 04:55:03 +02:00
if ( strMode = = " proposal " )
{
2015-05-18 14:02:18 +02:00
const UniValue & dataval = find_value ( oparam , " data " ) ;
2015-06-04 21:39:44 +02:00
if ( ! dataval . isStr ( ) )
2012-09-10 04:55:03 +02:00
throw JSONRPCError ( RPC_TYPE_ERROR , " Missing data String key for proposal " ) ;
CBlock block ;
if ( ! DecodeHexBlk ( block , dataval . get_str ( ) ) )
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , " Block decode failed " ) ;
uint256 hash = block . GetHash ( ) ;
BlockMap : : iterator mi = mapBlockIndex . find ( hash ) ;
if ( mi ! = mapBlockIndex . end ( ) ) {
CBlockIndex * pindex = mi - > second ;
if ( pindex - > IsValid ( BLOCK_VALID_SCRIPTS ) )
return " duplicate " ;
if ( pindex - > nStatus & BLOCK_FAILED_MASK )
return " duplicate-invalid " ;
return " duplicate-inconclusive " ;
}
CBlockIndex * const pindexPrev = chainActive . Tip ( ) ;
// TestBlockValidity only supports blocks built on the current Tip
if ( block . hashPrevBlock ! = pindexPrev - > GetBlockHash ( ) )
return " inconclusive-not-best-prevblk " ;
CValidationState state ;
2015-04-20 00:17:11 +02:00
TestBlockValidity ( state , Params ( ) , block , pindexPrev , false , true ) ;
2012-09-10 04:55:03 +02:00
return BIP22ValidationResult ( state ) ;
}
2016-06-01 18:47:36 +02:00
const UniValue & aClientRules = find_value ( oparam , " rules " ) ;
if ( aClientRules . isArray ( ) ) {
for ( unsigned int i = 0 ; i < aClientRules . size ( ) ; + + i ) {
const UniValue & v = aClientRules [ i ] ;
setClientRules . insert ( v . get_str ( ) ) ;
}
2016-06-01 18:51:54 +02:00
} else {
// NOTE: It is important that this NOT be read if versionbits is supported
const UniValue & uvMaxVersion = find_value ( oparam , " maxversion " ) ;
if ( uvMaxVersion . isNum ( ) ) {
nMaxVersionPreVB = uvMaxVersion . get_int64 ( ) ;
}
2016-06-01 18:47:36 +02:00
}
2012-08-21 08:21:33 +02:00
}
if ( strMode ! = " template " )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid mode " ) ;
2012-08-21 08:21:33 +02:00
2016-04-17 00:30:03 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
if ( g_connman - > GetNodeCount ( CConnman : : CONNECTIONS_ALL ) = = 0 )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_CLIENT_NOT_CONNECTED , " Bitcoin is not connected! " ) ;
2012-08-21 08:21:33 +02:00
if ( IsInitialBlockDownload ( ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_CLIENT_IN_INITIAL_DOWNLOAD , " Bitcoin is downloading blocks... " ) ;
2012-08-21 08:21:33 +02:00
static unsigned int nTransactionsUpdatedLast ;
2012-05-13 06:43:24 +02:00
2014-08-20 21:15:16 +02:00
if ( ! lpval . isNull ( ) )
2012-05-13 06:43:24 +02:00
{
// Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
uint256 hashWatchedChain ;
boost : : system_time checktxtime ;
unsigned int nTransactionsUpdatedLastLP ;
2014-08-20 21:15:16 +02:00
if ( lpval . isStr ( ) )
2012-05-13 06:43:24 +02:00
{
// Format: <hashBestChain><nTransactionsUpdatedLast>
std : : string lpstr = lpval . get_str ( ) ;
hashWatchedChain . SetHex ( lpstr . substr ( 0 , 64 ) ) ;
nTransactionsUpdatedLastLP = atoi64 ( lpstr . substr ( 64 ) ) ;
}
else
{
// NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
hashWatchedChain = chainActive . Tip ( ) - > GetBlockHash ( ) ;
nTransactionsUpdatedLastLP = nTransactionsUpdatedLast ;
}
// Release the wallet and main lock while waiting
LEAVE_CRITICAL_SECTION ( cs_main ) ;
{
checktxtime = boost : : get_system_time ( ) + boost : : posix_time : : minutes ( 1 ) ;
boost : : unique_lock < boost : : mutex > lock ( csBestBlock ) ;
while ( chainActive . Tip ( ) - > GetBlockHash ( ) = = hashWatchedChain & & IsRPCRunning ( ) )
{
if ( ! cvBlockChange . timed_wait ( lock , checktxtime ) )
{
// Timeout: Check transactions for update
if ( mempool . GetTransactionsUpdated ( ) ! = nTransactionsUpdatedLastLP )
break ;
checktxtime + = boost : : posix_time : : seconds ( 10 ) ;
}
}
}
ENTER_CRITICAL_SECTION ( cs_main ) ;
if ( ! IsRPCRunning ( ) )
throw JSONRPCError ( RPC_CLIENT_NOT_CONNECTED , " Shutting down " ) ;
// TODO: Maybe recheck connections/IBD and (if something wrong) send an expires-immediately template to stop miners?
}
// Update block
2012-08-21 08:21:33 +02:00
static CBlockIndex * pindexPrev ;
2013-04-13 07:13:08 +02:00
static int64_t nStart ;
2016-06-18 19:38:28 +02:00
static std : : unique_ptr < CBlockTemplate > pblocktemplate ;
2013-10-10 23:07:44 +02:00
if ( pindexPrev ! = chainActive . Tip ( ) | |
2013-08-27 07:51:57 +02:00
( mempool . GetTransactionsUpdated ( ) ! = nTransactionsUpdatedLast & & GetTime ( ) - nStart > 5 ) )
2012-08-21 08:21:33 +02:00
{
// Clear pindexPrev so future calls make a new block, despite any failures from here on
2016-06-18 19:38:28 +02:00
pindexPrev = nullptr ;
2012-08-21 08:21:33 +02:00
// Store the pindexBest used before CreateNewBlock, to avoid races
2013-08-27 07:51:57 +02:00
nTransactionsUpdatedLast = mempool . GetTransactionsUpdated ( ) ;
2015-04-10 07:33:06 +02:00
CBlockIndex * pindexPrevNew = chainActive . Tip ( ) ;
2012-08-21 08:21:33 +02:00
nStart = GetTime ( ) ;
// Create new block
2013-08-24 06:45:17 +02:00
CScript scriptDummy = CScript ( ) < < OP_TRUE ;
2015-12-15 21:26:44 +01:00
pblocktemplate = BlockAssembler ( Params ( ) ) . CreateNewBlock ( scriptDummy ) ;
2012-12-19 21:21:21 +01:00
if ( ! pblocktemplate )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_OUT_OF_MEMORY , " Out of memory " ) ;
2012-08-21 08:21:33 +02:00
// Need to update only after we know CreateNewBlock succeeded
pindexPrev = pindexPrevNew ;
}
2012-12-19 21:21:21 +01:00
CBlock * pblock = & pblocktemplate - > block ; // pointer for convenience
2016-04-24 01:30:20 +02:00
const Consensus : : Params & consensusParams = Params ( ) . GetConsensus ( ) ;
2012-08-21 08:21:33 +02:00
// Update nTime
2016-04-24 01:30:20 +02:00
UpdateTime ( pblock , consensusParams , pindexPrev ) ;
2012-08-21 08:21:33 +02:00
pblock - > nNonce = 0 ;
2016-08-08 23:16:40 +02:00
// NOTE: If at some point we support pre-segwit miners post-segwit-activation, this needs to take segwit support into consideration
const bool fPreSegWit = ( THRESHOLD_ACTIVE ! = VersionBitsState ( pindexPrev , consensusParams , Consensus : : DEPLOYMENT_SEGWIT , versionbitscache ) ) ;
2015-05-10 14:48:35 +02:00
UniValue aCaps ( UniValue : : VARR ) ; aCaps . push_back ( " proposal " ) ;
2012-09-10 04:55:03 +02:00
2015-05-10 14:48:35 +02:00
UniValue transactions ( UniValue : : VARR ) ;
2012-08-21 08:21:33 +02:00
map < uint256 , int64_t > setTxIndex ;
int i = 0 ;
2016-11-11 02:26:00 +01:00
for ( const auto & it : pblock - > vtx ) {
const CTransaction & tx = * it ;
2012-08-21 08:21:33 +02:00
uint256 txHash = tx . GetHash ( ) ;
setTxIndex [ txHash ] = i + + ;
if ( tx . IsCoinBase ( ) )
continue ;
2015-05-10 14:48:35 +02:00
UniValue entry ( UniValue : : VOBJ ) ;
2012-08-21 08:21:33 +02:00
2014-06-24 05:10:24 +02:00
entry . push_back ( Pair ( " data " , EncodeHexTx ( tx ) ) ) ;
2015-11-06 01:42:38 +01:00
entry . push_back ( Pair ( " txid " , txHash . GetHex ( ) ) ) ;
entry . push_back ( Pair ( " hash " , tx . GetWitnessHash ( ) . GetHex ( ) ) ) ;
2012-08-21 08:21:33 +02:00
2015-05-10 14:48:35 +02:00
UniValue deps ( UniValue : : VARR ) ;
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
BOOST_FOREACH ( const CTxIn & in , tx . vin )
2012-08-21 08:21:33 +02:00
{
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
if ( setTxIndex . count ( in . prevout . hash ) )
deps . push_back ( setTxIndex [ in . prevout . hash ] ) ;
}
entry . push_back ( Pair ( " depends " , deps ) ) ;
2012-08-21 08:21:33 +02:00
2013-02-08 00:54:22 +01:00
int index_in_template = i - 1 ;
2013-01-04 05:58:36 +01:00
entry . push_back ( Pair ( " fee " , pblocktemplate - > vTxFees [ index_in_template ] ) ) ;
2016-08-08 23:16:40 +02:00
int64_t nTxSigOps = pblocktemplate - > vTxSigOpsCost [ index_in_template ] ;
if ( fPreSegWit ) {
assert ( nTxSigOps % WITNESS_SCALE_FACTOR = = 0 ) ;
nTxSigOps / = WITNESS_SCALE_FACTOR ;
}
entry . push_back ( Pair ( " sigops " , nTxSigOps ) ) ;
2016-07-18 19:28:26 +02:00
entry . push_back ( Pair ( " weight " , GetTransactionWeight ( tx ) ) ) ;
2012-08-21 08:21:33 +02:00
transactions . push_back ( entry ) ;
}
2015-05-10 14:48:35 +02:00
UniValue aux ( UniValue : : VOBJ ) ;
2012-08-21 08:21:33 +02:00
aux . push_back ( Pair ( " flags " , HexStr ( COINBASE_FLAGS . begin ( ) , COINBASE_FLAGS . end ( ) ) ) ) ;
2014-12-16 15:43:03 +01:00
arith_uint256 hashTarget = arith_uint256 ( ) . SetCompact ( pblock - > nBits ) ;
2012-08-21 08:21:33 +02:00
2016-06-01 18:51:54 +02:00
UniValue aMutable ( UniValue : : VARR ) ;
aMutable . push_back ( " time " ) ;
aMutable . push_back ( " transactions " ) ;
aMutable . push_back ( " prevblock " ) ;
2012-08-21 08:21:33 +02:00
2015-05-10 14:48:35 +02:00
UniValue result ( UniValue : : VOBJ ) ;
2012-09-10 04:55:03 +02:00
result . push_back ( Pair ( " capabilities " , aCaps ) ) ;
2016-04-24 01:30:20 +02:00
UniValue aRules ( UniValue : : VARR ) ;
UniValue vbavailable ( UniValue : : VOBJ ) ;
2016-09-02 18:19:01 +02:00
for ( int j = 0 ; j < ( int ) Consensus : : MAX_VERSION_BITS_DEPLOYMENTS ; + + j ) {
Consensus : : DeploymentPos pos = Consensus : : DeploymentPos ( j ) ;
2016-04-24 01:30:20 +02:00
ThresholdState state = VersionBitsState ( pindexPrev , consensusParams , pos , versionbitscache ) ;
switch ( state ) {
case THRESHOLD_DEFINED :
case THRESHOLD_FAILED :
// Not exposed to GBT at all
break ;
case THRESHOLD_LOCKED_IN :
// Ensure bit is set in block version
pblock - > nVersion | = VersionBitsMask ( consensusParams , pos ) ;
// FALL THROUGH to get vbavailable set...
case THRESHOLD_STARTED :
2016-06-01 18:47:36 +02:00
{
const struct BIP9DeploymentInfo & vbinfo = VersionBitsDeploymentInfo [ pos ] ;
2016-04-24 01:30:20 +02:00
vbavailable . push_back ( Pair ( gbt_vb_name ( pos ) , consensusParams . vDeployments [ pos ] . bit ) ) ;
2016-06-01 18:47:36 +02:00
if ( setClientRules . find ( vbinfo . name ) = = setClientRules . end ( ) ) {
if ( ! vbinfo . gbt_force ) {
// If the client doesn't support this, don't indicate it in the [default] version
pblock - > nVersion & = ~ VersionBitsMask ( consensusParams , pos ) ;
}
}
2016-04-24 01:30:20 +02:00
break ;
2016-06-01 18:47:36 +02:00
}
2016-04-24 01:30:20 +02:00
case THRESHOLD_ACTIVE :
2016-06-01 18:47:36 +02:00
{
2016-04-24 01:30:20 +02:00
// Add to rules only
2016-06-01 18:47:36 +02:00
const struct BIP9DeploymentInfo & vbinfo = VersionBitsDeploymentInfo [ pos ] ;
2016-04-24 01:30:20 +02:00
aRules . push_back ( gbt_vb_name ( pos ) ) ;
2016-06-01 18:47:36 +02:00
if ( setClientRules . find ( vbinfo . name ) = = setClientRules . end ( ) ) {
// Not supported by the client; make sure it's safe to proceed
if ( ! vbinfo . gbt_force ) {
2016-06-01 18:51:54 +02:00
// If we do anything other than throw an exception here, be sure version/force isn't sent to old clients
2016-06-01 18:47:36 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " Support for '%s' rule requires explicit client support " , vbinfo . name ) ) ;
}
}
2016-04-24 01:30:20 +02:00
break ;
2016-06-01 18:47:36 +02:00
}
2016-04-24 01:30:20 +02:00
}
}
2012-08-21 08:21:33 +02:00
result . push_back ( Pair ( " version " , pblock - > nVersion ) ) ;
2016-04-24 01:30:20 +02:00
result . push_back ( Pair ( " rules " , aRules ) ) ;
result . push_back ( Pair ( " vbavailable " , vbavailable ) ) ;
result . push_back ( Pair ( " vbrequired " , int ( 0 ) ) ) ;
2016-06-01 18:51:54 +02:00
if ( nMaxVersionPreVB > = 2 ) {
// If VB is supported by the client, nMaxVersionPreVB is -1, so we won't get here
2016-08-13 19:21:13 +02:00
// Because BIP 34 changed how the generation transaction is serialized, we can only use version/force back to v2 blocks
2016-06-01 18:51:54 +02:00
// This is safe to do [otherwise-]unconditionally only because we are throwing an exception above if a non-force deployment gets activated
// Note that this can probably also be removed entirely after the first BIP9 non-force deployment (ie, probably segwit) gets activated
aMutable . push_back ( " version/force " ) ;
}
2012-08-21 08:21:33 +02:00
result . push_back ( Pair ( " previousblockhash " , pblock - > hashPrevBlock . GetHex ( ) ) ) ;
result . push_back ( Pair ( " transactions " , transactions ) ) ;
result . push_back ( Pair ( " coinbaseaux " , aux ) ) ;
2016-11-11 02:26:00 +01:00
result . push_back ( Pair ( " coinbasevalue " , ( int64_t ) pblock - > vtx [ 0 ] - > vout [ 0 ] . nValue ) ) ;
2012-05-13 06:43:24 +02:00
result . push_back ( Pair ( " longpollid " , chainActive . Tip ( ) - > GetBlockHash ( ) . GetHex ( ) + i64tostr ( nTransactionsUpdatedLast ) ) ) ;
2012-08-21 08:21:33 +02:00
result . push_back ( Pair ( " target " , hashTarget . GetHex ( ) ) ) ;
result . push_back ( Pair ( " mintime " , ( int64_t ) pindexPrev - > GetMedianTimePast ( ) + 1 ) ) ;
result . push_back ( Pair ( " mutable " , aMutable ) ) ;
result . push_back ( Pair ( " noncerange " , " 00000000ffffffff " ) ) ;
2016-08-08 23:16:40 +02:00
int64_t nSigOpLimit = MAX_BLOCK_SIGOPS_COST ;
if ( fPreSegWit ) {
assert ( nSigOpLimit % WITNESS_SCALE_FACTOR = = 0 ) ;
nSigOpLimit / = WITNESS_SCALE_FACTOR ;
}
result . push_back ( Pair ( " sigoplimit " , nSigOpLimit ) ) ;
2016-01-03 18:54:50 +01:00
result . push_back ( Pair ( " sizelimit " , ( int64_t ) MAX_BLOCK_SERIALIZED_SIZE ) ) ;
2016-07-18 19:28:26 +02:00
result . push_back ( Pair ( " weightlimit " , ( int64_t ) MAX_BLOCK_WEIGHT ) ) ;
2014-06-28 23:36:06 +02:00
result . push_back ( Pair ( " curtime " , pblock - > GetBlockTime ( ) ) ) ;
2014-06-27 13:28:08 +02:00
result . push_back ( Pair ( " bits " , strprintf ( " %08x " , pblock - > nBits ) ) ) ;
2012-08-21 08:21:33 +02:00
result . push_back ( Pair ( " height " , ( int64_t ) ( pindexPrev - > nHeight + 1 ) ) ) ;
2016-11-18 21:15:01 +01:00
const struct BIP9DeploymentInfo & segwit_info = VersionBitsDeploymentInfo [ Consensus : : DEPLOYMENT_SEGWIT ] ;
if ( ! pblocktemplate - > vchCoinbaseCommitment . empty ( ) & & setClientRules . find ( segwit_info . name ) ! = setClientRules . end ( ) ) {
2015-11-06 01:42:38 +01:00
result . push_back ( Pair ( " default_witness_commitment " , HexStr ( pblocktemplate - > vchCoinbaseCommitment . begin ( ) , pblocktemplate - > vchCoinbaseCommitment . end ( ) ) ) ) ;
}
2012-08-21 08:21:33 +02:00
return result ;
}
2014-10-20 06:18:00 +02:00
class submitblock_StateCatcher : public CValidationInterface
{
public :
uint256 hash ;
bool found ;
CValidationState state ;
submitblock_StateCatcher ( const uint256 & hashIn ) : hash ( hashIn ) , found ( false ) , state ( ) { } ;
protected :
virtual void BlockChecked ( const CBlock & block , const CValidationState & stateIn ) {
if ( block . GetHash ( ) ! = hash )
return ;
found = true ;
state = stateIn ;
} ;
} ;
2016-09-22 09:46:41 +02:00
UniValue submitblock ( const JSONRPCRequest & request )
2012-08-21 08:21:33 +02:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 )
2012-08-21 08:21:33 +02:00
throw runtime_error (
2013-10-29 12:29:44 +01:00
" submitblock \" hexdata \" ( \" jsonparametersobject \" ) \n "
" \n Attempts to submit new block to network. \n "
" The 'jsonparametersobject' parameter is currently ignored. \n "
" See https://en.bitcoin.it/wiki/BIP_0022 for full specification. \n "
2016-12-09 18:06:42 +01:00
" \n Arguments \n "
" 1. \" hexdata \" (string, required) the hex-encoded block data to submit \n "
2016-11-21 14:03:09 +01:00
" 2. \" parameters \" (string, optional) object of optional parameters \n "
2013-10-29 12:29:44 +01:00
" { \n "
" \" workid \" : \" id \" (string, optional) if the server provided a workid, it MUST be included with submissions \n "
" } \n "
" \n Result: \n "
" \n Examples: \n "
+ HelpExampleCli ( " submitblock " , " \" mydata \" " )
+ HelpExampleRpc ( " submitblock " , " \" mydata \" " )
) ;
2012-08-21 08:21:33 +02:00
2016-12-04 09:17:30 +01:00
std : : shared_ptr < CBlock > blockptr = std : : make_shared < CBlock > ( ) ;
CBlock & block = * blockptr ;
2016-09-22 09:46:41 +02:00
if ( ! DecodeHexBlk ( block , request . params [ 0 ] . get_str ( ) ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , " Block decode failed " ) ;
2012-08-21 08:21:33 +02:00
2014-11-18 20:09:20 +01:00
uint256 hash = block . GetHash ( ) ;
2015-04-13 18:55:41 +02:00
bool fBlockPresent = false ;
{
LOCK ( cs_main ) ;
BlockMap : : iterator mi = mapBlockIndex . find ( hash ) ;
if ( mi ! = mapBlockIndex . end ( ) ) {
CBlockIndex * pindex = mi - > second ;
if ( pindex - > IsValid ( BLOCK_VALID_SCRIPTS ) )
return " duplicate " ;
if ( pindex - > nStatus & BLOCK_FAILED_MASK )
return " duplicate-invalid " ;
// Otherwise, we might only have the header - process the block before returning
fBlockPresent = true ;
}
2012-08-21 08:21:33 +02:00
}
2015-11-06 01:42:38 +01:00
{
LOCK ( cs_main ) ;
BlockMap : : iterator mi = mapBlockIndex . find ( block . hashPrevBlock ) ;
if ( mi ! = mapBlockIndex . end ( ) ) {
UpdateUncommittedBlockStructures ( block , mi - > second , Params ( ) . GetConsensus ( ) ) ;
}
}
2014-11-18 20:09:20 +01:00
submitblock_StateCatcher sc ( block . GetHash ( ) ) ;
2014-10-20 06:18:00 +02:00
RegisterValidationInterface ( & sc ) ;
2016-12-04 09:23:17 +01:00
bool fAccepted = ProcessNewBlock ( Params ( ) , blockptr , true , NULL ) ;
2014-10-20 06:18:00 +02:00
UnregisterValidationInterface ( & sc ) ;
2015-04-13 18:55:41 +02:00
if ( fBlockPresent )
2014-11-18 20:09:20 +01:00
{
if ( fAccepted & & ! sc . found )
return " duplicate-inconclusive " ;
return " duplicate " ;
}
2016-10-27 22:30:17 +02:00
if ( ! sc . found )
return " inconclusive " ;
return BIP22ValidationResult ( sc . state ) ;
2012-08-21 08:21:33 +02:00
}
2014-03-17 13:19:54 +01:00
2016-09-22 09:46:41 +02:00
UniValue estimatefee ( const JSONRPCRequest & request )
2014-03-17 13:19:54 +01:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 1 )
2014-03-17 13:19:54 +01:00
throw runtime_error (
" estimatefee nblocks \n "
2015-07-08 21:40:14 +02:00
" \n Estimates the approximate fee per kilobyte needed for a transaction to begin \n "
2016-09-15 16:46:01 +02:00
" confirmation within nblocks blocks. Uses virtual transaction size of transaction \n "
" as defined in BIP 141 (witness data is discounted). \n "
2014-03-17 13:19:54 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. nblocks (numeric, required) \n "
2014-03-17 13:19:54 +01:00
" \n Result: \n "
2015-07-08 21:40:14 +02:00
" n (numeric) estimated fee-per-kilobyte \n "
2014-03-17 13:19:54 +01:00
" \n "
2015-07-08 21:40:14 +02:00
" A negative value is returned if not enough transactions and blocks \n "
" have been observed to make an estimate. \n "
2016-11-29 18:18:44 +01:00
" -1 is always returned for nblocks == 1 as it is impossible to calculate \n "
" a fee that is high enough to get reliably included in the next block. \n "
2014-03-17 13:19:54 +01:00
" \n Example: \n "
+ HelpExampleCli ( " estimatefee " , " 6 " )
) ;
2016-09-22 09:46:41 +02:00
RPCTypeCheck ( request . params , boost : : assign : : list_of ( UniValue : : VNUM ) ) ;
2014-03-17 13:19:54 +01:00
2016-09-22 09:46:41 +02:00
int nBlocks = request . params [ 0 ] . get_int ( ) ;
2014-03-17 13:19:54 +01:00
if ( nBlocks < 1 )
nBlocks = 1 ;
CFeeRate feeRate = mempool . estimateFee ( nBlocks ) ;
if ( feeRate = = CFeeRate ( 0 ) )
return - 1.0 ;
return ValueFromAmount ( feeRate . GetFeePerK ( ) ) ;
}
2016-09-22 09:46:41 +02:00
UniValue estimatepriority ( const JSONRPCRequest & request )
2014-03-17 13:19:54 +01:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 1 )
2014-03-17 13:19:54 +01:00
throw runtime_error (
" estimatepriority nblocks \n "
2016-03-21 18:04:40 +01:00
" \n DEPRECATED. Estimates the approximate priority a zero-fee transaction needs to begin \n "
2015-07-08 21:40:14 +02:00
" confirmation within nblocks blocks. \n "
2014-03-17 13:19:54 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. nblocks (numeric, required) \n "
2014-03-17 13:19:54 +01:00
" \n Result: \n "
2015-07-08 21:40:14 +02:00
" n (numeric) estimated priority \n "
2014-03-17 13:19:54 +01:00
" \n "
2015-07-08 21:40:14 +02:00
" A negative value is returned if not enough transactions and blocks \n "
" have been observed to make an estimate. \n "
2014-03-17 13:19:54 +01:00
" \n Example: \n "
+ HelpExampleCli ( " estimatepriority " , " 6 " )
) ;
2016-09-22 09:46:41 +02:00
RPCTypeCheck ( request . params , boost : : assign : : list_of ( UniValue : : VNUM ) ) ;
2014-03-17 13:19:54 +01:00
2016-09-22 09:46:41 +02:00
int nBlocks = request . params [ 0 ] . get_int ( ) ;
2014-03-17 13:19:54 +01:00
if ( nBlocks < 1 )
nBlocks = 1 ;
return mempool . estimatePriority ( nBlocks ) ;
}
2015-11-16 21:26:57 +01:00
2016-09-22 09:46:41 +02:00
UniValue estimatesmartfee ( const JSONRPCRequest & request )
2015-11-16 21:26:57 +01:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 1 )
2015-11-16 21:26:57 +01:00
throw runtime_error (
" estimatesmartfee nblocks \n "
" \n WARNING: This interface is unstable and may disappear or change! \n "
" \n Estimates the approximate fee per kilobyte needed for a transaction to begin \n "
" confirmation within nblocks blocks if possible and return the number of blocks \n "
2016-09-15 16:46:01 +02:00
" for which the estimate is valid. Uses virtual transaction size as defined \n "
" in BIP 141 (witness data is discounted). \n "
2015-11-16 21:26:57 +01:00
" \n Arguments: \n "
" 1. nblocks (numeric) \n "
" \n Result: \n "
" { \n "
" \" feerate \" : x.x, (numeric) estimate fee-per-kilobyte (in BTC) \n "
" \" blocks \" : n (numeric) block number where estimate was found \n "
" } \n "
" \n "
" A negative value is returned if not enough transactions and blocks \n "
" have been observed to make an estimate for any number of blocks. \n "
" However it will not return a value below the mempool reject fee. \n "
" \n Example: \n "
+ HelpExampleCli ( " estimatesmartfee " , " 6 " )
) ;
2016-09-22 09:46:41 +02:00
RPCTypeCheck ( request . params , boost : : assign : : list_of ( UniValue : : VNUM ) ) ;
2015-11-16 21:26:57 +01:00
2016-09-22 09:46:41 +02:00
int nBlocks = request . params [ 0 ] . get_int ( ) ;
2015-11-16 21:26:57 +01:00
UniValue result ( UniValue : : VOBJ ) ;
int answerFound ;
CFeeRate feeRate = mempool . estimateSmartFee ( nBlocks , & answerFound ) ;
result . push_back ( Pair ( " feerate " , feeRate = = CFeeRate ( 0 ) ? - 1.0 : ValueFromAmount ( feeRate . GetFeePerK ( ) ) ) ) ;
result . push_back ( Pair ( " blocks " , answerFound ) ) ;
return result ;
}
2016-09-22 09:46:41 +02:00
UniValue estimatesmartpriority ( const JSONRPCRequest & request )
2015-11-16 21:26:57 +01:00
{
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 1 )
2015-11-16 21:26:57 +01:00
throw runtime_error (
" estimatesmartpriority nblocks \n "
2016-03-21 18:04:40 +01:00
" \n DEPRECATED. WARNING: This interface is unstable and may disappear or change! \n "
2015-11-16 21:26:57 +01:00
" \n Estimates the approximate priority a zero-fee transaction needs to begin \n "
" confirmation within nblocks blocks if possible and return the number of blocks \n "
" for which the estimate is valid. \n "
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. nblocks (numeric, required) \n "
2015-11-16 21:26:57 +01:00
" \n Result: \n "
" { \n "
" \" priority \" : x.x, (numeric) estimated priority \n "
" \" blocks \" : n (numeric) block number where estimate was found \n "
" } \n "
" \n "
" A negative value is returned if not enough transactions and blocks \n "
" have been observed to make an estimate for any number of blocks. \n "
" However if the mempool reject fee is set it will return 1e9 * MAX_MONEY. \n "
" \n Example: \n "
+ HelpExampleCli ( " estimatesmartpriority " , " 6 " )
) ;
2016-09-22 09:46:41 +02:00
RPCTypeCheck ( request . params , boost : : assign : : list_of ( UniValue : : VNUM ) ) ;
2015-11-16 21:26:57 +01:00
2016-09-22 09:46:41 +02:00
int nBlocks = request . params [ 0 ] . get_int ( ) ;
2015-11-16 21:26:57 +01:00
UniValue result ( UniValue : : VOBJ ) ;
int answerFound ;
double priority = mempool . estimateSmartPriority ( nBlocks , & answerFound ) ;
result . push_back ( Pair ( " priority " , priority ) ) ;
result . push_back ( Pair ( " blocks " , answerFound ) ) ;
return result ;
}
2016-03-29 19:43:02 +02:00
static const CRPCCommand commands [ ] =
{ // category name actor (function) okSafeMode
// --------------------- ------------------------ ----------------------- ----------
2016-11-21 14:03:09 +01:00
{ " mining " , " getnetworkhashps " , & getnetworkhashps , true , { " nblocks " , " height " } } ,
{ " mining " , " getmininginfo " , & getmininginfo , true , { } } ,
{ " mining " , " prioritisetransaction " , & prioritisetransaction , true , { " txid " , " priority_delta " , " fee_delta " } } ,
{ " mining " , " getblocktemplate " , & getblocktemplate , true , { " template_request " } } ,
{ " mining " , " submitblock " , & submitblock , true , { " hexdata " , " parameters " } } ,
{ " generating " , " generate " , & generate , true , { " nblocks " , " maxtries " } } ,
{ " generating " , " generatetoaddress " , & generatetoaddress , true , { " nblocks " , " address " , " maxtries " } } ,
{ " util " , " estimatefee " , & estimatefee , true , { " nblocks " } } ,
{ " util " , " estimatepriority " , & estimatepriority , true , { " nblocks " } } ,
{ " util " , " estimatesmartfee " , & estimatesmartfee , true , { " nblocks " } } ,
{ " util " , " estimatesmartpriority " , & estimatesmartpriority , true , { " nblocks " } } ,
2016-03-29 19:43:02 +02:00
} ;
2016-06-07 18:42:42 +02:00
void RegisterMiningRPCCommands ( CRPCTable & t )
2016-03-29 19:43:02 +02:00
{
for ( unsigned int vcidx = 0 ; vcidx < ARRAYLEN ( commands ) ; vcidx + + )
2016-06-07 18:42:42 +02:00
t . appendCommand ( commands [ vcidx ] . name , & commands [ vcidx ] ) ;
2016-03-29 19:43:02 +02:00
}