Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP -- IGNORE ME #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bench/coin_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>
tx.vout[nInput].nValue = nValue;
std::unique_ptr<CWalletTx> wtx = std::make_unique<CWalletTx>(MakeTransactionRef(std::move(tx)));
set.emplace_back();
set.back().Insert(COutput(testWallet, *wtx, nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0, false);
set.back().Insert(COutput(testWallet, *wtx, nInput, 0, true, true, true).GetInputCoin(testWallet), 0, true, 0, 0, false);
wtxn.emplace_back(std::move(wtx));
}
// Copied from src/wallet/test/coinselector_tests.cpp
Expand Down
12 changes: 6 additions & 6 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@

#include <optional>

CInputCoin::CInputCoin(const CWalletTx* wtx, unsigned int i) {
CInputCoin::CInputCoin(const CWallet& wallet, const CWalletTx* wtx, unsigned int i) {
if (!wtx || !wtx->tx)
throw std::invalid_argument("tx should not be null");
if (i >= wtx->tx->vout.size())
throw std::out_of_range("The output index is out of range");

outpoint = COutPoint(wtx->tx->GetHash(), i);
txout = wtx->tx->vout[i];
effective_value = std::max<CAmount>(0, wtx->GetOutputValueOut(i));
value = wtx->GetOutputValueOut(i);
asset = wtx->GetOutputAsset(i);
bf_value = wtx->GetOutputAmountBlindingFactor(i);
bf_asset = wtx->GetOutputAssetBlindingFactor(i);
effective_value = std::max<CAmount>(0, wtx->GetOutputValueOut(wallet, i));
value = wtx->GetOutputValueOut(wallet, i);
asset = wtx->GetOutputAsset(wallet, i);
bf_value = wtx->GetOutputAmountBlindingFactor(wallet, i);
bf_asset = wtx->GetOutputAssetBlindingFactor(wallet, i);
}

// Descending order comparator
Expand Down
5 changes: 3 additions & 2 deletions src/wallet/coinselection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ static constexpr CAmount MIN_CHANGE{COIN / 100};
//! final minimum change amount after paying for fees
static const CAmount MIN_FINAL_CHANGE = MIN_CHANGE/2;

class CWallet;
class CWalletTx;
class uint256;

/** A UTXO under consideration for use in funding a new transaction. */
class CInputCoin {
public:
CInputCoin(const CWalletTx* wtx, unsigned int i);
CInputCoin(const CWallet& wallet, const CWalletTx* wtx, unsigned int i);

CInputCoin(const CWalletTx* wtx, unsigned int i, int input_bytes) : CInputCoin(wtx, i)
CInputCoin(const CWallet& wallet, const CWalletTx* wtx, unsigned int i, int input_bytes) : CInputCoin(wallet, wtx, i)
{
m_input_bytes = input_bytes;
}
Expand Down
10 changes: 5 additions & 5 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
const auto& txin = wtx.tx->vin[i];
result.txin_is_mine.emplace_back(InputIsMine(wallet, txin));
wtx.GetIssuanceAssets(i, &result.txin_issuance_asset[i], &result.txin_issuance_token[i]);
result.txin_issuance_asset_amount.emplace_back(wtx.GetIssuanceAmount(i, false));
result.txin_issuance_token_amount.emplace_back(wtx.GetIssuanceAmount(i, true));
result.txin_issuance_asset_amount.emplace_back(wtx.GetIssuanceAmount(wallet, i, false));
result.txin_issuance_token_amount.emplace_back(wtx.GetIssuanceAmount(wallet, i, true));
}
result.txout_is_mine.reserve(wtx.tx->vout.size());
result.txout_address.reserve(wtx.tx->vout.size());
Expand All @@ -74,12 +74,12 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
wallet.IsMine(result.txout_address.back()) :
ISMINE_NO);
result.txout_is_change.push_back(wallet.IsChange(txout));
result.txout_is_change.push_back(OutputIsChange(wallet, txout));
}
// ELEMENTS: Retrieve unblinded information about outputs
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
result.txout_amounts.emplace_back(wtx.GetOutputValueOut(i));
result.txout_assets.emplace_back(wtx.GetOutputAsset(i));
result.txout_amounts.emplace_back(wtx.GetOutputValueOut(wallet, i));
result.txout_assets.emplace_back(wtx.GetOutputAsset(wallet, i));
}
result.credit = CachedTxGetCredit(wallet, wtx, ISMINE_ALL);
result.debit = CachedTxGetDebit(wallet, wtx, ISMINE_ALL);
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool VerifyWallets(WalletContext& context)
fs::path wallet_dir = args.GetArg("-walletdir", "");
boost::system::error_code error;
// The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error).remove_trailing_separator();
if (error || !fs::exists(wallet_dir)) {
chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string()));
return false;
Expand Down
91 changes: 51 additions & 40 deletions src/wallet/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include <wallet/receive.h>
#include <wallet/transaction.h>
#include <wallet/wallet.h>
#include <iostream>

isminetype InputIsMine(const CWallet& wallet, const CTxIn &txin)
{
AssertLockHeld(wallet.cs_wallet);
return IsMine(wallet, txin.prevout);
return InputIsMine(wallet, txin.prevout);
}

isminetype InputIsMine(const CWallet& wallet, const COutPoint &outpoint)
Expand Down Expand Up @@ -48,35 +49,36 @@ bool AllInputsMine(const CWallet& wallet, const CTransaction& tx, const isminefi
return true;
}

CAmountMap OutputGetCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter) const {
CAmountMap OutputGetCredit(const CWallet& wallet, const CTransaction& tx, const size_t out_index, const isminefilter& filter) {
CAmountMap nCredit;
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
if (wallet.IsMine(wtx.tx->vout[i]) & filter) {
CAmount credit = std::max<CAmount>(0, wtx.GetOutputValueOut(i));
for (unsigned int i = 0; i < tx.vout.size(); ++i) {
if (wallet.IsMine(tx.vout[i]) & filter) {
CWalletTx wtx(MakeTransactionRef(std::move(tx)));
CAmount credit = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
if (!MoneyRange(credit))
throw std::runtime_error(std::string(__func__) + ": value out of range");

nCredit[wtx.GetOutputAsset(i)] += credit;
nCredit[wtx.GetOutputAsset(wallet, i)] += credit;
if (!MoneyRange(nCredit))
throw std::runtime_error(std::string(__func__) + ": value out of range");
}
}
return nCredit;
}

CAmountMap CWallet::GetChange(const CWalletTx& wtx) const {
CAmountMap GetChange(const CWallet& wallet, const CWalletTx& wtx) {
CAmountMap nChange;
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
if (IsChange(wtx.tx->vout[i])) {
CAmount change = wtx.GetOutputValueOut(i);
if (OutputIsChange(wallet, wtx.tx->vout[i])) {
CAmount change = wtx.GetOutputValueOut(wallet, i);
if (change < 0) {
continue;
}

if (!MoneyRange(change))
throw std::runtime_error(std::string(__func__) + ": value out of range");

nChange[wtx.GetOutputAsset(i)] += change;
nChange[wtx.GetOutputAsset(wallet, i)] += change;
if (!MoneyRange(nChange))
throw std::runtime_error(std::string(__func__) + ": value out of range");
}
Expand Down Expand Up @@ -111,7 +113,7 @@ bool ScriptIsChange(const CWallet& wallet, const CScript& script)
return false;
}

CAmount OutputGetChange(const CWallet& wallet, const CTxOut& txout)
CAmountMap OutputGetChange(const CWallet& wallet, const CTxOut& txout)
{
AssertLockHeld(wallet.cs_wallet);

Expand All @@ -122,7 +124,7 @@ CAmount OutputGetChange(const CWallet& wallet, const CTxOut& txout)
return (OutputIsChange(wallet, txout) ? change : CAmountMap());
}

CAmount TxGetChange(const CWallet& wallet, const CTransaction& tx)
CAmountMap TxGetChange(const CWallet& wallet, const CTransaction& tx)
{
LOCK(wallet.cs_wallet);
CAmountMap nChange;
Expand All @@ -135,12 +137,12 @@ CAmount TxGetChange(const CWallet& wallet, const CTransaction& tx)
return nChange;
}

static CAmountMap GetCachableAmount(const CWallet& wallet, const CWalletTx& wtx, CWalletTx::AmountType type, const isminefilter& filter, bool recalculate = false) NO_THREAD_SAFETY_ANALYSIS
static CAmountMap GetCachableAmount(const CWallet& wallet, const CWalletTx& wtx, CWalletTx::AmountType type, const isminefilter& filter, bool recalculate = false)
{
auto& amount = wtx.m_amounts[type];
if (recalculate || !amount.m_cached[filter]) {
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, *this, filter));//JAMES DELETE ME: TxGetCredit(wallet, *wtx.tx, filter));
m_is_cache_empty = false;
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, *wtx.tx, filter));
wtx.m_is_cache_empty = false;
}
return amount.m_value[filter];
}
Expand All @@ -162,23 +164,25 @@ CAmountMap CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const
return credit;
}

/*CAmountMap CWallet::GetCredit(const CTransaction& tx, const size_t out_index, const isminefilter& filter) const
CAmountMap TxGetCredit(const CWallet& wallet, const CTransaction& tx, const isminefilter& filter)
{
{
LOCK(cs_wallet);
std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(tx.GetHash());
if (mi != mapWallet.end())
LOCK(wallet.cs_wallet);
std::map<uint256, CWalletTx>::const_iterator mi = wallet.mapWallet.find(tx.GetHash());
if (mi != wallet.mapWallet.end())
{
const CWalletTx& wtx = (*mi).second;
if (out_index < wtx.tx->vout.size() && IsMine(wtx.tx->vout[out_index]) & filter) {
CAmountMap amounts;
amounts[wtx.GetOutputAsset(out_index)] = std::max<CAmount>(0, wtx.GetOutputValueOut(out_index));
return amounts;
for (size_t i = 0; i < wtx.tx->vout.size(); ++i) {
if (wallet.IsMine(wtx.tx->vout[i]) & filter) {
CAmountMap amounts;
amounts[wtx.GetOutputAsset(wallet, i)] = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
return amounts;
}
}
}
}
return CAmountMap();
}*/
}

CAmountMap CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
{
Expand All @@ -199,15 +203,23 @@ CAmountMap CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx)
{
if (wtx.fChangeCached)
return wtx.nChangeCached;
wtx.nChangeCached = TxGetChange(wallet, *this); //DELETEME (JAMES): *wtx.tx
wtx.nChangeCached = TxGetChange(wallet, *wtx.tx);
wtx.fChangeCached = true;
return wtx.nChangeCached;
}

CAmountMap CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, bool fUseCache)
{
std::cout << "CachedTxGetImmatureCredit: " << wallet.IsTxImmatureCoinBase(wtx) << " " << wallet.IsTxInMainChain(wtx) << std::endl;
if (wallet.IsTxImmatureCoinBase(wtx) && wallet.IsTxInMainChain(wtx)) {
return GetCachableAmount(wallet, wtx, CWalletTx::IMMATURE_CREDIT, ISMINE_SPENDABLE, !fUseCache);
std::cout << "in if statement" << std::endl;
auto temp = GetCachableAmount(wallet, wtx, CWalletTx::IMMATURE_CREDIT, ISMINE_SPENDABLE, !fUseCache);
std::cout << "size: " << temp.size() << std::endl;
for (auto& pair : temp) {
std::cout << "key: " << pair.first.GetHex() << std::endl;
std::cout << "value: " << pair.second << std::endl;
}
return temp;
}

return CAmountMap();
Expand Down Expand Up @@ -240,13 +252,13 @@ CAmountMap CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wt
uint256 hashTx = wtx.GetHash();
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++)
{
if (!wallet->IsSpent(hashTx, i) && (allow_used_addresses || !wallet->IsSpentKey(hashTx, i))) {
if (wallet->IsMine(tx->vout[i]) & filter) {
CAmount credit = std::max<CAmount>(0, GetOutputValueOut(i));
if (!wallet.IsSpent(hashTx, i) && (allow_used_addresses || !wallet.IsSpentKey(hashTx, i))) {
if (wallet.IsMine(wtx.tx->vout[i]) & filter) {
CAmount credit = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
if (!MoneyRange(credit))
throw std::runtime_error(std::string(__func__) + ": value out of range");

nCredit[GetOutputAsset(i)] += std::max<CAmount>(0, GetOutputValueOut(i));
nCredit[wtx.GetOutputAsset(wallet, i)] += std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
if (!MoneyRange(nCredit))
throw std::runtime_error(std::string(__func__) + ": value out of range");
}
Expand Down Expand Up @@ -281,9 +293,9 @@ void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx,
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i)
{
const CTxOut& txout = wtx.tx->vout[i];
CAmount output_value = GetOutputValueOut(i);
CAmount output_value = wtx.GetOutputValueOut(wallet, i);
// Don't list unknown assets
isminetype fIsMine = output_value != -1 ? wallet->IsMine(txout) : ISMINE_NO;
isminetype fIsMine = output_value != -1 ? wallet.IsMine(txout) : ISMINE_NO;
// Only need to handle txouts if AT LEAST one of these is true:
// 1) they debit from us (sent)
// 2) the output is to us (received)
Expand All @@ -306,7 +318,7 @@ void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx,
address = CNoDestination();
}

COutputEntry output = {address, output_value, (int)i, GetOutputAsset(i), GetOutputAmountBlindingFactor(i), GetOutputAssetBlindingFactor(i)};
COutputEntry output = {address, output_value, (int)i, wtx.GetOutputAsset(wallet, i), wtx.GetOutputAmountBlindingFactor(wallet, i), wtx.GetOutputAssetBlindingFactor(wallet, i)};

// If we are debited by the transaction, add the output as a "sent" entry
if (mapDebit > CAmountMap() && !txout.IsFee())
Expand All @@ -321,7 +333,7 @@ void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx,

bool CachedTxIsFromMe(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
{
return (CachedTxGetDebit(wallet, wtx, filter) > 0);
return (CachedTxGetDebit(wallet, wtx, filter) > CAmountMap());
}

bool CachedTxIsTrusted(const CWallet& wallet, const CWalletTx& wtx, std::set<uint256>& trusted_parents)
Expand Down Expand Up @@ -373,7 +385,7 @@ Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse)
for (const auto& entry : wallet.mapWallet)
{
const CWalletTx& wtx = entry.second;
const bool is_trusted{IsTrusted(wtx, trusted_parents)};
const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)};
const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
const CAmountMap tx_credit_mine{CachedTxGetAvailableCredit(wallet, wtx, /* fUseCache */ true, ISMINE_SPENDABLE | reuse_filter)};
const CAmountMap tx_credit_watchonly{CachedTxGetAvailableCredit(wallet, wtx, /* fUseCache */ true, ISMINE_WATCH_ONLY | reuse_filter)};
Expand Down Expand Up @@ -421,7 +433,7 @@ std::map<CTxDestination, CAmount> GetAddressBalances(const CWallet& wallet)
if(!ExtractDestination(wtx.tx->vout[i].scriptPubKey, addr))
continue;

CAmount n = wallet.IsSpent(walletEntry.first, i) ? 0 : wtx.GetOutputValueOut(i);
CAmount n = wallet.IsSpent(walletEntry.first, i) ? 0 : wtx.GetOutputValueOut(wallet, i);
if (n < 0) {
continue;
}
Expand Down Expand Up @@ -527,17 +539,16 @@ std::set< std::set<CTxDestination> > GetAddressGroupings(const CWallet& wallet)
}

// ELEMENTS
CAmountMap CWalletTx::GetIssuanceAssets(unsigned int input_index) const {
CAmountMap CWalletTx::GetIssuanceAssets(const CWallet& wallet, unsigned int input_index) const {
CAmountMap ret;
CAsset asset, token;
GetIssuanceAssets(input_index, &asset, &token);
if (!asset.IsNull()) {
ret[asset] = GetIssuanceAmount(input_index, false);
ret[asset] = GetIssuanceAmount(wallet, input_index, false);
}
if (!token.IsNull()) {
ret[token] = GetIssuanceAmount(input_index, true);
ret[token] = GetIssuanceAmount(wallet, input_index, true);
}
return ret;
}
// end ELEMENTS

10 changes: 7 additions & 3 deletions src/wallet/receive.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ bool OutputIsChange(const CWallet& wallet, const CTxOut& txout) EXCLUSIVE_LOCKS_
CAmountMap OutputGetChange(const CWallet& wallet, const CTxOut& txout) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
CAmountMap TxGetChange(const CWallet& wallet, const CTransaction& tx);
// ELEMENTS:
CAmountMap GetCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter) const EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
CAmountMap GetChange(const CWallet& wallet, const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
CAmountMap GetCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
CAmountMap GetChange(const CWallet& wallet, const CWalletTx& wtx) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);

CAmountMap CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter);
//! filter decides which addresses will count towards the debit
CAmountMap CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter);
CAmountMap CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx) const NO_THREAD_SAFETY_ANALYSIS;
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
// annotation "EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)". The
// annotation "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid
// having to resolve the issue of member access into incomplete type CWallet.
CAmountMap CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx) NO_THREAD_SAFETY_ANALYSIS;
CAmountMap CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, bool fUseCache = true);
CAmountMap CachedTxGetImmatureWatchOnlyCredit(const CWallet& wallet, const CWalletTx& wtx, const bool fUseCache = true);
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
Expand Down
5 changes: 2 additions & 3 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,10 +2237,10 @@ RPCHelpMan dumpissuanceblindingkey()
throw JSONRPCError(RPC_WALLET_ERROR, "Transaction input has no issuance");
}
// We can actually deblind the input
if (pcoin->GetIssuanceAmount(vindex, false) != -1 ) {
if (pcoin->GetIssuanceAmount(*pwallet, vindex, false) != -1 ) {
CScript blindingScript(CScript() << OP_RETURN << std::vector<unsigned char>(pcoin->tx->vin[vindex].prevout.hash.begin(), pcoin->tx->vin[vindex].prevout.hash.end()) << pcoin->tx->vin[vindex].prevout.n);
CKey key;
key = pwallet->GetBlindingKey(&blindingScript);
key = wallet->GetBlindingKey(&blindingScript);
return HexStr(Span<const unsigned char>(key.begin(), key.size()));
} else {
// We don't know how to deblind this using our wallet
Expand All @@ -2254,4 +2254,3 @@ RPCHelpMan dumpissuanceblindingkey()

// END ELEMENTS
//

Loading