Skip to content

Instantly share code, notes, and snippets.

@l0rinc
Created August 21, 2024 11:14
Show Gist options
  • Save l0rinc/92eb6c56858c92693e36dc776a247509 to your computer and use it in GitHub Desktop.
Save l0rinc/92eb6c56858c92693e36dc776a247509 to your computer and use it in GitHub Desktop.
488887ce32aaed65d395e329d9093596e7c2e733
Subject: [PATCH] TODO
---
Index: src/test/util_tests.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
--- a/src/test/util_tests.cpp (revision 8b13872aa2960e66e0bf8542fcb5b8dda68c5d8c)
+++ b/src/test/util_tests.cpp (date 1724237518191)
@@ -146,24 +146,30 @@
0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
0x5f
};
-static_assert((sizeof(HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT));
+static_assert(sizeof(HEX_PARSE_INPUT) / 2 == sizeof(HEX_PARSE_OUTPUT));
BOOST_AUTO_TEST_CASE(parse_hex)
{
std::vector<unsigned char> result;
std::vector<unsigned char> expected(std::begin(HEX_PARSE_OUTPUT), std::end(HEX_PARSE_OUTPUT));
+
// Basic test vector
- constexpr std::array<std::byte, 65> hex_literal_array{operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()};
+ auto hex_literal_array{operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()};
auto hex_literal_span{MakeUCharSpan(hex_literal_array)};
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
- const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
+
+ auto hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
hex_literal_span = MakeUCharSpan(hex_literal_vector);
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
- constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
+
+ auto hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end());
+
result = operator""_hex_v_u8<util::detail::Hex(HEX_PARSE_INPUT)>();
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+
result = ParseHex(HEX_PARSE_INPUT);
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+
result = TryParseHex<uint8_t>(HEX_PARSE_INPUT).value();
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
@@ -189,10 +195,6 @@
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
// Empty string is supported
- /*BOOST_CHECK_EQUAL(""_hex.size(), 0);
- BOOST_CHECK_EQUAL(""_hex_u8.size(), 0);
- BOOST_CHECK_EQUAL(""_hex_v.size(), 0);
- BOOST_CHECK_EQUAL(""_hex_v_u8.size(), 0);*/
BOOST_CHECK_EQUAL(ParseHex("").size(), 0);
BOOST_CHECK_EQUAL(TryParseHex<uint8_t>("").value().size(), 0);
Index: src/test/script_tests.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp
--- a/src/test/script_tests.cpp (revision 8b13872aa2960e66e0bf8542fcb5b8dda68c5d8c)
+++ b/src/test/script_tests.cpp (date 1724238311336)
@@ -1356,7 +1356,7 @@
CScript ToScript(const T& byte_container)
{
auto span{MakeUCharSpan(byte_container)};
- return {span.data(), span.data() + span.size()};
+ return {span.begin(), span.end()};
}
static CScript ScriptFromHex(const std::string& str)
Index: src/test/transaction_tests.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp
--- a/src/test/transaction_tests.cpp (revision 8b13872aa2960e66e0bf8542fcb5b8dda68c5d8c)
+++ b/src/test/transaction_tests.cpp (date 1724238623634)
@@ -384,7 +384,7 @@
auto createTransaction =[](size_t payloadSize) {
CMutableTransaction tx;
tx.vin.resize(1);
- tx.vout.emplace_back(1, CScript() << OP_RETURN << std::vector<unsigned char>(payloadSize));
+ tx.vout.emplace_back(1, CScript() << OP_RETURN << valtype(payloadSize));
return CTransaction(tx);
};
const auto maxTransactionSize = MAX_BLOCK_WEIGHT / WITNESS_SCALE_FACTOR;
@@ -408,8 +408,13 @@
BOOST_AUTO_TEST_CASE(basic_transaction_tests)
{
// Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
- unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
- std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
+ valtype vch {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00};
+
+ // TODO remove
+ // unsigned char ch_old[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
+ // std::vector<unsigned char> vch_old(ch_old, ch_old + sizeof(ch_old) -1);
+ // assert(vch_old == vch);
+
DataStream stream(vch);
CMutableTransaction tx;
stream >> TX_WITH_WITNESS(tx);
@@ -433,13 +438,13 @@
t1.vin.resize(3);
t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
t1.vin[0].prevout.n = 1;
- t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
+ t1.vin[0].scriptSig << valtype(65, 0);
t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
t1.vin[1].prevout.n = 0;
- t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
+ t1.vin[1].scriptSig << valtype(65, 0) << valtype(33, 4);
t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
t1.vin[2].prevout.n = 1;
- t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
+ t1.vin[2].scriptSig << valtype(65, 0) << valtype(33, 4);
t1.vout.resize(2);
t1.vout[0].nValue = 90*CENT;
t1.vout[0].scriptPubKey << OP_1;
@@ -516,7 +521,7 @@
std::vector<valtype> stack;
EvalScript(stack, script, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SigVersion::BASE);
assert(stack.size() > 0);
- stack.back() = std::vector<unsigned char>(redeemScript.begin(), redeemScript.end());
+ stack.back() = valtype(redeemScript.begin(), redeemScript.end());
script = PushAll(stack);
}
@@ -529,7 +534,7 @@
FillableSigningProvider keystore;
BOOST_CHECK(keystore.AddKeyPubKey(key, key.GetPubKey()));
CKeyID hash = key.GetPubKey().GetID();
- CScript scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(hash.begin(), hash.end());
+ CScript scriptPubKey = CScript() << OP_0 << valtype(hash.begin(), hash.end());
std::vector<int> sigHashes;
sigHashes.push_back(SIGHASH_NONE | SIGHASH_ANYONECANPAY);
@@ -790,7 +795,7 @@
t.vin.resize(1);
t.vin[0].prevout.hash = dummyTransactions[0].GetHash();
t.vin[0].prevout.n = 1;
- t.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
+ t.vin[0].scriptSig << valtype(65, 0);
t.vout.resize(1);
t.vout[0].nValue = 90*CENT;
CKey key = GenerateRandomKey();
@@ -861,7 +866,7 @@
CheckIsNotStandard(t, "scriptpubkey");
// Data payload can be encoded in any way...
- t.vout[0].scriptPubKey = CScript() << OP_RETURN << /*""_hex_v_u8*/valtype{};
+ t.vout[0].scriptPubKey = CScript() << OP_RETURN << valtype{};
CheckIsStandard(t);
t.vout[0].scriptPubKey = CScript() << OP_RETURN << "00"_hex_v_u8 << "01"_hex_v_u8;
CheckIsStandard(t);
@@ -901,22 +906,22 @@
t.vout[0].nValue = MAX_MONEY;
t.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
// OP_PUSHDATA2 with len (3 bytes) + data (1647 bytes) = 1650 bytes
- t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(1647, 0); // 1650
+ t.vin[0].scriptSig = CScript() << valtype(1647, 0); // 1650
CheckIsStandard(t);
- t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(1648, 0); // 1651
+ t.vin[0].scriptSig = CScript() << valtype(1648, 0); // 1651
CheckIsNotStandard(t, "scriptsig-size");
// Check scriptSig format (non-standard if there are any other ops than just PUSHs)
t.vin[0].scriptSig = CScript()
<< OP_TRUE << OP_0 << OP_1NEGATE << OP_16 // OP_n (single byte pushes: n = 1, 0, -1, 16)
- << std::vector<unsigned char>(75, 0) // OP_PUSHx [...x bytes...]
- << std::vector<unsigned char>(235, 0) // OP_PUSHDATA1 x [...x bytes...]
- << std::vector<unsigned char>(1234, 0) // OP_PUSHDATA2 x [...x bytes...]
+ << valtype(75, 0) // OP_PUSHx [...x bytes...]
+ << valtype(235, 0) // OP_PUSHDATA1 x [...x bytes...]
+ << valtype(1234, 0) // OP_PUSHDATA2 x [...x bytes...]
<< OP_9;
CheckIsStandard(t);
- const std::vector<unsigned char> non_push_ops = { // arbitrary set of non-push operations
+ const valtype non_push_ops = { // arbitrary set of non-push operations
OP_NOP, OP_VERIFY, OP_IF, OP_ROT, OP_3DUP, OP_SIZE, OP_EQUAL, OP_ADD, OP_SUB,
OP_HASH256, OP_CODESEPARATOR, OP_CHECKSIG, OP_CHECKLOCKTIMEVERIFY };
@@ -943,7 +948,7 @@
// Check tx-size (non-standard if transaction weight is > MAX_STANDARD_TX_WEIGHT)
t.vin.clear();
t.vin.resize(2438); // size per input (empty scriptSig): 41 bytes
- t.vout[0].scriptPubKey = CScript() << OP_RETURN << std::vector<unsigned char>(19, 0); // output size: 30 bytes
+ t.vout[0].scriptPubKey = CScript() << OP_RETURN << valtype(19, 0); // output size: 30 bytes
// tx header: 12 bytes => 48 weight units
// 2438 inputs: 2438*41 = 99958 bytes => 399832 weight units
// 1 output: 30 bytes => 120 weight units
@@ -953,7 +958,7 @@
CheckIsStandard(t);
// increase output size by one byte, so we end up with 400004 weight units
- t.vout[0].scriptPubKey = CScript() << OP_RETURN << std::vector<unsigned char>(20, 0); // output size: 31 bytes
+ t.vout[0].scriptPubKey = CScript() << OP_RETURN << valtype(20, 0); // output size: 31 bytes
BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(t)), 400004);
CheckIsNotStandard(t, "tx-size");
@@ -961,7 +966,7 @@
g_bare_multi = true;
t.vout[0].scriptPubKey = GetScriptForMultisig(1, {key.GetPubKey()}); // simple 1-of-1
t.vin.resize(1);
- t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(65, 0);
+ t.vin[0].scriptSig = CScript() << valtype(65, 0);
CheckIsStandard(t);
g_bare_multi = false;
@@ -969,49 +974,49 @@
g_bare_multi = DEFAULT_PERMIT_BAREMULTISIG;
// Check compressed P2PK outputs dust threshold (must have leading 02 or 03)
- t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG;
+ t.vout[0].scriptPubKey = CScript() << valtype(33, 0x02) << OP_CHECKSIG;
t.vout[0].nValue = 576;
CheckIsStandard(t);
t.vout[0].nValue = 575;
CheckIsNotStandard(t, "dust");
// Check uncompressed P2PK outputs dust threshold (must have leading 04/06/07)
- t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(65, 0x04) << OP_CHECKSIG;
+ t.vout[0].scriptPubKey = CScript() << valtype(65, 0x04) << OP_CHECKSIG;
t.vout[0].nValue = 672;
CheckIsStandard(t);
t.vout[0].nValue = 671;
CheckIsNotStandard(t, "dust");
// Check P2PKH outputs dust threshold
- t.vout[0].scriptPubKey = CScript() << OP_DUP << OP_HASH160 << std::vector<unsigned char>(20, 0) << OP_EQUALVERIFY << OP_CHECKSIG;
+ t.vout[0].scriptPubKey = CScript() << OP_DUP << OP_HASH160 << valtype(20, 0) << OP_EQUALVERIFY << OP_CHECKSIG;
t.vout[0].nValue = 546;
CheckIsStandard(t);
t.vout[0].nValue = 545;
CheckIsNotStandard(t, "dust");
// Check P2SH outputs dust threshold
- t.vout[0].scriptPubKey = CScript() << OP_HASH160 << std::vector<unsigned char>(20, 0) << OP_EQUAL;
+ t.vout[0].scriptPubKey = CScript() << OP_HASH160 << valtype(20, 0) << OP_EQUAL;
t.vout[0].nValue = 540;
CheckIsStandard(t);
t.vout[0].nValue = 539;
CheckIsNotStandard(t, "dust");
// Check P2WPKH outputs dust threshold
- t.vout[0].scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(20, 0);
+ t.vout[0].scriptPubKey = CScript() << OP_0 << valtype(20, 0);
t.vout[0].nValue = 294;
CheckIsStandard(t);
t.vout[0].nValue = 293;
CheckIsNotStandard(t, "dust");
// Check P2WSH outputs dust threshold
- t.vout[0].scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(32, 0);
+ t.vout[0].scriptPubKey = CScript() << OP_0 << valtype(32, 0);
t.vout[0].nValue = 330;
CheckIsStandard(t);
t.vout[0].nValue = 329;
CheckIsNotStandard(t, "dust");
// Check P2TR outputs dust threshold (Invalid xonly key ok!)
- t.vout[0].scriptPubKey = CScript() << OP_1 << std::vector<unsigned char>(32, 0);
+ t.vout[0].scriptPubKey = CScript() << OP_1 << valtype(32, 0);
t.vout[0].nValue = 330;
CheckIsStandard(t);
t.vout[0].nValue = 329;
@@ -1019,7 +1024,7 @@
// Check future Witness Program versions dust threshold (non-32-byte pushes are undefined for version 1)
for (int op = OP_1; op <= OP_16; op += 1) {
- t.vout[0].scriptPubKey = CScript() << (opcodetype)op << std::vector<unsigned char>(2, 0);
+ t.vout[0].scriptPubKey = CScript() << (opcodetype)op << valtype(2, 0);
t.vout[0].nValue = 240;
CheckIsStandard(t);
@@ -1028,7 +1033,7 @@
}
// Check anchor outputs
- t.vout[0].scriptPubKey = CScript() << OP_1 << std::vector<unsigned char>{0x4e, 0x73};
+ t.vout[0].scriptPubKey = CScript() << OP_1 << valtype{0x4e, 0x73};
BOOST_CHECK(t.vout[0].scriptPubKey.IsPayToAnchor());
t.vout[0].nValue = 240;
CheckIsStandard(t);
Index: src/util/strencodings.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/util/strencodings.h b/src/util/strencodings.h
--- a/src/util/strencodings.h (revision 8b13872aa2960e66e0bf8542fcb5b8dda68c5d8c)
+++ b/src/util/strencodings.h (date 1724237756530)
@@ -375,10 +375,9 @@
/** consteval version of HexDigit() without the lookup table. */
consteval uint8_t ConstevalHexDigit(const char c)
{
- if (c >= '0' && c <= '9') return c - '0';
- if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
-
- throw "Only lowercase hex digits are allowed, for consistency";
+ if (c >= '0' && c <= '9') return c - '0';
+ else if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
+ else throw "Only lowercase hex digits are allowed, for consistency";
}
namespace detail {
@@ -387,46 +386,33 @@
struct Hex {
std::array<std::byte, N / 2> bytes;
consteval Hex(const char (&hex_str)[N])
- // 2 hex digits required per byte + implicit null terminator
- requires(N % 2 == 1)
{
- if (hex_str[N - 1]) throw "null terminator required";
+ assert(N > 2 && hex_str[N - 1] == '\0' && N % 2 == 1); // non-empty, null-terminated, even hex digits
for (std::size_t i = 0; i < bytes.size(); ++i) {
bytes[i] = static_cast<std::byte>(
(ConstevalHexDigit(hex_str[2 * i]) << 4) |
ConstevalHexDigit(hex_str[2 * i + 1]));
}
}
-
- consteval auto Uint8() const { return std::bit_cast<std::array<uint8_t, N / 2>>(bytes); }
- std::vector<std::byte> Vec() const { return {bytes.begin(), bytes.end()}; }
- std::vector<uint8_t> VecUint8() const { return {UCharCast(bytes.data()), UCharCast(bytes.data()) + bytes.size()}; }
};
} // namespace detail
} // namespace util
-/**
- * Converts from hex string literal to std::array<std::byte, N> at compile time.
- *
- * @note It may be preferable to use _hex_v instead of
- * _hex to save stack space when declaring a local variable, if the hex
- * string is large. Alternately the variable could be declared constexpr to
- * avoid using stack space.
- */
+//! Converts from hex string literal to std::array<std::byte, N> at compile time.
template <util::detail::Hex str>
consteval auto operator"" _hex() { return str.bytes; }
-/** Same as _hex except that it returns std::array<uint8_t, N>. */
+//! Same as _hex, except that it returns std::array<uint8_t, N>.
template <util::detail::Hex str>
-constexpr auto operator"" _hex_u8() { return str.Uint8(); }
+consteval auto operator"" _hex_u8() { return std::bit_cast<std::array<uint8_t, sizeof(str.bytes)>>(str.bytes); }
-/** Same as _hex except that it returns std::vector<std::byte>. */
+//! Same as _hex, except that it returns std::vector<std::byte>.
template <util::detail::Hex str>
-constexpr auto operator"" _hex_v() { return str.Vec(); }
+constexpr auto operator"" _hex_v() { return std::vector<std::byte>{str.bytes.begin(), str.bytes.end()}; }
-/** Same as _hex except that it returns std::vector<uint8_t>. */
+//! Same as _hex, except that it returns std::vector<uint8_t>.
template <util::detail::Hex str>
-constexpr auto operator"" _hex_v_u8() { return str.VecUint8(); }
+constexpr auto operator"" _hex_v_u8() { return std::vector<uint8_t>{UCharCast(std::begin(str.bytes)), UCharCast(std::end(str.bytes))}; }
#endif // BITCOIN_UTIL_STRENCODINGS_H
Index: src/bench/bech32.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/bench/bech32.cpp b/src/bench/bech32.cpp
--- a/src/bench/bech32.cpp (revision 8b13872aa2960e66e0bf8542fcb5b8dda68c5d8c)
+++ b/src/bench/bech32.cpp (date 1724237997675)
@@ -13,7 +13,7 @@
static void Bech32Encode(benchmark::Bench& bench)
{
- constexpr std::array<uint8_t, 32> v{"c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db"_hex_u8};
+ auto v{"c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db"_hex_u8};
std::vector<unsigned char> tmp = {0};
tmp.reserve(1 + v.size() * 8 / 5);
ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, v.begin(), v.end());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment