| 1937 | } |
| 1938 | |
| 1939 | CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter) const |
| 1940 | { |
| 1941 | if (pwallet == nullptr) |
| 1942 | return 0; |
| 1943 | |
| 1944 | // Must wait until coinbase is safely deep enough in the chain before valuing it |
| 1945 | if (IsCoinBase() && GetBlocksToMaturity() > 0) |
| 1946 | return 0; |
| 1947 | |
| 1948 | CAmount* cache = nullptr; |
| 1949 | bool* cache_used = nullptr; |
| 1950 | |
| 1951 | if (filter == ISMINE_SPENDABLE) { |
| 1952 | cache = &nAvailableCreditCached; |
| 1953 | cache_used = &fAvailableCreditCached; |
| 1954 | } else if (filter == ISMINE_WATCH_ONLY) { |
| 1955 | cache = &nAvailableWatchCreditCached; |
| 1956 | cache_used = &fAvailableWatchCreditCached; |
| 1957 | } |
| 1958 | |
| 1959 | if (fUseCache && cache_used && *cache_used) { |
| 1960 | return *cache; |
| 1961 | } |
| 1962 | |
| 1963 | CAmount nCredit = 0; |
| 1964 | uint256 hashTx = GetHash(); |
| 1965 | for (unsigned int i = 0; i < tx->vout.size(); i++) |
| 1966 | { |
| 1967 | if (!pwallet->IsSpent(hashTx, i)) |
| 1968 | { |
| 1969 | const CTxOut &txout = tx->vout[i]; |
| 1970 | nCredit += pwallet->GetCredit(txout, filter); |
| 1971 | if (!MoneyRange(nCredit)) |
| 1972 | throw std::runtime_error(std::string(__func__) + " : value out of range"); |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | if (cache) { |
| 1977 | *cache = nCredit; |
| 1978 | assert(cache_used); |
| 1979 | *cache_used = true; |
| 1980 | } |
| 1981 | return nCredit; |
| 1982 | } |
| 1983 | |
| 1984 | CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const |
| 1985 | { |
no test coverage detected