aboutsummaryrefslogtreecommitdiff
path: root/src/util/overloaded.h
diff options
context:
space:
mode:
authorRussell Yanofsky <russ@yanofsky.org>2021-02-16 22:36:26 -0500
committerRussell Yanofsky <russ@yanofsky.org>2021-11-15 09:11:44 -0500
commitd8ee8f3cd32bbfefec931724f5798cbb088ceb6f (patch)
tree09bea6475822b6b37ca0050d03a4394946af35a3 /src/util/overloaded.h
parent2efc8c0999a4b99cfe3076f7312806e83e778261 (diff)
downloadbitcoin-d8ee8f3cd32bbfefec931724f5798cbb088ceb6f.tar.xz
refactor: Make CWalletTx sync state type-safe
Current CWalletTx state representation makes it possible to set inconsistent states that won't be handled correctly by wallet sync code or serialized & deserialized back into the same form. For example, it is possible to call setConflicted without setting a conflicting block hash, or setConfirmed with no transaction index. And it's possible update individual m_confirm and fInMempool data fields without setting an overall consistent state that can be serialized and handled correctly. Fix this without changing behavior by using std::variant, instead of an enum and collection of fields, to represent sync state, so state tracking code is safer and more legible. This is a first step to fixing state tracking bugs https://github.com/bitcoin-core/bitcoin-devwiki/wiki/Wallet-Transaction-Conflict-Tracking, by adding an extra margin of safety that can prevent new bugs from being introduced as existing bugs are fixed.
Diffstat (limited to 'src/util/overloaded.h')
-rw-r--r--src/util/overloaded.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/util/overloaded.h b/src/util/overloaded.h
new file mode 100644
index 0000000000..6be7453f81
--- /dev/null
+++ b/src/util/overloaded.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_UTIL_OVERLOADED_H
+#define BITCOIN_UTIL_OVERLOADED_H
+
+namespace util {
+//! Overloaded helper for std::visit. This helper and std::visit in general are
+//! useful to write code that switches on a variant type. Unlike if/else-if and
+//! switch/case statements, std::visit will trigger compile errors if there are
+//! unhandled cases.
+//!
+//! Implementation comes from and example usage can be found at
+//! https://en.cppreference.com/w/cpp/utility/variant/visit#Example
+template<class... Ts> struct Overloaded : Ts... { using Ts::operator()...; };
+
+//! Explicit deduction guide (not needed as of C++20)
+template<class... Ts> Overloaded(Ts...) -> Overloaded<Ts...>;
+} // namespace util
+
+#endif // BITCOIN_UTIL_OVERLOADED_H