Browse Source

Monitor incoming transactions for spends by (a copy of) your own wallet

miguelfreitas
Pieter Wuille 14 years ago committed by Gavin Andresen
parent
commit
8857aeb223
  1. 46
      main.cpp

46
main.cpp

@ -178,22 +178,12 @@ bool AddToWallet(const CWalletTx& wtxIn)
return true; return true;
} }
bool AddToWalletIfMine(const CTransaction& tx, const CBlock* pblock) bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false)
{ {
if (tx.IsMine() || mapWallet.count(tx.GetHash())) uint256 hash = tx.GetHash();
{ bool fExisted = mapWallet.count(hash);
CWalletTx wtx(tx); if (fExisted && !fUpdate) return false;
// Get merkle branch if transaction was found in a block if (fExisted || tx.IsMine() || tx.IsFromMe())
if (pblock)
wtx.SetMerkleBranch(pblock);
return AddToWallet(wtx);
}
return true;
}
bool AddToWalletIfFromMe(const CTransaction& tx, const CBlock* pblock)
{
if (tx.IsFromMe() || mapWallet.count(tx.GetHash()))
{ {
CWalletTx wtx(tx); CWalletTx wtx(tx);
// Get merkle branch if transaction was found in a block // Get merkle branch if transaction was found in a block
@ -201,7 +191,7 @@ bool AddToWalletIfFromMe(const CTransaction& tx, const CBlock* pblock)
wtx.SetMerkleBranch(pblock); wtx.SetMerkleBranch(pblock);
return AddToWallet(wtx); return AddToWallet(wtx);
} }
return true; return false;
} }
bool EraseFromWallet(uint256 hash) bool EraseFromWallet(uint256 hash)
@ -911,22 +901,8 @@ int ScanForWalletTransactions(CBlockIndex* pindexStart)
block.ReadFromDisk(pindex, true); block.ReadFromDisk(pindex, true);
foreach(CTransaction& tx, block.vtx) foreach(CTransaction& tx, block.vtx)
{ {
uint256 hash = tx.GetHash(); if (AddToWalletIfInvolvingMe(tx, &block))
if (mapWallet.count(hash)) continue; ret++;
AddToWalletIfMine(tx, &block);
if (mapWallet.count(hash))
{
++ret;
printf("Added missing RECEIVE %s\n", hash.ToString().c_str());
continue;
}
AddToWalletIfFromMe(tx, &block);
if (mapWallet.count(hash))
{
++ret;
printf("Added missing SEND %s\n", hash.ToString().c_str());
continue;
}
} }
pindex = pindex->pnext; pindex = pindex->pnext;
} }
@ -1471,7 +1447,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
// Watch for transactions paying to me // Watch for transactions paying to me
foreach(CTransaction& tx, vtx) foreach(CTransaction& tx, vtx)
AddToWalletIfMine(tx, this); AddToWalletIfInvolvingMe(tx, this, true);
return true; return true;
} }
@ -2714,7 +2690,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
bool fMissingInputs = false; bool fMissingInputs = false;
if (tx.AcceptToMemoryPool(true, &fMissingInputs)) if (tx.AcceptToMemoryPool(true, &fMissingInputs))
{ {
AddToWalletIfMine(tx, NULL); AddToWalletIfInvolvingMe(tx, NULL, true);
RelayMessage(inv, vMsg); RelayMessage(inv, vMsg);
mapAlreadyAskedFor.erase(inv); mapAlreadyAskedFor.erase(inv);
vWorkQueue.push_back(inv.hash); vWorkQueue.push_back(inv.hash);
@ -2735,7 +2711,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
if (tx.AcceptToMemoryPool(true)) if (tx.AcceptToMemoryPool(true))
{ {
printf(" accepted orphan tx %s\n", inv.hash.ToString().substr(0,10).c_str()); printf(" accepted orphan tx %s\n", inv.hash.ToString().substr(0,10).c_str());
AddToWalletIfMine(tx, NULL); AddToWalletIfInvolvingMe(tx, NULL, true);
RelayMessage(inv, vMsg); RelayMessage(inv, vMsg);
mapAlreadyAskedFor.erase(inv); mapAlreadyAskedFor.erase(inv);
vWorkQueue.push_back(inv.hash); vWorkQueue.push_back(inv.hash);

Loading…
Cancel
Save