Skip to content

Conversation

@promag
Copy link
Contributor

@promag promag commented Aug 31, 2020

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

A second lookup in mapOpNames is also removed.

@practicalswift
Copy link
Contributor

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

@promag
Copy link
Contributor Author

promag commented Sep 1, 2020

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

@theStack
Copy link
Contributor

theStack commented Sep 2, 2020

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

@practicalswift
Copy link
Contributor

practicalswift commented Sep 2, 2020

@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

@promag
Copy link
Contributor Author

promag commented Sep 2, 2020

Ok, let's do it here.

@theStack
Copy link
Contributor

theStack commented Sep 3, 2020

@promag: Feel free to add my commit: theStack@5dfc039 (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_") ....

@Empact
Copy link
Contributor

Empact commented Sep 3, 2020

Code review ACK 7b3a015

@epson121
Copy link

epson121 commented Sep 6, 2020

Code review ACK 7b3a0150a5e7d0cd8f31ff7bbd5a421d9020cfd1

@laanwj
Copy link
Member

laanwj commented Sep 29, 2020

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

@theStack
Copy link
Contributor

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

@promag
Copy link
Contributor Author

promag commented Oct 6, 2020

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

@theStack
Copy link
Contributor

theStack commented Oct 6, 2020

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

A second lookup in mapOpNames is also removed.
@promag promag force-pushed the 2020-08-parseopcode branch from 539efcc to c923872 Compare October 6, 2020 11:34
Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK c923872

@luke-jr
Copy link
Member

luke-jr commented Oct 24, 2020

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.

laanwj added a commit that referenced this pull request Nov 19, 2020
6f4e393 refactor: remove use of boost::algorithm::replace_first (Sebastian Falbesoner)

Pull request description:

  As discussed in #19851 (#19851 (comment)), this trivial PR substitutes the (only) use of `boost::algorithm::replace_first` by a direct implementation.

ACKs for top commit:
  laanwj:
    Code review ACK 6f4e393

Tree-SHA512: 2ef06498e19f864a4cbae10e8d1905e3440a2d1e8e5aae83de7597c23cdab92b4612d7fa1efbc49016e530debd127d1d50531c60ff159dbea0deaa8c836a2bfb
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 19, 2020
…e_first

6f4e393 refactor: remove use of boost::algorithm::replace_first (Sebastian Falbesoner)

Pull request description:

  As discussed in bitcoin#19851 (bitcoin#19851 (comment)), this trivial PR substitutes the (only) use of `boost::algorithm::replace_first` by a direct implementation.

ACKs for top commit:
  laanwj:
    Code review ACK 6f4e393

Tree-SHA512: 2ef06498e19f864a4cbae10e8d1905e3440a2d1e8e5aae83de7597c23cdab92b4612d7fa1efbc49016e530debd127d1d50531c60ff159dbea0deaa8c836a2bfb
@laanwj
Copy link
Member

laanwj commented Nov 20, 2020

ACK c923872

@laanwj laanwj merged commit 46f0b2f into bitcoin:master Nov 20, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 20, 2020
c923872 refactor: Extract ParseOpCode from ParseScript (João Barbosa)

Pull request description:

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

  A second lookup in `mapOpNames` is also removed.

ACKs for top commit:
  laanwj:
    ACK c923872
  theStack:
    re-ACK c923872

Tree-SHA512: d59d1964760622cf365479d44e3e676aa0bf46b60e77160140d967e012042df92121d3224c7551dc96eff5ff3294598cc6bade82adb3f60d28810e18e60e1257
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…e_first

6f4e393 refactor: remove use of boost::algorithm::replace_first (Sebastian Falbesoner)

Pull request description:

  As discussed in bitcoin#19851 (bitcoin#19851 (comment)), this trivial PR substitutes the (only) use of `boost::algorithm::replace_first` by a direct implementation.

ACKs for top commit:
  laanwj:
    Code review ACK 6f4e393

Tree-SHA512: 2ef06498e19f864a4cbae10e8d1905e3440a2d1e8e5aae83de7597c23cdab92b4612d7fa1efbc49016e530debd127d1d50531c60ff159dbea0deaa8c836a2bfb
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 15, 2021
…e_first

6f4e393 refactor: remove use of boost::algorithm::replace_first (Sebastian Falbesoner)

Pull request description:

  As discussed in bitcoin#19851 (bitcoin#19851 (comment)), this trivial PR substitutes the (only) use of `boost::algorithm::replace_first` by a direct implementation.

ACKs for top commit:
  laanwj:
    Code review ACK 6f4e393

Tree-SHA512: 2ef06498e19f864a4cbae10e8d1905e3440a2d1e8e5aae83de7597c23cdab92b4612d7fa1efbc49016e530debd127d1d50531c60ff159dbea0deaa8c836a2bfb
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Dec 14, 2021
A second lookup in mapOpNames is also removed.

Github-Pull: bitcoin#19851
Rebased-From: c923872
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 11, 2022
Summary:
Seems more natural to have `mapOpNames` "hidden" in `ParseOpCode` than in `ParseScript`.

A second lookup in mapOpNames is also removed.

This is a backport of [[bitcoin/bitcoin#19851 | core#19851]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10812
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants