Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
rpcblockchain.cpp
Go to the documentation of this file.
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 void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out);
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 = pindexBest;
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)
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  Array txs;
58  BOOST_FOREACH(const CTransaction&tx, block.vtx)
59  txs.push_back(tx.GetHash().GetHex());
60  result.push_back(Pair("tx", txs));
61  result.push_back(Pair("time", (boost::int64_t)block.GetBlockTime()));
62  result.push_back(Pair("nonce", (boost::uint64_t)block.nNonce));
63  result.push_back(Pair("bits", HexBits(block.nBits)));
64  result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
65 
66  if (blockindex->pprev)
67  result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
68  if (blockindex->pnext)
69  result.push_back(Pair("nextblockhash", blockindex->pnext->GetBlockHash().GetHex()));
70  return result;
71 }
72 
73 
74 Value getblockcount(const Array& params, bool fHelp)
75 {
76  if (fHelp || params.size() != 0)
77  throw runtime_error(
78  "getblockcount\n"
79  "Returns the number of blocks in the longest block chain.");
80 
81  return nBestHeight;
82 }
83 
84 Value getbestblockhash(const Array& params, bool fHelp)
85 {
86  if (fHelp || params.size() != 0)
87  throw runtime_error(
88  "getbestblockhash\n"
89  "Returns the hash of the best (tip) block in the longest block chain.");
90 
91  return hashBestChain.GetHex();
92 }
93 
94 Value getdifficulty(const Array& params, bool fHelp)
95 {
96  if (fHelp || params.size() != 0)
97  throw runtime_error(
98  "getdifficulty\n"
99  "Returns the proof-of-work difficulty as a multiple of the minimum difficulty.");
100 
101  return GetDifficulty();
102 }
103 
104 
105 Value settxfee(const Array& params, bool fHelp)
106 {
107  if (fHelp || params.size() < 1 || params.size() > 1)
108  throw runtime_error(
109  "settxfee <amount>\n"
110  "<amount> is a real and is rounded to the nearest 0.00000001");
111 
112  // Amount
113  int64 nAmount = 0;
114  if (params[0].get_real() != 0.0)
115  nAmount = AmountFromValue(params[0]); // rejects 0.0 amounts
116 
117  nTransactionFee = nAmount;
118  return true;
119 }
120 
121 Value getrawmempool(const Array& params, bool fHelp)
122 {
123  if (fHelp || params.size() != 0)
124  throw runtime_error(
125  "getrawmempool\n"
126  "Returns all transaction ids in memory pool.");
127 
128  vector<uint256> vtxid;
129  mempool.queryHashes(vtxid);
130 
131  Array a;
132  BOOST_FOREACH(const uint256& hash, vtxid)
133  a.push_back(hash.ToString());
134 
135  return a;
136 }
137 
138 Value getblockhash(const Array& params, bool fHelp)
139 {
140  if (fHelp || params.size() != 1)
141  throw runtime_error(
142  "getblockhash <index>\n"
143  "Returns hash of block in best-block-chain at <index>.");
144 
145  int nHeight = params[0].get_int();
146  if (nHeight < 0 || nHeight > nBestHeight)
147  throw runtime_error("Block number out of range.");
148 
149  CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
150  return pblockindex->phashBlock->GetHex();
151 }
152 
153 Value getblock(const Array& params, bool fHelp)
154 {
155  if (fHelp || params.size() < 1 || params.size() > 2)
156  throw runtime_error(
157  "getblock <hash> [verbose=true]\n"
158  "If verbose is false, returns a string that is serialized, hex-encoded data for block <hash>.\n"
159  "If verbose is true, returns an Object with information about block <hash>."
160  );
161 
162  std::string strHash = params[0].get_str();
163  uint256 hash(strHash);
164 
165  bool fVerbose = true;
166  if (params.size() > 1)
167  fVerbose = params[1].get_bool();
168 
169  if (mapBlockIndex.count(hash) == 0)
170  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
171 
172  CBlock block;
173  CBlockIndex* pblockindex = mapBlockIndex[hash];
174  block.ReadFromDisk(pblockindex);
175 
176  if (!fVerbose)
177  {
178  CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
179  ssBlock << block;
180  std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
181  return strHex;
182  }
183 
184  return blockToJSON(block, pblockindex);
185 }
186 
187 Value gettxoutsetinfo(const Array& params, bool fHelp)
188 {
189  if (fHelp || params.size() != 0)
190  throw runtime_error(
191  "gettxoutsetinfo\n"
192  "Returns statistics about the unspent transaction output set.");
193 
194  Object ret;
195 
197  if (pcoinsTip->GetStats(stats)) {
198  ret.push_back(Pair("height", (boost::int64_t)stats.nHeight));
199  ret.push_back(Pair("bestblock", stats.hashBlock.GetHex()));
200  ret.push_back(Pair("transactions", (boost::int64_t)stats.nTransactions));
201  ret.push_back(Pair("txouts", (boost::int64_t)stats.nTransactionOutputs));
202  ret.push_back(Pair("bytes_serialized", (boost::int64_t)stats.nSerializedSize));
203  ret.push_back(Pair("hash_serialized", stats.hashSerialized.GetHex()));
204  ret.push_back(Pair("total_amount", ValueFromAmount(stats.nTotalAmount)));
205  }
206  return ret;
207 }
208 
209 Value gettxout(const Array& params, bool fHelp)
210 {
211  if (fHelp || params.size() < 2 || params.size() > 3)
212  throw runtime_error(
213  "gettxout <txid> <n> [includemempool=true]\n"
214  "Returns details about an unspent transaction output.");
215 
216  Object ret;
217 
218  std::string strHash = params[0].get_str();
219  uint256 hash(strHash);
220  int n = params[1].get_int();
221  bool fMempool = true;
222  if (params.size() > 2)
223  fMempool = params[2].get_bool();
224 
225  CCoins coins;
226  if (fMempool) {
227  LOCK(mempool.cs);
229  if (!view.GetCoins(hash, coins))
230  return Value::null;
231  mempool.pruneSpent(hash, coins); // TODO: this should be done by the CCoinsViewMemPool
232  } else {
233  if (!pcoinsTip->GetCoins(hash, coins))
234  return Value::null;
235  }
236  if (n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull())
237  return Value::null;
238 
239  ret.push_back(Pair("bestblock", pcoinsTip->GetBestBlock()->GetBlockHash().GetHex()));
240  if ((unsigned int)coins.nHeight == MEMPOOL_HEIGHT)
241  ret.push_back(Pair("confirmations", 0));
242  else
243  ret.push_back(Pair("confirmations", pcoinsTip->GetBestBlock()->nHeight - coins.nHeight + 1));
244  ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
245  Object o;
246  ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o);
247  ret.push_back(Pair("scriptPubKey", o));
248  ret.push_back(Pair("version", coins.nVersion));
249  ret.push_back(Pair("coinbase", coins.fCoinBase));
250 
251  return ret;
252 }
253 
254 Value verifychain(const Array& params, bool fHelp)
255 {
256  if (fHelp || params.size() > 2)
257  throw runtime_error(
258  "verifychain [check level] [num blocks]\n"
259  "Verifies blockchain database.");
260 
261  int nCheckLevel = GetArg("-checklevel", 3);
262  int nCheckDepth = GetArg("-checkblocks", 288);
263  if (params.size() > 0)
264  nCheckLevel = params[0].get_int();
265  if (params.size() > 1)
266  nCheckDepth = params[1].get_int();
267 
268  return VerifyDB(nCheckLevel, nCheckDepth);
269 }
270 
CBlockIndex * FindBlockByHeight(int nHeight)
Find a block by height in the currently-connected chain.
Definition: main.cpp:1041
int nVersion
Definition: main.h:1283
uint256 hashBlock
Definition: main.h:2135
const_iterator begin() const
Definition: serialize.h:884
CBlockIndex * pprev
Definition: main.h:1633
std::vector< CTxOut > vout
Definition: main.h:903
int nHeight
Definition: main.h:2134
Definition: main.h:1334
Value getblockcount(const Array &params, bool fHelp)
int64 AmountFromValue(const Value &value)
Definition: bitcoinrpc.cpp:94
int nHeight
Definition: main.h:906
bool VerifyDB(int nCheckLevel, int nCheckDepth)
Verify consistency of the block and coin databases.
Definition: main.cpp:2730
uint256 GetHash() const
Definition: main.h:515
void queryHashes(std::vector< uint256 > &vtxid)
Definition: main.cpp:892
Value gettxoutsetinfo(const Array &params, bool fHelp)
Object blockToJSON(const CBlock &block, const CBlockIndex *blockindex)
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:799
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: main.h:896
Value getblock(const Array &params, bool fHelp)
Object JSONRPCError(int code, const string &message)
Definition: bitcoinrpc.cpp:46
bool GetStats(CCoinsStats &stats)
Definition: main.cpp:198
Value getdifficulty(const Array &params, bool fHelp)
Config::Object_type Object
Value settxfee(const Array &params, bool fHelp)
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: main.cpp:202
void ScriptPubKeyToJSON(const CScript &scriptPubKey, Object &out)
int64 nTransactionFee
Definition: main.cpp:84
uint256 hashSerialized
Definition: main.h:2139
int nVersion
Definition: main.h:910
uint256 hashMerkleRoot
Definition: main.h:1285
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:106
#define LOCK(cs)
Definition: sync.h:108
unsigned long long uint64_t
Definition: stdint.h:22
static const Value_impl null
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: main.cpp:274
int64 GetBlockTime() const
Definition: main.h:1326
unsigned int nBits
Definition: main.h:1667
unsigned int nNonce
Definition: main.h:1288
Config::Array_type Array
Value getrawmempool(const Array &params, bool fHelp)
const String_type & get_str() const
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
Definition: main.cpp:289
std::string GetHex() const
Definition: uint256.h:298
CCriticalSection cs
Definition: main.h:2101
CTxMemPool mempool
Definition: main.cpp:34
CBlockIndex * GetBestBlock()
Definition: main.cpp:241
std::string HexBits(unsigned int nBits)
Definition: bitcoinrpc.cpp:110
bool fCoinBase
Definition: main.h:900
256-bit unsigned integer
Definition: uint256.h:537
uint256 hashBestChain
Definition: main.cpp:44
void pruneSpent(const uint256 &hash, CCoins &coins)
Definition: main.cpp:645
uint256 GetHash() const
Definition: main.h:1321
CBlockIndex * pnext
Definition: main.h:1636
Value ValueFromAmount(int64 amount)
Definition: bitcoinrpc.cpp:105
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:1626
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:244
std::string ToString() const
Definition: uint256.h:343
double GetDifficulty(const CBlockIndex *blockindex)
Stats stats
Definition: db_bench.cc:291
int64 nTotalAmount
Definition: main.h:2140
int GetDepthInMainChain(CBlockIndex *&pindexRet) const
Definition: main.cpp:905
uint64 nTransactionOutputs
Definition: main.h:2137
Value getbestblockhash(const Array &params, bool fHelp)
signed long long int64_t
Definition: stdint.h:18
CBlockIndex * pindexBest
Definition: main.cpp:45
std::vector< CTransaction > vtx
Definition: main.h:1338
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:586
uint64 nTransactions
Definition: main.h:2136
Value getblockhash(const Array &params, bool fHelp)
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: main.h:477
Value gettxout(const Array &params, bool fHelp)
int nHeight
Definition: main.h:1639
unsigned int nBits
Definition: main.h:1287
int nBestHeight
Definition: main.cpp:41
uint64 nSerializedSize
Definition: main.h:2138
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Definition: util.h:292
Value verifychain(const Array &params, bool fHelp)
bool ReadFromDisk(const CDiskBlockPos &pos)
Definition: main.h:1468
Config::Pair_type Pair
CCoinsView that brings transactions from a memorypool into view.
Definition: main.h:2229
map< uint256, CBlockIndex * > mapBlockIndex
Definition: main.cpp:37
uint32_t hash
Definition: cache.cc:34
uint256 GetBlockHash() const
Definition: main.h:1744
A transaction with a merkle branch linking it to the block chain.
Definition: main.h:1123
const_iterator end() const
Definition: serialize.h:886
long long int64
Definition: serialize.h:25
const uint256 * phashBlock
Definition: main.h:1630