Repository navigation
consensus: Consistently encode and decode OP_1NEGATE similar to other small ints in Script - #29589
Christewart wants to merge 1 commit into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/29589. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. |
12cdde8 to
fcf944b
Compare
|
Should |
Done in 8bd0a8b18d68bbacee731f3e0583397f81ca078e , good catch 🙌 |
|
utACK 8bd0a8b18d68bbacee731f3e0583397f81ca078e |
|
Thanks for the code review @vostrnad , I took your changes and added them to this PR. I also cherry-picked them and tested them on 64bit-cscriptnum and everything ✔️ |
vostrnad
left a comment
There was a problem hiding this comment.
ACK 6cf08dd2d532766f1b7063f40e7ab25f8ee67e45 (one style nit not addressed)
PR should also be renamed, e.g. "tests: fix OP_1NEGATE handling in CScriptOp"
CScriptOp.encode_op_n() where OP_1NEGATE was not handled correctlyOP_1NEGATE handling in CScriptOp
|
Concept ACK Now that bitcoin/test/functional/test_framework/script.py Lines 446 to 450 in 31be1a4 |
|
Concept ACK I'm proposing this for python-bitcoinlib too, which is where all this code comes from originally: petertodd/python-bitcoinlib#299 |
Credit goes to Chris Stewart, bitcoin/bitcoin#29589
Done in f0c077a388b980566590d7887d8c6dd97cbaa20a |
|
I'm not sure that it is wise to change the behavior of Lines 506 to 519 in a85e5a7 Having different behavior between code in C++ and python I would suggest to just handle -1/OP_1NEGATE in-place where they are relevant, rather than changing the assumption 'N is from 0 to 16' as it clearly is handled this way by |
rkrux
left a comment
There was a problem hiding this comment.
- Make is successful
- All functional tests pass
Would it be possible to share where this error pops up in the branch specified in the description?
"When we don't encode -1 as OP_1NEGATE then we end up getting errors inside of CheckMinimalPush() saying -1 was not minimally encoded."
|
tested ACK f0c077a388b980566590d7887d8c6dd97cbaa20a
|
|
Hey @Christewart! Do you plan to update for cmake? |
6acb275 to
1d8777a
Compare
Done, i squashed everything into 1 commit as well. |
|
I'm ~0 on this leaning towards NACK. As far as I can tell, all code paths are currently gated by So this is really only changing the intention of those functions. And since it would be unsafe (because of |
mzumsande
left a comment
There was a problem hiding this comment.
I didn’t see earlier versions of this PR, but It’s not clear to me what the goal of this PR is from the OP / Title. Should the title be updated, is this still supposed to fix a bug?
As mentioned above, DecodeOP_N / EncodeOP_N currently don't need to handle OP_1NEGATE in any of the current use cases - so I think that there should be some explanation why they should be changed to support it.
I noticed that EncodeOP_N is currently called from PushAll in sign.cpp with an unsigned char (valtype), where there is already special-casing for OP_1NEGATE as 0x81, which seems like a possible source of confusion if the function was extended to deal with OP_1NEGATE as -1.
Also, the commit message could be fixed / made more coherent, it reads like it contains multiple commit messages from before a squash.
There was a problem hiding this comment.
This would mean that C++ (IsSmallInteger in solver.cpp) would have a different notion of a small interger.
There was a problem hiding this comment.
I think this was already the case before this PR? It seems that the currently implementation in solver.cpp doesn't handle OP_0 correctly, while the python codebase does?
Line 61 in 03cff2c
This goes to the purpose of this PR, handle them consistently across codebases and remove special cases such as OP_1NEGATE.
There was a problem hiding this comment.
In f2f8796 I modified IsSmallInteger() to handle OP_1NEGATE and OP_0 correctly. Previously only the python codebase handled OP_0 correctly.
There was a problem hiding this comment.
I don't think that "correct" is the right term here, it's more about what these functions are intended to be used for. In this case, the comment Test for "small positive integer" indicates that the function wasn't designed to deal with OP_0, so if it's just the name could just rename it to IsSmallPositiveInteger to not introduce a change in behavior.
There was a problem hiding this comment.
This seems like a reasonable thing to do. However it seems like the script.py implementation should still handle OP_1NEGATE imo.
IIUC the solver.cpp's IsSmallInteger() and script.py's is_small_int() serve 2 different purposes, and renaming the solver.cpp function name would help differentiate them.
However if we aren't going to change any functionality in this file, perhaps its best to not do anything and just revert it and move on? 🤷♂️
OP_1NEGATE handling in CScriptOpOP_1NEGATE similar to other small ints in Script
OP_1NEGATE similar to other small ints in ScriptOP_1NEGATE similar to other small ints in Script
1d8777a to
f2f8796
Compare
|
Thanks for looking at this @mzumsande !
Modified the title, hopefully its more clear.
I edited the original post to hopefully clarify why this change is useful.
done. |
|
Concept NACK. This introduces dead code and i don't think the justification for this meets the bar for touching consensus code. |
… small ints in Script
f2f8796 to
6940761
Compare
|
Rebased |
|
@mzumsande do you want to circle back for another look here? Or @ajtowns? |
I think it would make more sense to carry this patch as part of the 64 bit arithmetic patchset. If there's consensus to merge/activate 64 bit arithmetic, it could make sense at that point to split this into its own PR to make review easier, but without that motivation, I don't think this change makes much sense on its own. So Concept NACK from me too for now, unless there's some other tangible benefit now? |
|
Closing for now. |
… add unit test 3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner) Pull request description: In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method: https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593 This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`. Note that this was unnoticed, as the code part was never hit so far in the test framework. ACKs for top commit: achow101: ACK 3e9c736 Christewart: ACK 3e9c736 rkrux: tACK [3e9c736](bitcoin@3e9c736) hernanmarino: tACK 3e9c736 Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
… add unit test 3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner) Pull request description: In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method: https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593 This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`. Note that this was unnoticed, as the code part was never hit so far in the test framework. ACKs for top commit: achow101: ACK 3e9c736 Christewart: ACK 3e9c736 rkrux: tACK [3e9c736](bitcoin@3e9c736) hernanmarino: tACK 3e9c736 Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
… add unit test 3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner) Pull request description: In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method: https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593 This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`. Note that this was unnoticed, as the code part was never hit so far in the test framework. ACKs for top commit: achow101: ACK 3e9c736 Christewart: ACK 3e9c736 rkrux: tACK [3e9c736](bitcoin@3e9c736) hernanmarino: tACK 3e9c736 Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
… add unit test 3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner) Pull request description: In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method: https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593 This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`. Note that this was unnoticed, as the code part was never hit so far in the test framework. ACKs for top commit: achow101: ACK 3e9c736 Christewart: ACK 3e9c736 rkrux: tACK [3e9c736](bitcoin@3e9c736) hernanmarino: tACK 3e9c736 Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
This PR fixes inconsistent behavior for encoding/decoding small integers in Script discovered when working on #29221.
OP_1NEGATEis the minimally encoded version of the number-1in Script. All other minimally encoded small integers (OP_0,OP_1,...,OP_16) are handled correctly byDecodeOP_N()andEncodeOP_N().This PR extends this functionality to handling
OP_1NEGATEin the same way we treat other small integers in Script.We treat all small integers consistently in
EvalScript()- so we should treat their encoding and decoding helper functions consistently as well. It doesn't make sense to handleOP_1NEGATEseparately imo.This is useful for future extensions of the Script language - such as 64bit arithmetic. If this change isn't accepted, we need to adhoc handle
OP_1NEGATEseparately from other small integers when encoding/decodingOP_1NEGATE.