update to 0.4 preview
[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
14 double GetDifficulty(const CBlockIndex* blockindex)
15 {
16     // Floating point number that is a multiple of the minimum difficulty,
17     // minimum difficulty = 1.0.
18     if (blockindex == NULL)
19     {
20         if (pindexBest == NULL)
21             return 1.0;
22         else
23             blockindex = GetLastBlockIndex(pindexBest, false);
24     }
25
26     int nShift = (blockindex->nBits >> 24) & 0xff;
27
28     double dDiff =
29         (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);
30
31     while (nShift < 29)
32     {
33         dDiff *= 256.0;
34         nShift++;
35     }
36     while (nShift > 29)
37     {
38         dDiff /= 256.0;
39         nShift--;
40     }
41
42     return dDiff;
43 }
44
45
46 Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fPrintTransactionDetail)
47 {
48     Object result;
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)));
62
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()));
67
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)));
73     Array txinfo;
74     BOOST_FOREACH (const CTransaction& tx, block.vtx)
75     {
76         if (fPrintTransactionDetail)
77         {
78             Object entry;
79
80             entry.push_back(Pair("txid", tx.GetHash().GetHex()));
81             TxToJSON(tx, 0, entry);
82
83             txinfo.push_back(entry);
84         }
85         else
86             txinfo.push_back(tx.GetHash().GetHex());
87     }
88
89     result.push_back(Pair("tx", txinfo));
90     result.push_back(Pair("signature", HexStr(block.vchBlockSig.begin(), block.vchBlockSig.end())));
91
92     return result;
93 }
94
95
96 Value getblockcount(const Array& params, bool fHelp)
97 {
98     if (fHelp || params.size() != 0)
99         throw runtime_error(
100             "getblockcount\n"
101             "Returns the number of blocks in the longest block chain.");
102
103     return nBestHeight;
104 }
105
106
107 Value getdifficulty(const Array& params, bool fHelp)
108 {
109     if (fHelp || params.size() != 0)
110         throw runtime_error(
111             "getdifficulty\n"
112             "Returns the difficulty as a multiple of the minimum difficulty.");
113
114     Object obj;
115     obj.push_back(Pair("proof-of-work",        GetDifficulty()));
116     obj.push_back(Pair("proof-of-stake",       GetDifficulty(GetLastBlockIndex(pindexBest, true))));
117     obj.push_back(Pair("search-interval",      (int)nLastCoinStakeSearchInterval));
118     return obj;
119 }
120
121
122 Value settxfee(const Array& params, bool fHelp)
123 {
124     if (fHelp || params.size() < 1 || params.size() > 1 || AmountFromValue(params[0]) < MIN_TX_FEE)
125         throw runtime_error(
126             "settxfee <amount>\n"
127             "<amount> is a real and is rounded to the nearest 0.01");
128
129     nTransactionFee = AmountFromValue(params[0]);
130     nTransactionFee = (nTransactionFee / CENT) * CENT;  // round to cent
131
132     return true;
133 }
134
135 Value getrawmempool(const Array& params, bool fHelp)
136 {
137     if (fHelp || params.size() != 0)
138         throw runtime_error(
139             "getrawmempool\n"
140             "Returns all transaction ids in memory pool.");
141
142     vector<uint256> vtxid;
143     mempool.queryHashes(vtxid);
144
145     Array a;
146     BOOST_FOREACH(const uint256& hash, vtxid)
147         a.push_back(hash.ToString());
148
149     return a;
150 }
151
152 Value getblockhash(const Array& params, bool fHelp)
153 {
154     if (fHelp || params.size() != 1)
155         throw runtime_error(
156             "getblockhash <index>\n"
157             "Returns hash of block in best-block-chain at <index>.");
158
159     int nHeight = params[0].get_int();
160     if (nHeight < 0 || nHeight > nBestHeight)
161         throw runtime_error("Block number out of range.");
162
163     CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
164     return pblockindex->phashBlock->GetHex();
165 }
166
167 Value getblock(const Array& params, bool fHelp)
168 {
169     if (fHelp || params.size() < 1 || params.size() > 2)
170         throw runtime_error(
171             "getblock <hash> [txinfo]\n"
172             "txinfo optional to print more detailed tx info\n"
173             "Returns details of a block with given block-hash.");
174
175     std::string strHash = params[0].get_str();
176     uint256 hash(strHash);
177
178     if (mapBlockIndex.count(hash) == 0)
179         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
180
181     CBlock block;
182     CBlockIndex* pblockindex = mapBlockIndex[hash];
183     block.ReadFromDisk(pblockindex, true);
184
185     return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
186 }
187
188 Value getblockbynumber(const Array& params, bool fHelp)
189 {
190     if (fHelp || params.size() < 1 || params.size() > 2)
191         throw runtime_error(
192             "getblock <number> [txinfo]\n"
193             "txinfo optional to print more detailed tx info\n"
194             "Returns details of a block with given block-number.");
195
196     int nHeight = params[0].get_int();
197     if (nHeight < 0 || nHeight > nBestHeight)
198         throw runtime_error("Block number out of range.");
199
200     CBlock block;
201     CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
202     while (pblockindex->nHeight > nHeight)
203         pblockindex = pblockindex->pprev;
204
205     uint256 hash = *pblockindex->phashBlock;
206
207     pblockindex = mapBlockIndex[hash];
208     block.ReadFromDisk(pblockindex, true);
209
210     return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);
211 }
212
213 // ppcoin: get information of sync-checkpoint
214 Value getcheckpoint(const Array& params, bool fHelp)
215 {
216     if (fHelp || params.size() != 0)
217         throw runtime_error(
218             "getcheckpoint\n"
219             "Show info of synchronized checkpoint.\n");
220
221     Object result;
222     CBlockIndex* pindexCheckpoint;
223
224     result.push_back(Pair("synccheckpoint", Checkpoints::hashSyncCheckpoint.ToString().c_str()));
225     pindexCheckpoint = mapBlockIndex[Checkpoints::hashSyncCheckpoint];        
226     result.push_back(Pair("height", pindexCheckpoint->nHeight));
227     result.push_back(Pair("timestamp", DateTimeStrFormat(pindexCheckpoint->GetBlockTime()).c_str()));
228     if (mapArgs.count("-checkpointkey"))
229         result.push_back(Pair("checkpointmaster", true));
230
231     return result;
232 }