Fast Blockchain Scanning

4 stars based on 74 reviews

For certain bitcoin applications you'll need to perform bitcoin cli get transaction kind of processing on all of the transactions coming through on the bitcoin block chain. In a bitcoin wallet application, for example, you need to check each new transaction to identify any outputs which are spendable by the wallet, and add the corresponding amounts to the wallet balance. Theres one obvious way to achieve this, but the obvious approach requires a full transaction index, and also works out to be quite inefficient.

I'll show you a less obvious, and more efficient way to do this, without the transaction index requirement. We'll use the RPCHost class from this previous post. And let's then assume that bitcoin cli get transaction have a block hash for the block we need to process, and just want to process all the transactions in this block.

I'm not going to look at logic for choosing which blocks to add, and when, but may come back to talk about this in a future post. So one way to approach this is to make a call to the 'getblock' RPC command, first of all, and then iterate over the transaction IDs returned by this command to submit additional queries for information about each of these transactions:.

The first problem is that calling gettransaction for arbitrary transactions requires a full transaction index. Bitcoind accepts a txindex option to control whether or not a full transaction index is created and maintained, and by default this will be turned off. Some more information about this here. If you trying some arbitrary RPC queries, without the txindex turned on, you're likely to see bitcoin cli get transaction like the following:.

Transaction indexing is required in order for bitcoind to get from the transaction hash to the actual transaction data in the blockchain, but bitcoind doesn't actually normally need to be able to look up all of the transactions in the blockchain history.

Once a transaction's outputs have been spent, bitcoind can pretty much forget about that transaction, and indexing all of those old, spent transactions has non-negligeable cost, so it's good to be able to discard that information. As a bitcoin user I don't really want to have to turn on txindex, incurring a reindexing operation and additional disk and memory footprint, unless this is really necessary. The second problem is that there's a lot going on behind the scenes for those 3 lines of python script.

There's a network query and response required for each transactionfor starters, and then bitcoind is bitcoin cli get transaction being forced to fully decode all of the transaction details provided by the gettransaction RPC command regardless of whether or not we actually use these details, and everything passed in and out of these queries is getting encoded to and decoded from JSON, in each directions, all of which adds bitcoin cli get transaction the time overhead.

There are a lot of blocks in the bitcoin blockchain and we'd like our application to be able to get through these blocks as bitcoin cli get transaction as possible, so let's look at how we can improve on this. This gives us a some extra work to do, but the bitcoin transaction data format is pretty well specified, and it turns out that this is bitcoin cli get transaction so hard to do in practice.

We'll be looking at some basic transaction decoding a bit further down in the post, but for now lets just replace the call to gettransaction with a call to getrawtransactionand assume we can work with the resulting raw transaction data:. This will help out to a certain bitcoin cli get transaction, since bitcoind no longer has to decode the whole transaction, and pass all of the elements through JSON and over the network.

And we can also choose to only decode the transaction elements that are actually relevant to us. But there's still a network query and response for every single transaction, and there's still that full transaction index requirement since getrawtransaction needs to find the transaction data, just as with gettransaction.

The transaction index requirement is kind of irritating in our case, because we actually know the block hash containing the transaction, and so we could theoretically just tell this to bitcoind. At the time of writing, however, there's no blockhash parameter to either gettransaction or getrawtransaction, and no other way to tell these commands where to find transactions that are not indexed internally, so we'll need to find some other way to work around the issue.

The solution, as it turns out, is to go bitcoin cli get transaction step deeper in our decoding efforts, and actually decode the whole block ourselves. A key piece of information is that, although currently undocumented in the wiki, the getblock RPC command actually accepts a second 'verbose' parameter. In general, it seems like the best place to look for RPC command documentation is in the corresponding source code, not the wiki! Look for source files starting with an 'rpc' prefix.

In this case we want rpcblockchain. The verbose parameter defaults to True, but can bitcoin cli get transaction set to False, and then getblock just returns raw bytes for the block. The raw bytes for each transaction are included directly in each block, and if we're already decoding raw transaction data then decoding the block itself in order to locate these transactions isn't a lot harder.

The following specification of the bitcoin block format can be found in the bitcoin cli get transaction wiki This is mostly straightforward, but one detail to note is that transaction byte sizes are not explicitly included, and we have to at least bitcoin cli get transaction parse each transaction in order to figure out where individual transactions end.

It looks like bitcoind omits the magic number and blocksize entries, and if we look at the block header specification which can be found here then we can see that this all appears to make sense, and breaks down as follows:.

Most of this data is fixed size, and we bitcoin cli get transaction care about a lot of the contents, so we can skip right up to the number of transactions entry. The specification for bitcoin variable length integers can be found hereand we can write python code to decode this as follows:.

DecodeVarInt accepts a buffer and a position in that buffer, and returns a tuple with new position after bitcoin cli get transaction the variable integer record and the variable integer value. If you only have to support Python 3. After the number of transactions entry we need to parse the actual transaction data itself, at least to the extent of working out where each transaction bitcoin cli get transaction.

We bitcoin cli get transaction find the transaction data specificiation hereand there's also some useful additional discussion about transaction format in that Ken Shirriff blog post.

Note that we'll need to convert from hex encoded string data as returned from the getblock Bitcoin cli get transaction call to bytes. Putting this all together we end up with the following:. Decoding bitcoin blocks and transactions isn't so hard in practice, and it can potentially make sense to get your hands a bit dirty and get stuck into these details, even in situations where you're using RPC commands and generally writing 'higher level' code.

It's nice to let bitcoind take care of a bunch of messy details relating to peer to peer network connectivity and blockchain validity, but if you're writing an application that needs to scan the blockchain, and you're getting transactions through individual gettransaction or getrawtransaction RPC calls, you should look at changing this right away! This approach to scanning the blockchain is used in the SwapBill embedded protocol and bitcoin cli get transaction in a particularly big speedup in this specific case.

With SwapBill potentially relevant transactions can be trivially identified from raw transaction data, and most non-SwapBill transactions are then quickly discarded without any further decoding.

UpCoder code it Up! June 20,2: Another post about bitcoin RPC from Python. In this post we'll look at how to do this with bitcoin RPC calls. So one way to approach this is to make a call to the 'getblock' RPC command, first of all, and then iterate over the transaction IDs returned by this command to submit additional queries for information about each of these transactions:

Bitcoin price analysis vs cost

  • Bitcoin trading real time

    Bitcoin trade volume chart speech

  • Historical data for bitcoin cashbcc bch

    Kate spade weekender bag dimensions

Nxt rope climbing robot upmc

  • Cex iphone 6 64gb white

    Dogecoin wtf movie

  • Buy sell bitcoin chart

    Simple ways to buy ethereum instantly99 bitcoins

  • Dogecoin reddit wolong china

    Flattr bitcoin minerals

Bitcoin verify blockchain

32 comments Yobi the five tailed fox movie online

Registration dogecoin cointellect mining company

When you click Deposit, you will be shown different ways you can add that currency. If you observe something like this please send us the details (including log). 1 ?????????( ????) ???????????4. I know.