Bitcoin Forum
May 22, 2024, 08:08:45 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Theoretical vs Practical Extant Coin Supply Calculations  (Read 1588 times)
CoinHeavy (OP)
Full Member
***
Offline Offline

Activity: 221
Merit: 100


View Profile
May 22, 2014, 02:13:31 AM
 #1

Can extant (existing/available) coin supply only be calculated in theory according to the minting schedule or can it be determined programmatically by crawling the blockchain?

Blackcoin has a 'moneysupply' value that returns when `blackcoind getinfo` is called.  To the best of my knowledge, it is the only coin daemon that returns this figure.

The only references in code to money supply I can find appear on line 93 of src/rpcwallet.cpp at github.com/rat4/blackcoin

Code:
obj.push_back(Pair("moneysupply", ValueFromAmount(pindexBest->nMoneySupply)));

How is moneysupply being calculated from the blockchain? Is the same calculation possible with the bitcoin blockchain? If so, how do the calculations work? look like?

I have been told by bitcoin core devs that it is not possible to distinguish between miners fees and newly minted coins, making a practical calculation of extant coins (via blockchain crawl) impossible and forcing reliance on a theoretical calculation from block height, initial reward, and halving period.

I wrote the ruby solution below but it is theoretical and not derived directly from blockchain values.
If there is a way to crawl the blockchain for the 'real' amount, I would greatly appreciate help with learning how to do so. Thanks in advance.

Code:
# bitcoin example, halving schedule
initial_reward = 50
halving_period = 210000
 
# given a block height, calculate number of bitcoins minted thus far
blocks = `bitcoind getblockcount`
 
# number of eras -- rounded up to nearest int
era_count = (blocks / halving_period).ceil
total_coins = 0
 
# loop over each era
(1..era_count).each do |era|
 
  if era == era_count
  era_blocks = blocks % halving_period
  else
  era_blocks = halving_period
  end
 
  # reward halves each new era
  total_coins = total_coins + (initial_reward * 0.5^(era - 1) * era_blocks)
 
end
 
puts total_coins
CoinHeavy (OP)
Full Member
***
Offline Offline

Activity: 221
Merit: 100


View Profile
May 22, 2014, 02:16:38 AM
 #2

Additionally, how might OP_RETURNs be checked for unspendable amounts so that these could be subtracted from the extant coin supply?

While it is presumably infeasible to scan the entire bitcoin address space for burn addresses via an entropy threshold or via regex rules (many repeating characters), aren't there amounts that we can tell for certain are unspendable just by inspecting the blockchain?
jonald_fyookball
Legendary
*
Offline Offline

Activity: 1302
Merit: 1004


Core dev leaves me neg feedback #abuse #political


View Profile
May 22, 2014, 04:21:33 AM
 #3

Isn't this problem trivial? Just go to Blockchain.info and look at the latest block number.  
You can find  the number of blocks that mint 50 coins and the number that minted 25.

Am I missing something?

CoinHeavy (OP)
Full Member
***
Offline Offline

Activity: 221
Merit: 100


View Profile
May 22, 2014, 11:17:49 AM
 #4

Isn't this problem trivial? Just go to Blockchain.info and look at the latest block number. 
You can find  the number of blocks that mint 50 coins and the number that minted 25.

Am I missing something?

While that might be the shortest path to the figure in question, it doesn't address how the figure is calculated or the difference between theoretical and practical calculations.

When I use the term blockchain I mean the actual blockchain, not blockchain.info.  Please re-read the question.
jonald_fyookball
Legendary
*
Offline Offline

Activity: 1302
Merit: 1004


Core dev leaves me neg feedback #abuse #political


View Profile
May 22, 2014, 12:30:17 PM
 #5

Isn't this problem trivial? Just go to Blockchain.info and look at the latest block number. 
You can find  the number of blocks that mint 50 coins and the number that minted 25.

Am I missing something?

While that might be the shortest path to the figure in question, it doesn't address how the figure is calculated or the difference between theoretical and practical calculations.

When I use the term blockchain I mean the actual blockchain, not blockchain.info.  Please re-read the question.

Same thing though.  If you are a node, you HAVE a copy of the blockchain.  You just count the blocks essentially.

CoinHeavy (OP)
Full Member
***
Offline Offline

Activity: 221
Merit: 100


View Profile
May 22, 2014, 01:11:01 PM
 #6

Isn't this problem trivial? Just go to Blockchain.info and look at the latest block number. 
You can find  the number of blocks that mint 50 coins and the number that minted 25.

Am I missing something?

While that might be the shortest path to the figure in question, it doesn't address how the figure is calculated or the difference between theoretical and practical calculations.

When I use the term blockchain I mean the actual blockchain, not blockchain.info.  Please re-read the question.

Same thing though.  If you are a node, you HAVE a copy of the blockchain.  You just count the blocks essentially.

Sorry Jonald, perhaps I didn't explain it properly in the post but I'm not looking for block height to do a calculation that lives entirely outside of the blockchain; I'm looking to scan the blockchain to actually count the number of extant coins to get a number that is closer to reality rather than a theoretical figure.  Some of the implications extend to other cryptocurrencies with more complicated minting schedules and formulas.

Anyway, it looks like this calculation is being done via:

Code:
CBlock::ConnectBlock

Starting at line 1507 here: https://github.com/rat4/blackcoin/blob/850c9f683f925d12225e634fbb9e4f757daf8a35/src/main.cpp#L1507

This is a great feature.  I'm guessing that eventually it will be implemented more widely, given its usefulness. Kudos to rat4.
jonald_fyookball
Legendary
*
Offline Offline

Activity: 1302
Merit: 1004


Core dev leaves me neg feedback #abuse #political


View Profile
May 22, 2014, 03:21:28 PM
 #7

The coinbase rewards aren't theoretical.  You CAN use a simple multiplication here.

Otherwise, I'm afraid I either don't know what you mean, or I don't have enough knowledge...sorry.

DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
May 22, 2014, 03:31:54 PM
Last edit: May 22, 2014, 04:15:32 PM by DeathAndTaxes
 #8

The coinbase rewards aren't theoretical.  You CAN use a simple multiplication here.

Otherwise, I'm afraid I either don't know what you mean, or I don't have enough knowledge...sorry.

Actually you can't due to a small oversight in the protocol (or at least what I consider an oversight).

A block is valid if the coinbase reward is not more than the current subsidy plus transaction fees.  A block is valid if it is less than the max allowed reward and when that happens coins are lost and the supply is slightly less than the theoretical limit.  Miners have in the past (probably due to errors) have created coinbase transactions which payout less than the max allowed.  The two most common scenarios are a block which has a coinbase reward of zero and a block which has a coinbase reward equal to the subsidy when there are tx fees in the block (i.e. 25.0 BTC vs 25.00128 BTC).  Doing a computation based on block height will slightly overestimate the number of available coins.

To answer the OP the only way to get an exact count would involve parsing every block. Start at the genesis block and set a coin counter at zero.  For each block compute the amount of the transaction fees in the block.  Subtract the fees from the coinbase amount and the difference is the number of newly mined coins.  Add this to a coin counter and move to the next block.  Repeat until you reach the current block.  To be robust you may want to record the supply at all or some block heights.  In the event of a reorg you would need to return to that last known height prior to the reorg and recompute (differences due to reduced rewards are less likely now but not impossible).

You will also need to decide how to handle the genesis block.  There is a 50 BTC reward but the protocol currently doesn't allow them to be spent.  It is unlikely that will ever change. In my opinion that means it isn't part of the coin supply.  To improve your reported supply further you could subtract out all known provably lost coins and maybe ones which can be said to be unspenable with a high degree of confidence (i.e. coins sent to an address where the pubkeyhash is 1111111111111111111111111111111111111111111111111111 as the odds that by chance or design someone generated a pubkey which hashes to all zeroes is essentially zero.

So there are at least 4 potential supplies
1) theoretical supply (based on block height and subsidy calculations)
2) generated supply (based on blockchain and actual minting rewards)
3) provable maximal supply (based on #2 minus all provable lost coins after minting)
4) probable maximal supply (based on #3 minus all coins which have a high confidence of being lost)

Obviously each supply is slightly smaller than the one before it.  The difference is pretty small though (and very likely to diminish as % over time).   The last time I looked the difference between #1 and #4 was less than 0.5%.
jonald_fyookball
Legendary
*
Offline Offline

Activity: 1302
Merit: 1004


Core dev leaves me neg feedback #abuse #political


View Profile
May 22, 2014, 04:00:45 PM
 #9

Excellent.  I learn something new everyday here.

ShakyhandsBTCer
Sr. Member
****
Offline Offline

Activity: 448
Merit: 250


It's Money 2.0| It’s gold for nerds | It's Bitcoin


View Profile
June 14, 2014, 06:38:59 PM
 #10

Isn't this problem trivial? Just go to Blockchain.info and look at the latest block number. 
You can find  the number of blocks that mint 50 coins and the number that minted 25.

Am I missing something?

While that might be the shortest path to the figure in question, it doesn't address how the figure is calculated or the difference between theoretical and practical calculations.

When I use the term blockchain I mean the actual blockchain, not blockchain.info.  Please re-read the question.

blockchain.info and the blockchain really are one and the same.

Blockchain.info has all of the blocks mined on it's website for anyone to view.

The difference is that blockchain may display a block that gets orphaned prior to it being orphaned.
Peter R
Legendary
*
Offline Offline

Activity: 1162
Merit: 1007



View Profile
June 14, 2014, 06:54:25 PM
 #11

Can extant (existing/available) coin supply only be calculated in theory according to the minting schedule or can it be determined programmatically by crawling the blockchain?

Both.

DeathAndTaxes was recently working on an UXTO parser to categorize unspent coins into "bins."  You could use a version of this tool to empirical calculate coin supply from the unspent outputs.  The empirical coin supply should be equal to or less than the "theoretical" coin supply set by bitcoin's inflation schedule (because it is possible for a miner to make coins vanish).  

Here's some of the initial data (I'm not sure what block number this was based on):

Distribution of unspent outputs (haven't combined entries to the same identity).  RawScript includes "native" multisig.  It hasn't been broken out yet.

Code:
Type                 NumOutputs    OutputValue  AvgScriptLen
------------------------------------------------------------
PubKeyHash           11,236,706    11,018,046            20
PubKey                   41,665     1,844,048            33
ScriptHash               19,222        17,719            20
RawScript                45,160         2,627           121

UXTO Records:    3,297,375
Unique Outputs: 11,342,753
Output Value:   12,882,440 BTC

As expected the PubKeyHash, PubKey, and ScriptHash outputs combined make up 99.98% of the outstanding coins.



You could also modify the parser to remove "proveably unspendable" coins from your money-supply calculations.  I think all provably unspendable outputs would fall into the "RawScript" category, which means there's less than 2,627 BTC of provably destroyed coins (and probably a lot less than this because I expect most of the RawScript outputs to be native multisig).  

Run Bitcoin Unlimited (www.bitcoinunlimited.info)
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
June 14, 2014, 11:29:40 PM
Last edit: June 14, 2014, 11:41:43 PM by DeathAndTaxes
 #12

Good idea.  In hindsight obviously the UXTO must always be equal to current "money supply" (if it isn't we have problems Smiley ).

The snapshot was as of block height 305,303.
https://blockchain.info/block-index/438269/00000000000000001e16587f86dba9eca1d64b24b33c29a8c663e0b7de63e661

In the quoted segment I actually rounded the output value.  It is 12,882,439.79102850 (unless I did something wrong, which is possible).  The bitcoin-core also has the "gettxoutsetinfo" RPC call to get stats on the uxto and it has the total value so that is an easier way.  

IIRC OP_RETURN outputs are already pruned from the UXTO and thus any coins burned that way are already removed from the stat.


The following outputs can't be redeemed and may still be in the UXTO (simply because the client doesn't look for provably bad outputs to prune:
986 outputs with 32 bytes and no OP codes.
336 outputs with 36 bytes and no OP codes.
182 outputs with the gibberish script OP_IFDUP OP_IF OP_2SWAP OP_VERIFY OP_2OVER OP_DEPTH (the ASCII values for "script" was pushed rather than the actual script)
23 outputs with a single one byte zero as the PubKeyHash (oops) OP_DUP OP_HASH160 OP_0 OP_EQUALVERIFY OP_CHECKSIG

justusranvier
Legendary
*
Offline Offline

Activity: 1400
Merit: 1009



View Profile
June 15, 2014, 12:02:12 AM
 #13

The following outputs can't be redeemed and may still be in the UXTO (simply because the client doesn't look for provably bad outputs to prune:
986 outputs with 32 bytes and no OP codes.
336 outputs with 36 bytes and no OP codes.
182 outputs with the gibberish script OP_IFDUP OP_IF OP_2SWAP OP_VERIFY OP_2OVER OP_DEPTH (the ASCII values for "script" was pushed rather than the actual script)
23 outputs with a single one byte zero as the PubKeyHash (oops) OP_DUP OP_HASH160 OP_0 OP_EQUALVERIFY OP_CHECKSIG
Is there an automatic way to flag pubkey hashes which appear "fake", as in we're very confident that nobody possesses a corresponding private key? (1BitcoinEaterAddressDontSendf59kuE)

Maybe some kind of entropy measurement?
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
June 15, 2014, 12:07:52 AM
 #14

Maybe. Entropy is probably the way to go.  I would put them in category 4 (post #8) as they are only probably lost.  Maybe one could make a entropy parser to flag potential keys to add to that list (semi-automated).  Humans are sometimes better at that kind of stuff.  If nothing else compiling such a list would be useful as test cases from an automated tool.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!