diff options
author | Antoine Poinsot <darosior@protonmail.com> | 2022-04-11 14:07:25 +0200 |
---|---|---|
committer | Antoine Poinsot <darosior@protonmail.com> | 2022-04-28 16:44:40 +0200 |
commit | ed45ee3882e69266d550b56ff69388e071f0ad1b (patch) | |
tree | 93f46dab00683b0b15f0ac9048a16934c2c5bdf0 /src/test | |
parent | 1ab8d89fd1bdb3c0f2a506b4a10df6c23ba21c48 (diff) |
miniscript: use optional instead of bool/outarg
Co-authored-by: Pieter Wuille <pieter.wuille@gmail.com>
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/fuzz/miniscript_decode.cpp | 27 | ||||
-rw-r--r-- | src/test/miniscript_tests.cpp | 19 |
2 files changed, 23 insertions, 23 deletions
diff --git a/src/test/fuzz/miniscript_decode.cpp b/src/test/fuzz/miniscript_decode.cpp index 4cc0a1be8f..1e2378cb17 100644 --- a/src/test/fuzz/miniscript_decode.cpp +++ b/src/test/fuzz/miniscript_decode.cpp @@ -21,9 +21,8 @@ using miniscript::operator""_mst; struct Converter { typedef CPubKey Key; - bool ToString(const Key& key, std::string& ret) const { - ret = HexStr(key); - return true; + std::optional<std::string> ToString(const Key& key) const { + return HexStr(key); } const std::vector<unsigned char> ToPKBytes(const Key& key) const { return {key.begin(), key.end()}; @@ -34,20 +33,21 @@ struct Converter { } template<typename I> - bool FromString(I first, I last, Key& key) const { + std::optional<Key> FromString(I first, I last) const { const auto bytes = ParseHex(std::string(first, last)); - key.Set(bytes.begin(), bytes.end()); - return key.IsValid(); + Key key{bytes.begin(), bytes.end()}; + if (key.IsValid()) return key; + return {}; } template<typename I> - bool FromPKBytes(I first, I last, CPubKey& key) const { - key.Set(first, last); - return key.IsValid(); + std::optional<Key> FromPKBytes(I first, I last) const { + Key key{first, last}; + if (key.IsValid()) return key; + return {}; } template<typename I> - bool FromPKHBytes(I first, I last, CPubKey& key) const { - assert(last - first == 20); - return false; + std::optional<Key> FromPKHBytes(I first, I last) const { + return {}; } }; @@ -63,8 +63,7 @@ FUZZ_TARGET(miniscript_decode) if (!ms) return; // We can roundtrip it to its string representation. - std::string ms_str; - assert(ms->ToString(CONVERTER, ms_str)); + std::string ms_str = *ms->ToString(CONVERTER); assert(*miniscript::FromString(ms_str, CONVERTER) == *ms); // The Script representation must roundtrip since we parsed it this way the first time. const CScript ms_script = ms->ToScript(CONVERTER); diff --git a/src/test/miniscript_tests.cpp b/src/test/miniscript_tests.cpp index 930582ea24..212525537a 100644 --- a/src/test/miniscript_tests.cpp +++ b/src/test/miniscript_tests.cpp @@ -84,27 +84,28 @@ struct KeyConverter { //! Parse a public key from a range of hex characters. template<typename I> - bool FromString(I first, I last, CPubKey& key) const { + std::optional<Key> FromString(I first, I last) const { auto bytes = ParseHex(std::string(first, last)); - key.Set(bytes.begin(), bytes.end()); - return key.IsValid(); + Key key{bytes.begin(), bytes.end()}; + if (key.IsValid()) return key; + return {}; } template<typename I> - bool FromPKBytes(I first, I last, CPubKey& key) const { - key.Set(first, last); - return key.IsValid(); + std::optional<Key> FromPKBytes(I first, I last) const { + Key key{first, last}; + if (key.IsValid()) return key; + return {}; } template<typename I> - bool FromPKHBytes(I first, I last, CPubKey& key) const { + std::optional<Key> FromPKHBytes(I first, I last) const { assert(last - first == 20); CKeyID keyid; std::copy(first, last, keyid.begin()); auto it = g_testdata->pkmap.find(keyid); assert(it != g_testdata->pkmap.end()); - key = it->second; - return true; + return it->second; } }; |