@@ -113,7 +113,9 @@ struct TestData {
113
113
struct ParserContext {
114
114
typedef CPubKey Key;
115
115
116
- MsCtx script_ctx{MsCtx::P2WSH};
116
+ const MsCtx script_ctx;
117
+
118
+ constexpr ParserContext (MsCtx ctx) noexcept : script_ctx(ctx) {}
117
119
118
120
bool KeyCompare (const Key& a, const Key& b) const {
119
121
return a < b;
@@ -178,11 +180,13 @@ struct ParserContext {
178
180
MsCtx MsContext () const {
179
181
return script_ctx;
180
182
}
181
- } PARSER_CTX ;
183
+ };
182
184
183
185
// ! Context that implements naive conversion from/to script only, for roundtrip testing.
184
186
struct ScriptParserContext {
185
- MsCtx script_ctx{MsCtx::P2WSH};
187
+ const MsCtx script_ctx;
188
+
189
+ constexpr ScriptParserContext (MsCtx ctx) noexcept : script_ctx(ctx) {}
186
190
187
191
// ! For Script roundtrip we never need the key from a key hash.
188
192
struct Key {
@@ -228,10 +232,13 @@ struct ScriptParserContext {
228
232
MsCtx MsContext () const {
229
233
return script_ctx;
230
234
}
231
- } SCRIPT_PARSER_CONTEXT ;
235
+ };
232
236
233
237
// ! Context to produce a satisfaction for a Miniscript node using the pre-computed data.
234
- struct SatisfierContext : ParserContext {
238
+ struct SatisfierContext : ParserContext {
239
+
240
+ constexpr SatisfierContext (MsCtx ctx) noexcept : ParserContext(ctx) {}
241
+
235
242
// Timelock challenges satisfaction. Make the value (deterministically) vary to explore different
236
243
// paths.
237
244
bool CheckAfter (uint32_t value) const { return value % 2 ; }
@@ -267,12 +274,10 @@ struct SatisfierContext: ParserContext {
267
274
miniscript::Availability SatHASH160 (const std::vector<unsigned char >& hash, std::vector<unsigned char >& preimage) const {
268
275
return LookupHash (hash, preimage, TEST_DATA.hash160_preimages );
269
276
}
270
- } SATISFIER_CTX ;
277
+ };
271
278
272
279
// ! Context to check a satisfaction against the pre-computed data.
273
- struct CheckerContext : BaseSignatureChecker {
274
- TestData *test_data;
275
-
280
+ const struct CheckerContext : BaseSignatureChecker {
276
281
// Signature checker methods. Checks the right dummy signature is used.
277
282
bool CheckECDSASignature (const std::vector<unsigned char >& sig, const std::vector<unsigned char >& vchPubKey,
278
283
const CScript& scriptCode, SigVersion sigversion) const override
@@ -294,7 +299,7 @@ struct CheckerContext: BaseSignatureChecker {
294
299
} CHECKER_CTX;
295
300
296
301
// ! Context to check for duplicates when instancing a Node.
297
- struct KeyComparator {
302
+ const struct KeyComparator {
298
303
bool KeyCompare (const CPubKey& a, const CPubKey& b) const {
299
304
return a < b;
300
305
}
@@ -1027,15 +1032,15 @@ void TestNode(const MsCtx script_ctx, const NodeRef& node, FuzzedDataProvider& p
1027
1032
if (!node) return ;
1028
1033
1029
1034
// Check that it roundtrips to text representation
1030
- PARSER_CTX. script_ctx = script_ctx;
1031
- std::optional<std::string> str{node->ToString (PARSER_CTX )};
1035
+ const ParserContext parser_ctx{ script_ctx} ;
1036
+ std::optional<std::string> str{node->ToString (parser_ctx )};
1032
1037
assert (str);
1033
- auto parsed = miniscript::FromString (*str, PARSER_CTX );
1038
+ auto parsed = miniscript::FromString (*str, parser_ctx );
1034
1039
assert (parsed);
1035
1040
assert (*parsed == *node);
1036
1041
1037
1042
// Check consistency between script size estimation and real size.
1038
- auto script = node->ToScript (PARSER_CTX );
1043
+ auto script = node->ToScript (parser_ctx );
1039
1044
assert (node->ScriptSize () == script.size ());
1040
1045
1041
1046
// Check consistency of "x" property with the script (type K is excluded, because it can end
@@ -1049,12 +1054,12 @@ void TestNode(const MsCtx script_ctx, const NodeRef& node, FuzzedDataProvider& p
1049
1054
if (!node->IsValidTopLevel ()) return ;
1050
1055
1051
1056
// Check roundtrip to script
1052
- auto decoded = miniscript::FromScript (script, PARSER_CTX );
1057
+ auto decoded = miniscript::FromScript (script, parser_ctx );
1053
1058
assert (decoded);
1054
1059
// Note we can't use *decoded == *node because the miniscript representation may differ, so we check that:
1055
1060
// - The script corresponding to that decoded form matches exactly
1056
1061
// - The type matches exactly
1057
- assert (decoded->ToScript (PARSER_CTX ) == script);
1062
+ assert (decoded->ToScript (parser_ctx ) == script);
1058
1063
assert (decoded->GetType () == node->GetType ());
1059
1064
1060
1065
// Optionally pad the script or the witness in order to increase the sensitivity of the tests of
@@ -1091,19 +1096,19 @@ void TestNode(const MsCtx script_ctx, const NodeRef& node, FuzzedDataProvider& p
1091
1096
}
1092
1097
}
1093
1098
1094
- SATISFIER_CTX. script_ctx = script_ctx;
1099
+ const SatisfierContext satisfier_ctx{ script_ctx} ;
1095
1100
1096
1101
// Get the ScriptPubKey for this script, filling spend data if it's Taproot.
1097
1102
TaprootBuilder builder;
1098
1103
const CScript script_pubkey{ScriptPubKey (script_ctx, script, builder)};
1099
1104
1100
1105
// Run malleable satisfaction algorithm.
1101
1106
std::vector<std::vector<unsigned char >> stack_mal;
1102
- const bool mal_success = node->Satisfy (SATISFIER_CTX , stack_mal, false ) == miniscript::Availability::YES;
1107
+ const bool mal_success = node->Satisfy (satisfier_ctx , stack_mal, false ) == miniscript::Availability::YES;
1103
1108
1104
1109
// Run non-malleable satisfaction algorithm.
1105
1110
std::vector<std::vector<unsigned char >> stack_nonmal;
1106
- const bool nonmal_success = node->Satisfy (SATISFIER_CTX , stack_nonmal, true ) == miniscript::Availability::YES;
1111
+ const bool nonmal_success = node->Satisfy (satisfier_ctx , stack_nonmal, true ) == miniscript::Availability::YES;
1107
1112
1108
1113
if (nonmal_success) {
1109
1114
// Non-malleable satisfactions are bounded by the satisfaction size plus:
@@ -1229,13 +1234,13 @@ FUZZ_TARGET(miniscript_string, .init = FuzzInit)
1229
1234
if (buffer.empty ()) return ;
1230
1235
FuzzedDataProvider provider (buffer.data (), buffer.size ());
1231
1236
auto str = provider.ConsumeBytesAsString (provider.remaining_bytes () - 1 );
1232
- PARSER_CTX. script_ctx = (MsCtx)provider.ConsumeBool ();
1233
- auto parsed = miniscript::FromString (str, PARSER_CTX );
1237
+ const ParserContext parser_ctx{ (MsCtx)provider.ConsumeBool ()} ;
1238
+ auto parsed = miniscript::FromString (str, parser_ctx );
1234
1239
if (!parsed) return ;
1235
1240
1236
- const auto str2 = parsed->ToString (PARSER_CTX );
1241
+ const auto str2 = parsed->ToString (parser_ctx );
1237
1242
assert (str2);
1238
- auto parsed2 = miniscript::FromString (*str2, PARSER_CTX );
1243
+ auto parsed2 = miniscript::FromString (*str2, parser_ctx );
1239
1244
assert (parsed2);
1240
1245
assert (*parsed == *parsed2);
1241
1246
}
@@ -1247,9 +1252,9 @@ FUZZ_TARGET(miniscript_script)
1247
1252
const std::optional<CScript> script = ConsumeDeserializable<CScript>(fuzzed_data_provider);
1248
1253
if (!script) return ;
1249
1254
1250
- SCRIPT_PARSER_CONTEXT. script_ctx = (MsCtx)fuzzed_data_provider.ConsumeBool ();
1251
- const auto ms = miniscript::FromScript (*script, SCRIPT_PARSER_CONTEXT );
1255
+ const ScriptParserContext script_parser_ctx{ (MsCtx)fuzzed_data_provider.ConsumeBool ()} ;
1256
+ const auto ms = miniscript::FromScript (*script, script_parser_ctx );
1252
1257
if (!ms) return ;
1253
1258
1254
- assert (ms->ToScript (SCRIPT_PARSER_CONTEXT ) == *script);
1259
+ assert (ms->ToScript (script_parser_ctx ) == *script);
1255
1260
}
0 commit comments