Skip to content

Instantly share code, notes, and snippets.

@metacoin
Created May 2, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metacoin/13acf0c0bbc62aa3b2108259ff9e2970 to your computer and use it in GitHub Desktop.
Save metacoin/13acf0c0bbc62aa3b2108259ff9e2970 to your computer and use it in GitHub Desktop.
FLO_getbalance_notes.md

GetBalance RPC command research notes

Main function defined in wallet.cpp

GetBalance() is defined in wallet.cpp:

CAmount CWallet::GetBalance() const 
{
    CAmount nTotal = 0;
    {
        LOCK2(cs_main, cs_wallet);
        for (const auto& entry : mapWallet)
        {
            const CWalletTx* pcoin = &entry.second;
            if (pcoin->IsTrusted())
                nTotal += pcoin->GetAvailableCredit();
        }
    }

    return nTotal;
}

Overview of GetBalance() function

Steps taken in this method:

  1. Begin function setting up temporary variables
  • Set nTotal to 0 first, we will later use nTotal to sum up coins
  • Start a new code block
  • Lock cs_main and cs_wallet
  1. Iterate through mapWallet: for (const auto& entry : mapWallet)
  • auto& automatically converts entry to whatever type the members of mapWallet are (?)
  • mapWallet is defined in wallet.h as a mapping of uint256 to CWalletTx. Therefore, entry is a single pair of the mapping <uint256, CWalletTx>
  • More detailed information on mapWallet below
  1. For each <uint256, CWalletTx> pair in mapWallet do the following:
  • Create a pointer to the CWalletTx in that pair: const CWalletTx* pcoin = &entry.second;
  • if (pcoin->IsTrusted() then add pcoin->GetAvailableCredit() to nTotal
@defgroup mapWallet
 *
 * @{
 */

(?)

mapWallet defined in wallet.h:

    std::map<uint256, CWalletTx> mapWallet;

it's a mapping of uint256 (the transaction hash) to CWalletTx (the class used to store Wallet Transactions)

from the definition of class CWalletTx:

 * A transaction with a bunch of additional info that only the owner cares about.
 * It includes any unrecorded transactions needed to link it back to the block chain.

CWalletTx has various balances each with a cache and a mutable amount:

  • Debit
  • Credit
  • Immature
  • Available
  • Change
  • Watch-only (not relevant to our examination)

There are also various

GetAvailableCredit in wallet.cpp

GetAvailableCredit() caches the available credit when it's called. It's called from these functions:

  • GetBalance() which is called by:
    • GetCredit(const isminefilter& filter) which is called by:
      • UniValue gettransaction() from rpcwallet.cpp which is called by the RPC only

      • decomeposeTransaction) in transactionrecord.cpp which is called by:

        • refreshWallet() which is a member function of class TransactionTablePriv called by:
          • TransactionTableModel constructor which is called:
          • Any time a TransactionTablePriv is constructed
        • updateWallet() which is a member function of class TransactionTablePriv
          • TransactionTableModel::updateTransaction which is called by:

      • GetAvailableCredit() (recursive?) (?)

      • GetImmatureWatchOnlyCredit() which is called by:

      • GetAvailableWatchOnlyCredit() which is called by:

      • GetImmatureCredit() which is called by:

  • GetUnconfirmedBalance() which is called by:

Other Notes and Ideas

COINBASE_MATURITY

Observations:

  1. In consensus.h, COINBASE_MATURITY is set to 100 for bitcoin
  2. In FLO it is currently 100 as well

Solution:

  • For FLO it should be 1500
  • Optional: make it 1040 for simplicity in the next hard fork, maybe?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment