Guest User

bitcoin-getblock

a guest
Apr 23rd, 2011
172
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. From 4de74efdcfc0e6da6cda365774ea3530adc49035 Mon Sep 17 00:00:00 2001
  2. From: Fahrzin Hemmati <fahhem@fahhem.com>
  3. Date: Sat, 23 Apr 2011 16:07:36 -0700
  4. Subject: [PATCH] getblock
  5.  
  6. ---
  7.  rpc.cpp |  114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  8.  1 files changed, 114 insertions(+), 0 deletions(-)
  9.  
  10. diff --git a/rpc.cpp b/rpc.cpp
  11. index 90e7f15..7bb9a30 100644
  12. --- a/rpc.cpp
  13. +++ b/rpc.cpp
  14. @@ -181,6 +181,118 @@ Value getblocknumber(const Array& params, bool fHelp)
  15.      return nBestHeight;
  16.  }
  17.  
  18. +Value getblock(const Array& params, bool fHelp)
  19. +{
  20. +    if (fHelp || params.size() != 1)
  21. +        throw runtime_error(
  22. +            "getblock height\n"
  23. +            "Dumps the block existing at specified height");
  24. +
  25. +    int64 height = params[0].get_int64();
  26. +    if (height > nBestHeight)
  27. +        throw runtime_error(
  28. +            "getblock height\n"
  29. +            "Dumps the block existing at specified height");
  30. +
  31. +    string blkname = strprintf("blk%d", height);
  32. +
  33. +    CBlockIndex* pindex;
  34. +    bool found = false;
  35. +
  36. +    for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin();
  37. +         mi != mapBlockIndex.end(); ++mi)
  38. +    {
  39. +       pindex = (*mi).second;
  40. +   if (pindex->nHeight == height) {
  41. +       found = true;
  42. +       break;
  43. +   }
  44. +    }
  45. +
  46. +    if (!found)
  47. +        throw runtime_error(
  48. +            "getblock height\n"
  49. +            "Dumps the block existing at specified height");
  50. +
  51. +    CBlock block;
  52. +    block.ReadFromDisk(pindex);
  53. +    block.BuildMerkleTree();
  54. +
  55. +    Object obj;
  56. +    obj.push_back(Pair("hash", block.GetHash().ToString().c_str()));
  57. +    obj.push_back(Pair("ver", block.nVersion));
  58. +    obj.push_back(Pair("prev_block", block.hashPrevBlock.ToString().c_str()));
  59. +    obj.push_back(Pair("mrkl_root", block.hashMerkleRoot.ToString().c_str()));
  60. +    obj.push_back(Pair("time", (uint64_t)block.nTime));
  61. +    obj.push_back(Pair("bits", (uint64_t)block.nBits));
  62. +    obj.push_back(Pair("nonce", (uint64_t)block.nNonce));
  63. +    obj.push_back(Pair("n_tx", (int)block.vtx.size()));
  64. +
  65. +    Array tx;
  66. +    for (int i = 0; i < block.vtx.size(); i++) {
  67. +       Object txobj;
  68. +
  69. +   txobj.push_back(Pair("hash", block.vtx[i].GetHash().ToString().c_str()));
  70. +   txobj.push_back(Pair("ver", block.vtx[i].nVersion));
  71. +   txobj.push_back(Pair("vin_sz", (int)block.vtx[i].vin.size()));
  72. +   txobj.push_back(Pair("vout_sz", (int)block.vtx[i].vout.size()));
  73. +   txobj.push_back(Pair("lock_time", (uint64_t)block.vtx[i].nLockTime));
  74. +
  75. +   Array tx_vin;
  76. +   for (int j = 0; j < block.vtx[i].vin.size(); j++) {
  77. +       Object vino;
  78. +
  79. +       Object vino_outpt;
  80. +
  81. +       vino_outpt.push_back(Pair("hash",
  82. +           block.vtx[i].vin[j].prevout.hash.ToString().c_str()));
  83. +       vino_outpt.push_back(Pair("n", (uint64_t)block.vtx[i].vin[j].prevout.n));
  84. +
  85. +       vino.push_back(Pair("prev_out", vino_outpt));
  86. +
  87. +       if (block.vtx[i].vin[j].prevout.IsNull())
  88. +           vino.push_back(Pair("coinbase", HexStr(
  89. +           block.vtx[i].vin[j].scriptSig.begin(),
  90. +           block.vtx[i].vin[j].scriptSig.end(), false).c_str()));
  91. +       else
  92. +           vino.push_back(Pair("scriptSig",
  93. +           block.vtx[i].vin[j].scriptSig.ToString().c_str()));
  94. +       if (block.vtx[i].vin[j].nSequence != UINT_MAX)
  95. +           vino.push_back(Pair("sequence", (uint64_t)block.vtx[i].vin[j].nSequence));
  96. +
  97. +       tx_vin.push_back(vino);
  98. +   }
  99. +
  100. +   Array tx_vout;
  101. +   for (int j = 0; j < block.vtx[i].vout.size(); j++) {
  102. +       Object vouto;
  103. +
  104. +       vouto.push_back(Pair("value",
  105. +           (double)block.vtx[i].vout[j].nValue / (double)COIN));
  106. +       vouto.push_back(Pair("scriptPubKey",
  107. +       block.vtx[i].vout[j].scriptPubKey.ToString().c_str()));
  108. +
  109. +       tx_vout.push_back(vouto);
  110. +   }
  111. +
  112. +   txobj.push_back(Pair("in", tx_vin));
  113. +   txobj.push_back(Pair("out", tx_vout));
  114. +
  115. +   tx.push_back(txobj);
  116. +    }
  117. +
  118. +    obj.push_back(Pair("tx", tx));
  119. +
  120. +    Array mrkl;
  121. +    for (int i = 0; i < block.vMerkleTree.size(); i++)
  122. +       mrkl.push_back(block.vMerkleTree[i].ToString().c_str());
  123. +
  124. +    obj.push_back(Pair("mrkl_tree", mrkl));
  125. +
  126. +    return obj;
  127. +}
  128. +
  129. +
  130.  
  131.  Value getconnectioncount(const Array& params, bool fHelp)
  132.  {
  133. @@ -1402,6 +1514,7 @@ pair<string, rpcfn_type> pCallTable[] =
  134.  {
  135.      make_pair("help",                  &help),
  136.      make_pair("stop",                  &stop),
  137. +    make_pair("getblock",         &getblock),
  138.      make_pair("getblockcount",         &getblockcount),
  139.      make_pair("getblocknumber",        &getblocknumber),
  140.      make_pair("getconnectioncount",    &getconnectioncount),
  141. @@ -2097,6 +2210,7 @@ int CommandLineRPC(int argc, char *argv[])
  142.              params[1] = v.get_obj();
  143.          }
  144.          if (strMethod == "sendmany"                && n > 2) ConvertTo<boost::int64_t>(params[2]);
  145. +        if (strMethod == "getblock"                && n > 0) ConvertTo<boost::int64_t>(params[0]);
  146.  
  147.          // Execute
  148.          Object reply = CallRPC(strMethod, params);
  149. --
  150. 1.7.2.3
RAW Paste Data