Pages:
Author

Topic: [PATCH] getblockbycount, getblockbyhash RPCs (Read 30450 times)

legendary
Activity: 1596
Merit: 1011
Democracy is vulnerable to a 51% attack.
November 02, 2011, 06:07:36 PM
#42
Doesn't this code access mapBlockIndex without holding the cs_main lock? If that's legal, what protects mapBlockIndex? If that's illegal, isn't the patch broken? (If this question is obsolete because there's a newer version of the patch, where can I find it?)
full member
Activity: 175
Merit: 100
Thank you incredibly Furyan.

Glad to help Smiley

Now if I can get Gavin's Monitor patch in there... much more invasive and stomps on some patches I've already applied..
newbie
Activity: 42
Merit: 0
Thank you incredibly Furyan.
full member
Activity: 175
Merit: 100
Screw that, won't work. Need a clean diff.

Oh, ok, this is the latest:

https://github.com/jgarzik/bitcoin/commit/168f5a4b30d5307281eb5bc4447afb04348e9f5d.patch

But you'd need a unified diff of all changes to that file.  There's a way to do that using git on the command line but I'm not familiar with it.

Edit: Here you go. This is against the 0.3.24 release version so if you've made other custom changes you'll need to apply the patch more carefully.

Code:
--- rpc.cpp       2011-07-21 11:37:38.000000000 -0700
+++ rpc.cpp     2011-07-21 11:39:21.000000000 -0700
@@ -168,6 +168,157 @@
 }


+Value BlockToValue(CBlock &block)
+{
+    Object obj;
+    obj.push_back(Pair("hash", block.GetHash().ToString().c_str()));
+    obj.push_back(Pair("version", block.nVersion));
+    obj.push_back(Pair("prev_block", block.hashPrevBlock.ToString().c_str()));
+    obj.push_back(Pair("mrkl_root", block.hashMerkleRoot.ToString().c_str()));
+    obj.push_back(Pair("time", (uint64_t)block.nTime));
+    obj.push_back(Pair("bits", (uint64_t)block.nBits));
+    obj.push_back(Pair("nonce", (uint64_t)block.nNonce));
+    obj.push_back(Pair("n_tx", (int)block.vtx.size()));
+    obj.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK)));
+
+    Array tx;
+    for (int i = 0; i < block.vtx.size(); i++) {
+       Object txobj;
+
+   txobj.push_back(Pair("hash", block.vtx[i].GetHash().ToString().c_str()));
+   txobj.push_back(Pair("version", block.vtx[i].nVersion));
+   txobj.push_back(Pair("lock_time", (uint64_t)block.vtx[i].nLockTime));
+   txobj.push_back(Pair("size",
+       (int)::GetSerializeSize(block.vtx[i], SER_NETWORK)));
+
+   Array tx_vin;
+   for (int j = 0; j < block.vtx[i].vin.size(); j++) {
+       Object vino;
+
+       Object vino_outpt;
+
+       vino_outpt.push_back(Pair("hash",
+           block.vtx[i].vin[j].prevout.hash.ToString().c_str()));
+       vino_outpt.push_back(Pair("n", (uint64_t)block.vtx[i].vin[j].prevout.n));
+
+       vino.push_back(Pair("prev_out", vino_outpt));
+
+       if (block.vtx[i].vin[j].prevout.IsNull())
+           vino.push_back(Pair("coinbase", HexStr(
+           block.vtx[i].vin[j].scriptSig.begin(),
+           block.vtx[i].vin[j].scriptSig.end(), false).c_str()));
+       else
+           vino.push_back(Pair("scriptSig",
+           block.vtx[i].vin[j].scriptSig.ToString().c_str()));
+       if (block.vtx[i].vin[j].nSequence != UINT_MAX)
+           vino.push_back(Pair("sequence", (uint64_t)block.vtx[i].vin[j].nSequence));
+
+       tx_vin.push_back(vino);
+   }
+
+   Array tx_vout;
+   for (int j = 0; j < block.vtx[i].vout.size(); j++) {
+       Object vouto;
+
+       vouto.push_back(Pair("value",
+           (double)block.vtx[i].vout[j].nValue / (double)COIN));
+       vouto.push_back(Pair("scriptPubKey",
+       block.vtx[i].vout[j].scriptPubKey.ToString().c_str()));
+
+       tx_vout.push_back(vouto);
+   }
+
+   txobj.push_back(Pair("in", tx_vin));
+   txobj.push_back(Pair("out", tx_vout));
+
+   tx.push_back(txobj);
+    }
+
+    obj.push_back(Pair("tx", tx));
+
+    Array mrkl;
+    for (int i = 0; i < block.vMerkleTree.size(); i++)
+       mrkl.push_back(block.vMerkleTree[i].ToString().c_str());
+
+    obj.push_back(Pair("mrkl_tree", mrkl));
+
+    return obj;
+}
+
+
+Value getblockbycount(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 1)
+        throw runtime_error(
+            "getblockbycount height\n"
+            "Dumps the block existing at specified height");
+
+    int64 height = params[0].get_int64();
+    if (height > nBestHeight)
+        throw runtime_error(
+            "getblockbycount height\n"
+            "Dumps the block existing at specified height");
+
+    string blkname = strprintf("blk%d", height);
+
+    CBlock block;
+    CBlockIndex* pindex;
+    bool found = false;
+
+    CRITICAL_BLOCK(cs_main)
+    {
+
+    for (map::iterator mi = mapBlockIndex.begin();
+         mi != mapBlockIndex.end(); ++mi)
+    {
+       pindex = (*mi).second;
+   if ((pindex->nHeight == height) && (pindex->IsInMainChain())) {
+       found = true;
+       break;
+   }
+    }
+
+    if (!found)
+        throw runtime_error(
+            "getblockbycount height\n"
+            "Dumps the block existing at specified height");
+
+    block.ReadFromDisk(pindex);
+    block.BuildMerkleTree();
+    }
+
+    return BlockToValue(block);
+}
+
+
+Value getblockbyhash(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 1)
+        throw runtime_error(
+            "getblockbyhash hash\n"
+            "Dumps the block with specified hash");
+
+    uint256 hash;
+    hash.SetHex(params[0].get_str());
+    CBlock block;
+
+    CRITICAL_BLOCK(cs_main)
+    {
+
+    map::iterator mi = mapBlockIndex.find(hash);
+    if (mi == mapBlockIndex.end())
+        throw JSONRPCError(-18, "hash not found");
+
+    CBlockIndex* pindex = (*mi).second;
+
+    block.ReadFromDisk(pindex);
+    block.BuildMerkleTree();
+    }
+
+    return BlockToValue(block);
+}
+
+
 Value getblockcount(const Array& params, bool fHelp)
 {
     if (fHelp || params.size() != 0)
@@ -1434,6 +1585,8 @@
 {
     make_pair("help",                  &help),
     make_pair("stop",                  &stop),
+    make_pair("getblockbycount",       &getblockbycount),
+    make_pair("getblockbyhash",        &getblockbyhash),
     make_pair("getblockcount",         &getblockcount),
     make_pair("getblocknumber",        &getblocknumber),
     make_pair("getconnectioncount",    &getconnectioncount),
@@ -2130,6 +2283,7 @@
         if (strMethod == "listtransactions"       && n > 1) ConvertTo(params[1]);
         if (strMethod == "listtransactions"       && n > 2) ConvertTo(params[2]);
         if (strMethod == "listaccounts"           && n > 0) ConvertTo(params[0]);
+        if (strMethod == "getblockbycount"        && n > 0) ConvertTo(params[0]);
         if (strMethod == "sendmany"               && n > 1)
         {
             string s = params[1].get_str();
                                                                                 

I suggest you use the -b flag to patch to make a backup of your rpc.cpp.

Works great!
full member
Activity: 175
Merit: 100
Any reason this shouldn't be merged with the main tree?

Seconded.

Thirded!

This is amazing stuff though, thanks jgarzik! Perfect for the novice pool operator.

Can somebody please explain to me how to add this patch to the main bitcoind client? I have never done a patch before :S

You need the diff to apply the patch.  This should be against 0.3.24 source but I haven't checked.

You can get the diff at github (sort of) -

https://github.com/jgarzik/bitcoin/commit/168f5a4b30d5307281eb5bc4447afb04348e9f5d#diff-0

Copy that into a file (e.g. diff.txt), then run

Code:
patch -i diff.txt rpc.cpp

You may want to add

Code:
-r rejects.txt

to catch any rejections that may happen so you can fix them.

Will be doing this when I get home, I like this patch.

Edit:
Actually it looks like JoelKatz' pull request has the complete diff....

Unless jgarzik would be so kind as to generate one for us Smiley

(Reason I need a diff is that I have other patches applied to my local copy - most of JoelKatz' work).
newbie
Activity: 42
Merit: 0
Any reason this shouldn't be merged with the main tree?

Seconded.

Thirded!

This is amazing stuff though, thanks jgarzik! Perfect for the novice pool operator.

Can somebody please explain to me how to add this patch to the main bitcoind client? I have never done a patch before :S
hero member
Activity: 530
Merit: 500
Right, got it to work. (Thanks to the bitcoin-dev channel)
Right now I get this:

Code:
array
  'hash' => string '000000000000053eb0059c82f86013cbf205033d82754b5252a04059e331c55d' (length=64)
  'version' => int 1
  'prev_block' => string '00000000000009e1e09847ee394f5394a3eed9d7c37949ac7ef0648760b0e488' (length=64)
  'mrkl_root' => string '53f1acacb93d62107d7ba62bdb50beefcc803dd4e3dfd33ed26857108901006d' (length=64)
  'time' => int 1309386843
  'bits' => int 437004818
  'nonce' => float 3446647446
  'n_tx' => int 81
  'size' => int 36104
  'tx' =>
    array
      0 =>
        array
          'hash' => string '1720c9d73186c87f31e9da0e3189fb67ce3252f8b300f603f8ae659b6f2bf60f' (length=64)
          'version' => int 1
          'lock_time' => int 0
          'size' => int 134
          'in' =>
            array
              ...
          'out' =>
            array
              ...
      1 =>
        array
          'hash' => string 'd3cd0851e088481afebd0178537cbb4cb9464ea03454b1c646643a1097727915' (length=64)
          'version' => int 1
          'lock_time' => int 0
          'size' => int 258
          'in' =>
            array
              ...
          'out' =>
            array
              ...
      2 =>
        array
          'hash' => string 'cb81abf818ad18c51c78c9724dbd9b74be49c7d7d748d9a3eafcd8f4b346b498' (length=64)
          'version' => int 1
          'lock_time' => int 0
          'size' => int 223
          'in' =>
            array
              ...
          'out' =>
            array
              ...

etc..


Why are the IN and OUT arrays empty?
When I check the startpost I see there is a another array in there?

EDIT:
Also I am not sure what I should do with Berkeley DB.
The readme told me to make but thats all.
How do I need to create and connect the DB to the Bitcoin server?
Or am I tottaly misunderstanding this.
legendary
Activity: 1596
Merit: 1022
Only when I restarted my Linux server and try for instance:
Code:
bitcoin getblock 71995

I get the error:
Code:
error: {"code":-32601,"message":"Method not found"}

That's because it is called 'getblockbycount' not 'getblock' these days.

hero member
Activity: 530
Merit: 500
Hello,

Thank you so much for developing this patch.
I am trying to install it right now on my bitcoin server.
I followed your instructions that are in the manual and all went fine.

I tried:
Code:
bitcoin getblockcount

Wich obviously worked flawlessly.

Only when I restarted my Linux server and try for instance:
Code:
bitcoin getblock 71995

I get the error:
Code:
error: {"code":-32601,"message":"Method not found"}

I builded the patch with wxWidgets.

Thanks again.
member
Activity: 98
Merit: 10
getblockbycount / getblockbyhash RPCs updated to latest git.
legendary
Activity: 1372
Merit: 1002
1davout
Could it be possible that getblockbyhash returns an "orphan_block" flag ?

See this example that I generated on testnet :
Code:
[email protected]:~/rails/bitcoin-pool$ bitcoin getblockbyhash 00000000003d7e088aba9d12e34cc3f75cd10ffa48a026fa535c0771e5c1b0c8
{
    "hash" : "00000000003d7e088aba9d12e34cc3f75cd10ffa48a026fa535c0771e5c1b0c8",
    "version" : 1,
    "prev_block" : "0000000000176fa50f3c759b2025acdbb0b4f311372e2b94acb0e69f73ae7c6b",
    "mrkl_root" : "46689b6020b67534e63fb81ddf379bb666a96ff55de27f8eebe8dffc924c09e7",
    "time" : 1307350469,
    "bits" : 470193746,
    "nonce" : 3293063839,
    "n_tx" : 1,
    "size" : 215,
    "tx" : [
        {
            "hash" : "46689b6020b67534e63fb81ddf379bb666a96ff55de27f8eebe8dffc924c09e7",
            "version" : 1,
            "lock_time" : 0,
            "size" : 134,
            "in" : [
                {
                    "prev_out" : {
                        "hash" : "0000000000000000000000000000000000000000000000000000000000000000",
                        "n" : 4294967295
                    },
                    "coinbase" : "045296061c0112"
                }
            ],
            "out" : [
                {
                    "value" : 50.00000000,
                    "scriptPubKey" : "04d98927faa89cd92df4861f08933ff68f50182850775fc590965049e34de80f6277cbcc9d5446ed43abf9794d70df9813b9fb7c589805a70676ddaee8e803d806 OP_CHECKSIG"
                }
            ]
        }
    ],
    "mrkl_tree" : [
        "46689b6020b67534e63fb81ddf379bb666a96ff55de27f8eebe8dffc924c09e7"
    ]
}

Nothing wrong here, but when I want to get details about the generation tx this is what I get :
Code:
[email protected]:~/rails/bitcoin-pool$ bitcoin gettransaction 46689b6020b67534e63fb81ddf379bb666a96ff55de27f8eebe8dffc924c09e7
{
    "amount" : 0.00000000,
    "confirmations" : 0,
    "txid" : "46689b6020b67534e63fb81ddf379bb666a96ff55de27f8eebe8dffc924c09e7",
    "time" : 1307350469,
    "details" : [
        {
            "account" : "",
            "category" : "orphan",
            "amount" : 50.00000000
        }
    ]
}

Also this should get pulled in, it's incredibly useful when building decent bitcoin handling apps.
hero member
Activity: 588
Merit: 500
Any reason this shouldn't be merged with the main tree?

Seconded.
member
Activity: 98
Merit: 10

getblockbycount RPC updated to latest git.  As the top post notes, this work is now maintained at github, jgarzik/bitcoin's "getblockbycount" branch.

Notable addition:  new RPC "getblockbyhash" added, to retrieve block by block header hash.

newbie
Activity: 11
Merit: 0
Patch updated to latest git.  See URL at top of thread.


I updated it to the latest git.  http://pastebin.com/FdrYDVF5

I made it as a git format-patch so it can be applied using git-apply. To make it a normal patch, just delete the first 9 lines.
legendary
Activity: 1596
Merit: 1022
February 23, 2011, 07:44:18 PM
#28
Patch updated to latest git.  See URL at top of thread.
legendary
Activity: 2506
Merit: 1032
Any reason this shouldn't be merged with the main tree?
legendary
Activity: 1596
Merit: 1022
December 02, 2010, 12:50:44 AM
#26
Patch updated for latest SVN.
legendary
Activity: 1596
Merit: 1022
November 27, 2010, 02:04:13 PM
#25
Patch updated to latest SVN, with additional block/TX size output.
administrator
Activity: 4228
Merit: 8647
November 27, 2010, 08:48:16 AM
#24
To get block/tx size, you can add this to block processing:
Code:
obj.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK)));

And add this to tx processing:
Code:
txobj.push_back(Pair("size", (int)::GetSerializeSize(block.vtx[i], SER_NETWORK)));

I don't know that this is the best way of doing this (I'm not really familiar with C++). That cast to integer is probably not necessary, but it works and I don't want to test another version (overflow is impossible for the foreseeable future).
legendary
Activity: 1596
Merit: 1022
November 22, 2010, 07:05:11 PM
#23
Updated to SVN r188 (accounts).
Pages:
Jump to: