refactor: Prepare for ParseHex -> ""_hex scripted-diff

- Adds using namespace.
- Extracts ToScript helper function from ScriptFromHex, to be used heavily in the next commit.
- Changes ScriptFromHex from using ParseHex to TryParseHex, now asserting the string is valid.
- Use even number of hex digits in comment (and apply replacement from next commit to only touch line once).
This commit is contained in:
Hodlinator 2024-08-27 21:01:13 +02:00
parent 50bc017040
commit 9cb687351f
No known key found for this signature in database
3 changed files with 12 additions and 3 deletions

View file

@ -25,6 +25,8 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
using namespace util::hex_literals;
// Very simple block filter index sync benchmark, only using coinbase outputs. // Very simple block filter index sync benchmark, only using coinbase outputs.
static void BlockFilterIndexSync(benchmark::Bench& bench) static void BlockFilterIndexSync(benchmark::Bench& bench)
{ {

View file

@ -16,6 +16,7 @@
#include <univalue.h> #include <univalue.h>
using namespace util::hex_literals;
BOOST_FIXTURE_TEST_SUITE(script_standard_tests, BasicTestingSetup) BOOST_FIXTURE_TEST_SUITE(script_standard_tests, BasicTestingSetup)

View file

@ -1356,10 +1356,16 @@ BOOST_AUTO_TEST_CASE(script_GetScriptAsm)
BOOST_CHECK_EQUAL(derSig + "83 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "83")) << vchPubKey)); BOOST_CHECK_EQUAL(derSig + "83 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "83")) << vchPubKey));
} }
template <typename T>
CScript ToScript(const T& byte_container)
{
auto span{MakeUCharSpan(byte_container)};
return {span.begin(), span.end()};
}
static CScript ScriptFromHex(const std::string& str) static CScript ScriptFromHex(const std::string& str)
{ {
std::vector<unsigned char> data = ParseHex(str); return ToScript(*Assert(TryParseHex(str)));
return CScript(data.begin(), data.end());
} }
BOOST_AUTO_TEST_CASE(script_FindAndDelete) BOOST_AUTO_TEST_CASE(script_FindAndDelete)
@ -1393,7 +1399,7 @@ BOOST_AUTO_TEST_CASE(script_FindAndDelete)
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1); BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
BOOST_CHECK(s == expect); BOOST_CHECK(s == expect);
s = ScriptFromHex("0302ff030302ff03"); // PUSH 0x2ff03 PUSH 0x2ff03 s = ToScript("0302ff030302ff03"_hex); // PUSH 0x02ff03 PUSH 0x02ff03
d = ScriptFromHex("0302ff03"); d = ScriptFromHex("0302ff03");
expect = CScript(); expect = CScript();
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2); BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);