Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: jgarzik on August 06, 2010, 07:34:52 AM



Title: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: jgarzik on August 06, 2010, 07:34:52 AM
Gavin's bitcointools are nice for dumping blocks, but they require you to shutdown bitcoind, because bitcoin sets DB_PRIVATE flag, preventing outside users from using the multi-user database :)

See "getblockbycount" branch of git repository https://github.com/jgarzik/bitcoin

Here is sample command usage and output for block at height 71995:
Code:
$ /spare/repo/bitcoin.hacks/bitcoind -datadir=/garz/bitcoin/data getblock 71995
{
    "hash" : "00000000002d1a4380793affbc610885aa2e0b224eeedd64ffe108044ec7d434",
    "ver" : 1,
    "prev_block" : "000000000103fcffbd8020ff7459f3635eb41102ee3b22fa466a7fdfc05bad58",
    "mrkl_root" : "9d436c694968454ea0d17f4aece3b829930027c3cb918e5107a1605aa2eeae33",
    "time" : 1280823515,
    "bits" : 469830746,
    "nonce" : 2918845955,
    "n_tx" : 4,
    "tx" : [
        {
            "hash" : "f85e77e4379694c8d2c1232d6fddfc7792073fb8484bdac37a9ba5ed1d245c57",
            "ver" : 1,
            "vin_sz" : 1,
            "vout_sz" : 1,
            "lock_time" : 0,
            "in" : [
                {
                    "prev_out" : {
                        "hash" : "0000000000000000000000000000000000000000000000000000000000000000",
                        "n" : 4294967295
                    },
                    "coinbase" : "045a0c011c0143"
                }
            ],
            "out" : [
                {
                    "value" : 50.00000000000000,
                    "scriptPubKey" : "0xAD2D2527C630A3CF951703C4F44BB70F8C7524823F7095253D9412A1E9CAD1782B6B83228083A02485C20BC1870FB1A06C09DB768A5D27326A3E2FD859E7799204 OP_CHECKSIG"
                }
            ]
        },
        {
            "hash" : "38431f2f029a37a74a5b5bf0327f41a67b83aef8ad60a2efe918a8f1f0e7df1b",
            "ver" : 1,
            "vin_sz" : 1,
            "vout_sz" : 1,
            "lock_time" : 0,
            "in" : [
                {
                    "prev_out" : {
                        "hash" : "d9b308ca3484b7be6599c5050fec2cd2d31e654a0d2560989ffd28590bef6e6a",
                        "n" : 0
                    },
                    "scriptSig" : "0x01D82CD24A2C12E108087B9D9F81C49EF550F24D12668381D1D333D383B8EEB717200209F92E3246912CE4965E728963DF65FA63D9CCA10513CEF35C9BDCDC8018695F20024430"
                }
            ],
            "out" : [
                {
                    "value" : 50.00000000000000,
                    "scriptPubKey" : "OP_DUP OP_HASH160 0x403FC36C7A1B5A9390F29343F4317F148A3ACB18 OP_EQUALVERIFY OP_CHECKSIG"
                }
            ]
        },
        {
            "hash" : "c61a96af68cce7329a450c18e3ac359d3052bda9187515a42fa7d262405213bd",
            "ver" : 1,
            "vin_sz" : 1,
            "vout_sz" : 2,
            "lock_time" : 0,
            "in" : [
                {
                    "prev_out" : {
                        "hash" : "1ff50ec1208497b333972da7dfdb2ba92c18da901df997d630b32981a013b783",
                        "n" : 0
                    },
                    "scriptSig" : "0x0132831D85F7395034FD537CADFB5BC3DF134347E73BD76352D31314686785B17320024351730466BEC02AD0A9D1F2BFBF4F246AFA8EE4B1BEE3FA8B83801B8301AF5820024430 0x51342C8A6D8C38C33413C63BE8CA93AD060BEA212D961EA63F4C76013E81978A06934604515C7941A729450A508CF556AD6B5061ADABF74C8F881C44B2405CDA04"
                }
            ],
            "out" : [
                {
                    "value" : 0.05000000000000000,
                    "scriptPubKey" : "OP_DUP OP_HASH160 0xE183BC5BB9CEF757C51BCD8B864A8F2210114373 OP_EQUALVERIFY OP_CHECKSIG"
                },
                {
                    "value" : 0.4300000000000000,
                    "scriptPubKey" : "OP_DUP OP_HASH160 0x209F7B58DC860A2B0D2547FE659D78DFFC68FA77 OP_EQUALVERIFY OP_CHECKSIG"
                }
            ]
        },
        {
            "hash" : "d561a3594fcf97dd1a1abe7a1eda15c8e335aaaecf97f959de0595298d87c6d5",
            "ver" : 1,
            "vin_sz" : 1,
            "vout_sz" : 2,
            "lock_time" : 0,
            "in" : [
                {
                    "prev_out" : {
                        "hash" : "c61a96af68cce7329a450c18e3ac359d3052bda9187515a42fa7d262405213bd",
                        "n" : 1
                    },
                    "scriptSig" : "0x01DCDA3BAD3620FE246F2D973D6128123FC5F054121624852D98CEA4A3333128D00021025FFA265DA89861FC609792AD193204FCF0B2C1823B2D23052BCBD468274367BA0021024630 0x53B6705A0E13FD31A87C4F5F43ABDA77995B7CDA87B521043488254B16A122AFE99EA0BAE780CA19C6DCB733ECA9F42404B1F77951703B4057362704C126BD2304"
                }
            ],
            "out" : [
                {
                    "value" : 0.05000000000000000,
                    "scriptPubKey" : "OP_DUP OP_HASH160 0x388E046D522A5EFDBFD272EAE11E9718F8C10FA0 OP_EQUALVERIFY OP_CHECKSIG"
                },
                {
                    "value" : 0.3800000000000000,
                    "scriptPubKey" : "OP_DUP OP_HASH160 0x1FC4B63556EB262B048F4A908875A62E338F7364 OP_EQUALVERIFY OP_CHECKSIG"
                }
            ]
        }
    ],
    "mrkl_tree" : [
        "f85e77e4379694c8d2c1232d6fddfc7792073fb8484bdac37a9ba5ed1d245c57",
        "38431f2f029a37a74a5b5bf0327f41a67b83aef8ad60a2efe918a8f1f0e7df1b",
        "c61a96af68cce7329a450c18e3ac359d3052bda9187515a42fa7d262405213bd",
        "d561a3594fcf97dd1a1abe7a1eda15c8e335aaaecf97f959de0595298d87c6d5",
        "b626f3cd7c1fd229bffbec34fab2700bc31659dbf2a74f7916701a18490125b4",
        "dd74eea07e9bf2744655a9bab3a8cdbc5fe9a0c86ad18f42a2c259d40a83decb",
        "9d436c694968454ea0d17f4aece3b829930027c3cb918e5107a1605aa2eeae33"
    ]
}


Title: Re: [PATCH] implement getblock RPC command
Post by: knightmb on August 06, 2010, 07:47:48 AM
Hey cool, you can even see the generation and other transactions in it, nice  ;D


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on August 07, 2010, 05:11:58 AM
Updated patch to v2, with a simple cast fix for 32-bit machines.

Same URL: http://gtf.org/garzik/bitcoin/patch.bitcoin-getblock (http://gtf.org/garzik/bitcoin/patch.bitcoin-getblock)


Title: Re: [PATCH] implement getblock RPC command
Post by: throughput on August 09, 2010, 12:41:25 PM
Oh! You have already implemented the feature I need!

Great!


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on August 13, 2010, 07:42:34 PM

Patch updated for current SVN, r130.  Same patch URL, no change in implementation or operation.



Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on September 01, 2010, 06:39:57 PM

Patch updated for current SVN, r146, aka the "break patches with a CR/LF policy change" revision.

Same patch URL (see top of this thread), no change in implementation or operation.


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on September 18, 2010, 07:45:08 PM
Patch updated to latest SVN.


Title: Re: [PATCH] implement getblock RPC command
Post by: Gavin Andresen on September 27, 2010, 05:57:53 PM
I've been working on a "monitoraddress" / "monitorblocks" patch, so bitcoind will POST transaction/block information to an arbitrary URL.

And it seems to me "getblock" should be split into "getblockbycount", "getblockbyhash" and "gettransaction".  I also have some suggestions for making the naming more consistent:

I'd suggest getblockby* return:
Code:
{
    "hash" : "00000000002d1a4380793affbc610885aa2e0b224eeedd64ffe108044ec7d434",
    "blockcount" : 71995,
    "version" : 1,
    "merkleroot" : "9d436c694968454ea0d17f4aece3b829930027c3cb918e5107a1605aa2eeae33",
    "time" : 1280823515,
    "nonce" : 2918845955,
    "tx" : [  "f85e77e4379694c8d2c1232d6fddfc7792073fb8484bdac37a9ba5ed1d245c57", etc ]
}

Reasoning:
  blockcount instead of 'height' or 'depth' because getinfo returns "blockcount"
  getblockbyhash might return a block not on the "best" chain; return blockcount=-1 in that case?
  version instead of 'ver' because abbrvs shd b avoided (thy cn b hrd to rd/rmbr)
  only return transaction hashes; if you need more transaction details, call gettransaction.
  No n_tx (or n_anything) because it is implicit in length of returned array

(stuff I'm not sure about:  what is 'bits' in the implemented getblock?  Is that difficulty?  Does anybody really need all the detailed merkle tree info?)

I'd suggest gettransaction return:
Code:
{
  "txid" : ...transaction id passed in,
  "version" : 1,
  "time" : ...transaction time...,
  "confirmations" : n,
  "block" : "block hash",   #  (if confirmations > 0)
  "from" : [ [ "txid", n ], ... ], # Empty if coin generation transaction
  "to" : [ [ "address", amount ], ... ],
}

Reasoning:
  Returning the CScript string seems like it is both too much detail AND not very useful (I want the bitcoin addresses, NOT OP_DUP 0xblahblahblah ...)
  "from" is just a list of pointers to previous transaction "to"s
  confirmations is duplicate info (you could getblockbyhash and then look at its blockcount), but is so useful I think it belongs here.


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on September 27, 2010, 06:20:46 PM
Is there an index that indexes all transaction ids for all blocks?

Or should 'gettransaction' only work for wallet transactions?


Title: Re: [PATCH] implement getblock RPC command
Post by: Gavin Andresen on September 27, 2010, 07:40:20 PM
blkindex.dat contains all the transactions, in records that look like:
  key:  "tx" + HASH_OF_TRANSACTION
  value: serialized CTxIndex

The CTxIndex points to a record in one of the blk000n.dat files.

That does bring up an interesting point, though:  if lightweight clients are implemented, they wouldn't be able to support an arbitrary 'gettransaction'.


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on September 29, 2010, 05:30:52 PM

Patch updated to latest SVN, with a few of the suggested gavinchanges:

  • 'getblock' renamed to 'getblockbycount'
  • s/ver/version/
  • vin_sz and vout_sz removed, redundant


Title: Re: [PATCH] implement getblock RPC command
Post by: mizerydearia on October 14, 2010, 09:27:46 PM
Just a quick note that this patch is not compatible with http://bitcointalk.org/index.php?topic=1319.msg15065#msg15065

Quote
<gavinandresen> I reimplemented getblock in the monitor* patch
<gavinandresen> (so it returned the same JSON data structures as monitorblock )
<necrodearia> Ah, so your patch makes getblock patch deprecated, right?
<gavinandresen> Yup
<necrodearia> Yay! ^_^


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on October 15, 2010, 12:40:53 AM
That is incorrect.

The content in gavin's patch makes my getblock largely redundant -- but they are not incompatible.


Title: Re: [PATCH] implement getblock RPC command
Post by: mizerydearia on October 15, 2010, 02:49:25 AM
That is incorrect.

The content in gavin's patch makes my getblock largely redundant -- but they are not incompatible.

Hummm...  I was able to use one patch or the other.  When trying to use both patches, I couldn't apply the patches.  Perhaps I did something wrong.  I will try again.

When applying gavin's monitor patch first and your getblock patch second, the following error occurs: http://pastebin.com/CUr1cvgS
When applying your getblock patch first and gavin's monitor patch second, the following error occurs: http://pastebin.com/6UY4dscY


Title: Re: [PATCH] implement getblock RPC command
Post by: theymos on October 15, 2010, 03:50:24 AM
Hummm...  I was able to use one patch or the other.  When trying to use both patches, I couldn't apply the patches.  Perhaps I did something wrong.  I will try again.

patch is not meant to be used blindly. You're supposed to look at the reject file manually and figure it out.

The problem is caused when trying to patch the section of rpc.cpp around line 1330. Both patches try to apply their changes to the bottom of the list, with context looking up. They modify each other's context. To fix this, they should be applied to the top looking only up or the bottom looking only down. (Not sure if you can actually do this, however.)


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on October 21, 2010, 07:36:49 PM
Patch updated to latest SVN (v0.3.14).


Title: Re: [PATCH] implement getblock RPC command
Post by: theymos on November 07, 2010, 05:00:05 AM
I noticed a bug in getblock (and bitcointools). Block 67300 is missing several transactions when compared to the output of Bitcoin's printblock, and the hash is also wrong.

I ran into the bug while parsing the entire chain: transaction e7a995 is spent in 67301, but according to getblock data that transaction was never made.


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on November 07, 2010, 04:08:50 PM
I noticed a bug in getblock (and bitcointools). Block 67300 is missing several transactions when compared to the output of Bitcoin's printblock, and the hash is also wrong.

I ran into the bug while parsing the entire chain: transaction e7a995 is spent in 67301, but according to getblock data that transaction was never made.

hmmmm.  That's quite strange, considering the code simply loops over all 'vin' and 'vout', duplicating the code pattern used elsewhere to dump a block to the debug log.


Title: Re: [PATCH] implement getblock RPC command
Post by: theymos on November 11, 2010, 02:36:39 AM
ArtForz figured out that this is caused because getblock is showing an orphan block at that position instead of the real block. Deleting the block database files fixes it because you won't re-download orphan blocks.


Title: Re: [PATCH] implement getblock RPC command
Post by: theymos on November 11, 2010, 05:03:09 PM
I think that this can be fixed by changing this:

Code:
	if (pindex->nHeight == height) {

to this:

Code:
	if (pindex->nHeight == height && pindex->IsInMainChain()) {

jgarzik: I don't have any orphan blocks right now, so can you test this on your block 90539 and compare it to the real one (http://theymos.ath.cx:64150/bbe/rawblock/00000000000c80a30b1e4242c30a6795b7fbcc2b1ce6e59132fd11fd47c729fd)?


Title: Re: [PATCH] implement getblock RPC command
Post by: theymos on November 13, 2010, 08:31:36 AM
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.


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on November 13, 2010, 07:20:35 PM
Patch updated to latest SVN + theymos's fix.  See top of thread for URL.


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on November 23, 2010, 12:05:11 AM
Updated to SVN r188 (accounts).


Title: Re: [PATCH] implement getblock RPC command
Post by: theymos on November 27, 2010, 01:48:16 PM
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).


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on November 27, 2010, 07:04:13 PM
Patch updated to latest SVN, with additional block/TX size output.


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on December 02, 2010, 05:50:44 AM
Patch updated for latest SVN.


Title: Re: [PATCH] implement getblock RPC command
Post by: Luke-Jr on January 29, 2011, 03:07:49 PM
Any reason this shouldn't be merged with the main tree?


Title: Re: [PATCH] implement getblock RPC command
Post by: jgarzik on February 24, 2011, 12:44:18 AM
Patch updated to latest git.  See URL at top of thread.


Title: Re: [PATCH] implement getblock RPC command
Post by: fahhem on April 23, 2011, 11:11:26 PM
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.


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: xf2_org on May 15, 2011, 07:05:20 AM

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.



Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: error on May 23, 2011, 03:01:47 AM
Any reason this shouldn't be merged with the main tree?

Seconded.


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: davout on June 06, 2011, 12:19:21 PM
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.


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: xf2_org on June 11, 2011, 02:58:41 AM
getblockbycount / getblockbyhash RPCs updated to latest git.


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: Grouver (BtcBalance) on June 29, 2011, 08:11:02 PM
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.


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: jgarzik on June 29, 2011, 11:39:33 PM
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.



Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: Grouver (BtcBalance) on June 30, 2011, 05:46:46 AM
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.


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: TeraPool on July 20, 2011, 07:08:27 AM
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


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: Furyan on July 21, 2011, 04:09:57 PM
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 :)

(Reason I need a diff is that I have other patches applied to my local copy - most of JoelKatz' work).


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: Furyan on July 21, 2011, 04:50:57 PM
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!


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: TeraPool on July 21, 2011, 09:56:24 PM
Thank you incredibly Furyan.


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: Furyan on July 22, 2011, 11:47:21 AM
Thank you incredibly Furyan.

Glad to help :)

Now if I can get Gavin's Monitor patch in there... much more invasive and stomps on some patches I've already applied..


Title: Re: [PATCH] getblockbycount, getblockbyhash RPCs
Post by: JoelKatz on November 02, 2011, 11:07:36 PM
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?)