Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Xenland on April 22, 2012, 06:54:57 AM



Title: Reading the block chain with a library?
Post by: Xenland on April 22, 2012, 06:54:57 AM
How do I read the Bitcoin blockchain with an api or library with a programming language like php, python or java?


Title: Re: Reading the block chain with a library?
Post by: ThomasV on April 22, 2012, 06:58:17 AM
How do I read the Bitcoin blockchain with an api or library with a programming language like php, python or java?

use libbitcoin


Title: Re: Reading the block chain with a library?
Post by: kokjo on April 22, 2012, 06:58:30 AM
libbitcoin: https://github.com/libbitcoin/libbitcoin


Title: Re: Reading the block chain with a library?
Post by: arby on April 22, 2012, 07:07:40 AM
libbitoin as i see is in cpp, not php or java, python as the OP mentioned.

I think he is interested in using this on linux, and not using any executables.

I am searching for this also, but have not found anything yet.

What I found that you can reliably use with PHP is:

https://blockchain.info/api

https://blockchain.info/q

To explore the blockchain.


Title: Re: Reading the block chain with a library?
Post by: kokjo on April 22, 2012, 07:09:42 AM
libbitoin as i see is in cpp, not php or java, python as the OP mentioned.

I thin he is interested in using this on linux, and not using any executables.
swig!


Title: Re: Reading the block chain with a library?
Post by: arby on April 22, 2012, 07:14:45 AM
libbitoin as i see is in cpp, not php or java, python as the OP mentioned.

I thin he is interested in using this on linux, and not using any executables.
swig!

Ok I will give that a try also, thanks for the info


Title: Re: Reading the block chain with a library?
Post by: ThomasV on April 22, 2012, 07:54:46 AM
libbitoin as i see is in cpp, not php or java, python as the OP mentioned.

yes, it has a python frontend


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 22, 2012, 08:35:55 PM
Nice responses mates, libbitcoin should work fine for what i need


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 23, 2012, 05:54:10 AM
So how do I use this libbitcoin thing, for example something simple like a balance of a particular address, or a list of details of a particular address? (with python)


Title: Re: Reading the block chain with a library?
Post by: kokjo on April 23, 2012, 07:31:27 AM
So how do I use this libbitcoin thing, for example something simple like a balance of a particular address, or a list of details of a particular address? (with python)
don't know, try read the api... im not a libbitcoin guru.


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 23, 2012, 08:49:31 AM
Libbitcoin is quite confusing even looking through the source code, are there any alternatives to libbitcoin that could help me query the blockchain?


Title: Re: Reading the block chain with a library?
Post by: etotheipi on April 23, 2012, 02:30:11 PM
Armory is perfect this.  It is C++ accessed in Python (via SWIG).  You only ever touch python.

I'm not at my development computer now but there is example code on my github page (https://github.com/etotheipi/BitcoinArmory) (though many of the examples were created before RAM reduction, they will mostly still work)

Code:
   from armoryengine import *

   print '\n\nLoading Blockchain from:', BLK0001_PATH
   BDM_LoadBlockchainFile()  # optional argument to specify blk0001.dat location
   print 'Done!'

   print '\n\nCurrent Top Block is:', TheBDM.getTopBlockHeader().getBlockHeight()
   TheBDM.getTopBlockHeader().pprint()

   tx = TheBDM.getTxByHash( hex_to_binary('9c633b5689e462ddf3d52a6edc64226cedd1e1749d0b8e2f70cd9550bfa74c72') )
   tx.pprint()

   # Create and register the wallet before the LoadBlockchainFile call, to merge the scans
   # For now, this will rescan the blockchain
   cppWallet = Cpp.BtcWallet()
   cppWallet.addAddress_1_( hex_to_binary('9c633b56899c633b56899c633b56899c633b5689') )  # hash160
   TheBDM.registerWallet(cppWallet)
   TheBDM.scanBlockchainForTx(cppWallet)
   print cppWallet.getSpendableBalance()

   unspentTxOuts = cppWallet.getSpendableTxOutList()
   for utxo in unspentTxOuts:
      utxo.pprintOneLine()
  
(note, this code is notional since I can't test it where I am, but the syntax is nearly correct)

When I get home later, I can post more example code for how to access what you want.  If you tell me what you're trying to do with it, I can write more directed code samples for you.


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 23, 2012, 08:31:30 PM
Armory is perfect this.  It is C++ accessed in Python (via SWIG).  You only ever touch python.

I'm not at my development computer now but there is example code on my github page (https://github.com/etotheipi/BitcoinArmory) (though many of the examples were created before RAM reduction, they will mostly still work)

Code:
   from armoryengine import *

   print '\n\nLoading Blockchain from:', BLK0001_PATH
   BDM_LoadBlockchainFile()  # optional argument to specify blk0001.dat location
   print 'Done!'

   print '\n\nCurrent Top Block is:', TheBDM.getTopBlockHeader().getBlockHeight()
   TheBDM.getTopBlockHeader().pprint()

   tx = TheBDM.getTxByHash( hex_to_binary('9c633b5689e462ddf3d52a6edc64226cedd1e1749d0b8e2f70cd9550bfa74c72') )
   tx.pprint()

   # Create and register the wallet before the LoadBlockchainFile call, to merge the scans
   # For now, this will rescan the blockchain
   cppWallet = Cpp.BtcWallet()
   cppWallet.addAddress_1_( hex_to_binary('9c633b56899c633b56899c633b56899c633b5689') )  # hash160
   TheBDM.registerWallet(cppWallet)
   TheBDM.scanBlockchainForTx(cppWallet)
   print cppWallet.getSpendableBalance()

   unspentTxOuts = cppWallet.getSpendableTxOutList()
   for utxo in unspentTxOuts:
      utxo.pprintOneLine()
  
(note, this code is notional since I can't test it where I am, but the syntax is nearly correct)

When I get home later, I can post more example code for how to access what you want.  If you tell me what you're trying to do with it, I can write more directed code samples for you.

Nice I heard about armory but I thought it was just a GUI client with advanced features. I'll try it out with python see how it is.


Title: Re: Reading the block chain with a library?
Post by: etotheipi on April 24, 2012, 01:05:10 AM
Here you go!  I just put together a script that not only scans the blockchain, it does something useful!  (it's at the bottom of the post)

  • It took 18 seconds for me to scan the whole blockchain from a cold start
  • It took 0.2 seconds to collect a list of every difficulty change since the genesis block.
  • It took 24 seconds on my system to count 3,532,497 unique addresses in the blockchain! (as of block 176953)

Timings will vary depending on RAM.  If you have a lot of RAM, just about the whole blockchain will be cached from the first scan, and rescans will be nearly instantaneous.  If you want to avoid rescans, make sure everything is added to your wallet and registered with TheBDM.registerWallet before BDM_LoadBlockChain().

The sample python script at the bottom of this post produced the following output (the difficulty changes):
Code:
Collect all difficulty changes...
     Block       Diff   Date
         0        1.0 2009-Jan-03 01:15pm
     32256        1.2 2009-Dec-30 01:11am
     34272        1.3 2010-Jan-11 05:48pm
     36288        1.3 2010-Jan-25 08:07am
     38304        1.8 2010-Feb-04 04:43pm
     40320        2.5 2010-Feb-14 06:52pm
     42336        3.8 2010-Feb-24 03:41am
     44352        4.5 2010-Mar-07 08:14pm
     46368        4.6 2010-Mar-21 06:54pm
     48384        6.1 2010-Apr-01 07:07am
     50400        7.8 2010-Apr-12 04:39am
     52416       11.5 2010-Apr-21 05:52pm
     54432       12.8 2010-May-04 05:46am
     56448       11.8 2010-May-19 10:13am
     58464       16.6 2010-May-29 09:57am
     60480       17.4 2010-Jun-11 07:26pm
     62496       19.4 2010-Jun-24 08:27am
     64512       23.5 2010-Jul-05 09:57pm
     66528       45.4 2010-Jul-13 04:03am
     68544      181.5 2010-Jul-16 12:29pm
     70560      244.2 2010-Jul-26 10:42pm
     72576      352.2 2010-Aug-05 03:46pm
     74592      511.8 2010-Aug-15 07:11am
     76608      623.4 2010-Aug-26 07:13pm
     78624      712.9 2010-Sep-08 01:04am
     80640      917.8 2010-Sep-18 10:04pm
     82656     1318.7 2010-Sep-28 03:58pm
     84672     1378.0 2010-Oct-12 01:35am
     86688     2149.0 2010-Oct-21 01:13am
     88704     3091.7 2010-Oct-30 06:58pm
     90720     4536.4 2010-Nov-09 07:29am
     92736     6866.9 2010-Nov-18 01:44pm
     94752     8078.2 2010-Nov-30 11:37am
     96768    12252.0 2010-Dec-09 05:20pm
     98784    14484.2 2010-Dec-21 01:34pm
    100800    16307.4 2011-Jan-03 12:10am
    102816    18437.6 2011-Jan-15 09:26am
    104832    22012.4 2011-Jan-27 03:16am
    106848    25997.9 2011-Feb-07 11:53pm
    108864    36459.9 2011-Feb-18 12:15am
    110880    55589.5 2011-Feb-27 04:59am
    112896    76192.6 2011-Mar-09 10:25am
    114912    68977.8 2011-Mar-24 10:39pm
    116928    82345.6 2011-Apr-05 04:09pm
    118944    92347.6 2011-Apr-18 03:49am
    120960   109670.1 2011-Apr-29 10:53pm
    122976   157416.4 2011-May-09 05:17pm
    124992   244112.5 2011-May-18 06:04pm
    127008   434877.0 2011-May-26 02:41pm
    129024   567269.5 2011-Jun-06 08:25am
    131040   876954.5 2011-Jun-15 09:49am
    133056  1379192.3 2011-Jun-24 07:45am
    135072  1563028.0 2011-Jul-06 04:35pm
    137088  1690895.8 2011-Jul-19 03:23pm
    139104  1888786.7 2011-Aug-01 04:11am
    141120  1805700.8 2011-Aug-15 07:44pm
    143136  1777774.5 2011-Aug-30 01:15am
    145152  1755425.3 2011-Sep-13 05:31am
    147168  1689334.4 2011-Sep-27 06:47pm
    149184  1468195.4 2011-Oct-13 09:44pm
    151200  1203461.9 2011-Oct-30 11:42pm
    153216  1192497.8 2011-Nov-14 01:56am
    155232  1090715.7 2011-Nov-29 09:20am
    157248  1155038.3 2011-Dec-12 02:42pm
    159264  1159929.5 2011-Dec-26 01:43pm
    161280  1250757.7 2012-Jan-08 01:26pm
    163296  1307728.4 2012-Jan-21 10:55pm
    165312  1379647.4 2012-Feb-04 05:32am
    167328  1376302.3 2012-Feb-18 06:24am
    169344  1496978.6 2012-Mar-02 03:25am
    171360  1498294.4 2012-Mar-16 04:09am
    173376  1626553.5 2012-Mar-29 01:41am
    175392  1577913.5 2012-Apr-12 12:04pm
Took 0.2 seconds to collect difficulty list

And here is the script itself.  It illustrates a variety of ways you can access block data.  Mainly, scanning for addresses in the blockchain with balances and unspent outputs,  and walking through every TxOut of every Tx of every Block -- we grab the address from ever standard TxOut and add it to a set() object which only allows unique addresses.

Code:
from armoryengine import *

# NOTE:
#     ALL ADDRESSES THROUGHOUT EVERYTHING ARE IN 20-BYTE BINARY FORM (hash160/addr20)
#     Use hash160_to_addrStr() and addrStr_to_hash160() to convert...

print '\n\nCreating a new C++ wallet, add a few addresses...'
cppWallet = Cpp.BtcWallet()
cppWallet.addAddress_1_( hex_to_binary('11b366edfc0a8b66feebae5c2e25a7b6a5d1cf31') )  # hash160
cppWallet.addAddress_1_( addrStr_to_hash160('1EbAUHsitefy3rSECh8eK2fdAWTUbpVUDN') )   # addrStr
cppWallet.addAddress_1_('\x1b~\xa7*\x85\t\x12\xb7=\xd4G\xf3\xbd\xc1\x00\xf1\x00\x8b\xde\xb0') # binary


print 'Addresses in this wallet:'
for i in range(cppWallet.getNumAddr()):
   print '\t', hash160_to_addrStr(cppWallet.getAddrByIndex(i).getAddrStr20())


print '\n\nRegistering the wallet with the BlockDataManager & loading...'
start = RightNow()
TheBDM.registerWallet(cppWallet)
BDM_LoadBlockchainFile()  # optional argument to specify blk0001.dat location
print 'Loading blockchain took %0.1f sec' % (RightNow() - start)


topBlock = TheBDM.getTopBlockHeight()
print '\n\nCurrent Top Block is:', topBlock
TheBDM.getTopBlockHeader().pprint()


# Add new addresses -- will rescan (which will be super fast if you ahve a lot of RAM)
cppWallet.addAddress_1_( hex_to_binary('0cdcd0f388a31b11ff11b1d8d7a9f978b37bc7af') )
TheBDM.scanBlockchainForTx(cppWallet)



print '\n\nBalance of this wallet:', coin2str(cppWallet.getSpendableBalance())
print 'Unspent outputs:'
unspentTxOuts = cppWallet.getSpendableTxOutList(topBlock)
for utxo in unspentTxOuts:
   utxo.pprintOneLine()

print '\n\nTransaction history of this wallet:'
ledger = cppWallet.getTxLedger()
for le in ledger:
   le.pprintOneLine()

print '\n\n'
print '-'*80
print 'Now for something completely different...'
start = RightNow()
print '\n\nCollect all difficulty changes...'
prevDiff = 0
for h in xrange(0,topBlock+1):
   header = TheBDM.getHeaderByHeight(h)
   currDiff = header.getDifficulty()
   if not prevDiff==currDiff:
      print str(h).rjust(10),
      print ('%0.1f'%currDiff).rjust(10),
      print '\t',unixTimeToFormatStr(header.getTimestamp())
   prevDiff = currDiff

print 'Took %0.1f seconds to collect difficulty list' % (RightNow()-start)
      
      
print '\n\nCount the number of unique addresses in the blockchain'
start = RightNow()
allAddr = set()
for h in xrange(0,topBlock+1):
   if h%10000 == 0:
      print '\tScanned %d blocks' % h
      
   header = TheBDM.getHeaderByHeight(h)
   txList = header.getTxRefPtrList()
   for tx in txList:
      for nout in range(tx.getNumTxOut()):
         txout = tx.getTxOutRef(nout)
         if txout.isStandard():
            allAddr.add(txout.getRecipientAddr())

print 'Took %0.1f seconds to count all addresses' % (RightNow()-start)
print 'There are %d unique addresses in the blockchain!' % len(allAddr)


This scanning is lower-level than what happens in Armory, so you are mainly using C++ objects.  You can find a full list of all the methods in BlockObjRef.h and BlockUtils.h.  Please ask questions if something isn't clear, or if you want to use some functionality not exemplified here.  



Title: Re: Reading the block chain with a library?
Post by: Sukrim on April 24, 2012, 01:11:23 AM
If you tell me what you're trying to do with it, I can write more directed code samples for you.
I personally want to get the timestamp of blocks with a certain block height once per day, but didn't get around to looking at either libbitcoin or Armory.
Ideally it would be something like "TheBDM.getHeaderByHeight(12345).getTimestamp()"...

Also for armory I still have to run bitcoind to get the blockchain updated, right?

Edit:
Wow - this is awesome, you manage to answer my posts before I even post them! :o
Edit2:
And I even guessed both function names correctly to the letter! This is getting creepy...


Title: Re: Reading the block chain with a library?
Post by: etotheipi on April 24, 2012, 01:13:40 AM
If you tell me what you're trying to do with it, I can write more directed code samples for you.
I personally want to get the timestamp of blocks with a certain block height once per day, but didn't get around to looking at either libbitcoin or Armory.
Ideally it would be something like "TheBDM.getHeaderByHeight(12345).getTimestamp()"...

Also for armory I still have to run bitcoind to get the blockchain updated, right?

To use Armory, you need bitcoind running to receive blockchain updates and send&receive transactions.  For the example code above, you only need to have the blk0001.dat file produced by the bitcoind, but it doesn't need to be running when you run the script.

Edit:  Wow, I must have telepathy.  Glad I could answer your question... before you asked it!


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 24, 2012, 05:55:41 AM
How does one just query the balance of a Bitcoin address with armory? is there any calculations involved(like looping over tx history and sum up a balance) or can I just call it and print out a simple balance?

Im' sure a lot of programmers could benefit the solution to this question to write their own software to use armory for like making their own Blockchain.info website or w/e.


Title: Re: Reading the block chain with a library?
Post by: etotheipi on April 24, 2012, 03:53:52 PM
How does one just query the balance of a Bitcoin address with armory? is there any calculations involved(like looping over tx history and sum up a balance) or can I just call it and print out a simple balance?

Im' sure a lot of programmers could benefit the solution to this question to write their own software to use armory for like making their own Blockchain.info website or w/e.

The code was in my example, but I guess my example was too long.  So here's an extraction of it.

Code:
from armoryengine import *

cppWallet = Cpp.BtcWallet()
cppWallet.addAddress_1_( addrStr_to_hash160('1EbAUHsitefy3rSECh8eK2fdAWTUbpVUDN') )   # addrStr
TheBDM.registerWallet(cppWallet)
BDM_LoadBlockchainFile()
TheBDM.scanBlockchainForTx(cppWallet)

fullBalance = cppWallet.getFullBalance()

print '\n\nBalance of this wallet:', coin2str(fullBalance)



Title: Re: Reading the block chain with a library?
Post by: Xenland on April 25, 2012, 12:02:20 AM
How does one just query the balance of a Bitcoin address with armory? is there any calculations involved(like looping over tx history and sum up a balance) or can I just call it and print out a simple balance?

Im' sure a lot of programmers could benefit the solution to this question to write their own software to use armory for like making their own Blockchain.info website or w/e.

The code was in my example, but I guess my example was too long.  So here's an extraction of it.

Code:
from armoryengine import *

cppWallet = Cpp.BtcWallet()
cppWallet.addAddress_1_( addrStr_to_hash160('1EbAUHsitefy3rSECh8eK2fdAWTUbpVUDN') )   # addrStr
TheBDM.registerWallet(cppWallet)
BDM_LoadBlockchainFile()
TheBDM.scanBlockchainForTx(cppWallet)

fullBalance = cppWallet.getFullBalance()

print '\n\nBalance of this wallet:', coin2str(fullBalance)



Oh yeah I noticed that part of your code, I just assumed that there was a quick way of going about this since it takes a few seconds to scan the block chain for one address and I'm looking for a way to scan and update the database for multiple addresses. Does that sound possible or am I stuck with this route you presented me? Thanks for your help btw I appreciate it.


Title: Re: Reading the block chain with a library?
Post by: etotheipi on April 25, 2012, 01:20:07 AM
Oh yeah I noticed that part of your code, I just assumed that there was a quick way of going about this since it takes a few seconds to scan the block chain for one address and I'm looking for a way to scan and update the database for multiple addresses. Does that sound possible or am I stuck with this route you presented me? Thanks for your help btw I appreciate it.

You can use as many addAddress_1_() calls as you want before a rescan, and the rescan will still take the same amount of time.  Load 20 addresses into your wallet before loading the blockchain, and it will take the same time as if you loaded in 10,000.

After the blockchain is loaded, if you add any new addresses to the wallet, the chain will be re-scanned on the "scanBlockchainForTx()" call, and again, it won't matter how many addresses that is, it'll take the same amount of time.


Title: Re: Reading the block chain with a library?
Post by: Pieter Wuille on April 25, 2012, 01:55:28 AM
If you tell me what you're trying to do with it, I can write more directed code samples for you.
I personally want to get the timestamp of blocks with a certain block height once per day, but didn't get around to looking at either libbitcoin or Armory.
Ideally it would be something like "TheBDM.getHeaderByHeight(12345).getTimestamp()"...

libbitcoin or armory's library will surely give you a much more flexible interface, but you *can* just do this using bitcoind as well. See the getblockhash and getblock RPC commands.


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 25, 2012, 03:57:50 AM
Oh yeah I noticed that part of your code, I just assumed that there was a quick way of going about this since it takes a few seconds to scan the block chain for one address and I'm looking for a way to scan and update the database for multiple addresses. Does that sound possible or am I stuck with this route you presented me? Thanks for your help btw I appreciate it.
Oh yeah I noticed that part of your code, I just assumed that there was a quick way of going about this since it takes a few seconds to scan the block chain for one address and I'm looking for a way to scan and update the database for multiple addresses. Does that sound possible or am I stuck with this route you presented me? Thanks for your help btw I appreciate it.

You can use as many addAddress_1_() calls as you want before a rescan, and the rescan will still take the same amount of time.  Load 20 addresses into your wallet before loading the blockchain, and it will take the same time as if you loaded in 10,000.

After the blockchain is loaded, if you add any new addresses to the wallet, the chain will be re-scanned on the "scanBlockchainForTx()" call, and again, it won't matter how many addresses that is, it'll take the same amount of time.
Thanks mate! that cleared things up.
Nice work btw


Title: Re: Reading the block chain with a library?
Post by: etotheipi on April 25, 2012, 04:40:06 PM
I started the whole project because I wanted blockchain access from python.  I quickly found out that it just wasn't possible to scan the blockchain with python, at least if I wanted it to finish within an hour.  So the C++ layer does that and SWIG makes it possible to use it from python.  Unfortunately, SWIG has some quirks, such as difficulties with function overloading (hence the addAddress_1_() method instead of addAddress(), etc).  But, in general, it's been extraordinarily reliable.  Most of the C++ classes and structures are available from python as if they were native. 

That's why I recommended looking at BlockUtils.h and BlockObjRef.h, since that lists the all the functions available for most of the object types you will need.

Please let me know if there's more things you want to do with it, and I'll add more example code.    I had always wanted to setup some kind of tutorial for describing how to use armoryengine like this, but I never had a good excuse until now :)


P.S. - Pieter is right... a lot of the much simpler stuff could be done by just running bitcoind and scripting RPC calls to it.  But if you ever want to expand the functionality, I think a full library will be worth the time investment.


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 25, 2012, 08:17:19 PM
I tried the bitcoind getblockhash thing... I had no idea what information it was presenting me or how'd I utilize that information.

I ended up using Abe since it was best for my situation. Which was for me to be able to query things with PHP and MySql databases. At least myself and others have a thread to checkout later when they need some good bitcoin libraries.


Title: Re: Reading the block chain with a library?
Post by: Pieter Wuille on April 26, 2012, 12:11:24 PM

$ ./bitcoind getblockhash 100000
000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506
$ ./bitcoind getblock 000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506
{
    "hash" : "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506",
    "size" : 957,
    "height" : 100000,
    "version" : 1,
    "merkleroot" : "f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766",
    "time" : 1293623863,
    "nonce" : 274148111,
    "bits" : "1b04864c",
    "difficulty" : 14484.16236123,
    "tx" : [
        "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
        "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4",
        "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4",
        "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d"
    ],
    "previousblockhash" : "000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250",
    "nextblockhash" : "00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090"
}


First call getblochash to find the hash of a block at a particular height, then use getblock to query information about the block with that given hash. The "time" field will tell you the block's timestamp (in seconds since epoch).


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 26, 2012, 11:49:54 PM

$ ./bitcoind getblockhash 100000
000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506
$ ./bitcoind getblock 000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506
{
    "hash" : "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506",
    "size" : 957,
    "height" : 100000,
    "version" : 1,
    "merkleroot" : "f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766",
    "time" : 1293623863,
    "nonce" : 274148111,
    "bits" : "1b04864c",
    "difficulty" : 14484.16236123,
    "tx" : [
        "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
        "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4",
        "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4",
        "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d"
    ],
    "previousblockhash" : "000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250",
    "nextblockhash" : "00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090"
}


First call getblochash to find the hash of a block at a particular height, then use getblock to query information about the block with that given hash. The "time" field will tell you the block's timestamp (in seconds since epoch).
Ah that makes alot more sense. so im guessing with the array of tx data id have to query the tx info one by one to get the balances or transaction history? (maybe not transaction history but atleast an update for that parituclar block of the addresses involved)


Title: Re: Reading the block chain with a library?
Post by: Sukrim on April 27, 2012, 12:01:20 AM
I think that was rather related to my question about block timestamps... thanks by the way!


Title: Re: Reading the block chain with a library?
Post by: Xenland on April 27, 2012, 12:49:32 AM
I think that was rather related to my question about block timestamps... thanks by the way!
Still either way it helped me understand what those functions are used for and I'm still quite curious too. Apologies to any awkwardness felt from my responses.


Title: Re: Reading the block chain with a library?
Post by: Sukrim on May 08, 2012, 10:23:01 PM
This scanning is lower-level than what happens in Armory, so you are mainly using C++ objects.  You can find a full list of all the methods in BlockObjRef.h and BlockUtils.h.  Please ask questions if something isn't clear, or if you want to use some functionality not exemplified here.

I'm currently trying to write a program that connects multiple addresses to "entities". If there are 2 (or more) inputs used in a transaction, I assume that these belong to the same person. Atm all I want to do is to get a transaction and then get a list consisting of either ["Mined"] (if it was a coinbase) or the input address(es).

My code so far:
Code:
def getListOfInputAdresses(tx):
  allAddr = set()
  for nin in range(tx.getNumTxIn()):
    txin = tx.getTxInRef(nin)
    if txin.isCoinbase():
      return ["Mined"]
    else:
    if txin.getSenderAddrIfAvailable():
      parent = binary_to_hex(txin.getSenderAddrIfAvailable()) #<-- Hash160
      allAddr.add(parent)
  return list(allAddr)

getSenderAddrIfAvailable() however seems not to catch a lot of input addresses, compared to your "count all addresses" that just counts the txouts I get only a fraction. I already read the comment

Code:
   // Not all TxIns have sendor info. Might have to go to the Outpoint and get
   // the corresponding TxOut to find the sender. In the case the sender is
   // not available, return false and don't write the output

but I'm kinda lost after calling txin.getOutPoint() - how do I then get to the OutTx object again and how do I know which of it's (possibly multiple) output addresses is the input address I'm looking for?


Title: Re: Reading the block chain with a library?
Post by: etotheipi on May 09, 2012, 05:11:58 AM
I'm currently trying to write a program that connects multiple addresses to "entities". If there are 2 (or more) inputs used in a transaction, I assume that these belong to the same person. Atm all I want to do is to get a transaction and then get a list consisting of either ["Mined"] (if it was a coinbase) or the input address(es).

...

getSenderAddrIfAvailable() however seems not to catch a lot of input addresses, compared to your "count all addresses" that just counts the txouts I get only a fraction. I already read the comment

Code:
   // Not all TxIns have sendor info. Might have to go to the Outpoint and get
   // the corresponding TxOut to find the sender. In the case the sender is
   // not available, return false and don't write the output

but I'm kinda lost after calling txin.getOutPoint() - how do I then get to the OutTx object again and how do I know which of it's (possibly multiple) output addresses is the input address I'm looking for?

I wrote a special method in the BlockDataManager specifically for this situation (and I use it all the time).  It's because you can't always determine the identity behind a TxIn without actually retrieving the previous Tx that is being spent.  Thus, you need the help of the BlockDataManager, because a Tx object knows nothing about other transactions!

What you're looking for is this:

Code:
theTxIn = tx.getTxInRef(i)
senderAddr20 = TheBDM.getSenderAddr20(theTxIn)

Method "getSenderAddrIfAvailable()" does exactly what it says:  if it can deduce it from the TxIn, then it will return it.  However, coinbase TxIns do not have that info (though it could usually be deduced using some fancy math, but I never got around to implementing that).




Title: Re: Reading the block chain with a library?
Post by: Mike Hearn on May 09, 2012, 05:46:43 AM
You can also use bitcoinj for this kind of task, from either Java or C++. Take a look at the documentation here:

http://code.google.com/p/bitcoinj/


Title: Re: Reading the block chain with a library?
Post by: runeks on August 06, 2012, 05:47:34 PM
How does one just query the balance of a Bitcoin address with armory? is there any calculations involved(like looping over tx history and sum up a balance) or can I just call it and print out a simple balance?

Im' sure a lot of programmers could benefit the solution to this question to write their own software to use armory for like making their own Blockchain.info website or w/e.

The code was in my example, but I guess my example was too long.  So here's an extraction of it.

Code:
from armoryengine import *

cppWallet = Cpp.BtcWallet()
cppWallet.addAddress_1_( addrStr_to_hash160('1EbAUHsitefy3rSECh8eK2fdAWTUbpVUDN') )   # addrStr
TheBDM.registerWallet(cppWallet)
BDM_LoadBlockchainFile()
TheBDM.scanBlockchainForTx(cppWallet)

fullBalance = cppWallet.getFullBalance()

print '\n\nBalance of this wallet:', coin2str(fullBalance)


I'm getting the following error when executing the above code:

Code:
rune@rune-desktop:~/Desktop$ /home/rune/Programming/BitcoinArmory/armory-scan.py 
********************************************************************************
Loading Armory Engine:
   Armory Version:       0.81
   PyBtcAddress Version: 1.00
   PyBtcWallet  Version: 1.35
Detected Operating system: Linux
   User home-directory   : /home/rune
   Satoshi BTC directory : /home/rune/.bitcoin/
   Armory home dir       : /home/rune/.armory/
Traceback (most recent call last):
  File "/home/rune/Programming/BitcoinArmory/armory-scan.py", line 12, in <module>
    BDM_LoadBlockchainFile()
  File "/home/rune/Programming/BitcoinArmory/armoryengine.py", line 894, in BDM_LoadBlockchainFile
    return TheBDM.parseEntireBlockchain(blkdir)
  File "/home/rune/Programming/BitcoinArmory/CppBlockUtils.py", line 1115, in <lambda>
  File "/home/rune/Programming/BitcoinArmory/CppBlockUtils.py", line 50, in _swig_getattr
AttributeError: parseEntireBlockchain

Any idea what this is about? bitcoin-qt is running and accepting connections over RPC.

EDIT: Running the newest version from git. On the master branch.


Title: Re: Reading the block chain with a library?
Post by: etotheipi on August 06, 2012, 05:59:59 PM
Any idea what this is about? bitcoin-qt is running and accepting connections over RPC.

EDIT: Running the newest version from git. On the master branch.

Did you "make swig" after pulling the 0.81 code?   A bunch of stuff changed under the hood with 0.80+, including the way that blockchain scanning is done.  This error looks like you're using the latest python code, but the underlying C++ utilities are still the 0.77-compiled versions.


Title: Re: Reading the block chain with a library?
Post by: runeks on August 13, 2012, 05:31:37 PM
^ If I 'make swig' from the BitcoinArmory root directory I get an error ("make: *** No rule to make target `swig'.  Stop."). If I enter the "cppForSwig" directory and 'make', I still get the error:

Code:
rune@rune-desktop:~/Programming/BitcoinArmory/cppForSwig$ make
make BlockUtilsTest.out
make[1]: Entering directory `/home/rune/Programming/BitcoinArmory/cppForSwig'
make[1]: warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
g++  -c -O2 -pipe -fPIC  -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  UniversalTimer.cpp
g++  -c -O2 -pipe -fPIC  -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  BinaryData.cpp
g++  -c -O2 -pipe -fPIC  -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  FileDataPtr.cpp
g++  -c -O2 -pipe -fPIC  -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  BtcUtils.cpp
g++  -c -O2 -pipe -fPIC  -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  BlockObj.cpp
g++  -c -O2 -pipe -fPIC  -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  BlockUtils.cpp
g++  -c -O2 -pipe -fPIC  -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  EncryptionUtils.cpp
cd cryptopp; make libcryptopp.a; mv libcryptopp.a ..
make[2]: Entering directory `/home/rune/Programming/BitcoinArmory/cppForSwig/cryptopp'
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c 3way.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c adler32.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c algebra.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c algparam.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c arc4.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c asn.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c authenc.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c base32.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c base64.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c basecode.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c bfinit.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c blowfish.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c blumshub.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c camellia.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c cast.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c casts.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c cbcmac.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c ccm.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c channels.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c cmac.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c cpu.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c crc.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c cryptlib_bds.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c cryptlib.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c default.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c des.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c dessp.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c dh2.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c dh.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c dll.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c dsa.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c eax.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c ec2n.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c eccrypto.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c ecp.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c elgamal.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c emsa2.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c eprecomp.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c esign.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c files.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c filters.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c fips140.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c fipstest.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c gcm.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c gf2_32.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c gf256.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c gf2n.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c gfpcrypt.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c gost.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c gzip.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c hex.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c hmac.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c hrtimer.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c ida.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c idea.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c integer.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c iterhash.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c luc.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c md2.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c md4.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c md5.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c misc.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c modes.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c mqueue.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c mqv.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c nbtheory.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c network.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c oaep.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c osrng.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c pch.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c pkcspad.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c polynomi.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c pssr.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c pubkey.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c queue.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rabin.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c randpool.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rc2.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rc5.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rc6.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rdtables.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rijndael.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c ripemd.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rng.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rsa.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c rw.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c safer.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c salsa.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c seal.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c seed.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c serpent.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c shacal2.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c sha.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c sharkbox.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c shark.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c simple.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c skipjack.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c socketft.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c sosemanuk.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c square.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c squaretb.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c strciphr.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c tea.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c tftables.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c tiger.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c tigertab.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c trdlocal.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c ttmac.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c twofish.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c vmac.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c wait.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c wake.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c whrlpool.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c winpipes.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c xtr.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c xtrcrypt.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c zdeflate.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c zinflate.cpp
g++ -DNDEBUG -g -O2 -fPIC -mtune=generic -pipe -c zlib.cpp
ar -cr libcryptopp.a 3way.o adler32.o algebra.o algparam.o arc4.o asn.o authenc.o base32.o base64.o basecode.o bfinit.o blowfish.o blumshub.o camellia.o cast.o casts.o cbcmac.o ccm.o channels.o cmac.o cpu.o crc.o cryptlib_bds.o cryptlib.o default.o des.o dessp.o dh2.o dh.o dll.o dsa.o eax.o ec2n.o eccrypto.o ecp.o elgamal.o emsa2.o eprecomp.o esign.o files.o filters.o fips140.o fipstest.o gcm.o gf2_32.o gf256.o gf2n.o gfpcrypt.o gost.o gzip.o hex.o hmac.o hrtimer.o ida.o idea.o integer.o iterhash.o luc.o md2.o md4.o md5.o misc.o modes.o mqueue.o mqv.o nbtheory.o network.o oaep.o osrng.o pch.o pkcspad.o polynomi.o pssr.o pubkey.o queue.o rabin.o randpool.o rc2.o rc5.o rc6.o rdtables.o rijndael.o ripemd.o rng.o rsa.o rw.o safer.o salsa.o seal.o seed.o serpent.o shacal2.o sha.o sharkbox.o shark.o simple.o skipjack.o socketft.o sosemanuk.o square.o squaretb.o strciphr.o tea.o tftables.o tiger.o tigertab.o trdlocal.o ttmac.o twofish.o vmac.o wait.o wake.o whrlpool.o winpipes.o xtr.o xtrcrypt.o zdeflate.o zinflate.o zlib.o
ranlib libcryptopp.a
make[2]: Leaving directory `/home/rune/Programming/BitcoinArmory/cppForSwig/cryptopp'
g++  UniversalTimer.o BinaryData.o FileDataPtr.o BtcUtils.o BlockObj.o BlockUtils.o EncryptionUtils.o libcryptopp.a -o BlockUtilsTest.out -Icryptopp -DUSE_CRYPTOPP -D__STDC_LIMIT_MACROS  -lpthread  BlockUtilsTest.cpp
make[1]: Leaving directory `/home/rune/Programming/BitcoinArmory/cppForSwig'
rune@rune-desktop:~/Programming/BitcoinArmory/cppForSwig$ cd ..
rune@rune-desktop:~/Programming/BitcoinArmory$ ./armory-scan.py
********************************************************************************
Loading Armory Engine:
   Armory Version:       0.81
   PyBtcAddress Version: 1.00
   PyBtcWallet  Version: 1.35
Detected Operating system: Linux
   User home-directory   : /home/rune
   Satoshi BTC directory : /home/rune/.bitcoin/
   Armory home dir       : /home/rune/.armory/
Traceback (most recent call last):
  File "./armory-scan.py", line 12, in <module>
    BDM_LoadBlockchainFile()
  File "/home/rune/Programming/BitcoinArmory/armoryengine.py", line 894, in BDM_LoadBlockchainFile
    return TheBDM.parseEntireBlockchain(blkdir)
  File "/home/rune/Programming/BitcoinArmory/CppBlockUtils.py", line 1115, in <lambda>
  File "/home/rune/Programming/BitcoinArmory/CppBlockUtils.py", line 50, in _swig_getattr
AttributeError: parseEntireBlockchain


Title: Re: Reading the block chain with a library?
Post by: etotheipi on August 13, 2012, 05:43:26 PM
^ If I 'make swig' from the BitcoinArmory root directory I get an error ("make: *** No rule to make target `swig'.  Stop."). If I enter the "cppForSwig" directory and 'make', I still get the error:


Make in the root directory should call "make swig" in the cppForSwig directory.  Or you can go into the cppForSwig directory and "make swig".  In both cases, it will recompile all the stuff and re-run swig to provide armoryengine.py access to the latest C++ utilities.  The error you are reporting is indicative of a version mismatch between the C++ and python code.

I recommend doing a fresh checkout, then go into the cppForSwig directory and type "make swig" (to be sure), and then try running it again.  If that doesn't work, then... ?


Title: Re: Reading the block chain with a library?
Post by: runeks on August 13, 2012, 08:00:36 PM
Did it all from scratch, works perfectly now :). Thanks!


Title: Re: Reading the block chain with a library?
Post by: kayrice on August 14, 2012, 01:21:44 AM
For what it's worth I wrote some code in PHP that read the entire block-chain as downloaded from a "daily source" in tgz format early in development. I needed the block-chain and stats from it but didn't want to wrestle with APIs or wait for bitcoind to finish. Downloading the tgz is fast and pretty recent.

The binary format of blk0001.dat is well documented and you can parse the entire blockchain and validate it easily. My quad-core system under a highly limited VM can parse the entire block chain and validate the double SHA-256 hashes within about 10 seconds.


Title: Re: Reading the block chain with a library?
Post by: Xenland on August 14, 2012, 02:33:44 AM
For what it's worth I wrote some code in PHP that read the entire block-chain as downloaded from a "daily source" in tgz format early in development. I needed the block-chain and stats from it but didn't want to wrestle with APIs or wait for bitcoind to finish. Downloading the tgz is fast and pretty recent.

The binary format of blk0001.dat is well documented and you can parse the entire blockchain and validate it easily. My quad-core system under a highly limited VM can parse the entire block chain and validate the double SHA-256 hashes within about 10 seconds.

Hmm I haven't seen this "well documented" information quite yet would you mind posting a link so I can dive in?


Title: Re: Reading the block chain with a library?
Post by: etotheipi on August 14, 2012, 02:53:41 AM
For what it's worth I wrote some code in PHP that read the entire block-chain as downloaded from a "daily source" in tgz format early in development. I needed the block-chain and stats from it but didn't want to wrestle with APIs or wait for bitcoind to finish. Downloading the tgz is fast and pretty recent.

The binary format of blk0001.dat is well documented and you can parse the entire blockchain and validate it easily. My quad-core system under a highly limited VM can parse the entire block chain and validate the double SHA-256 hashes within about 10 seconds.

Hmm I haven't seen this "well documented" information quite yet would you mind posting a link so I can dive in?

The format is stupid simple:

Magic Bytes (4)
Num Block Bytes (4)
Header (80)
Num Tx in Block (VAR_INT)
Tx1
Tx2
...
TxN
<Repeat>



Title: Re: Reading the block chain with a library?
Post by: runeks on October 02, 2012, 10:14:36 PM
How does one just query the balance of a Bitcoin address with armory? is there any calculations involved(like looping over tx history and sum up a balance) or can I just call it and print out a simple balance?

Im' sure a lot of programmers could benefit the solution to this question to write their own software to use armory for like making their own Blockchain.info website or w/e.

The code was in my example, but I guess my example was too long.  So here's an extraction of it.

Code:
from armoryengine import *

cppWallet = Cpp.BtcWallet()
cppWallet.addAddress_1_( addrStr_to_hash160('1EbAUHsitefy3rSECh8eK2fdAWTUbpVUDN') )   # addrStr
TheBDM.registerWallet(cppWallet)
BDM_LoadBlockchainFile()
TheBDM.scanBlockchainForTx(cppWallet)

fullBalance = cppWallet.getFullBalance()

print '\n\nBalance of this wallet:', coin2str(fullBalance)
etotheipi, say I want to avoid loading the blockchain file every time I want to use a Python program to find the balance of a bunch of addresses, what would be the appropriate way of doing this?

I have some code where I want to be able to query balances as quickly as possible, and would prefer not to wait while loading the now 3 GB block chain.

This way doesn't work for me. The first scan works fine, but the second scan hangs at TheBDM.scanBlockchainForTx(cppWallet).

Code:
from armoryengine import *

def init()
   BDM_LoadBlockchainFile()

def get_balance(addresses)
   cppWallet = Cpp.BtcWallet()

   for address in addresses:
      cppWallet.addAddress_1_( address )

   TheBDM.registerWallet(cppWallet)
   TheBDM.scanBlockchainForTx(cppWallet)
   fullBalance = cppWallet.getFullBalance()
   TheBDM.unregisterWallet(cppWallet)

   return fullBalance
Here's the gdb output from attaching to the python process.
Code:
(gdb) where
#0  0x00007f1728fc7980 in free@plt () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007f1729426f4a in BtcWallet::scanTx(Tx&, unsigned int, unsigned int, unsigned int) () from /home/rune/Programming/BitcoinArmory/_CppBlockUtils.so
#2  0x00007f172942ad3a in BlockDataManager_FileRefs::scanRegisteredTxForWallet(BtcWallet&, unsigned int, unsigned int) () from /home/rune/Programming/BitcoinArmory/_CppBlockUtils.so
#3  0x00007f172942b09d in BlockDataManager_FileRefs::scanBlockchainForTx(BtcWallet&, unsigned int, unsigned int) () from /home/rune/Programming/BitcoinArmory/_CppBlockUtils.so
#4  0x00007f172950cc12 in _wrap_BlockDataManager_FileRefs_scanBlockchainForTx () from /home/rune/Programming/BitcoinArmory/_CppBlockUtils.so
#5  0x000000000049c4d8 in PyEval_EvalFrameEx ()
#6  0x000000000049f1c0 in PyEval_EvalCodeEx ()
#7  0x00000000004983b8 in PyEval_EvalFrameEx ()
#8  0x000000000049f1c0 in PyEval_EvalCodeEx ()
#9  0x00000000004a9081 in PyRun_FileExFlags ()
#10 0x00000000004a9311 in PyRun_SimpleFileExFlags ()
#11 0x00000000004aa8bd in Py_Main ()
#12 0x00007f172a7a476d in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#13 0x000000000041b9b1 in _start ()

So I guess I'd like to know:

  • What's the right way to make the library load the blockchain in advance, so it's always ready to process new addresses?
  • How do I properly make "TheBDM" forget the old addresses that were loaded via registerWallet? I'm just guessing unregisterWallet will do it, but I don't know. Is resetRegisteredWallets necessary as well?


Title: Re: Reading the block chain with a library?
Post by: runeks on October 07, 2012, 08:55:12 PM
Never mind. I was structuring my code wrong so the previous cppWallet wasn't deleted from memory before starting with the next (I think that's what was wrong). This works:

Code:
from armoryengine import *


BDM_LoadBlockchainFile()
   
def get_balance(addresses):
   cppWallet = Cpp.BtcWallet()

   for address in addresses:
      cppWallet.addAddress_1_( address )

   TheBDM.registerWallet(cppWallet)
   TheBDM.scanBlockchainForTx(cppWallet)
   fullBalance = cppWallet.getFullBalance()
   balance = coin2str(fullBalance)
   TheBDM.unregisterWallet(cppWallet)

   return balance