test: check proper OP_2ROT behavior #33047

pull brunoerg wants to merge 1 commits into bitcoin:master from brunoerg:2025-07-test-op2rot changing 1 files +1 −0
  1. brunoerg commented at 4:55 pm on July 23, 2025: contributor

    According to corecheck, the following mutant is not caught by any test (https://corecheck.dev/mutation/src/script/interpreter.cpp).

     0diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp
     1index 61ea7f4503..4f6fa34836 100644
     2--- a/src/script/interpreter.cpp
     3+++ b/src/script/interpreter.cpp
     4@@ -746,7 +746,6 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
     5                         return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
     6                     valtype vch1 = stacktop(-6);
     7                     valtype vch2 = stacktop(-5);
     8-                    stack.erase(stack.end()-6, stack.end()-4);
     9                     stack.push_back(vch1);
    10                     stack.push_back(vch2);
    11                 }
    

    It means we’re not testing the behavior of the OP_2ROT opcode properly. The normal behavior is: [1, 2, 3, 4, 5, 6] → OP_2ROT → [3, 4, 5, 6, 1, 2] (6 elements). However, by deleting the erase, it becomes: [1, 2, 3, 4, 5, 6] → OP_2ROT → [1, 2, 3, 4, 5, 6, 1, 2] (8 elements) which is obviously wrong. In script_tests.json, we have some test cases that includes the OP_2ROT which correctly tests the move part of 2ROT but not the erase one. See:

    0["25 24 23 22 21 20", "2ROT 24 EQUAL", "P2SH,STRICTENC", "OK"],
    1["25 24 23 22 21 20", "2ROT DROP 25 EQUAL", "P2SH,STRICTENC", "OK"],
    2["25 24 23 22 21 20", "2ROT 2DROP 20 EQUAL", "P2SH,STRICTENC", "OK"],
    3["25 24 23 22 21 20", "2ROT 2DROP DROP 21 EQUAL", "P2SH,STRICTENC", "OK"],
    4["25 24 23 22 21 20", "2ROT 2DROP 2DROP 22 EQUAL", "P2SH,STRICTENC", "OK"],
    5["25 24 23 22 21 20", "2ROT 2DROP 2DROP DROP 23 EQUAL", "P2SH,STRICTENC", "OK"],
    6["25 24 23 22 21 20", "2ROT 2ROT 22 EQUAL", "P2SH,STRICTENC", "OK"],
    7["25 24 23 22 21 20", "2ROT 2ROT 2ROT 20 EQUAL", "P2SH,STRICTENC", "OK"],
    

    That said, this PR adds one more test case to the case mentioned.

  2. test: check proper OP_2ROT behavior b94c6356a2
  3. DrahtBot added the label Tests on Jul 23, 2025
  4. DrahtBot commented at 4:55 pm on July 23, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33047.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK l0rinc, w0xlt, maflcko

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

  5. l0rinc approved
  6. l0rinc commented at 6:44 pm on July 23, 2025: contributor

    ACK b94c6356a29b03def6337c91caabb3b8642187e8

    I can confirm that the whole test suite passes when the stack erase is removed from the script interpreter code.

    0rm -rf build && cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug && ninja -C build && build/bin/test_bitcoin --run_test=script_tests
    
     0diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp
     1index 61ea7f4503..efb1af8137 100644
     2--- a/src/script/interpreter.cpp
     3+++ b/src/script/interpreter.cpp
     4@@ -746,7 +746,7 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
     5                         return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
     6                     valtype vch1 = stacktop(-6);
     7                     valtype vch2 = stacktop(-5);
     8-                    stack.erase(stack.end()-6, stack.end()-4);
     9+                    // stack.erase(stack.end()-6, stack.end()-4);
    10                     stack.push_back(vch1);
    11                     stack.push_back(vch2);
    12                 }
    13diff --git a/src/test/data/script_tests.json b/src/test/data/script_tests.json
    14index 6282f66649..c11bd68ae8 100644
    15--- a/src/test/data/script_tests.json
    16+++ b/src/test/data/script_tests.json
    17@@ -105,7 +105,6 @@
    18 ["25 24 23 22 21 20", "2ROT 2DROP 2DROP DROP 23 EQUAL", "P2SH,STRICTENC", "OK"],
    19 ["25 24 23 22 21 20", "2ROT 2ROT 22 EQUAL", "P2SH,STRICTENC", "OK"],
    20 ["25 24 23 22 21 20", "2ROT 2ROT 2ROT 20 EQUAL", "P2SH,STRICTENC", "OK"],
    21-["1 2 3 4 5 6", "2ROT DEPTH 6 EQUAL", "P2SH,STRICTENC", "OK"],
    22 ["1 0", "SWAP 1 EQUALVERIFY 0 EQUAL", "P2SH,STRICTENC", "OK"],
    23 ["0 1", "TUCK DEPTH 3 EQUALVERIFY SWAP 2DROP", "P2SH,STRICTENC", "OK"],
    24 ["13 14", "2DUP ROT EQUALVERIFY EQUAL", "P2SH,STRICTENC", "OK"],
    

    And that it fails as expected with the new script_tests.json addition!

  7. maflcko commented at 6:55 am on July 24, 2025: member
    lgtm ACK b94c6356a29b03def6337c91caabb3b8642187e8
  8. fanquake merged this on Jul 24, 2025
  9. fanquake closed this on Jul 24, 2025


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-08-12 09:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me