2012-08-21 08:21:33 +02:00
// Copyright (c) 2010 Satoshi Nakamoto
2014-11-27 17:26:22 +01:00
// Copyright (c) 2009-2014 The Bitcoin developers
2014-12-11 16:58:38 +01:00
// Copyright (c) 2014 The Darkcoin developers
2012-08-21 08:21:33 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-11-20 14:18:57 +01:00
# include "rpcserver.h"
2015-01-08 19:02:10 +01:00
# include "base58.h"
2013-05-07 15:16:25 +02:00
# include "chainparams.h"
2012-08-21 08:21:33 +02:00
# include "init.h"
2013-04-13 07:13:08 +02:00
# include "net.h"
# include "main.h"
2013-07-31 15:43:35 +02:00
# include "miner.h"
2013-12-18 20:46:43 +01:00
# ifdef ENABLE_WALLET
# include "db.h"
2013-04-13 07:13:08 +02:00
# include "wallet.h"
2013-12-18 20:46:43 +01:00
# endif
2013-04-13 07:13:08 +02:00
# include <stdint.h>
# include "json/json_spirit_utils.h"
# include "json/json_spirit_value.h"
2012-08-21 08:21:33 +02:00
using namespace json_spirit ;
using namespace std ;
2013-12-08 15:26:08 +01:00
# ifdef ENABLE_WALLET
// Key used by getwork miners.
2013-05-30 15:51:41 +02:00
// Allocated in InitRPCMining, free'd in ShutdownRPCMining
static CReserveKey * pMiningKey = NULL ;
void InitRPCMining ( )
{
2013-08-25 06:00:02 +02:00
if ( ! pwalletMain )
return ;
2013-05-30 15:51:41 +02:00
// getwork/getblocktemplate mining rewards paid here:
pMiningKey = new CReserveKey ( pwalletMain ) ;
}
void ShutdownRPCMining ( )
{
2013-08-25 06:00:02 +02:00
if ( ! pMiningKey )
return ;
2013-05-30 15:51:41 +02:00
delete pMiningKey ; pMiningKey = NULL ;
}
2013-12-08 15:26:08 +01:00
# else
void InitRPCMining ( )
{
}
void ShutdownRPCMining ( )
{
}
# endif
2013-05-30 15:51:41 +02:00
2013-05-17 12:57:05 +02: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.
Value 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 )
lookup = pb - > nHeight % 2016 + 1 ;
// 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 ;
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
2014-05-05 20:08:13 +02:00
return ( int64_t ) ( workDiff . getdouble ( ) / timeDiff ) ;
2013-05-17 12:57:05 +02:00
}
Value getnetworkhashps ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getnetworkhashps ( blocks height ) \n "
" \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 "
" 1. blocks (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change. \n "
" 2. height (numeric, optional, default=-1) To estimate at the time of the given height. \n "
" \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
return GetNetworkHashPS ( params . size ( ) > 0 ? params [ 0 ] . get_int ( ) : 120 , params . size ( ) > 1 ? params [ 1 ] . get_int ( ) : - 1 ) ;
}
2013-12-08 15:26:08 +01:00
# ifdef ENABLE_WALLET
2012-08-21 08:21:33 +02:00
Value getgenerate ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
throw runtime_error (
" getgenerate \n "
2013-10-29 12:29:44 +01:00
" \n Return if the server is set to generate coins or not. The default is false. \n "
2014-11-27 17:26:22 +01:00
" It is set with the command line argument -gen (or darkcoin.conf setting gen) \n "
2013-10-29 12:29:44 +01:00
" It can also be set with the setgenerate call. \n "
" \n Result \n "
" true|false (boolean) If the server is set to generate coins or not \n "
" \n Examples: \n "
+ HelpExampleCli ( " getgenerate " , " " )
+ HelpExampleRpc ( " getgenerate " , " " )
) ;
2012-08-21 08:21:33 +02:00
2013-08-25 06:00:02 +02:00
if ( ! pMiningKey )
return false ;
2013-04-28 17:37:50 +02:00
return GetBoolArg ( " -gen " , false ) ;
2012-08-21 08:21:33 +02:00
}
Value setgenerate ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" setgenerate generate ( genproclimit ) \n "
" \n Set 'generate' true or false to turn generation on or off. \n "
" Generation is limited to 'genproclimit' processors, -1 is unlimited. \n "
" See the getgenerate call for the current setting. \n "
" \n Arguments: \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 "
2013-11-21 05:07:55 +01:00
" Note: in -regtest mode, genproclimit controls how many blocks are generated immediately. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
" \n Set the generation on with a limit of one processor \n "
+ HelpExampleCli ( " setgenerate " , " true 1 " ) +
" \n Check the setting \n "
+ HelpExampleCli ( " getgenerate " , " " ) +
" \n Turn off generation \n "
+ HelpExampleCli ( " setgenerate " , " false " ) +
" \n Using json rpc \n "
+ HelpExampleRpc ( " setgenerate " , " true, 1 " )
) ;
2012-08-21 08:21:33 +02:00
2013-11-21 05:07:55 +01:00
if ( pwalletMain = = NULL )
throw JSONRPCError ( RPC_METHOD_NOT_FOUND , " Method not found (disabled) " ) ;
2012-08-21 08:21:33 +02:00
bool fGenerate = true ;
if ( params . size ( ) > 0 )
fGenerate = params [ 0 ] . get_bool ( ) ;
2013-11-21 05:07:55 +01:00
int nGenProcLimit = - 1 ;
2012-08-21 08:21:33 +02:00
if ( params . size ( ) > 1 )
{
2013-11-21 05:07:55 +01:00
nGenProcLimit = params [ 1 ] . get_int ( ) ;
2012-08-21 08:21:33 +02:00
if ( nGenProcLimit = = 0 )
fGenerate = false ;
}
2013-11-21 05:07:55 +01:00
// -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 " ) ;
2014-03-21 22:54:22 +01:00
mapArgs [ " -genproclimit " ] = itostr ( nGenProcLimit ) ;
2013-11-21 05:07:55 +01:00
GenerateBitcoins ( fGenerate , pwalletMain , nGenProcLimit ) ;
}
2012-08-21 08:21:33 +02:00
return Value : : null ;
}
Value gethashespersec ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
throw runtime_error (
" gethashespersec \n "
2013-10-29 12:29:44 +01:00
" \n Returns a recent hashes per second performance measurement while generating. \n "
" See the getgenerate and setgenerate calls to turn generation on and off. \n "
" \n Result: \n "
" n (numeric) The recent hashes per second when generation is on (will return 0 if generation is off) \n "
" \n Examples: \n "
+ HelpExampleCli ( " gethashespersec " , " " )
+ HelpExampleRpc ( " gethashespersec " , " " )
) ;
2012-08-21 08:21:33 +02:00
if ( GetTimeMillis ( ) - nHPSTimerStart > 8000 )
2014-05-05 20:08:13 +02:00
return ( int64_t ) 0 ;
return ( int64_t ) dHashesPerSec ;
2012-08-21 08:21:33 +02:00
}
2013-12-08 15:26:08 +01:00
# endif
2012-08-21 08:21:33 +02:00
Value getmininginfo ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
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 "
" \" currentblocktx \" : nnn, (numeric) The last block transaction \n "
" \" difficulty \" : xxx.xxxxx (numeric) The current difficulty \n "
" \" errors \" : \" ... \" (string) Current errors \n "
" \" generate \" : true|false (boolean) If the generation is on or off (see getgenerate or setgenerate calls) \n "
" \" genproclimit \" : n (numeric) The processor limit for generation. -1 if no generation. (see getgenerate or setgenerate calls) \n "
" \" hashespersec \" : n (numeric) The hashes per second of the generation, or 0 if no generation. \n "
" \" pooledtx \" : n (numeric) The size of the mem pool \n "
" \" testnet \" : true|false (boolean) If using testnet or not \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getmininginfo " , " " )
+ HelpExampleRpc ( " getmininginfo " , " " )
) ;
2012-08-21 08:21:33 +02:00
Object obj ;
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 ) ) ;
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 ( " genproclimit " , ( int ) GetArg ( " -genproclimit " , - 1 ) ) ) ;
2013-05-17 12:57:05 +02:00
obj . push_back ( Pair ( " networkhashps " , getnetworkhashps ( params , false ) ) ) ;
2013-04-28 17:37:50 +02:00
obj . push_back ( Pair ( " pooledtx " , ( uint64_t ) mempool . size ( ) ) ) ;
2013-05-07 15:16:25 +02:00
obj . push_back ( Pair ( " testnet " , TestNet ( ) ) ) ;
2013-12-08 15:26:08 +01:00
# ifdef ENABLE_WALLET
obj . push_back ( Pair ( " generate " , getgenerate ( params , false ) ) ) ;
obj . push_back ( Pair ( " hashespersec " , gethashespersec ( params , false ) ) ) ;
# endif
2012-08-21 08:21:33 +02:00
return obj ;
}
2013-12-08 15:26:08 +01:00
# ifdef ENABLE_WALLET
2012-08-21 08:21:33 +02:00
Value getwork ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 1 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getwork ( \" data \" ) \n "
" \n If 'data' is not specified, it returns the formatted hash data to work on. \n "
" If 'data' is specified, tries to solve the block and returns true if it was successful. \n "
" \n Arguments: \n "
" 1. \" data \" (string, optional) The hex encoded data to solve \n "
" \n Result (when 'data' is not specified): \n "
" { \n "
" \" midstate \" : \" xxxx \" , (string) The precomputed hash state after hashing the first half of the data (DEPRECATED) \n " // deprecated
" \" data \" : \" xxxxx \" , (string) The block data \n "
" \" hash1 \" : \" xxxxx \" , (string) The formatted hash buffer for second hash (DEPRECATED) \n " // deprecated
" \" target \" : \" xxxx \" (string) The little endian hash target \n "
" } \n "
" \n Result (when 'data' is specified): \n "
" true|false (boolean) If solving the block specified in the 'data' was successfull \n "
" \n Examples: \n "
+ HelpExampleCli ( " getwork " , " " )
+ HelpExampleRpc ( " getwork " , " " )
) ;
2012-08-21 08:21:33 +02:00
if ( vNodes . empty ( ) )
2014-12-12 12:44:25 +01:00
throw JSONRPCError ( RPC_CLIENT_NOT_CONNECTED , " Darkcoin is not connected! " ) ;
2012-08-21 08:21:33 +02:00
if ( IsInitialBlockDownload ( ) )
2014-12-12 12:44:25 +01:00
throw JSONRPCError ( RPC_CLIENT_IN_INITIAL_DOWNLOAD , " Darkcoin is downloading blocks... " ) ;
2012-08-21 08:21:33 +02:00
typedef map < uint256 , pair < CBlock * , CScript > > mapNewBlock_t ;
static mapNewBlock_t mapNewBlock ; // FIXME: thread safety
2012-12-19 21:21:21 +01:00
static vector < CBlockTemplate * > vNewBlockTemplate ;
2012-08-21 08:21:33 +02:00
if ( params . size ( ) = = 0 )
{
// Update block
static unsigned int nTransactionsUpdatedLast ;
static CBlockIndex * pindexPrev ;
2013-04-13 07:13:08 +02:00
static int64_t nStart ;
2012-12-19 21:21:21 +01:00
static 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 > 60 ) )
2012-08-21 08:21:33 +02:00
{
2013-10-10 23:07:44 +02:00
if ( pindexPrev ! = chainActive . Tip ( ) )
2012-08-21 08:21:33 +02:00
{
// Deallocate old blocks since they're obsolete now
mapNewBlock . clear ( ) ;
2012-12-19 21:21:21 +01:00
BOOST_FOREACH ( CBlockTemplate * pblocktemplate , vNewBlockTemplate )
delete pblocktemplate ;
vNewBlockTemplate . clear ( ) ;
2012-08-21 08:21:33 +02:00
}
// Clear pindexPrev so future getworks make a new block, despite any failures from here on
pindexPrev = NULL ;
2014-12-09 02:17:57 +01:00
// Store the chainActive.Tip() used before CreateNewBlock, to avoid races
2013-08-27 07:51:57 +02:00
nTransactionsUpdatedLast = mempool . GetTransactionsUpdated ( ) ;
2013-10-10 23:07:44 +02:00
CBlockIndex * pindexPrevNew = chainActive . Tip ( ) ;
2012-08-21 08:21:33 +02:00
nStart = GetTime ( ) ;
// Create new block
2013-08-24 06:33:46 +02:00
pblocktemplate = CreateNewBlockWithKey ( * pMiningKey ) ;
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-12-19 21:21:21 +01:00
vNewBlockTemplate . push_back ( pblocktemplate ) ;
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
2012-08-21 08:21:33 +02:00
// Update nTime
2013-01-08 23:58:06 +01:00
UpdateTime ( * pblock , pindexPrev ) ;
2012-08-21 08:21:33 +02:00
pblock - > nNonce = 0 ;
// Update nExtraNonce
static unsigned int nExtraNonce = 0 ;
IncrementExtraNonce ( pblock , pindexPrev , nExtraNonce ) ;
// Save
mapNewBlock [ pblock - > hashMerkleRoot ] = make_pair ( pblock , pblock - > vtx [ 0 ] . vin [ 0 ] . scriptSig ) ;
// Pre-build hash buffers
char pmidstate [ 32 ] ;
char pdata [ 128 ] ;
char phash1 [ 64 ] ;
FormatHashBuffers ( pblock , pmidstate , pdata , phash1 ) ;
uint256 hashTarget = CBigNum ( ) . SetCompact ( pblock - > nBits ) . getuint256 ( ) ;
Object result ;
result . push_back ( Pair ( " midstate " , HexStr ( BEGIN ( pmidstate ) , END ( pmidstate ) ) ) ) ; // deprecated
result . push_back ( Pair ( " data " , HexStr ( BEGIN ( pdata ) , END ( pdata ) ) ) ) ;
result . push_back ( Pair ( " hash1 " , HexStr ( BEGIN ( phash1 ) , END ( phash1 ) ) ) ) ; // deprecated
result . push_back ( Pair ( " target " , HexStr ( BEGIN ( hashTarget ) , END ( hashTarget ) ) ) ) ;
return result ;
}
else
{
// Parse parameters
vector < unsigned char > vchData = ParseHex ( params [ 0 ] . get_str ( ) ) ;
if ( vchData . size ( ) ! = 128 )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter " ) ;
2012-08-21 08:21:33 +02:00
CBlock * pdata = ( CBlock * ) & vchData [ 0 ] ;
// Byte reverse
for ( int i = 0 ; i < 128 / 4 ; i + + )
( ( unsigned int * ) pdata ) [ i ] = ByteReverse ( ( ( unsigned int * ) pdata ) [ i ] ) ;
// Get saved block
if ( ! mapNewBlock . count ( pdata - > hashMerkleRoot ) )
return false ;
CBlock * pblock = mapNewBlock [ pdata - > hashMerkleRoot ] . first ;
pblock - > nTime = pdata - > nTime ;
pblock - > nNonce = pdata - > nNonce ;
pblock - > vtx [ 0 ] . vin [ 0 ] . scriptSig = mapNewBlock [ pdata - > hashMerkleRoot ] . second ;
pblock - > hashMerkleRoot = pblock - > BuildMerkleTree ( ) ;
2013-08-25 06:00:02 +02:00
assert ( pwalletMain ! = NULL ) ;
2013-05-23 20:13:05 +02:00
return CheckWork ( pblock , * pwalletMain , * pMiningKey ) ;
2012-08-21 08:21:33 +02:00
}
}
2013-12-08 15:26:08 +01:00
# endif
2012-08-21 08:21:33 +02:00
Value getblocktemplate ( const Array & params , bool fHelp )
{
2012-10-24 18:39:46 +02:00
if ( fHelp | | params . size ( ) > 1 )
2012-08-21 08:21:33 +02:00
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getblocktemplate ( \" jsonrequestobject \" ) \n "
" \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 "
" See https://en.bitcoin.it/wiki/BIP_0022 for full specification. \n "
" \n Arguments: \n "
" 1. \" jsonrequestobject \" (string, optional) A json object in the following spec \n "
" { \n "
" \" mode \" : \" template \" (string, optional) This must be set to \" template \" or omitted \n "
" \" capabilities \" :[ (array, optional) A list of strings \n "
" \" support \" (string) client side supported feature, 'longpoll', 'coinbasetxn', 'coinbasevalue', 'proposal', 'serverlist', 'workid' \n "
" ,... \n "
" ] \n "
" } \n "
" \n "
" \n Result: \n "
" { \n "
" \" version \" : n, (numeric) The block version \n "
" \" previousblockhash \" : \" xxxx \" , (string) The hash of current highest block \n "
" \" transactions \" : [ (array) contents of non-coinbase transactions that should be included in the next block \n "
" { \n "
" \" data \" : \" xxxx \" , (string) transaction data encoded in hexadecimal (byte-for-byte) \n "
" \" hash \" : \" xxxx \" , (string) hash/id encoded in little-endian hexadecimal \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 "
" ,... \n "
" ], \n "
" \" 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 number of SigOps, as counted for purposes of block limits; if key is not present, sigop count is unknown and clients MUST NOT assume there aren't any \n "
" \" required \" : true|false (boolean) if provided and true, this transaction must be in the final block \n "
" } \n "
" ,... \n "
" ], \n "
" \" coinbaseaux \" : { (json object) data that should be included in the coinbase's scriptSig content \n "
" \" flags \" : \" flags \" (string) \n "
" }, \n "
" \" 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 "
" ,... \n "
" ], \n "
" \" noncerange \" : \" 00000000ffffffff \" , (string) A range of valid nonces \n "
" \" sigoplimit \" : n, (numeric) limit of sigops in blocks \n "
" \" sizelimit \" : n, (numeric) limit of block size \n "
" \" curtime \" : ttt, (numeric) current timestamp in seconds since epoch (Jan 1 1970 GMT) \n "
" \" bits \" : \" xxx \" , (string) compressed target of next block \n "
" \" height \" : n (numeric) The height of the next block \n "
2015-01-08 19:02:10 +01:00
" \" payee \" : required payee \n "
" \" payee_amount \" : required amount to pay \n "
" \" votes \" : show vote candidates for this block \n "
2013-10-29 12:29:44 +01:00
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getblocktemplate " , " " )
+ HelpExampleRpc ( " getblocktemplate " , " " )
) ;
2012-08-21 08:21:33 +02:00
std : : string strMode = " template " ;
if ( params . size ( ) > 0 )
{
const Object & oparam = params [ 0 ] . get_obj ( ) ;
const Value & modeval = find_value ( oparam , " mode " ) ;
if ( modeval . type ( ) = = str_type )
strMode = modeval . get_str ( ) ;
2012-09-01 09:12:50 +02:00
else if ( modeval . type ( ) = = null_type )
{
/* 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-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
if ( vNodes . empty ( ) )
2014-12-12 12:44:25 +01:00
throw JSONRPCError ( RPC_CLIENT_NOT_CONNECTED , " Darkcoin is not connected! " ) ;
2012-08-21 08:21:33 +02:00
if ( IsInitialBlockDownload ( ) )
2014-12-12 12:44:25 +01:00
throw JSONRPCError ( RPC_CLIENT_IN_INITIAL_DOWNLOAD , " Darkcoin is downloading blocks... " ) ;
2012-08-21 08:21:33 +02:00
// Update block
static unsigned int nTransactionsUpdatedLast ;
static CBlockIndex * pindexPrev ;
2013-04-13 07:13:08 +02:00
static int64_t nStart ;
2012-12-19 21:21:21 +01:00
static 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
pindexPrev = NULL ;
2014-12-09 02:17:57 +01:00
// Store the chainActive.Tip() used before CreateNewBlock, to avoid races
2013-08-27 07:51:57 +02:00
nTransactionsUpdatedLast = mempool . GetTransactionsUpdated ( ) ;
2013-10-10 23:07:44 +02:00
CBlockIndex * pindexPrevNew = chainActive . Tip ( ) ;
2012-08-21 08:21:33 +02:00
nStart = GetTime ( ) ;
// Create new block
2012-12-19 21:21:21 +01:00
if ( pblocktemplate )
2012-08-21 08:21:33 +02:00
{
2012-12-19 21:21:21 +01:00
delete pblocktemplate ;
pblocktemplate = NULL ;
2012-08-21 08:21:33 +02:00
}
2013-08-24 06:45:17 +02:00
CScript scriptDummy = CScript ( ) < < OP_TRUE ;
pblocktemplate = 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
2012-08-21 08:21:33 +02:00
// Update nTime
2013-01-08 23:58:06 +01:00
UpdateTime ( * pblock , pindexPrev ) ;
2012-08-21 08:21:33 +02:00
pblock - > nNonce = 0 ;
Array transactions ;
map < uint256 , int64_t > setTxIndex ;
int i = 0 ;
BOOST_FOREACH ( CTransaction & tx , pblock - > vtx )
{
uint256 txHash = tx . GetHash ( ) ;
setTxIndex [ txHash ] = i + + ;
if ( tx . IsCoinBase ( ) )
continue ;
Object entry ;
CDataStream ssTx ( SER_NETWORK , PROTOCOL_VERSION ) ;
ssTx < < tx ;
entry . push_back ( Pair ( " data " , HexStr ( ssTx . begin ( ) , ssTx . end ( ) ) ) ) ;
entry . push_back ( Pair ( " hash " , txHash . GetHex ( ) ) ) ;
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
Array deps ;
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 ] ) ) ;
entry . push_back ( Pair ( " sigops " , pblocktemplate - > vTxSigOps [ index_in_template ] ) ) ;
2012-08-21 08:21:33 +02:00
transactions . push_back ( entry ) ;
}
Object aux ;
aux . push_back ( Pair ( " flags " , HexStr ( COINBASE_FLAGS . begin ( ) , COINBASE_FLAGS . end ( ) ) ) ) ;
uint256 hashTarget = CBigNum ( ) . SetCompact ( pblock - > nBits ) . getuint256 ( ) ;
static Array aMutable ;
if ( aMutable . empty ( ) )
{
aMutable . push_back ( " time " ) ;
aMutable . push_back ( " transactions " ) ;
aMutable . push_back ( " prevblock " ) ;
}
2015-01-08 19:02:10 +01:00
Array aVotes ;
2012-08-21 08:21:33 +02:00
Object result ;
result . push_back ( Pair ( " version " , pblock - > nVersion ) ) ;
result . push_back ( Pair ( " previousblockhash " , pblock - > hashPrevBlock . GetHex ( ) ) ) ;
result . push_back ( Pair ( " transactions " , transactions ) ) ;
result . push_back ( Pair ( " coinbaseaux " , aux ) ) ;
result . push_back ( Pair ( " coinbasevalue " , ( int64_t ) pblock - > vtx [ 0 ] . vout [ 0 ] . nValue ) ) ;
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 " ) ) ;
result . push_back ( Pair ( " sigoplimit " , ( int64_t ) MAX_BLOCK_SIGOPS ) ) ;
result . push_back ( Pair ( " sizelimit " , ( int64_t ) MAX_BLOCK_SIZE ) ) ;
result . push_back ( Pair ( " curtime " , ( int64_t ) pblock - > nTime ) ) ;
result . push_back ( Pair ( " bits " , HexBits ( pblock - > nBits ) ) ) ;
result . push_back ( Pair ( " height " , ( int64_t ) ( pindexPrev - > nHeight + 1 ) ) ) ;
2015-01-08 19:02:10 +01:00
result . push_back ( Pair ( " votes " , aVotes ) ) ;
if ( pblock - > payee ! = CScript ( ) ) {
CTxDestination address1 ;
ExtractDestination ( pblock - > payee , address1 ) ;
CBitcoinAddress address2 ( address1 ) ;
result . push_back ( Pair ( " payee " , address2 . ToString ( ) . c_str ( ) ) ) ;
result . push_back ( Pair ( " payee_amount " , ( int64_t ) GetMasternodePayment ( pindexPrev - > nHeight + 1 , pblock - > vtx [ 0 ] . GetValueOut ( ) ) ) ) ;
} else {
result . push_back ( Pair ( " payee " , " " ) ) ;
result . push_back ( Pair ( " payee_amount " , " " ) ) ;
}
bool MasternodePayments = false ;
if ( TestNet ( ) ) {
if ( pblock - > nTime > START_MASTERNODE_PAYMENTS_TESTNET ) MasternodePayments = true ;
} else {
if ( pblock - > nTime > START_MASTERNODE_PAYMENTS ) MasternodePayments = true ;
}
result . push_back ( Pair ( " masternode_payments " , MasternodePayments ) ) ;
result . push_back ( Pair ( " enforce_masternode_payments " , true ) ) ;
2012-08-21 08:21:33 +02:00
return result ;
}
Value submitblock ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
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 "
" \n Arguments \n "
" 1. \" hexdata \" (string, required) the hex-encoded block data to submit \n "
" 2. \" jsonparametersobject \" (string, optional) object of optional parameters \n "
" { \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
vector < unsigned char > blockData ( ParseHex ( params [ 0 ] . get_str ( ) ) ) ;
CDataStream ssBlock ( blockData , SER_NETWORK , PROTOCOL_VERSION ) ;
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
CBlock pblock ;
2012-08-21 08:21:33 +02:00
try {
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
ssBlock > > pblock ;
2012-08-21 08:21:33 +02:00
}
catch ( std : : exception & e ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , " Block decode failed " ) ;
2012-08-21 08:21:33 +02:00
}
2013-01-27 00:14:11 +01:00
CValidationState state ;
bool fAccepted = ProcessBlock ( state , NULL , & pblock ) ;
2012-08-21 08:21:33 +02:00
if ( ! fAccepted )
2013-01-27 00:14:11 +01:00
return " rejected " ; // TODO: report validation state
2012-08-21 08:21:33 +02:00
2012-08-21 08:41:46 +02:00
return Value : : null ;
2012-08-21 08:21:33 +02:00
}