refactor: Extract ParseOpCode from ParseScript #19851

pull promag wants to merge 1 commits into bitcoin:master from promag:2020-08-parseopcode changing 2 files +17 −10
  1. promag commented at 6:32 PM on August 31, 2020: member

    Seems more natural to have mapOpNames "hidden" in ParseOpCode than in ParseScript.

    A second lookup in mapOpNames is also removed.

  2. DrahtBot added the label Refactoring on Aug 31, 2020
  3. practicalswift commented at 7:16 PM on August 31, 2020: contributor

    When touching ParseScript: what also about removing the uses of boost::algorithm::replace_first and boost::algorithm::split in it?

  4. promag commented at 12:10 AM on September 1, 2020: member

    @practicalswift that's fine but I guess not here?

  5. theStack commented at 7:32 AM on September 2, 2020: member

    ACK 7b3a0150a5e7d0cd8f31ff7bbd5a421d9020cfd1 📃

    When touching ParseScript: what also about removing the uses of boost::algorithm::replace_first and boost::algorithm::split in it? @practicalswift: Could you elaborate on why boost::algorithm::split should be avoided? I can totally see that boost::algorithm::replace_first is overkill here and can be easily done via STL, for splitting though there doesn't seem to be a non-ugly solution. (E.g. https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/ also recommends boost for this task).

  6. practicalswift commented at 7:58 AM on September 2, 2020: contributor

    @theStack The long term plan is to remove all usages of Boost, so I thought it would be nice to take one small step in that direction when creating ParseOpCode (and thus touching this code). I agree that removing the trivial boost::algorithm::replace_first before the less trivial boost::algorithm::split makes sense. Also, only replace_first is used in the new ParseOpCode :)

    The unnecessary Boost usage in ParseOpCode:

    https://github.com/bitcoin/bitcoin/blob/7b3a0150a5e7d0cd8f31ff7bbd5a421d9020cfd1/src/core_read.cpp#L42-L43

  7. promag commented at 10:27 PM on September 2, 2020: member

    Ok, let's do it here.

  8. theStack commented at 11:13 AM on September 3, 2020: member

    @promag: Feel free to add my commit: https://github.com/theStack/bitcoin/commit/5dfc039e89107a261c7f8eda70bae1fe63c581c7 (branch https://github.com/theStack/bitcoin/tree/20200903-refactor-get-rid-of-boost-replace_first):

    diff --git a/src/core_read.cpp b/src/core_read.cpp
    index 1829f18..155f443 100644
    --- a/src/core_read.cpp
    +++ b/src/core_read.cpp
    @@ -15,7 +15,6 @@
     #include <version.h>
    
     #include <boost/algorithm/string/classification.hpp>
    -#include <boost/algorithm/string/replace.hpp>
     #include <boost/algorithm/string/split.hpp>
    
     #include <algorithm>
    @@ -40,8 +39,8 @@ opcodetype ParseOpCode(const std::string& s)
                     continue;
                 mapOpNames[strName] = static_cast<opcodetype>(op);
                 // Convenience: OP_ADD and just ADD are both recognized:
    -            boost::algorithm::replace_first(strName, "OP_", "");
    -            mapOpNames[strName] = static_cast<opcodetype>(op);
    +            if (strName.rfind("OP_", 0) == 0)
    +                mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
             }
         }
    

    Another solution would be to use if (strName.substr(0,3) == "OP_") ....

  9. epson121 commented at 11:53 AM on September 6, 2020: none

    Code review ACK 7b3a0150a5e7d0cd8f31ff7bbd5a421d9020cfd1

  10. laanwj commented at 2:00 PM on September 29, 2020: member

    @theStack I don't think your code is equivalent. In the orignal case, both OP_X and X are accepted. In the new case, only OP_X is. (concept ACK on getting rid of boost::algorithm::replace_first in favor of a direct implementation though)

  11. theStack commented at 2:21 PM on September 29, 2020: member

    @theStack I don't think your code is equivalent. In the orignal case, both OP_X and X are accepted. In the new case, only OP_X is.

    Hm, I guess the .rfind() call is confusing here, leading you to think that the condition is true if the string doesn't start with "OP_"? Note that the condition foo.rfind(bar, 0) == 0 simply checks if foo starts with bar, e.g. it would be equal to a (fictional) foo.startswith(bar). (See https://stackoverflow.com/a/40441240)

    (concept ACK on getting rid of boost::algorithm::replace_first in favor of a direct implementation though)

    Will come up with a follow-up PR then.

  12. in src/core_read.cpp:49 in 7b3a0150a5 outdated
      44 | @@ -45,6 +45,17 @@ CScript ParseScript(const std::string& s)
      45 |          }
      46 |      }
      47 |  
      48 | +    auto it = mapOpNames.find(s);
      49 | +    if (it == mapOpNames.end()) throw std::runtime_error("script parse error");
    


    laanwj commented at 1:52 PM on September 30, 2020:

    You could be more specific about the error here. E.g. like the out of range error:

    throw std::runtime_error("script parse error: unknown opcode");
    

    promag commented at 7:59 AM on October 6, 2020:

    Done.

  13. promag force-pushed on Oct 6, 2020
  14. promag commented at 7:59 AM on October 6, 2020: member

    Decided to keep boost in this PR, don't want to drag this because of that unrelated change. Applied @laanwj suggestion.

  15. theStack commented at 10:58 AM on October 6, 2020: member

    I'd suggest to adapt the expected result string for the util test case "Create a new transaction with an invalid output script":

    diff --git a/test/util/data/bitcoin-util-test.json b/test/util/data/bitcoin-util-test.json
    index 99cd4ab..0a9846b 100644
    --- a/test/util/data/bitcoin-util-test.json
    +++ b/test/util/data/bitcoin-util-test.json
    @@ -221,7 +221,7 @@
       { "exec": "./bitcoin-tx",
         "args": ["-create", "outscript=0:123badscript"],
         "return_code": 1,
    -    "error_txt": "error: script parse error",
    +    "error_txt": "error: script parse error: unknown opcode",
         "description": "Create a new transaction with an invalid output script"
       },
       { "exec": "./bitcoin-tx",
    

    (The test still passes without this patch though, as matching a substring is sufficient).

  16. refactor: Extract ParseOpCode from ParseScript
    A second lookup in mapOpNames is also removed.
    c92387232f
  17. promag force-pushed on Oct 6, 2020
  18. theStack approved
  19. theStack commented at 11:37 AM on October 6, 2020: member

    re-ACK c92387232f750397da7d131f262c150a608408c2

  20. luke-jr commented at 6:01 PM on October 24, 2020: member

    The long term plan is to remove all usages of Boost, @practicalswift No... rather C++ standards are preferred over Boost when appropriate, and Boost isn't allowed in libconsensus. Boost still provides many useful things the standards don't, and I doubt we will ever remove its use entirely.

  21. laanwj referenced this in commit fbb2bee82d on Nov 19, 2020
  22. sidhujag referenced this in commit f8a3fab2dc on Nov 19, 2020
  23. laanwj commented at 4:35 AM on November 20, 2020: member

    ACK c92387232f750397da7d131f262c150a608408c2

  24. laanwj merged this on Nov 20, 2020
  25. laanwj closed this on Nov 20, 2020

  26. sidhujag referenced this in commit c613d08472 on Nov 20, 2020
  27. PastaPastaPasta referenced this in commit f8f01cccbe on Jun 27, 2021
  28. PastaPastaPasta referenced this in commit ee970bfe48 on Jun 28, 2021
  29. PastaPastaPasta referenced this in commit e3fb5fd83d on Jun 29, 2021
  30. PastaPastaPasta referenced this in commit a58b0f10e1 on Jul 1, 2021
  31. PastaPastaPasta referenced this in commit ac8fd2a1c5 on Jul 1, 2021
  32. PastaPastaPasta referenced this in commit 21307a3345 on Jul 15, 2021
  33. PastaPastaPasta referenced this in commit 96cf458083 on Jul 15, 2021
  34. luke-jr referenced this in commit 94134bd76b on Dec 14, 2021
  35. Fabcien referenced this in commit b2738e0305 on Jan 11, 2022
  36. DrahtBot locked this on Feb 15, 2022

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-04-14 21:14 UTC

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