Bitcoin Forum
December 11, 2016, 04:20:08 AM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: « 1 [2] 3 »  All
  Print  
Author Topic: [PATCH] getblockbycount, getblockbyhash RPCs  (Read 20397 times)
theymos
Administrator
Legendary
*
expert
Offline Offline

Activity: 2506


View Profile
November 13, 2010, 08:31:36 AM
 #21

An orphan block was created at 91405, and I was able to verify that my change to getblock fixes the problem. I suggest that it be included in the patch.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
1481430008
Hero Member
*
Offline Offline

Posts: 1481430008

View Profile Personal Message (Offline)

Ignore
1481430008
Reply with quote  #2

1481430008
Report to moderator
1481430008
Hero Member
*
Offline Offline

Posts: 1481430008

View Profile Personal Message (Offline)

Ignore
1481430008
Reply with quote  #2

1481430008
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481430008
Hero Member
*
Offline Offline

Posts: 1481430008

View Profile Personal Message (Offline)

Ignore
1481430008
Reply with quote  #2

1481430008
Report to moderator
1481430008
Hero Member
*
Offline Offline

Posts: 1481430008

View Profile Personal Message (Offline)

Ignore
1481430008
Reply with quote  #2

1481430008
Report to moderator
1481430008
Hero Member
*
Offline Offline

Posts: 1481430008

View Profile Personal Message (Offline)

Ignore
1481430008
Reply with quote  #2

1481430008
Report to moderator
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1470


View Profile
November 13, 2010, 07:20:35 PM
 #22

Patch updated to latest SVN + theymos's fix.  See top of thread for URL.

Jeff Garzik, bitcoin core dev team and BitPay engineer; opinions are my own, not my employer.
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1470


View Profile
November 23, 2010, 12:05:11 AM
 #23

Updated to SVN r188 (accounts).

Jeff Garzik, bitcoin core dev team and BitPay engineer; opinions are my own, not my employer.
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
theymos
Administrator
Legendary
*
expert
Offline Offline

Activity: 2506


View Profile
November 27, 2010, 01:48:16 PM
 #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).

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1470


View Profile
November 27, 2010, 07:04:13 PM
 #25

Patch updated to latest SVN, with additional block/TX size output.

Jeff Garzik, bitcoin core dev team and BitPay engineer; opinions are my own, not my employer.
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1470


View Profile
December 02, 2010, 05:50:44 AM
 #26

Patch updated for latest SVN.

Jeff Garzik, bitcoin core dev team and BitPay engineer; opinions are my own, not my employer.
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
Luke-Jr
Legendary
*
expert
Offline Offline

Activity: 2100



View Profile
January 29, 2011, 03:07:49 PM
 #27

Any reason this shouldn't be merged with the main tree?

jgarzik
Legendary
*
qt
Offline Offline

Activity: 1470


View Profile
February 24, 2011, 12:44:18 AM
 #28

Patch updated to latest git.  See URL at top of thread.

Jeff Garzik, bitcoin core dev team and BitPay engineer; opinions are my own, not my employer.
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
fahhem
Newbie
*
Offline Offline

Activity: 10


View Profile
April 23, 2011, 11:11:26 PM
 #29

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.
xf2_org
Member
**
Offline Offline

Activity: 70


View Profile
May 15, 2011, 07:05:20 AM
 #30


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.

error
Hero Member
*****
Offline Offline

Activity: 574



View Profile
May 23, 2011, 03:01:47 AM
 #31

Any reason this shouldn't be merged with the main tree?

Seconded.

15UFyv6kfWgq83Pp3yhXPr8rknv9m6581W
davout
Legendary
*
Offline Offline

Activity: 1358


1davout


View Profile WWW
June 06, 2011, 12:19:21 PM
 #32

Could it be possible that getblockbyhash returns an "orphan_block" flag ?

See this example that I generated on testnet :
Code:
david@bankbox:~/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:
david@bankbox:~/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.

xf2_org
Member
**
Offline Offline

Activity: 70


View Profile
June 11, 2011, 02:58:41 AM
 #33

getblockbycount / getblockbyhash RPCs updated to latest git.
Grouver (BtcBalance)
Hero Member
*****
Offline Offline

Activity: 530



View Profile WWW
June 29, 2011, 08:11:02 PM
 #34

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.

jgarzik
Legendary
*
qt
Offline Offline

Activity: 1470


View Profile
June 29, 2011, 11:39:33 PM
 #35

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.


Jeff Garzik, bitcoin core dev team and BitPay engineer; opinions are my own, not my employer.
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
Grouver (BtcBalance)
Hero Member
*****
Offline Offline

Activity: 530



View Profile WWW
June 30, 2011, 05:46:46 AM
 #36

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.

TeraPool
Jr. Member
*
Offline Offline

Activity: 42


View Profile
July 20, 2011, 07:08:27 AM
 #37

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

Furyan
Full Member
***
Offline Offline

Activity: 175



View Profile
July 21, 2011, 04:09:57 PM
 #38

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).
Furyan
Full Member
***
Offline Offline

Activity: 175



View Profile
July 21, 2011, 04:50:57 PM
 #39

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<uint256, CBlockIndex*>::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<uint256, CBlockIndex*>::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<boost::int64_t>(params[1]);
         if (strMethod == "listtransactions"       && n > 2) ConvertTo<boost::int64_t>(params[2]);
         if (strMethod == "listaccounts"           && n > 0) ConvertTo<boost::int64_t>(params[0]);
+        if (strMethod == "getblockbycount"        && n > 0) ConvertTo<boost::int64_t>(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!
TeraPool
Jr. Member
*
Offline Offline

Activity: 42


View Profile
July 21, 2011, 09:56:24 PM
 #40

Thank you incredibly Furyan.

Pages: « 1 [2] 3 »  All
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!