<getbalance> double counts account balance #4572

issue taichitai opened this issue on July 22, 2014
  1. taichitai commented at 2:55 PM on July 22, 2014: none

    Function <getbalance> return previously spent satoshis (i.e. double counting)

    ****NOTE: Issue only occurs when accounts are specified (i.e. <getbalance 0 1>) but no error occurs when total balance is requested (i.e. <getbalance * 1>)


    To reproduce:

    1. Send satoshis to address on account A (e.g. 0.01 BTC)
    2. Spend satoshis (e.g. send 0.01 BTC to address on account B)
    3. Send new satoshis to address on account A (e.g. 0.02 BTC)
    4. Call <getbalance> (it return 0.03BTC)

    Example: On 22 JUL 2014, I make a transfer of all balance on <testnet> from address: mia8ujFiaor9CcL5tLJM7NSQBmHmbFU2Vc (account 1) to address: muTXdCf4tvsBcM2kvmmbPDiZjcFbNTCAB5 (other account) TxId: 81c2591e83917d5dc0f19258de7c96a8b0b222767f2383b49fc52086730a298f

    <getbalance> reports 1.5165 BTC on account 1 despite it having 0 BTC

    Afterwhich, I transfer back the full amount from address: muTXdCf4tvsBcM2kvmmbPDiZjcFbNTCAB5 (other accout) to address: n4fq4EvrKy4rPazYfvKfP8gijZsRwh9FRo (account 1) TxId: bca4be28260b98870980f42c5d708476462d82ebf3a119d87401a747560a1439

    <getbalance> reports 2.1045 BTC on account 1 despite it having 0.588 BTC


    Bitcoind version: v0.9.2.1-g354c0f3-beta (64-bit) All calls made via modified json-rpc API and tested in console, giving the same results.


  2. cozz commented at 3:51 PM on July 22, 2014: contributor

    There is just a misunderstanding of how accounts are supposed to work: You associate an address with an account, but this is only for Receiving transactions. Every transaction paying to that address, will be credited to the account. But sending from that address, does Not debit the account. To debit an account, there are 3 rpc-calls:

    • move
    • sendfrom
    • sendmany

    Note, that using sendfrom and sendmany can choose coins from Any address in your wallet, its just, that the value being sent will be deducted from the account balance. So account-balances do not necessarily correspond with the underlying coins in the blockchain.

    See: https://en.bitcoin.it/wiki/Accounts_explained

  3. laanwj commented at 4:22 PM on July 22, 2014: member

    The account system is just a ledger. When an incoming transaction is detected, the coins are credited to the account associated with the receiving address. When you send coins using 'sendfrom', coins are subtracted from the provided account.

    This ledger does not correspond in any way to the block chain. Coins can be arbitrarily moved between accounts with the 'move' command, which is purely an internal operation.

    This means that after initially receiving coins there is no correspondence between the accounts and how many coins are 'on' the addresses.

    The ledger does not influence coin selection when you send coins. No matter whether you use sendfrom, sendmany or other send functions, the coin selection chooses from all coins (unless you use 'coin control' in the GUI).

    Many people get confused by the account system and it is probably the worst code in all of Bitcoin Core. For this reason and others it is planned to be removed in a future version, see #3816.

  4. sipa commented at 4:26 PM on July 22, 2014: member

    Working as intended.

  5. sipa closed this on Jul 22, 2014

  6. taichitai commented at 4:44 PM on July 22, 2014: none

    Hi, thanks for the explanation. Isn't it strange that the getbalance does not get the actual underlying balance of the account? Isn't that a bug in itself? I do not see any purpose of getting a balance that does not reflect the actual underlying balance of the account.

    Also, there is no way to clear the wrong balance and make the account accurate even by dumping the inaccurate and useless balance into another account using the "sendfrom" function which will return a "Request error: Insufficient funds" error.

    The only way is to give up on the account and leave it as it is, isn't that a bug?

  7. sipa commented at 4:45 PM on July 22, 2014: member

    getbalance "account" does get the balance of the account. It just does not have anything to do with anything in the block chain - it's a local bean counter only.

    What must always be true is that the sum of all account balances is equal to the balance of the entire wallet (which is the value of the actual unspent coins).

  8. sipa commented at 4:46 PM on July 22, 2014: member

    To correct balances, use 'move', not 'sendfrom'. Sendfrom does an actual on-blockchain transaction that moves coins (deducting the used funds from the specified account). Move just does an internal move by subtracting the balance from one account and adding it to another.

  9. taichitai commented at 4:46 PM on July 22, 2014: none

    Hi, my wallet's current balance is 0.5879 BTC, but the account balance of account 1 is 1.6044 BTC and account 2 is 0.5 BTC.

  10. sipa commented at 4:47 PM on July 22, 2014: member

    And the balance of the default account ("") will be negative, as that is where all your sends deducted their funds from.

  11. taichitai commented at 4:49 PM on July 22, 2014: none

    Oh, thanks alot for the explanation and clearing up. Apologies for any rude comments. I would suggest that it would be great that the account balance actually reflects the actual amount of bitcoin to avoid any confusions, don't really see the point of it existing this way.

  12. sipa commented at 4:51 PM on July 22, 2014: member

    The confusion is thinking that accounts have anything to do with the blockchain. This is why we want to get rid of the feature. Its behaviour is however well-defined, and people rely on it, so it will not be "fixed" to mean something completely else.

  13. taichitai commented at 4:55 PM on July 22, 2014: none

    I would say that the generating new address for the account after every payment is a good feature especially when I have to track different account from different persons on the same wallet and don't have to worry anything about the different addresses in the account.

  14. luke-jr commented at 6:01 PM on July 22, 2014: member

    Addresses aren't "in" accounts. There is a one-directional association from addresses to accounts. That means when bitcoins are received with the address, the account in question gets the funds added to it. That's the end of the connection. Accounts don't track/hold individual coins, just a number.

  15. Kinnardian cross-referenced this on Jul 14, 2015 from issue Deprecation of Accounts by Kinnardian
  16. bitcoin deleted a comment on Jun 9, 2017
  17. bitcoin locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-20 06:55 UTC