Skip to content

Commit 46b4937

Browse files
committed
Merge bitcoin/bitcoin#23142: Return false on corrupt tx rather than asserting
0ab4c3b Return false on corrupt tx rather than asserting (Samuel Dobson) Pull request description: Takes up #19793 Rather than asserting, we log an error and return CORRUPT so that the user is informed. This type of error isn't critical so it isn't worth `assert`ing. ACKs for top commit: achow101: ACK 0ab4c3b laanwj: Code review ACK 0ab4c3b ryanofsky: Code review ACK 0ab4c3b. There may be room for more improvements later like better error messages or easier recovery options, but changing from an assert to an error seems like a clear improvement, and this seems to avoid all the pitfalls of the last PR that tried this. Tree-SHA512: 4a1a412e7c473d176c4e09123b85f390a6b0ea195e78d28ebd50b13814b7852f8225a172511a2efb6affb555b11bd4e667c19eb8c78b060c5444b62f0fae5f7a
2 parents 29b030b + 0ab4c3b commit 46b4937

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/wallet/walletdb.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ class CWalletScanState {
311311
std::map<std::pair<uint256, CKeyID>, CKey> m_descriptor_keys;
312312
std::map<std::pair<uint256, CKeyID>, std::pair<CPubKey, std::vector<unsigned char>>> m_descriptor_crypt_keys;
313313
std::map<uint160, CHDChain> m_hd_chains;
314+
bool tx_corrupt{false};
314315

315316
CWalletScanState() {
316317
}
@@ -345,7 +346,13 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
345346
// LoadToWallet call below creates a new CWalletTx that fill_wtx
346347
// callback fills with transaction metadata.
347348
auto fill_wtx = [&](CWalletTx& wtx, bool new_tx) {
348-
assert(new_tx);
349+
if(!new_tx) {
350+
// There's some corruption here since the tx we just tried to load was already in the wallet.
351+
// We don't consider this type of corruption critical, and can fix it by removing tx data and
352+
// rescanning.
353+
wss.tx_corrupt = true;
354+
return false;
355+
}
349356
ssValue >> wtx;
350357
if (wtx.GetHash() != hash)
351358
return false;
@@ -819,6 +826,11 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
819826
} else if (strType == DBKeys::FLAGS) {
820827
// reading the wallet flags can only fail if unknown flags are present
821828
result = DBErrors::TOO_NEW;
829+
} else if (wss.tx_corrupt) {
830+
pwallet->WalletLogPrintf("Error: Corrupt transaction found. This can be fixed by removing transactions from wallet and rescanning.\n");
831+
// Set tx_corrupt back to false so that the error is only printed once (per corrupt tx)
832+
wss.tx_corrupt = false;
833+
result = DBErrors::CORRUPT;
822834
} else {
823835
// Leave other errors alone, if we try to fix them we might make things worse.
824836
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.

0 commit comments

Comments
 (0)