Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: jl777 on March 15, 2016, 12:13:36 PM



Title: address balances at specific block?
Post by: jl777 on March 15, 2016, 12:13:36 PM
Does anybody know how to query balances for all addresses as of a specific block?

I would think some sort of query layer on top of insight block explorer?

My use case is that I want to do a brute force verification of all addresses that I have generated with the iguana parallel sync. Now that I have this data, I cant find any easy way to verify it...

James


Title: Re: address balances at specific block?
Post by: danda on March 15, 2016, 08:54:44 PM
I'm not aware of any way to do this ( balance of an address at specific time/block in the past ).  It's an interesting question.

"balance of an address" is a tricky thing (http://bitcoin.stackexchange.com/a/10101/31594) to begin with.  So at best I think you can achieve "best guess balance" anyway, unless you have the private keys for all addresses in question.

Block explorers such as toshi and insight store do store balance information, but I'm not sure if they do it per address per block or only per address (all time best chain). 





Title: Re: address balances at specific block?
Post by: watashi-kokoto on March 15, 2016, 09:35:38 PM
just look up all transactions (debit credit) affecting certain address,


 and filter out tx with less than n confirmations,

then sum debit credit of the rest

ideal way to do this would be sql.

most addresses have 1 tx, so the balance before top-up is 0BTC and after the value of the utxoBTC

so i would start the test with the non-reused addresses


Title: Re: address balances at specific block?
Post by: jl777 on March 16, 2016, 01:33:27 AM
just look up all transactions (debit credit) affecting certain address,


 and filter out tx with less than n confirmations,

then sum debit credit of the rest

ideal way to do this would be sql.

most addresses have 1 tx, so the balance before top-up is 0BTC and after the value of the utxoBTC

so i would start the test with the non-reused addresses
I figured out how to get iguana to generate the balance for ANY address as of ANY block, with about 2 milliseconds compute time using a 1.3Ghz i5 CPU. With faster CPU and 8 cores, this would be able to be sped up to 100 microseconds. As I have written extensively, I dont use any DB, I use readonly parallel datasets.

Also generating a full dataset would be much faster as I can batch process all addresses in the same pass.

My issue is how do I verify that I have the right data? So, assume I have a file with (address, balance) as of the start of block 400,000, how can I verify that it is 100% correct


Title: Re: address balances at specific block?
Post by: funkenstein on March 16, 2016, 02:22:25 AM
Walk the chain from block 0 and build your own database. 


Title: Re: address balances at specific block?
Post by: jl777 on March 16, 2016, 02:55:05 AM
Walk the chain from block 0 and build your own database. 
I already did that.
How do I verify independently from a known accurate source

James

P.S. my "database" is not actually a database, but for this purpose you can consider it a database


Title: Re: address balances at specific block?
Post by: danda on March 16, 2016, 02:55:40 AM
100% correct?    I'm not sure you can be.

What if an output can be unlocked by, say, 1 of a set of 3 keys? Do you credit the balances of all three keys, or none?

more on this here:
http://bitcoin.stackexchange.com/a/37186/31594


Title: Re: address balances at specific block?
Post by: jl777 on March 16, 2016, 03:03:50 AM
100% correct?    I'm not sure you can be.

What if an output can be unlocked by, say, 1 of a set of 3 keys? Do you credit the balances of all three keys, or none?

more on this here:
http://bitcoin.stackexchange.com/a/37186/31594
I update balances based on the multisig (or p2sh ) address in that case. So the address that is credited is the multisig address and not the pubkeys that compose the multisig

this is a practicaly way of doing things and is what blockchain.info does

I think what is needed is a way to select out data from insight database, but I never used insight and not sure how best to do that


Title: Re: address balances at specific block?
Post by: bitcreditscc on March 16, 2016, 01:58:30 PM
There is nothing better than building your own database, i also built my own but adding some code to core, updated every new block.


Title: Re: address balances at specific block?
Post by: jl777 on March 16, 2016, 05:17:24 PM
There is nothing better than building your own database, i also built my own but adding some code to core, updated every new block.
I would feel much better for independent validation. Otherwise it is me verifying my own data with another dataset that I generate.

It seems that such would require that others trust that I didnt make any errors.

could you generate a dataset for balances before block 400,000, ie 0 to 399,999 processed? I can pay a reasonable bounty for it. How do you deal with multisig address balances and non-standard outputs?

James


Title: Re: address balances at specific block?
Post by: gmaxwell on March 16, 2016, 10:56:19 PM
Since nothing in the Bitcoin consensus algorithm works with balances, using them for comparison would be potentially unwise-- it's perfectly possible to have an incorrect 'balance' that is actually a latently corrupted state.  Bitcoin Core gettxoutsetinfo will give you a hash of the serialized utxo set for this kind of diagnostic purpose, though there is no text specification for the particular serialization it uses, you'd have to extract that from the implementation.


Title: Re: address balances at specific block?
Post by: jl777 on March 16, 2016, 11:13:16 PM
Since nothing in the Bitcoin consensus algorithm works with balances, using them for comparison would be potentially unwise-- it's perfectly possible to have an incorrect 'balance' that is actually a latently corrupted state.  Bitcoin Core gettxoutsetinfo will give you a hash of the serialized utxo set for this kind of diagnostic purpose, though there is no text specification for the particular serialization it uses, you'd have to extract that from the implementation.
when does the (sum of vouts) - (sum of vins) for an address not match what it should be?

For things that use scripts with multisig, if statements, CLTV, etc. I just treat the p2sh hash as the "address" and not deal with the probabilistic value allocations to the potential redeemers. I think even for the strange anyone can spend outputs, it is either spent or not spent, so the difference of sums would work

I have commissioned an independently tabulated balances that I will cross check against. The plan would be to analyze any that are not matching, which will hopefully be not many. the gettxoutsetinfo should be quite useful for that, i forgot about that one. thanks

After all, block explorers are displaying a balance (potentially inaccurate it might be). I always assume my data is wrong, until I get it verified. Granted if the independently generated balances match iguana's output, it is possible for something to slip through, but at least for an initial test it seems like a good place to start and since the independent balances will be generated from bitcoin core, I know it is 100% correct

James


Title: Re: address balances at specific block?
Post by: esotericizm on March 16, 2016, 11:41:50 PM
I havent reviewed the latest insight-api code but it stands to reason you could run a local copy changing the code that grabs the current blockindex and setting that to the blockhash of your chosen height.