Skip to content

Commit 5487725

Browse files
committed
test: avoid sporadic MINIMALDATA failure in feature_taproot.py (fixes #27595)
The functional test feature_taproot.py fails in some rare cases on the execution of the `"branched_codesep"` spending script. The problem occurs if the first data-push (having random content with a random length in the range [0, 510]) has a length of 1 and the single byte has value of [1...16] or [-1]; in this case, the data-push is not minimally encoded by test framework's CScript class (i.e. doesn't use the special op-codes OP_1...OP_16 or OP_1NEGATE) and the script interpreter throws an SCRIPT_ERR_MINIMALDATA error: ``` test_framework.authproxy.JSONRPCException: non-mandatory-script-verify-flag (Data push larger than necessary) (-26) ``` Background: The functional test framework's CScript class translates passed bytes/bytearrays always to data pushes using OP_PUSHx/OP_PUSHDATA{1,2,4} op-codes. E.g. the expression `CScript(bytes([1]))` yields `bytes([OP_PUSH1, 1])` instead of the minimal-encoded `bytes([OP_1])`. Fix this by adapting the random-size range to [2,...], i.e. never pass byte-arrays below length two to be pushed. Closes #27595.
1 parent 7f20197 commit 5487725

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

test/functional/feature_taproot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,11 @@ def spenders_taproot_active():
738738
scripts = [
739739
("pk_codesep", CScript(random_checksig_style(pubs[1]) + bytes([OP_CODESEPARATOR]))), # codesep after checksig
740740
("codesep_pk", CScript(bytes([OP_CODESEPARATOR]) + random_checksig_style(pubs[1]))), # codesep before checksig
741-
("branched_codesep", CScript([random_bytes(random.randrange(511)), OP_DROP, OP_IF, OP_CODESEPARATOR, pubs[0], OP_ELSE, OP_CODESEPARATOR, pubs[1], OP_ENDIF, OP_CHECKSIG])), # branch dependent codesep
741+
("branched_codesep", CScript([random_bytes(random.randrange(2, 511)), OP_DROP, OP_IF, OP_CODESEPARATOR, pubs[0], OP_ELSE, OP_CODESEPARATOR, pubs[1], OP_ENDIF, OP_CHECKSIG])), # branch dependent codesep
742+
# Note that the first data push in the "branched_codesep" script has the purpose of
743+
# randomizing the sighash, both by varying script size and content. In order to
744+
# avoid MINIMALDATA script verification errors caused by not-minimal-encoded data
745+
# pushes (e.g. `OP_PUSH1 1` instead of `OP_1`), we set a minimum data size of 2 bytes.
742746
]
743747
random.shuffle(scripts)
744748
tap = taproot_construct(pubs[0], scripts)

0 commit comments

Comments
 (0)