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);
14 double GetDifficulty(const CBlockIndex* blockindex)
16 // Floating point number that is a multiple of the minimum difficulty,
17 // minimum difficulty = 1.0.
18 if (blockindex == NULL)
20 if (pindexBest == NULL)
23 blockindex = GetLastBlockIndex(pindexBest, false);
26 int nShift = (blockindex->nBits >> 24) & 0xff;
29 (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);
46 Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPrintTransactionDetail)
49 result.push_back(Pair("hash", block.GetHash().GetHex()));
50 CMerkleTx txGen(block.vtx[0]);
51 txGen.SetMerkleBranch(&block);
52 result.push_back(Pair("confirmations", (int)txGen.GetDepthInMainChain()));
53 result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)));
54 result.push_back(Pair("height", blockindex->nHeight));
55 result.push_back(Pair("version", block.nVersion));
56 result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
57 result.push_back(Pair("mint", ValueFromAmount(blockindex->nMint)));
58 result.push_back(Pair("time", (boost::int64_t)block.GetBlockTime()));
59 result.push_back(Pair("nonce", (boost::uint64_t)block.nNonce));
60 result.push_back(Pair("bits", HexBits(block.nBits)));
61 result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
63 if (blockindex->pprev)
64 result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
65 if (blockindex->pnext)
66 result.push_back(Pair("nextblockhash", blockindex->pnext->GetBlockHash().GetHex()));
68 result.push_back(Pair("flags", strprintf("%s%s", blockindex->IsProofOfStake()? "proof-of-stake" : "proof-of-work", blockindex->GeneratedStakeModifier()? " stake-modifier": "")));
69 result.push_back(Pair("proofhash", blockindex->IsProofOfStake()? blockindex->hashProofOfStake.GetHex() : blockindex->GetBlockHash().GetHex()));
70 result.push_back(Pair("entropybit", (int)blockindex->GetStakeEntropyBit()));
71 result.push_back(Pair("modifier", strprintf("%016"PRI64x, blockindex->nStakeModifier)));
72 result.push_back(Pair("modifierchecksum", strprintf("%08x", blockindex->nStakeModifierChecksum)));
74 BOOST_FOREACH (const CTransaction& tx, block.vtx)
76 if (fPrintTransactionDetail)
80 entry.push_back(Pair("txid", tx.GetHash().GetHex()));
81 TxToJSON(tx, 0, entry);
83 txinfo.push_back(entry);
86 txinfo.push_back(tx.GetHash().GetHex());
89 result.push_back(Pair("tx", txinfo));
91 if ( block.IsProofOfStake() || (!fTestNet && block.GetBlockTime() < CHAINCHECKS_SWITCH_TIME) )
92 result.push_back(Pair("signature", HexStr(block.vchBlockSig.begin(), block.vchBlockSig.end())));
97 Value getbestblockhash(const Array& params, bool fHelp)
99 if (fHelp || params.size() != 0)
102 "Returns the hash of the best block in the longest block chain.");
104 return hashBestChain.GetHex();
107 Value getblockcount(const Array& params, bool fHelp)
109 if (fHelp || params.size() != 0)
112 "Returns the number of blocks in the longest block chain.");
118 Value getdifficulty(const Array& params, bool fHelp)
120 if (fHelp || params.size() != 0)
123 "Returns the difficulty as a multiple of the minimum difficulty.");
126 obj.push_back(Pair("proof-of-work", GetDifficulty()));
127 obj.push_back(Pair("proof-of-stake", GetDifficulty(GetLastBlockIndex(pindexBest, true))));
128 obj.push_back(Pair("search-interval", (int)nLastCoinStakeSearchInterval));
133 Value settxfee(const Array& params, bool fHelp)
135 if (fHelp || params.size() < 1 || params.size() > 1 || AmountFromValue(params[0]) < MIN_TX_FEE)
137 "settxfee <amount>\n"
138 "<amount> is a real and is rounded to the nearest 0.01");
140 nTransactionFee = AmountFromValue(params[0]);
141 nTransactionFee = (nTransactionFee / CENT) * CENT; // round to cent
146 Value getrawmempool(const Array& params, bool fHelp)
148 if (fHelp || params.size() != 0)
151 "Returns all transaction ids in memory pool.");
153 vector<uint256> vtxid;
154 mempool.queryHashes(vtxid);
157 BOOST_FOREACH(const uint256& hash, vtxid)
158 a.push_back(hash.ToString());
163 Value getblockhash(const Array& params, bool fHelp)
165 if (fHelp || params.size() != 1)
167 "getblockhash <index>\n"
168 "Returns hash of block in best-block-chain at <index>.");
170 int nHeight = params[0].get_int();
171 if (nHeight < 0 || nHeight > nBestHeight)
172 throw runtime_error("Block number out of range.");
174 CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
175 return pblockindex->phashBlock->GetHex();
178 Value getblock(const Array& params, bool fHelp)
180 if (fHelp || params.size() < 1 || params.size() > 2)
182 "getblock <hash> [txinfo]\n"
183 "txinfo optional to print more detailed tx info\n"
184 "Returns details of a block with given block-hash.");
186 std::string strHash = params[0].get_str();
187 uint256 hash(strHash);
189 if (mapBlockIndex.count(hash) == 0)
190 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
193 CBlockIndex* pblockindex = mapBlockIndex[hash];
194 block.ReadFromDisk(pblockindex, true);
196 return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
199 Value getblockbynumber(const Array& params, bool fHelp)
201 if (fHelp || params.size() < 1 || params.size() > 2)
203 "getblock <number> [txinfo]\n"
204 "txinfo optional to print more detailed tx info\n"
205 "Returns details of a block with given block-number.");
207 int nHeight = params[0].get_int();
208 if (nHeight < 0 || nHeight > nBestHeight)
209 throw runtime_error("Block number out of range.");
212 CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
213 while (pblockindex->nHeight > nHeight)
214 pblockindex = pblockindex->pprev;
216 uint256 hash = *pblockindex->phashBlock;
218 pblockindex = mapBlockIndex[hash];
219 block.ReadFromDisk(pblockindex, true);
221 return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
224 // ppcoin: get information of sync-checkpoint
225 Value getcheckpoint(const Array& params, bool fHelp)
227 if (fHelp || params.size() != 0)
230 "Show info of synchronized checkpoint.\n");
233 CBlockIndex* pindexCheckpoint;
235 result.push_back(Pair("synccheckpoint", Checkpoints::hashSyncCheckpoint.ToString().c_str()));
236 pindexCheckpoint = mapBlockIndex[Checkpoints::hashSyncCheckpoint];
237 result.push_back(Pair("height", pindexCheckpoint->nHeight));
238 result.push_back(Pair("timestamp", DateTimeStrFormat(pindexCheckpoint->GetBlockTime()).c_str()));
239 if (mapArgs.count("-checkpointkey"))
240 result.push_back(Pair("checkpointmaster", true));