1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "bitcoinrpc.h"
9 using namespace json_spirit;
12 extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, json_spirit::Object& entry);
13 extern enum Checkpoints::CPMode CheckpointsMode;
15 double GetDifficulty(const CBlockIndex* blockindex)
17 // Floating point number that is a multiple of the minimum difficulty,
18 // minimum difficulty = 1.0.
19 if (blockindex == NULL)
21 if (pindexBest == NULL)
24 blockindex = GetLastBlockIndex(pindexBest, false);
27 int nShift = (blockindex->nBits >> 24) & 0xff;
30 (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);
46 double GetPoWMHashPS()
48 int nPoWInterval = 72;
49 int64 nTargetSpacingWorkMin = 30, nTargetSpacingWork = 30;
51 CBlockIndex* pindex = pindexGenesisBlock;
52 CBlockIndex* pindexPrevWork = pindexGenesisBlock;
56 if (pindex->IsProofOfWork())
58 int64 nActualSpacingWork = pindex->GetBlockTime() - pindexPrevWork->GetBlockTime();
59 nTargetSpacingWork = ((nPoWInterval - 1) * nTargetSpacingWork + nActualSpacingWork + nActualSpacingWork) / (nPoWInterval + 1);
60 nTargetSpacingWork = max(nTargetSpacingWork, nTargetSpacingWorkMin);
61 pindexPrevWork = pindex;
64 pindex = pindex->pnext;
67 return GetDifficulty() * 4294.967296 / nTargetSpacingWork;
70 double GetPoSKernelPS()
72 int nPoSInterval = 72;
73 double dStakeKernelsTriedAvg = 0;
74 int nStakesHandled = 0, nStakesTime = 0;
76 CBlockIndex* pindex = pindexBest;;
77 CBlockIndex* pindexPrevStake = NULL;
79 while (pindex && nStakesHandled < nPoSInterval)
81 if (pindex->IsProofOfStake())
83 dStakeKernelsTriedAvg += GetDifficulty(pindex) * 4294967296.0;
84 nStakesTime += pindexPrevStake ? (pindexPrevStake->nTime - pindex->nTime) : 0;
85 pindexPrevStake = pindex;
89 pindex = pindex->pprev;
92 return dStakeKernelsTriedAvg / nStakesTime;
95 Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPrintTransactionDetail)
98 result.push_back(Pair("hash", block.GetHash().GetHex()));
99 CMerkleTx txGen(block.vtx[0]);
100 txGen.SetMerkleBranch(&block);
101 result.push_back(Pair("confirmations", (int)txGen.GetDepthInMainChain()));
102 result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)));
103 result.push_back(Pair("height", blockindex->nHeight));
104 result.push_back(Pair("version", block.nVersion));
105 result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
106 result.push_back(Pair("mint", ValueFromAmount(blockindex->nMint)));
107 result.push_back(Pair("time", (boost::int64_t)block.GetBlockTime()));
108 result.push_back(Pair("nonce", (boost::uint64_t)block.nNonce));
109 result.push_back(Pair("bits", HexBits(block.nBits)));
110 result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
111 result.push_back(Pair("blocktrust", leftTrim(blockindex->GetBlockTrust().GetHex(), '0')));
112 result.push_back(Pair("chaintrust", leftTrim(blockindex->nChainTrust.GetHex(), '0')));
113 if (blockindex->pprev)
114 result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
115 if (blockindex->pnext)
116 result.push_back(Pair("nextblockhash", blockindex->pnext->GetBlockHash().GetHex()));
118 result.push_back(Pair("flags", strprintf("%s%s", blockindex->IsProofOfStake()? "proof-of-stake" : "proof-of-work", blockindex->GeneratedStakeModifier()? " stake-modifier": "")));
119 result.push_back(Pair("proofhash", blockindex->IsProofOfStake()? blockindex->hashProofOfStake.GetHex() : blockindex->GetBlockHash().GetHex()));
120 result.push_back(Pair("entropybit", (int)blockindex->GetStakeEntropyBit()));
121 result.push_back(Pair("modifier", strprintf("%016"PRI64x, blockindex->nStakeModifier)));
122 result.push_back(Pair("modifierchecksum", strprintf("%08x", blockindex->nStakeModifierChecksum)));
124 BOOST_FOREACH (const CTransaction& tx, block.vtx)
126 if (fPrintTransactionDetail)
130 entry.push_back(Pair("txid", tx.GetHash().GetHex()));
131 TxToJSON(tx, 0, entry);
133 txinfo.push_back(entry);
136 txinfo.push_back(tx.GetHash().GetHex());
139 result.push_back(Pair("tx", txinfo));
141 if ( block.IsProofOfStake() || (!fTestNet && block.GetBlockTime() < ENTROPY_SWITCH_TIME) )
142 result.push_back(Pair("signature", HexStr(block.vchBlockSig.begin(), block.vchBlockSig.end())));
147 Value getbestblockhash(const Array& params, bool fHelp)
149 if (fHelp || params.size() != 0)
152 "Returns the hash of the best block in the longest block chain.");
154 return hashBestChain.GetHex();
157 Value getblockcount(const Array& params, bool fHelp)
159 if (fHelp || params.size() != 0)
162 "Returns the number of blocks in the longest block chain.");
168 Value getdifficulty(const Array& params, bool fHelp)
170 if (fHelp || params.size() != 0)
173 "Returns the difficulty as a multiple of the minimum difficulty.");
176 obj.push_back(Pair("proof-of-work", GetDifficulty()));
177 obj.push_back(Pair("proof-of-stake", GetDifficulty(GetLastBlockIndex(pindexBest, true))));
178 obj.push_back(Pair("search-interval", (int)nLastCoinStakeSearchInterval));
183 Value settxfee(const Array& params, bool fHelp)
185 if (fHelp || params.size() < 1 || params.size() > 1 || AmountFromValue(params[0]) < MIN_TX_FEE)
187 "settxfee <amount>\n"
188 "<amount> is a real and is rounded to the nearest 0.01");
190 nTransactionFee = AmountFromValue(params[0]);
191 nTransactionFee = (nTransactionFee / CENT) * CENT; // round to cent
196 Value getrawmempool(const Array& params, bool fHelp)
198 if (fHelp || params.size() != 0)
201 "Returns all transaction ids in memory pool.");
203 vector<uint256> vtxid;
204 mempool.queryHashes(vtxid);
207 BOOST_FOREACH(const uint256& hash, vtxid)
208 a.push_back(hash.ToString());
213 Value getblockhash(const Array& params, bool fHelp)
215 if (fHelp || params.size() != 1)
217 "getblockhash <index>\n"
218 "Returns hash of block in best-block-chain at <index>.");
220 int nHeight = params[0].get_int();
221 if (nHeight < 0 || nHeight > nBestHeight)
222 throw runtime_error("Block number out of range.");
224 CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
225 return pblockindex->phashBlock->GetHex();
228 Value getblock(const Array& params, bool fHelp)
230 if (fHelp || params.size() < 1 || params.size() > 2)
232 "getblock <hash> [txinfo]\n"
233 "txinfo optional to print more detailed tx info\n"
234 "Returns details of a block with given block-hash.");
236 std::string strHash = params[0].get_str();
237 uint256 hash(strHash);
239 if (mapBlockIndex.count(hash) == 0)
240 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
243 CBlockIndex* pblockindex = mapBlockIndex[hash];
244 block.ReadFromDisk(pblockindex, true);
246 return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
249 Value getblockbynumber(const Array& params, bool fHelp)
251 if (fHelp || params.size() < 1 || params.size() > 2)
253 "getblock <number> [txinfo]\n"
254 "txinfo optional to print more detailed tx info\n"
255 "Returns details of a block with given block-number.");
257 int nHeight = params[0].get_int();
258 if (nHeight < 0 || nHeight > nBestHeight)
259 throw runtime_error("Block number out of range.");
262 CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
263 while (pblockindex->nHeight > nHeight)
264 pblockindex = pblockindex->pprev;
266 uint256 hash = *pblockindex->phashBlock;
268 pblockindex = mapBlockIndex[hash];
269 block.ReadFromDisk(pblockindex, true);
271 return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
274 // ppcoin: get information of sync-checkpoint
275 Value getcheckpoint(const Array& params, bool fHelp)
277 if (fHelp || params.size() != 0)
280 "Show info of synchronized checkpoint.\n");
283 CBlockIndex* pindexCheckpoint;
285 result.push_back(Pair("synccheckpoint", Checkpoints::hashSyncCheckpoint.ToString().c_str()));
286 pindexCheckpoint = mapBlockIndex[Checkpoints::hashSyncCheckpoint];
287 result.push_back(Pair("height", pindexCheckpoint->nHeight));
288 result.push_back(Pair("timestamp", DateTimeStrFormat(pindexCheckpoint->GetBlockTime()).c_str()));
290 // Check that the block satisfies synchronized checkpoint
291 if (CheckpointsMode == Checkpoints::STRICT)
292 result.push_back(Pair("policy", "strict"));
294 if (CheckpointsMode == Checkpoints::ADVISORY)
295 result.push_back(Pair("policy", "advisory"));
297 if (CheckpointsMode == Checkpoints::PERMISSIVE)
298 result.push_back(Pair("policy", "permissive"));
300 if (mapArgs.count("-checkpointkey"))
301 result.push_back(Pair("checkpointmaster", true));