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).

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

    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:

    ["25 24 23 22 21 20", "2ROT 24 EQUAL", "P2SH,STRICTENC", "OK"],
    ["25 24 23 22 21 20", "2ROT DROP 25 EQUAL", "P2SH,STRICTENC", "OK"],
    ["25 24 23 22 21 20", "2ROT 2DROP 20 EQUAL", "P2SH,STRICTENC", "OK"],
    ["25 24 23 22 21 20", "2ROT 2DROP DROP 21 EQUAL", "P2SH,STRICTENC", "OK"],
    ["25 24 23 22 21 20", "2ROT 2DROP 2DROP 22 EQUAL", "P2SH,STRICTENC", "OK"],
    ["25 24 23 22 21 20", "2ROT 2DROP 2DROP DROP 23 EQUAL", "P2SH,STRICTENC", "OK"],
    ["25 24 23 22 21 20", "2ROT 2ROT 22 EQUAL", "P2SH,STRICTENC", "OK"],
    ["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

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

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

    <!--021abf342d371248e50ceaed478a90ca-->

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  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.

    <details> <summary>Details</summary>

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

    </details>

    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: 2026-05-02 06:12 UTC

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