wallet: Skip transactions with no shielded data in CWallet::SetBestChain()

Co-authored-by: Daira Hopwood <daira@jacaranda.org>

Closes #3495.
This commit is contained in:
Jack Grigg 2018-11-28 00:56:42 +01:00
parent 6bb3124367
commit 0de659946e
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
2 changed files with 68 additions and 4 deletions

View File

@ -1627,6 +1627,63 @@ TEST(WalletTests, WriteWitnessCache) {
wallet.SetBestChain(walletdb, loc);
}
TEST(WalletTests, SetBestChainIgnoresTxsWithoutShieldedData) {
SelectParams(CBaseChainParams::REGTEST);
TestWallet wallet;
MockWalletDB walletdb;
CBlockLocator loc;
// Set up transparent address
CKey tsk = DecodeSecret(tSecretRegtest);
wallet.AddKey(tsk);
auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID());
// Set up a Sprout address
auto sk = libzcash::SproutSpendingKey::random();
wallet.AddSproutSpendingKey(sk);
// Generate a transparent transaction that is ours
CMutableTransaction t;
t.vout.resize(1);
t.vout[0].nValue = 90*CENT;
t.vout[0].scriptPubKey = scriptPubKey;
CWalletTx wtxTransparent {nullptr, t};
wallet.AddToWallet(wtxTransparent, true, NULL);
// Generate a Sprout transaction that is ours
auto wtxSprout = GetValidReceive(sk, 10, true);
auto noteMap = wallet.FindMySproutNotes(wtxSprout);
wtxSprout.SetSproutNoteData(noteMap);
wallet.AddToWallet(wtxSprout, true, NULL);
// Generate a Sprout transaction that only involves our transparent address
auto sk2 = libzcash::SproutSpendingKey::random();
auto wtxInput = GetValidReceive(sk2, 10, true);
auto note = GetNote(sk2, wtxInput, 0, 0);
auto wtxTmp = GetValidSpend(sk2, note, 5);
CMutableTransaction mtx {wtxTmp};
mtx.vout[0].scriptPubKey = scriptPubKey;
CWalletTx wtxSproutTransparent {NULL, mtx};
wallet.AddToWallet(wtxSproutTransparent, true, NULL);
EXPECT_CALL(walletdb, TxnBegin())
.WillOnce(Return(true));
EXPECT_CALL(walletdb, WriteTx(wtxTransparent.GetHash(), wtxTransparent))
.Times(0);
EXPECT_CALL(walletdb, WriteTx(wtxSprout.GetHash(), wtxSprout))
.Times(1).WillOnce(Return(true));
EXPECT_CALL(walletdb, WriteTx(wtxSproutTransparent.GetHash(), wtxSproutTransparent))
.Times(0);
EXPECT_CALL(walletdb, WriteWitnessCacheSize(0))
.WillOnce(Return(true));
EXPECT_CALL(walletdb, WriteBestBlock(loc))
.WillOnce(Return(true));
EXPECT_CALL(walletdb, TxnCommit())
.WillOnce(Return(true));
wallet.SetBestChain(walletdb, loc);
}
TEST(WalletTests, UpdateSproutNullifierNoteMap) {
TestWallet wallet;
uint256 r {GetRandHash()};

View File

@ -790,12 +790,19 @@ protected:
}
try {
for (std::pair<const uint256, CWalletTx>& wtxItem : mapWallet) {
if (!walletdb.WriteTx(wtxItem.first, wtxItem.second)) {
auto wtx = wtxItem.second;
// We skip transactions for which mapSproutNoteData and mapSaplingNoteData
// are empty. This covers transactions that have no Sprout or Sapling data
// (i.e. are purely transparent), as well as shielding and unshielding
// transactions in which we only have transparent addresses involved.
if (!(wtx.mapSproutNoteData.empty() && wtx.mapSaplingNoteData.empty())) {
if (!walletdb.WriteTx(wtxItem.first, wtx)) {
LogPrintf("SetBestChain(): Failed to write CWalletTx, aborting atomic write\n");
walletdb.TxnAbort();
return;
}
}
}
if (!walletdb.WriteWitnessCacheSize(nWitnessCacheSize)) {
LogPrintf("SetBestChain(): Failed to write nWitnessCacheSize, aborting atomic write\n");
walletdb.TxnAbort();