Split getmininginfo implementation
[novacoin.git] / src / rpcblockchain.cpp
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.
5
6 #include "main.h"
7 #include "bitcoinrpc.h"
8
9 using namespace json_spirit;
10 using namespace std;
11
12 extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, json_spirit::Object& entry);
13 extern enum Checkpoints::CPMode CheckpointsMode;
14
15 double GetDifficulty(const CBlockIndex* blockindex)
16 {
17     // Floating point number that is a multiple of the minimum difficulty,
18     // minimum difficulty = 1.0.
19     if (blockindex == NULL)
20     {
21         if (pindexBest == NULL)
22             return 1.0;
23         else
24             blockindex = GetLastBlockIndex(pindexBest, false);
25     }
26
27     int nShift = (blockindex->nBits >> 24) & 0xff;
28
29     double dDiff =
30         (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);
31
32     while (nShift < 29)
33     {
34         dDiff *= 256.0;
35         nShift++;
36     }
37     while (nShift > 29)
38     {
39         dDiff /= 256.0;
40         nShift--;
41     }
42
43     return dDiff;
44 }
45
46 double GetPoWMHashPS()
47 {
48     int nPoWInterval = 72;
49     int64 nTargetSpacingWorkMin = 30, nTargetSpacingWork = 30;
50
51     CBlockIndex* pindex = pindexGenesisBlock;
52     CBlockIndex* pindexPrevWork = pindexGenesisBlock;
53
54     while (pindex)
55     {
56         if (pindex->IsProofOfWork())
57         {
58             int64 nActualSpacingWork = pindex->GetBlockTime() - pindexPrevWork->GetBlockTime();
59             nTargetSpacingWork = ((nPoWInterval - 1) * nTargetSpacingWork + nActualSpacingWork + nActualSpacingWork) / (nPoWInterval + 1);
60             nTargetSpacingWork = max(nTargetSpacingWork, nTargetSpacingWorkMin);
61             pindexPrevWork = pindex;
62         }
63
64         pindex = pindex->pnext;
65     }
66
67     return GetDifficulty() * 4294.967296 / nTargetSpacingWork;
68 }
69
70 double GetPoSKernelPS()
71 {
72     int nPoSInterval = 72;
73     double dStakeKernelsTriedAvg = 0;
74     int nStakesHandled = 0, nStakesTime = 0;
75
76     CBlockIndex* pindex = pindexBest;;
77     CBlockIndex* pindexPrevStake = NULL;
78
79     while (pindex && nStakesHandled < nPoSInterval)
80     {
81         if (pindex->IsProofOfStake())
82         {
83             dStakeKernelsTriedAvg += GetDifficulty(pindex) * 4294967296.0;
84             nStakesTime += pindexPrevStake ? (pindexPrevStake->nTime - pindex->nTime) : 0;
85             pindexPrevStake = pindex;
86             nStakesHandled++;
87         }
88
89         pindex = pindex->pprev;
90     }
91
92     return dStakeKernelsTriedAvg / nStakesTime;
93 }
94
95 Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPrintTransactionDetail)
96 {
97     Object result;
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", blockindex->GetBlockTrust().GetHex()));
112     result.push_back(Pair("chaintrust", blockindex->nChainTrust.GetHex()));
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()));
117
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)));
123     Array txinfo;
124     BOOST_FOREACH (const CTransaction& tx, block.vtx)
125     {
126         if (fPrintTransactionDetail)
127         {
128             Object entry;
129
130             entry.push_back(Pair("txid", tx.GetHash().GetHex()));
131             TxToJSON(tx, 0, entry);
132
133             txinfo.push_back(entry);
134         }
135         else
136             txinfo.push_back(tx.GetHash().GetHex());
137     }
138
139     result.push_back(Pair("tx", txinfo));
140
141     if ( block.IsProofOfStake() || (!fTestNet && block.GetBlockTime() < CHAINCHECKS_SWITCH_TIME) )
142         result.push_back(Pair("signature", HexStr(block.vchBlockSig.begin(), block.vchBlockSig.end())));
143
144     return result;
145 }
146
147 Value getbestblockhash(const Array& params, bool fHelp)
148 {
149     if (fHelp || params.size() != 0)
150         throw runtime_error(
151             "getbestblockhash\n"
152             "Returns the hash of the best block in the longest block chain.");
153
154     return hashBestChain.GetHex();
155 }
156
157 Value getblockcount(const Array& params, bool fHelp)
158 {
159     if (fHelp || params.size() != 0)
160         throw runtime_error(
161             "getblockcount\n"
162             "Returns the number of blocks in the longest block chain.");
163
164     return nBestHeight;
165 }
166
167
168 Value getdifficulty(const Array& params, bool fHelp)
169 {
170     if (fHelp || params.size() != 0)
171         throw runtime_error(
172             "getdifficulty\n"
173             "Returns the difficulty as a multiple of the minimum difficulty.");
174
175     Object obj;
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));
179     return obj;
180 }
181
182
183 Value settxfee(const Array& params, bool fHelp)
184 {
185     if (fHelp || params.size() < 1 || params.size() > 1 || AmountFromValue(params[0]) < MIN_TX_FEE)
186         throw runtime_error(
187             "settxfee <amount>\n"
188             "<amount> is a real and is rounded to the nearest 0.01");
189
190     nTransactionFee = AmountFromValue(params[0]);
191     nTransactionFee = (nTransactionFee / CENT) * CENT;  // round to cent
192
193     return true;
194 }
195
196 Value getrawmempool(const Array& params, bool fHelp)
197 {
198     if (fHelp || params.size() != 0)
199         throw runtime_error(
200             "getrawmempool\n"
201             "Returns all transaction ids in memory pool.");
202
203     vector<uint256> vtxid;
204     mempool.queryHashes(vtxid);
205
206     Array a;
207     BOOST_FOREACH(const uint256& hash, vtxid)
208         a.push_back(hash.ToString());
209
210     return a;
211 }
212
213 Value getblockhash(const Array& params, bool fHelp)
214 {
215     if (fHelp || params.size() != 1)
216         throw runtime_error(
217             "getblockhash <index>\n"
218             "Returns hash of block in best-block-chain at <index>.");
219
220     int nHeight = params[0].get_int();
221     if (nHeight < 0 || nHeight > nBestHeight)
222         throw runtime_error("Block number out of range.");
223
224     CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
225     return pblockindex->phashBlock->GetHex();
226 }
227
228 Value getblock(const Array& params, bool fHelp)
229 {
230     if (fHelp || params.size() < 1 || params.size() > 2)
231         throw runtime_error(
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.");
235
236     std::string strHash = params[0].get_str();
237     uint256 hash(strHash);
238
239     if (mapBlockIndex.count(hash) == 0)
240         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
241
242     CBlock block;
243     CBlockIndex* pblockindex = mapBlockIndex[hash];
244     block.ReadFromDisk(pblockindex, true);
245
246     return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
247 }
248
249 Value getblockbynumber(const Array& params, bool fHelp)
250 {
251     if (fHelp || params.size() < 1 || params.size() > 2)
252         throw runtime_error(
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.");
256
257     int nHeight = params[0].get_int();
258     if (nHeight < 0 || nHeight > nBestHeight)
259         throw runtime_error("Block number out of range.");
260
261     CBlock block;
262     CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
263     while (pblockindex->nHeight > nHeight)
264         pblockindex = pblockindex->pprev;
265
266     uint256 hash = *pblockindex->phashBlock;
267
268     pblockindex = mapBlockIndex[hash];
269     block.ReadFromDisk(pblockindex, true);
270
271     return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
272 }
273
274 // ppcoin: get information of sync-checkpoint
275 Value getcheckpoint(const Array& params, bool fHelp)
276 {
277     if (fHelp || params.size() != 0)
278         throw runtime_error(
279             "getcheckpoint\n"
280             "Show info of synchronized checkpoint.\n");
281
282     Object result;
283     CBlockIndex* pindexCheckpoint;
284
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()));
289
290     // Check that the block satisfies synchronized checkpoint
291     if (CheckpointsMode == Checkpoints::STRICT)
292         result.push_back(Pair("policy", "strict"));
293
294     if (CheckpointsMode == Checkpoints::ADVISORY)
295         result.push_back(Pair("policy", "advisory"));
296
297     if (CheckpointsMode == Checkpoints::PERMISSIVE)
298         result.push_back(Pair("policy", "permissive"));
299
300     if (mapArgs.count("-checkpointkey"))
301         result.push_back(Pair("checkpointmaster", true));
302
303     return result;
304 }